usb_util/
descriptor.rs

1// Copyright 2019 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use std::collections::BTreeMap;
6use std::mem::size_of;
7use std::ops::Deref;
8
9use base::warn;
10use zerocopy::FromBytes;
11
12use crate::types;
13use crate::types::Descriptor;
14use crate::types::DescriptorHeader;
15use crate::types::EndpointDescriptor;
16use crate::Error;
17use crate::Result;
18
19#[derive(Clone)]
20pub struct DeviceDescriptorTree {
21    // Full descriptor tree in the original format returned by the device.
22    raw: Vec<u8>,
23    inner: types::DeviceDescriptor,
24    // Map of bConfigurationValue to ConfigDescriptor
25    config_descriptors: BTreeMap<u8, ConfigDescriptorTree>,
26    // Map of config index to bConfigurationValue.
27    config_values: BTreeMap<u8, u8>,
28}
29
30#[derive(Clone)]
31pub struct ConfigDescriptorTree {
32    offset: usize,
33    inner: types::ConfigDescriptor,
34    // Map of (bInterfaceNumber, bAlternateSetting) to InterfaceDescriptor
35    interface_descriptors: BTreeMap<(u8, u8), InterfaceDescriptorTree>,
36}
37
38#[derive(Clone)]
39pub struct InterfaceDescriptorTree {
40    offset: usize,
41    inner: types::InterfaceDescriptor,
42    // Map of bEndpointAddress to EndpointDescriptor
43    endpoint_descriptors: BTreeMap<u8, EndpointDescriptor>,
44}
45
46impl DeviceDescriptorTree {
47    pub fn get_config_descriptor(&self, config_value: u8) -> Option<&ConfigDescriptorTree> {
48        self.config_descriptors.get(&config_value)
49    }
50
51    /// Retrieve the Nth configuration descriptor in the device descriptor.
52    /// `config_index`: 0-based index into the list of configuration descriptors.
53    pub fn get_config_descriptor_by_index(
54        &self,
55        config_index: u8,
56    ) -> Option<&ConfigDescriptorTree> {
57        self.config_descriptors
58            .get(self.config_values.get(&config_index)?)
59    }
60
61    /// Access the raw descriptor tree as a slice of bytes.
62    pub fn raw(&self) -> &[u8] {
63        &self.raw
64    }
65}
66
67impl Deref for DeviceDescriptorTree {
68    type Target = types::DeviceDescriptor;
69
70    fn deref(&self) -> &types::DeviceDescriptor {
71        &self.inner
72    }
73}
74
75impl ConfigDescriptorTree {
76    /// Get interface by number and alt setting.
77    pub fn get_interface_descriptor(
78        &self,
79        interface_num: u8,
80        alt_setting: u8,
81    ) -> Option<&InterfaceDescriptorTree> {
82        self.interface_descriptors
83            .get(&(interface_num, alt_setting))
84    }
85
86    /// Get the offset of this configuration descriptor within the raw descriptor tree.
87    pub fn offset(&self) -> usize {
88        self.offset
89    }
90}
91
92impl Deref for ConfigDescriptorTree {
93    type Target = types::ConfigDescriptor;
94
95    fn deref(&self) -> &types::ConfigDescriptor {
96        &self.inner
97    }
98}
99
100impl InterfaceDescriptorTree {
101    pub fn get_endpoint_descriptor(&self, ep_idx: u8) -> Option<&EndpointDescriptor> {
102        self.endpoint_descriptors.get(&ep_idx)
103    }
104
105    /// Get the offset of this interface descriptor within the raw descriptor tree.
106    pub fn offset(&self) -> usize {
107        self.offset
108    }
109}
110
111impl Deref for InterfaceDescriptorTree {
112    type Target = types::InterfaceDescriptor;
113
114    fn deref(&self) -> &types::InterfaceDescriptor {
115        &self.inner
116    }
117}
118
119/// Given `data` containing a full set of descriptors as provided by the Linux kernel
120/// usbdevfs `descriptors` file, parse the descriptors into a tree data structure.
121pub fn parse_usbfs_descriptors(data: &[u8]) -> Result<DeviceDescriptorTree> {
122    let mut offset = 0;
123
124    // Find the next descriptor of type T and return it and its offset.
125    // Any other descriptors encountered while searching for the expected type are skipped.
126    // The `offset` parameter will be advanced to point to the next byte after the returned
127    // descriptor.
128    fn next_descriptor<T: Descriptor + FromBytes>(
129        data: &[u8],
130        offset: &mut usize,
131    ) -> Result<(T, usize)> {
132        let desc_type = T::descriptor_type() as u8;
133        loop {
134            let hdr = DescriptorHeader::read_from_bytes(
135                data.get(*offset..*offset + size_of::<DescriptorHeader>())
136                    .ok_or(Error::DescriptorParse)?,
137            )
138            .map_err(|_| Error::DescriptorParse)?;
139            if hdr.bDescriptorType == desc_type {
140                if usize::from(hdr.bLength) < size_of::<DescriptorHeader>() + size_of::<T>() {
141                    return Err(Error::DescriptorParse);
142                }
143
144                let desc_offset = *offset;
145
146                *offset += size_of::<DescriptorHeader>();
147                let desc = T::read_from_bytes(
148                    data.get(*offset..*offset + size_of::<T>())
149                        .ok_or(Error::DescriptorParse)?,
150                )
151                .map_err(|_| Error::DescriptorParse)?;
152                *offset += hdr.bLength as usize - size_of::<DescriptorHeader>();
153                return Ok((desc, desc_offset));
154            } else {
155                // Finding a ConfigDescriptor while looking for InterfaceDescriptor means
156                // that we should advance to the next device configuration.
157                if desc_type == types::InterfaceDescriptor::descriptor_type() as u8
158                    && hdr.bDescriptorType == types::ConfigDescriptor::descriptor_type() as u8
159                {
160                    return Err(Error::DescriptorParse);
161                }
162
163                // Make sure we make forward progress.
164                if hdr.bLength == 0 {
165                    return Err(Error::DescriptorParse);
166                }
167
168                // Skip this entire descriptor, since it's not the right type.
169                *offset += hdr.bLength as usize;
170            }
171        }
172    }
173
174    let (raw_device_descriptor, _) = next_descriptor::<types::DeviceDescriptor>(data, &mut offset)?;
175    let mut device_descriptor = DeviceDescriptorTree {
176        raw: data.into(),
177        inner: raw_device_descriptor,
178        config_descriptors: BTreeMap::new(),
179        config_values: BTreeMap::new(),
180    };
181
182    for cfg_idx in 0..device_descriptor.bNumConfigurations {
183        if let Ok((raw_config_descriptor, config_offset)) =
184            next_descriptor::<types::ConfigDescriptor>(&device_descriptor.raw, &mut offset)
185        {
186            let mut config_descriptor = ConfigDescriptorTree {
187                offset: config_offset,
188                inner: raw_config_descriptor,
189                interface_descriptors: BTreeMap::new(),
190            };
191
192            while let Ok((raw_interface_descriptor, interface_offset)) =
193                next_descriptor::<types::InterfaceDescriptor>(&device_descriptor.raw, &mut offset)
194            {
195                let mut interface_descriptor = InterfaceDescriptorTree {
196                    offset: interface_offset,
197                    inner: raw_interface_descriptor,
198                    endpoint_descriptors: BTreeMap::new(),
199                };
200
201                for ep_idx in 0..interface_descriptor.bNumEndpoints {
202                    if let Ok((endpoint_descriptor, _)) =
203                        next_descriptor::<EndpointDescriptor>(&device_descriptor.raw, &mut offset)
204                    {
205                        interface_descriptor
206                            .endpoint_descriptors
207                            .insert(ep_idx, endpoint_descriptor);
208                    } else {
209                        warn!("Could not read endpoint descriptor {}", ep_idx);
210                        break;
211                    }
212                }
213
214                config_descriptor.interface_descriptors.insert(
215                    (
216                        interface_descriptor.bInterfaceNumber,
217                        interface_descriptor.bAlternateSetting,
218                    ),
219                    interface_descriptor,
220                );
221            }
222
223            for intf_idx in 0..config_descriptor.bNumInterfaces {
224                if !config_descriptor
225                    .interface_descriptors
226                    .contains_key(&(intf_idx, 0))
227                {
228                    warn!("device interface {} has no interface descriptors", intf_idx);
229                }
230            }
231
232            device_descriptor
233                .config_values
234                .insert(cfg_idx, config_descriptor.bConfigurationValue);
235            device_descriptor
236                .config_descriptors
237                .insert(config_descriptor.bConfigurationValue, config_descriptor);
238        } else {
239            warn!("Could not read config descriptor {}", cfg_idx);
240            break;
241        }
242    }
243
244    Ok(device_descriptor)
245}
246
247#[cfg(test)]
248#[allow(clippy::useless_conversion)]
249mod tests {
250    use super::*;
251    #[test]
252    fn parse_descriptors_mass_storage() {
253        let data: &[u8] = &[
254            0x12, 0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 0x09, 0x81, 0x07, 0x80, 0x55, 0x10, 0x00,
255            0x01, 0x02, 0x03, 0x01, 0x09, 0x02, 0x2C, 0x00, 0x01, 0x01, 0x00, 0x80, 0x32, 0x09,
256            0x04, 0x00, 0x00, 0x02, 0x08, 0x06, 0x50, 0x00, 0x07, 0x05, 0x81, 0x02, 0x00, 0x04,
257            0x00, 0x06, 0x30, 0x0F, 0x00, 0x00, 0x00, 0x07, 0x05, 0x02, 0x02, 0x00, 0x04, 0x00,
258            0x06, 0x30, 0x0F, 0x00, 0x00, 0x00,
259        ];
260
261        let d = parse_usbfs_descriptors(data).expect("parse_usbfs_descriptors failed");
262
263        // The seemingly-redundant u16::from() calls avoid borrows of packed fields.
264
265        assert_eq!(u16::from(d.bcdUSB), 0x03_00);
266        assert_eq!(d.bDeviceClass, 0x00);
267        assert_eq!(d.bDeviceSubClass, 0x00);
268        assert_eq!(d.bDeviceProtocol, 0x00);
269        assert_eq!(d.bMaxPacketSize0, 9);
270        assert_eq!(u16::from(d.idVendor), 0x0781);
271        assert_eq!(u16::from(d.idProduct), 0x5580);
272        assert_eq!(u16::from(d.bcdDevice), 0x00_10);
273        assert_eq!(d.iManufacturer, 1);
274        assert_eq!(d.iProduct, 2);
275        assert_eq!(d.iSerialNumber, 3);
276        assert_eq!(d.bNumConfigurations, 1);
277
278        let c = d
279            .get_config_descriptor(1)
280            .expect("could not get config descriptor 1");
281        assert_eq!(u16::from(c.wTotalLength), 44);
282        assert_eq!(c.bNumInterfaces, 1);
283        assert_eq!(c.bConfigurationValue, 1);
284        assert_eq!(c.iConfiguration, 0);
285        assert_eq!(c.bmAttributes, 0x80);
286        assert_eq!(c.bMaxPower, 50);
287
288        let i = c
289            .get_interface_descriptor(0, 0)
290            .expect("could not get interface descriptor 0 alt setting 0");
291        assert_eq!(i.bInterfaceNumber, 0);
292        assert_eq!(i.bAlternateSetting, 0);
293        assert_eq!(i.bNumEndpoints, 2);
294        assert_eq!(i.bInterfaceClass, 0x08);
295        assert_eq!(i.bInterfaceSubClass, 0x06);
296        assert_eq!(i.bInterfaceProtocol, 0x50);
297        assert_eq!(i.iInterface, 0);
298
299        let e = i
300            .get_endpoint_descriptor(0)
301            .expect("could not get endpoint 0 descriptor");
302        assert_eq!(e.bEndpointAddress, 0x81);
303        assert_eq!(e.bmAttributes, 0x02);
304        assert_eq!(u16::from(e.wMaxPacketSize), 0x0400);
305        assert_eq!(e.bInterval, 0);
306
307        let e = i
308            .get_endpoint_descriptor(1)
309            .expect("could not get endpoint 1 descriptor");
310        assert_eq!(e.bEndpointAddress, 0x02);
311        assert_eq!(e.bmAttributes, 0x02);
312        assert_eq!(u16::from(e.wMaxPacketSize), 0x0400);
313        assert_eq!(e.bInterval, 0);
314    }
315
316    #[test]
317    fn parse_descriptors_servo() {
318        let data: &[u8] = &[
319            0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xd1, 0x18, 0x1b, 0x50, 0x00, 0x01,
320            0x01, 0x02, 0x03, 0x01, 0x09, 0x02, 0x7c, 0x00, 0x06, 0x01, 0x04, 0xc0, 0xfa, 0x09,
321            0x04, 0x00, 0x00, 0x02, 0xff, 0x50, 0x01, 0x06, 0x07, 0x05, 0x81, 0x02, 0x40, 0x00,
322            0x0a, 0x07, 0x05, 0x01, 0x02, 0x40, 0x00, 0x00, 0x09, 0x04, 0x02, 0x00, 0x02, 0xff,
323            0x52, 0x01, 0x05, 0x07, 0x05, 0x83, 0x02, 0x40, 0x00, 0x0a, 0x07, 0x05, 0x03, 0x02,
324            0x40, 0x00, 0x00, 0x09, 0x04, 0x03, 0x00, 0x02, 0xff, 0x50, 0x01, 0x07, 0x07, 0x05,
325            0x84, 0x02, 0x10, 0x00, 0x0a, 0x07, 0x05, 0x04, 0x02, 0x10, 0x00, 0x00, 0x09, 0x04,
326            0x04, 0x00, 0x02, 0xff, 0x50, 0x01, 0x08, 0x07, 0x05, 0x85, 0x02, 0x10, 0x00, 0x0a,
327            0x07, 0x05, 0x05, 0x02, 0x10, 0x00, 0x00, 0x09, 0x04, 0x05, 0x00, 0x02, 0xff, 0x53,
328            0xff, 0x09, 0x07, 0x05, 0x86, 0x02, 0x40, 0x00, 0x0a, 0x07, 0x05, 0x06, 0x02, 0x40,
329            0x00, 0x00,
330        ];
331
332        // Note: configuration 1 has bNumInterfaces == 6, but it actually only contains 5
333        // interface descriptors. This causes us to try to read beyond EOF, which should be
334        // silently ignored by parse_usbfs_descriptors so that we can use the rest of the
335        // descriptors.
336        let d = parse_usbfs_descriptors(data).expect("parse_usbfs_descriptors failed");
337
338        // The seemingly-redundant u16::from() calls avoid borrows of packed fields.
339
340        assert_eq!(u16::from(d.bcdUSB), 0x02_00);
341        assert_eq!(d.bDeviceClass, 0x00);
342        assert_eq!(d.bDeviceSubClass, 0x00);
343        assert_eq!(d.bDeviceProtocol, 0x00);
344        assert_eq!(d.bMaxPacketSize0, 64);
345        assert_eq!(u16::from(d.idVendor), 0x18d1);
346        assert_eq!(u16::from(d.idProduct), 0x501b);
347        assert_eq!(u16::from(d.bcdDevice), 0x01_00);
348        assert_eq!(d.iManufacturer, 1);
349        assert_eq!(d.iProduct, 2);
350        assert_eq!(d.iSerialNumber, 3);
351        assert_eq!(d.bNumConfigurations, 1);
352
353        let c = d
354            .get_config_descriptor(1)
355            .expect("could not get config descriptor 1");
356        assert_eq!(u16::from(c.wTotalLength), 124);
357        assert_eq!(c.bNumInterfaces, 6);
358        assert_eq!(c.bConfigurationValue, 1);
359        assert_eq!(c.iConfiguration, 4);
360        assert_eq!(c.bmAttributes, 0xc0);
361        assert_eq!(c.bMaxPower, 250);
362
363        let i = c
364            .get_interface_descriptor(0, 0)
365            .expect("could not get interface descriptor 0 alt setting 0");
366        assert_eq!(i.bInterfaceNumber, 0);
367        assert_eq!(i.bAlternateSetting, 0);
368        assert_eq!(i.bNumEndpoints, 2);
369        assert_eq!(i.bInterfaceClass, 0xff);
370        assert_eq!(i.bInterfaceSubClass, 0x50);
371        assert_eq!(i.bInterfaceProtocol, 0x01);
372        assert_eq!(i.iInterface, 6);
373
374        let e = i
375            .get_endpoint_descriptor(0)
376            .expect("could not get endpoint 0 descriptor");
377        assert_eq!(e.bEndpointAddress, 0x81);
378        assert_eq!(e.bmAttributes, 0x02);
379        assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
380        assert_eq!(e.bInterval, 10);
381
382        let e = i
383            .get_endpoint_descriptor(1)
384            .expect("could not get endpoint 1 descriptor");
385        assert_eq!(e.bEndpointAddress, 0x01);
386        assert_eq!(e.bmAttributes, 0x02);
387        assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
388        assert_eq!(e.bInterval, 0);
389
390        let i = c
391            .get_interface_descriptor(2, 0)
392            .expect("could not get interface descriptor 2 alt setting 0");
393        assert_eq!(i.bInterfaceNumber, 2);
394        assert_eq!(i.bAlternateSetting, 0);
395        assert_eq!(i.bNumEndpoints, 2);
396        assert_eq!(i.bInterfaceClass, 0xff);
397        assert_eq!(i.bInterfaceSubClass, 0x52);
398        assert_eq!(i.bInterfaceProtocol, 0x01);
399        assert_eq!(i.iInterface, 5);
400
401        let e = i
402            .get_endpoint_descriptor(0)
403            .expect("could not get endpoint 0 descriptor");
404        assert_eq!(e.bEndpointAddress, 0x83);
405        assert_eq!(e.bmAttributes, 0x02);
406        assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
407        assert_eq!(e.bInterval, 10);
408
409        let e = i
410            .get_endpoint_descriptor(1)
411            .expect("could not get endpoint 1 descriptor");
412        assert_eq!(e.bEndpointAddress, 0x03);
413        assert_eq!(e.bmAttributes, 0x02);
414        assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
415        assert_eq!(e.bInterval, 0);
416
417        let i = c
418            .get_interface_descriptor(3, 0)
419            .expect("could not get interface descriptor 3 alt setting 0");
420        assert_eq!(i.bInterfaceNumber, 3);
421        assert_eq!(i.bAlternateSetting, 0);
422        assert_eq!(i.bNumEndpoints, 2);
423        assert_eq!(i.bInterfaceClass, 0xff);
424        assert_eq!(i.bInterfaceSubClass, 0x50);
425        assert_eq!(i.bInterfaceProtocol, 0x01);
426        assert_eq!(i.iInterface, 7);
427
428        let e = i
429            .get_endpoint_descriptor(0)
430            .expect("could not get endpoint 0 descriptor");
431        assert_eq!(e.bEndpointAddress, 0x84);
432        assert_eq!(e.bmAttributes, 0x02);
433        assert_eq!(u16::from(e.wMaxPacketSize), 0x0010);
434        assert_eq!(e.bInterval, 10);
435
436        let e = i
437            .get_endpoint_descriptor(1)
438            .expect("could not get endpoint 1 descriptor");
439        assert_eq!(e.bEndpointAddress, 0x04);
440        assert_eq!(e.bmAttributes, 0x02);
441        assert_eq!(u16::from(e.wMaxPacketSize), 0x0010);
442        assert_eq!(e.bInterval, 0);
443
444        let i = c
445            .get_interface_descriptor(4, 0)
446            .expect("could not get interface descriptor 4 alt setting 0");
447        assert_eq!(i.bInterfaceNumber, 4);
448        assert_eq!(i.bAlternateSetting, 0);
449        assert_eq!(i.bNumEndpoints, 2);
450        assert_eq!(i.bInterfaceClass, 0xff);
451        assert_eq!(i.bInterfaceSubClass, 0x50);
452        assert_eq!(i.bInterfaceProtocol, 0x01);
453        assert_eq!(i.iInterface, 8);
454
455        let e = i
456            .get_endpoint_descriptor(0)
457            .expect("could not get endpoint 0 descriptor");
458        assert_eq!(e.bEndpointAddress, 0x85);
459        assert_eq!(e.bmAttributes, 0x02);
460        assert_eq!(u16::from(e.wMaxPacketSize), 0x0010);
461        assert_eq!(e.bInterval, 10);
462
463        let e = i
464            .get_endpoint_descriptor(1)
465            .expect("could not get endpoint 1 descriptor");
466        assert_eq!(e.bEndpointAddress, 0x05);
467        assert_eq!(e.bmAttributes, 0x02);
468        assert_eq!(u16::from(e.wMaxPacketSize), 0x0010);
469        assert_eq!(e.bInterval, 0);
470
471        let i = c
472            .get_interface_descriptor(5, 0)
473            .expect("could not get interface descriptor 5 alt setting 0");
474        assert_eq!(i.bInterfaceNumber, 5);
475        assert_eq!(i.bAlternateSetting, 0);
476        assert_eq!(i.bNumEndpoints, 2);
477        assert_eq!(i.bInterfaceClass, 0xff);
478        assert_eq!(i.bInterfaceSubClass, 0x53);
479        assert_eq!(i.bInterfaceProtocol, 0xff);
480        assert_eq!(i.iInterface, 9);
481
482        let e = i
483            .get_endpoint_descriptor(0)
484            .expect("could not get endpoint 0 descriptor");
485        assert_eq!(e.bEndpointAddress, 0x86);
486        assert_eq!(e.bmAttributes, 0x02);
487        assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
488        assert_eq!(e.bInterval, 10);
489
490        let e = i
491            .get_endpoint_descriptor(1)
492            .expect("could not get endpoint 1 descriptor");
493        assert_eq!(e.bEndpointAddress, 0x06);
494        assert_eq!(e.bmAttributes, 0x02);
495        assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
496        assert_eq!(e.bInterval, 0);
497    }
498
499    #[test]
500    fn parse_descriptors_adb() {
501        let data: &[u8] = &[
502            0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xd1, 0x18, 0xe7, 0x4e, 0x10, 0x03,
503            0x01, 0x02, 0x03, 0x01, 0x09, 0x02, 0x20, 0x00, 0x01, 0x01, 0x00, 0x80, 0xfa, 0x09,
504            0x04, 0x00, 0x00, 0x02, 0xff, 0x42, 0x01, 0x05, 0x07, 0x05, 0x01, 0x02, 0x00, 0x02,
505            0x00, 0x07, 0x05, 0x81, 0x02, 0x00, 0x02, 0x00,
506        ];
507
508        let d = parse_usbfs_descriptors(data).expect("parse_usbfs_descriptors failed");
509
510        // The seemingly-redundant u16::from() calls avoid borrows of packed fields.
511
512        assert_eq!(u16::from(d.bcdUSB), 0x02_00);
513        assert_eq!(d.bDeviceClass, 0x00);
514        assert_eq!(d.bDeviceSubClass, 0x00);
515        assert_eq!(d.bDeviceProtocol, 0x00);
516        assert_eq!(d.bMaxPacketSize0, 64);
517        assert_eq!(u16::from(d.idVendor), 0x18d1);
518        assert_eq!(u16::from(d.idProduct), 0x4ee7);
519        assert_eq!(u16::from(d.bcdDevice), 0x03_10);
520        assert_eq!(d.iManufacturer, 1);
521        assert_eq!(d.iProduct, 2);
522        assert_eq!(d.iSerialNumber, 3);
523        assert_eq!(d.bNumConfigurations, 1);
524
525        let c = d
526            .get_config_descriptor(1)
527            .expect("could not get config descriptor 1");
528        assert_eq!(u16::from(c.wTotalLength), 32);
529        assert_eq!(c.bNumInterfaces, 1);
530        assert_eq!(c.bConfigurationValue, 1);
531        assert_eq!(c.iConfiguration, 0);
532        assert_eq!(c.bmAttributes, 0x80);
533        assert_eq!(c.bMaxPower, 250);
534
535        let i = c
536            .get_interface_descriptor(0, 0)
537            .expect("could not get interface descriptor 0 alt setting 0");
538        assert_eq!(i.bInterfaceNumber, 0);
539        assert_eq!(i.bAlternateSetting, 0);
540        assert_eq!(i.bNumEndpoints, 2);
541        assert_eq!(i.bInterfaceClass, 0xff);
542        assert_eq!(i.bInterfaceSubClass, 0x42);
543        assert_eq!(i.bInterfaceProtocol, 0x01);
544        assert_eq!(i.iInterface, 5);
545
546        let e = i
547            .get_endpoint_descriptor(0)
548            .expect("could not get endpoint 0 descriptor");
549        assert_eq!(e.bEndpointAddress, 0x01);
550        assert_eq!(e.bmAttributes, 0x02);
551        assert_eq!(u16::from(e.wMaxPacketSize), 0x200);
552        assert_eq!(e.bInterval, 0);
553
554        let e = i
555            .get_endpoint_descriptor(1)
556            .expect("could not get endpoint 1 descriptor");
557        assert_eq!(e.bEndpointAddress, 0x81);
558        assert_eq!(e.bmAttributes, 0x02);
559        assert_eq!(u16::from(e.wMaxPacketSize), 0x0200);
560        assert_eq!(e.bInterval, 0);
561    }
562
563    #[test]
564    fn parse_descriptors_multiple_altsettings() {
565        let data: &[u8] = &[
566            // DeviceDescriptor
567            0x12, 0x01, 0x00, 0x02, 0xef, 0x02, 0x01, 0x40, 0x6d, 0x04, 0x43, 0x08, 0x13, 0x00,
568            0x00, 0x02, 0x01, 0x01, // ConfigDescriptor
569            0x09, 0x02, 0x0d, 0x0a, 0x03, 0x01, 0x00, 0x80, 0xfa,
570            // InterfaceDescriptor 0, 0
571            0x09, 0x04, 0x00, 0x00, 0x01, 0x0e, 0x01, 0x00, 0x00, // EndpointDescriptor
572            0x07, 0x05, 0x86, 0x03, 0x40, 0x00, 0x08, // InterfaceDescriptor 1, 0
573            0x09, 0x04, 0x01, 0x00, 0x00, 0x0e, 0x02, 0x00, 0x00,
574            // InterfaceDescriptor 1, 1
575            0x09, 0x04, 0x01, 0x01, 0x01, 0x0e, 0x02, 0x00, 0x00, // EndpointDescriptor
576            0x07, 0x05, 0x81, 0x05, 0xc0, 0x00, 0x01, // InterfaceDescriptor 2, 0
577            0x09, 0x04, 0x02, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00,
578        ];
579
580        let d = parse_usbfs_descriptors(data).expect("parse_usbfs_descriptors failed");
581
582        // The seemingly-redundant u16::from() calls avoid borrows of packed fields.
583
584        assert_eq!(u16::from(d.bcdUSB), 0x02_00);
585        assert_eq!(d.bDeviceClass, 0xef);
586        assert_eq!(d.bDeviceSubClass, 0x02);
587        assert_eq!(d.bDeviceProtocol, 0x01);
588        assert_eq!(d.bMaxPacketSize0, 64);
589        assert_eq!(u16::from(d.idVendor), 0x046d);
590        assert_eq!(u16::from(d.idProduct), 0x0843);
591        assert_eq!(u16::from(d.bcdDevice), 0x00_13);
592        assert_eq!(d.iManufacturer, 0);
593        assert_eq!(d.iProduct, 2);
594        assert_eq!(d.iSerialNumber, 1);
595        assert_eq!(d.bNumConfigurations, 1);
596
597        let c = d
598            .get_config_descriptor(1)
599            .expect("could not get config descriptor 1");
600        assert_eq!(u16::from(c.wTotalLength), 2573);
601        assert_eq!(c.bNumInterfaces, 3);
602        assert_eq!(c.bConfigurationValue, 1);
603        assert_eq!(c.iConfiguration, 0);
604        assert_eq!(c.bmAttributes, 0x80);
605        assert_eq!(c.bMaxPower, 250);
606
607        let i = c
608            .get_interface_descriptor(0, 0)
609            .expect("could not get interface descriptor 0 alt setting 0");
610        assert_eq!(i.bInterfaceNumber, 0);
611        assert_eq!(i.bAlternateSetting, 0);
612        assert_eq!(i.bNumEndpoints, 1);
613        assert_eq!(i.bInterfaceClass, 0x0e);
614        assert_eq!(i.bInterfaceSubClass, 0x01);
615        assert_eq!(i.bInterfaceProtocol, 0x00);
616        assert_eq!(i.iInterface, 0x00);
617
618        let e = i
619            .get_endpoint_descriptor(0)
620            .expect("could not get endpoint 0 descriptor");
621        assert_eq!(e.bEndpointAddress, 0x86);
622        assert_eq!(e.bmAttributes, 0x03);
623        assert_eq!(u16::from(e.wMaxPacketSize), 0x40);
624        assert_eq!(e.bInterval, 0x08);
625
626        let i = c
627            .get_interface_descriptor(1, 0)
628            .expect("could not get interface descriptor 1 alt setting 0");
629        assert_eq!(i.bInterfaceNumber, 1);
630        assert_eq!(i.bAlternateSetting, 0);
631        assert_eq!(i.bNumEndpoints, 0);
632        assert_eq!(i.bInterfaceClass, 0x0e);
633        assert_eq!(i.bInterfaceSubClass, 0x02);
634        assert_eq!(i.bInterfaceProtocol, 0x00);
635        assert_eq!(i.iInterface, 0x00);
636
637        let i = c
638            .get_interface_descriptor(1, 1)
639            .expect("could not get interface descriptor 1 alt setting 1");
640        assert_eq!(i.bInterfaceNumber, 1);
641        assert_eq!(i.bAlternateSetting, 1);
642        assert_eq!(i.bNumEndpoints, 1);
643        assert_eq!(i.bInterfaceClass, 0x0e);
644        assert_eq!(i.bInterfaceSubClass, 0x02);
645        assert_eq!(i.bInterfaceProtocol, 0x00);
646        assert_eq!(i.iInterface, 0x00);
647
648        let e = i
649            .get_endpoint_descriptor(0)
650            .expect("could not get endpoint 0 descriptor");
651        assert_eq!(e.bEndpointAddress, 0x81);
652        assert_eq!(e.bmAttributes, 0x05);
653        assert_eq!(u16::from(e.wMaxPacketSize), 0xc0);
654        assert_eq!(e.bInterval, 0x01);
655
656        let i = c
657            .get_interface_descriptor(2, 0)
658            .expect("could not get interface descriptor 2 alt setting 0");
659        assert_eq!(i.bInterfaceNumber, 2);
660        assert_eq!(i.bAlternateSetting, 0);
661        assert_eq!(i.bNumEndpoints, 0);
662        assert_eq!(i.bInterfaceClass, 0x01);
663        assert_eq!(i.bInterfaceSubClass, 0x01);
664        assert_eq!(i.bInterfaceProtocol, 0x00);
665        assert_eq!(i.iInterface, 0x00);
666    }
667
668    #[test]
669    fn parse_descriptors_length_0() {
670        // Device descriptor followed by a bogus descriptor with bLength == 0.
671        // Note that this was generated by a fuzzer, so field values may not make sense.
672        let data: &[u8] = &[
673            0x10, 0x00, 0x18, 0x25, 0x80, 0x80, 0xAC, 0x03, 0x22, 0x05, 0x00, 0x00, 0x00, 0x00,
674            0xC3, 0x2A, 0x00, 0x32, 0x00,
675        ];
676
677        let d = parse_usbfs_descriptors(data);
678        if d.is_ok() {
679            panic!("parse_usbfs_descriptors should have failed");
680        }
681    }
682}