• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 use std::collections::BTreeMap;
6 use std::mem::size_of;
7 use std::ops::Deref;
8 
9 use base::warn;
10 use zerocopy::FromBytes;
11 
12 use crate::types;
13 use crate::types::Descriptor;
14 use crate::types::DescriptorHeader;
15 use crate::types::EndpointDescriptor;
16 use crate::Error;
17 use crate::Result;
18 
19 #[derive(Clone)]
20 pub 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)]
31 pub 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)]
39 pub struct InterfaceDescriptorTree {
40     offset: usize,
41     inner: types::InterfaceDescriptor,
42     // Map of bEndpointAddress to EndpointDescriptor
43     endpoint_descriptors: BTreeMap<u8, EndpointDescriptor>,
44 }
45 
46 impl DeviceDescriptorTree {
get_config_descriptor(&self, config_value: u8) -> Option<&ConfigDescriptorTree>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.
get_config_descriptor_by_index( &self, config_index: u8, ) -> Option<&ConfigDescriptorTree>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.
raw(&self) -> &[u8]62     pub fn raw(&self) -> &[u8] {
63         &self.raw
64     }
65 }
66 
67 impl Deref for DeviceDescriptorTree {
68     type Target = types::DeviceDescriptor;
69 
deref(&self) -> &types::DeviceDescriptor70     fn deref(&self) -> &types::DeviceDescriptor {
71         &self.inner
72     }
73 }
74 
75 impl ConfigDescriptorTree {
76     /// Get interface by number and alt setting.
get_interface_descriptor( &self, interface_num: u8, alt_setting: u8, ) -> Option<&InterfaceDescriptorTree>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.
offset(&self) -> usize87     pub fn offset(&self) -> usize {
88         self.offset
89     }
90 }
91 
92 impl Deref for ConfigDescriptorTree {
93     type Target = types::ConfigDescriptor;
94 
deref(&self) -> &types::ConfigDescriptor95     fn deref(&self) -> &types::ConfigDescriptor {
96         &self.inner
97     }
98 }
99 
100 impl InterfaceDescriptorTree {
get_endpoint_descriptor(&self, ep_idx: u8) -> Option<&EndpointDescriptor>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.
offset(&self) -> usize106     pub fn offset(&self) -> usize {
107         self.offset
108     }
109 }
110 
111 impl Deref for InterfaceDescriptorTree {
112     type Target = types::InterfaceDescriptor;
113 
deref(&self) -> &types::InterfaceDescriptor114     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.
parse_usbfs_descriptors(data: &[u8]) -> Result<DeviceDescriptorTree>121 pub 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(
135                 data.get(*offset..*offset + size_of::<DescriptorHeader>())
136                     .ok_or(Error::DescriptorParse)?,
137             )
138             .ok_or(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(
148                     data.get(*offset..*offset + size_of::<T>())
149                         .ok_or(Error::DescriptorParse)?,
150                 )
151                 .ok_or(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                     .get(&(intf_idx, 0))
227                     .is_none()
228                 {
229                     warn!("device interface {} has no interface descriptors", intf_idx);
230                 }
231             }
232 
233             device_descriptor
234                 .config_values
235                 .insert(cfg_idx, config_descriptor.bConfigurationValue);
236             device_descriptor
237                 .config_descriptors
238                 .insert(config_descriptor.bConfigurationValue, config_descriptor);
239         } else {
240             warn!("Could not read config descriptor {}", cfg_idx);
241             break;
242         }
243     }
244 
245     Ok(device_descriptor)
246 }
247 
248 #[cfg(test)]
249 #[allow(clippy::useless_conversion)]
250 mod tests {
251     use super::*;
252     #[test]
parse_descriptors_mass_storage()253     fn parse_descriptors_mass_storage() {
254         let data: &[u8] = &[
255             0x12, 0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 0x09, 0x81, 0x07, 0x80, 0x55, 0x10, 0x00,
256             0x01, 0x02, 0x03, 0x01, 0x09, 0x02, 0x2C, 0x00, 0x01, 0x01, 0x00, 0x80, 0x32, 0x09,
257             0x04, 0x00, 0x00, 0x02, 0x08, 0x06, 0x50, 0x00, 0x07, 0x05, 0x81, 0x02, 0x00, 0x04,
258             0x00, 0x06, 0x30, 0x0F, 0x00, 0x00, 0x00, 0x07, 0x05, 0x02, 0x02, 0x00, 0x04, 0x00,
259             0x06, 0x30, 0x0F, 0x00, 0x00, 0x00,
260         ];
261 
262         let d = parse_usbfs_descriptors(data).expect("parse_usbfs_descriptors failed");
263 
264         // The seemingly-redundant u16::from() calls avoid borrows of packed fields.
265 
266         assert_eq!(u16::from(d.bcdUSB), 0x03_00);
267         assert_eq!(d.bDeviceClass, 0x00);
268         assert_eq!(d.bDeviceSubClass, 0x00);
269         assert_eq!(d.bDeviceProtocol, 0x00);
270         assert_eq!(d.bMaxPacketSize0, 9);
271         assert_eq!(u16::from(d.idVendor), 0x0781);
272         assert_eq!(u16::from(d.idProduct), 0x5580);
273         assert_eq!(u16::from(d.bcdDevice), 0x00_10);
274         assert_eq!(d.iManufacturer, 1);
275         assert_eq!(d.iProduct, 2);
276         assert_eq!(d.iSerialNumber, 3);
277         assert_eq!(d.bNumConfigurations, 1);
278 
279         let c = d
280             .get_config_descriptor(1)
281             .expect("could not get config descriptor 1");
282         assert_eq!(u16::from(c.wTotalLength), 44);
283         assert_eq!(c.bNumInterfaces, 1);
284         assert_eq!(c.bConfigurationValue, 1);
285         assert_eq!(c.iConfiguration, 0);
286         assert_eq!(c.bmAttributes, 0x80);
287         assert_eq!(c.bMaxPower, 50);
288 
289         let i = c
290             .get_interface_descriptor(0, 0)
291             .expect("could not get interface descriptor 0 alt setting 0");
292         assert_eq!(i.bInterfaceNumber, 0);
293         assert_eq!(i.bAlternateSetting, 0);
294         assert_eq!(i.bNumEndpoints, 2);
295         assert_eq!(i.bInterfaceClass, 0x08);
296         assert_eq!(i.bInterfaceSubClass, 0x06);
297         assert_eq!(i.bInterfaceProtocol, 0x50);
298         assert_eq!(i.iInterface, 0);
299 
300         let e = i
301             .get_endpoint_descriptor(0)
302             .expect("could not get endpoint 0 descriptor");
303         assert_eq!(e.bEndpointAddress, 0x81);
304         assert_eq!(e.bmAttributes, 0x02);
305         assert_eq!(u16::from(e.wMaxPacketSize), 0x0400);
306         assert_eq!(e.bInterval, 0);
307 
308         let e = i
309             .get_endpoint_descriptor(1)
310             .expect("could not get endpoint 1 descriptor");
311         assert_eq!(e.bEndpointAddress, 0x02);
312         assert_eq!(e.bmAttributes, 0x02);
313         assert_eq!(u16::from(e.wMaxPacketSize), 0x0400);
314         assert_eq!(e.bInterval, 0);
315     }
316 
317     #[test]
parse_descriptors_servo()318     fn parse_descriptors_servo() {
319         let data: &[u8] = &[
320             0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xd1, 0x18, 0x1b, 0x50, 0x00, 0x01,
321             0x01, 0x02, 0x03, 0x01, 0x09, 0x02, 0x7c, 0x00, 0x06, 0x01, 0x04, 0xc0, 0xfa, 0x09,
322             0x04, 0x00, 0x00, 0x02, 0xff, 0x50, 0x01, 0x06, 0x07, 0x05, 0x81, 0x02, 0x40, 0x00,
323             0x0a, 0x07, 0x05, 0x01, 0x02, 0x40, 0x00, 0x00, 0x09, 0x04, 0x02, 0x00, 0x02, 0xff,
324             0x52, 0x01, 0x05, 0x07, 0x05, 0x83, 0x02, 0x40, 0x00, 0x0a, 0x07, 0x05, 0x03, 0x02,
325             0x40, 0x00, 0x00, 0x09, 0x04, 0x03, 0x00, 0x02, 0xff, 0x50, 0x01, 0x07, 0x07, 0x05,
326             0x84, 0x02, 0x10, 0x00, 0x0a, 0x07, 0x05, 0x04, 0x02, 0x10, 0x00, 0x00, 0x09, 0x04,
327             0x04, 0x00, 0x02, 0xff, 0x50, 0x01, 0x08, 0x07, 0x05, 0x85, 0x02, 0x10, 0x00, 0x0a,
328             0x07, 0x05, 0x05, 0x02, 0x10, 0x00, 0x00, 0x09, 0x04, 0x05, 0x00, 0x02, 0xff, 0x53,
329             0xff, 0x09, 0x07, 0x05, 0x86, 0x02, 0x40, 0x00, 0x0a, 0x07, 0x05, 0x06, 0x02, 0x40,
330             0x00, 0x00,
331         ];
332 
333         // Note: configuration 1 has bNumInterfaces == 6, but it actually only contains 5
334         // interface descriptors. This causes us to try to read beyond EOF, which should be
335         // silently ignored by parse_usbfs_descriptors so that we can use the rest of the
336         // descriptors.
337         let d = parse_usbfs_descriptors(data).expect("parse_usbfs_descriptors failed");
338 
339         // The seemingly-redundant u16::from() calls avoid borrows of packed fields.
340 
341         assert_eq!(u16::from(d.bcdUSB), 0x02_00);
342         assert_eq!(d.bDeviceClass, 0x00);
343         assert_eq!(d.bDeviceSubClass, 0x00);
344         assert_eq!(d.bDeviceProtocol, 0x00);
345         assert_eq!(d.bMaxPacketSize0, 64);
346         assert_eq!(u16::from(d.idVendor), 0x18d1);
347         assert_eq!(u16::from(d.idProduct), 0x501b);
348         assert_eq!(u16::from(d.bcdDevice), 0x01_00);
349         assert_eq!(d.iManufacturer, 1);
350         assert_eq!(d.iProduct, 2);
351         assert_eq!(d.iSerialNumber, 3);
352         assert_eq!(d.bNumConfigurations, 1);
353 
354         let c = d
355             .get_config_descriptor(1)
356             .expect("could not get config descriptor 1");
357         assert_eq!(u16::from(c.wTotalLength), 124);
358         assert_eq!(c.bNumInterfaces, 6);
359         assert_eq!(c.bConfigurationValue, 1);
360         assert_eq!(c.iConfiguration, 4);
361         assert_eq!(c.bmAttributes, 0xc0);
362         assert_eq!(c.bMaxPower, 250);
363 
364         let i = c
365             .get_interface_descriptor(0, 0)
366             .expect("could not get interface descriptor 0 alt setting 0");
367         assert_eq!(i.bInterfaceNumber, 0);
368         assert_eq!(i.bAlternateSetting, 0);
369         assert_eq!(i.bNumEndpoints, 2);
370         assert_eq!(i.bInterfaceClass, 0xff);
371         assert_eq!(i.bInterfaceSubClass, 0x50);
372         assert_eq!(i.bInterfaceProtocol, 0x01);
373         assert_eq!(i.iInterface, 6);
374 
375         let e = i
376             .get_endpoint_descriptor(0)
377             .expect("could not get endpoint 0 descriptor");
378         assert_eq!(e.bEndpointAddress, 0x81);
379         assert_eq!(e.bmAttributes, 0x02);
380         assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
381         assert_eq!(e.bInterval, 10);
382 
383         let e = i
384             .get_endpoint_descriptor(1)
385             .expect("could not get endpoint 1 descriptor");
386         assert_eq!(e.bEndpointAddress, 0x01);
387         assert_eq!(e.bmAttributes, 0x02);
388         assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
389         assert_eq!(e.bInterval, 0);
390 
391         let i = c
392             .get_interface_descriptor(2, 0)
393             .expect("could not get interface descriptor 2 alt setting 0");
394         assert_eq!(i.bInterfaceNumber, 2);
395         assert_eq!(i.bAlternateSetting, 0);
396         assert_eq!(i.bNumEndpoints, 2);
397         assert_eq!(i.bInterfaceClass, 0xff);
398         assert_eq!(i.bInterfaceSubClass, 0x52);
399         assert_eq!(i.bInterfaceProtocol, 0x01);
400         assert_eq!(i.iInterface, 5);
401 
402         let e = i
403             .get_endpoint_descriptor(0)
404             .expect("could not get endpoint 0 descriptor");
405         assert_eq!(e.bEndpointAddress, 0x83);
406         assert_eq!(e.bmAttributes, 0x02);
407         assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
408         assert_eq!(e.bInterval, 10);
409 
410         let e = i
411             .get_endpoint_descriptor(1)
412             .expect("could not get endpoint 1 descriptor");
413         assert_eq!(e.bEndpointAddress, 0x03);
414         assert_eq!(e.bmAttributes, 0x02);
415         assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
416         assert_eq!(e.bInterval, 0);
417 
418         let i = c
419             .get_interface_descriptor(3, 0)
420             .expect("could not get interface descriptor 3 alt setting 0");
421         assert_eq!(i.bInterfaceNumber, 3);
422         assert_eq!(i.bAlternateSetting, 0);
423         assert_eq!(i.bNumEndpoints, 2);
424         assert_eq!(i.bInterfaceClass, 0xff);
425         assert_eq!(i.bInterfaceSubClass, 0x50);
426         assert_eq!(i.bInterfaceProtocol, 0x01);
427         assert_eq!(i.iInterface, 7);
428 
429         let e = i
430             .get_endpoint_descriptor(0)
431             .expect("could not get endpoint 0 descriptor");
432         assert_eq!(e.bEndpointAddress, 0x84);
433         assert_eq!(e.bmAttributes, 0x02);
434         assert_eq!(u16::from(e.wMaxPacketSize), 0x0010);
435         assert_eq!(e.bInterval, 10);
436 
437         let e = i
438             .get_endpoint_descriptor(1)
439             .expect("could not get endpoint 1 descriptor");
440         assert_eq!(e.bEndpointAddress, 0x04);
441         assert_eq!(e.bmAttributes, 0x02);
442         assert_eq!(u16::from(e.wMaxPacketSize), 0x0010);
443         assert_eq!(e.bInterval, 0);
444 
445         let i = c
446             .get_interface_descriptor(4, 0)
447             .expect("could not get interface descriptor 4 alt setting 0");
448         assert_eq!(i.bInterfaceNumber, 4);
449         assert_eq!(i.bAlternateSetting, 0);
450         assert_eq!(i.bNumEndpoints, 2);
451         assert_eq!(i.bInterfaceClass, 0xff);
452         assert_eq!(i.bInterfaceSubClass, 0x50);
453         assert_eq!(i.bInterfaceProtocol, 0x01);
454         assert_eq!(i.iInterface, 8);
455 
456         let e = i
457             .get_endpoint_descriptor(0)
458             .expect("could not get endpoint 0 descriptor");
459         assert_eq!(e.bEndpointAddress, 0x85);
460         assert_eq!(e.bmAttributes, 0x02);
461         assert_eq!(u16::from(e.wMaxPacketSize), 0x0010);
462         assert_eq!(e.bInterval, 10);
463 
464         let e = i
465             .get_endpoint_descriptor(1)
466             .expect("could not get endpoint 1 descriptor");
467         assert_eq!(e.bEndpointAddress, 0x05);
468         assert_eq!(e.bmAttributes, 0x02);
469         assert_eq!(u16::from(e.wMaxPacketSize), 0x0010);
470         assert_eq!(e.bInterval, 0);
471 
472         let i = c
473             .get_interface_descriptor(5, 0)
474             .expect("could not get interface descriptor 5 alt setting 0");
475         assert_eq!(i.bInterfaceNumber, 5);
476         assert_eq!(i.bAlternateSetting, 0);
477         assert_eq!(i.bNumEndpoints, 2);
478         assert_eq!(i.bInterfaceClass, 0xff);
479         assert_eq!(i.bInterfaceSubClass, 0x53);
480         assert_eq!(i.bInterfaceProtocol, 0xff);
481         assert_eq!(i.iInterface, 9);
482 
483         let e = i
484             .get_endpoint_descriptor(0)
485             .expect("could not get endpoint 0 descriptor");
486         assert_eq!(e.bEndpointAddress, 0x86);
487         assert_eq!(e.bmAttributes, 0x02);
488         assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
489         assert_eq!(e.bInterval, 10);
490 
491         let e = i
492             .get_endpoint_descriptor(1)
493             .expect("could not get endpoint 1 descriptor");
494         assert_eq!(e.bEndpointAddress, 0x06);
495         assert_eq!(e.bmAttributes, 0x02);
496         assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
497         assert_eq!(e.bInterval, 0);
498     }
499 
500     #[test]
parse_descriptors_adb()501     fn parse_descriptors_adb() {
502         let data: &[u8] = &[
503             0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xd1, 0x18, 0xe7, 0x4e, 0x10, 0x03,
504             0x01, 0x02, 0x03, 0x01, 0x09, 0x02, 0x20, 0x00, 0x01, 0x01, 0x00, 0x80, 0xfa, 0x09,
505             0x04, 0x00, 0x00, 0x02, 0xff, 0x42, 0x01, 0x05, 0x07, 0x05, 0x01, 0x02, 0x00, 0x02,
506             0x00, 0x07, 0x05, 0x81, 0x02, 0x00, 0x02, 0x00,
507         ];
508 
509         let d = parse_usbfs_descriptors(data).expect("parse_usbfs_descriptors failed");
510 
511         // The seemingly-redundant u16::from() calls avoid borrows of packed fields.
512 
513         assert_eq!(u16::from(d.bcdUSB), 0x02_00);
514         assert_eq!(d.bDeviceClass, 0x00);
515         assert_eq!(d.bDeviceSubClass, 0x00);
516         assert_eq!(d.bDeviceProtocol, 0x00);
517         assert_eq!(d.bMaxPacketSize0, 64);
518         assert_eq!(u16::from(d.idVendor), 0x18d1);
519         assert_eq!(u16::from(d.idProduct), 0x4ee7);
520         assert_eq!(u16::from(d.bcdDevice), 0x03_10);
521         assert_eq!(d.iManufacturer, 1);
522         assert_eq!(d.iProduct, 2);
523         assert_eq!(d.iSerialNumber, 3);
524         assert_eq!(d.bNumConfigurations, 1);
525 
526         let c = d
527             .get_config_descriptor(1)
528             .expect("could not get config descriptor 1");
529         assert_eq!(u16::from(c.wTotalLength), 32);
530         assert_eq!(c.bNumInterfaces, 1);
531         assert_eq!(c.bConfigurationValue, 1);
532         assert_eq!(c.iConfiguration, 0);
533         assert_eq!(c.bmAttributes, 0x80);
534         assert_eq!(c.bMaxPower, 250);
535 
536         let i = c
537             .get_interface_descriptor(0, 0)
538             .expect("could not get interface descriptor 0 alt setting 0");
539         assert_eq!(i.bInterfaceNumber, 0);
540         assert_eq!(i.bAlternateSetting, 0);
541         assert_eq!(i.bNumEndpoints, 2);
542         assert_eq!(i.bInterfaceClass, 0xff);
543         assert_eq!(i.bInterfaceSubClass, 0x42);
544         assert_eq!(i.bInterfaceProtocol, 0x01);
545         assert_eq!(i.iInterface, 5);
546 
547         let e = i
548             .get_endpoint_descriptor(0)
549             .expect("could not get endpoint 0 descriptor");
550         assert_eq!(e.bEndpointAddress, 0x01);
551         assert_eq!(e.bmAttributes, 0x02);
552         assert_eq!(u16::from(e.wMaxPacketSize), 0x200);
553         assert_eq!(e.bInterval, 0);
554 
555         let e = i
556             .get_endpoint_descriptor(1)
557             .expect("could not get endpoint 1 descriptor");
558         assert_eq!(e.bEndpointAddress, 0x81);
559         assert_eq!(e.bmAttributes, 0x02);
560         assert_eq!(u16::from(e.wMaxPacketSize), 0x0200);
561         assert_eq!(e.bInterval, 0);
562     }
563 
564     #[test]
parse_descriptors_multiple_altsettings()565     fn parse_descriptors_multiple_altsettings() {
566         let data: &[u8] = &[
567             // DeviceDescriptor
568             0x12, 0x01, 0x00, 0x02, 0xef, 0x02, 0x01, 0x40, 0x6d, 0x04, 0x43, 0x08, 0x13, 0x00,
569             0x00, 0x02, 0x01, 0x01, // ConfigDescriptor
570             0x09, 0x02, 0x0d, 0x0a, 0x03, 0x01, 0x00, 0x80, 0xfa,
571             // InterfaceDescriptor 0, 0
572             0x09, 0x04, 0x00, 0x00, 0x01, 0x0e, 0x01, 0x00, 0x00, // EndpointDescriptor
573             0x07, 0x05, 0x86, 0x03, 0x40, 0x00, 0x08, // InterfaceDescriptor 1, 0
574             0x09, 0x04, 0x01, 0x00, 0x00, 0x0e, 0x02, 0x00, 0x00,
575             // InterfaceDescriptor 1, 1
576             0x09, 0x04, 0x01, 0x01, 0x01, 0x0e, 0x02, 0x00, 0x00, // EndpointDescriptor
577             0x07, 0x05, 0x81, 0x05, 0xc0, 0x00, 0x01, // InterfaceDescriptor 2, 0
578             0x09, 0x04, 0x02, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00,
579         ];
580 
581         let d = parse_usbfs_descriptors(data).expect("parse_usbfs_descriptors failed");
582 
583         // The seemingly-redundant u16::from() calls avoid borrows of packed fields.
584 
585         assert_eq!(u16::from(d.bcdUSB), 0x02_00);
586         assert_eq!(d.bDeviceClass, 0xef);
587         assert_eq!(d.bDeviceSubClass, 0x02);
588         assert_eq!(d.bDeviceProtocol, 0x01);
589         assert_eq!(d.bMaxPacketSize0, 64);
590         assert_eq!(u16::from(d.idVendor), 0x046d);
591         assert_eq!(u16::from(d.idProduct), 0x0843);
592         assert_eq!(u16::from(d.bcdDevice), 0x00_13);
593         assert_eq!(d.iManufacturer, 0);
594         assert_eq!(d.iProduct, 2);
595         assert_eq!(d.iSerialNumber, 1);
596         assert_eq!(d.bNumConfigurations, 1);
597 
598         let c = d
599             .get_config_descriptor(1)
600             .expect("could not get config descriptor 1");
601         assert_eq!(u16::from(c.wTotalLength), 2573);
602         assert_eq!(c.bNumInterfaces, 3);
603         assert_eq!(c.bConfigurationValue, 1);
604         assert_eq!(c.iConfiguration, 0);
605         assert_eq!(c.bmAttributes, 0x80);
606         assert_eq!(c.bMaxPower, 250);
607 
608         let i = c
609             .get_interface_descriptor(0, 0)
610             .expect("could not get interface descriptor 0 alt setting 0");
611         assert_eq!(i.bInterfaceNumber, 0);
612         assert_eq!(i.bAlternateSetting, 0);
613         assert_eq!(i.bNumEndpoints, 1);
614         assert_eq!(i.bInterfaceClass, 0x0e);
615         assert_eq!(i.bInterfaceSubClass, 0x01);
616         assert_eq!(i.bInterfaceProtocol, 0x00);
617         assert_eq!(i.iInterface, 0x00);
618 
619         let e = i
620             .get_endpoint_descriptor(0)
621             .expect("could not get endpoint 0 descriptor");
622         assert_eq!(e.bEndpointAddress, 0x86);
623         assert_eq!(e.bmAttributes, 0x03);
624         assert_eq!(u16::from(e.wMaxPacketSize), 0x40);
625         assert_eq!(e.bInterval, 0x08);
626 
627         let i = c
628             .get_interface_descriptor(1, 0)
629             .expect("could not get interface descriptor 1 alt setting 0");
630         assert_eq!(i.bInterfaceNumber, 1);
631         assert_eq!(i.bAlternateSetting, 0);
632         assert_eq!(i.bNumEndpoints, 0);
633         assert_eq!(i.bInterfaceClass, 0x0e);
634         assert_eq!(i.bInterfaceSubClass, 0x02);
635         assert_eq!(i.bInterfaceProtocol, 0x00);
636         assert_eq!(i.iInterface, 0x00);
637 
638         let i = c
639             .get_interface_descriptor(1, 1)
640             .expect("could not get interface descriptor 1 alt setting 1");
641         assert_eq!(i.bInterfaceNumber, 1);
642         assert_eq!(i.bAlternateSetting, 1);
643         assert_eq!(i.bNumEndpoints, 1);
644         assert_eq!(i.bInterfaceClass, 0x0e);
645         assert_eq!(i.bInterfaceSubClass, 0x02);
646         assert_eq!(i.bInterfaceProtocol, 0x00);
647         assert_eq!(i.iInterface, 0x00);
648 
649         let e = i
650             .get_endpoint_descriptor(0)
651             .expect("could not get endpoint 0 descriptor");
652         assert_eq!(e.bEndpointAddress, 0x81);
653         assert_eq!(e.bmAttributes, 0x05);
654         assert_eq!(u16::from(e.wMaxPacketSize), 0xc0);
655         assert_eq!(e.bInterval, 0x01);
656 
657         let i = c
658             .get_interface_descriptor(2, 0)
659             .expect("could not get interface descriptor 2 alt setting 0");
660         assert_eq!(i.bInterfaceNumber, 2);
661         assert_eq!(i.bAlternateSetting, 0);
662         assert_eq!(i.bNumEndpoints, 0);
663         assert_eq!(i.bInterfaceClass, 0x01);
664         assert_eq!(i.bInterfaceSubClass, 0x01);
665         assert_eq!(i.bInterfaceProtocol, 0x00);
666         assert_eq!(i.iInterface, 0x00);
667     }
668 
669     #[test]
parse_descriptors_length_0()670     fn parse_descriptors_length_0() {
671         // Device descriptor followed by a bogus descriptor with bLength == 0.
672         // Note that this was generated by a fuzzer, so field values may not make sense.
673         let data: &[u8] = &[
674             0x10, 0x00, 0x18, 0x25, 0x80, 0x80, 0xAC, 0x03, 0x22, 0x05, 0x00, 0x00, 0x00, 0x00,
675             0xC3, 0x2A, 0x00, 0x32, 0x00,
676         ];
677 
678         let d = parse_usbfs_descriptors(data);
679         if d.is_ok() {
680             panic!("parse_usbfs_descriptors should have failed");
681         }
682     }
683 }
684