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
use core::cmp::min;

use crate::bus::{InterfaceNumber, StringIndex, UsbBus};
use crate::device;
use crate::endpoint::{Endpoint, EndpointDirection};
use crate::{Result, UsbError};

/// Standard descriptor types
#[allow(missing_docs)]
pub mod descriptor_type {
    pub const DEVICE: u8 = 1;
    pub const CONFIGURATION: u8 = 2;
    pub const STRING: u8 = 3;
    pub const INTERFACE: u8 = 4;
    pub const ENDPOINT: u8 = 5;
    pub const IAD: u8 = 11;
    pub const BOS: u8 = 15;
    pub const CAPABILITY: u8 = 16;
}

/// String descriptor language IDs.
pub mod lang_id {
    /// English (US)
    ///
    /// Recommended for use as the first language ID for compatibility.
    pub const ENGLISH_US: u16 = 0x0409;
}

/// Standard capability descriptor types
#[allow(missing_docs)]
pub mod capability_type {
    pub const WIRELESS_USB: u8 = 1;
    pub const USB_2_0_EXTENSION: u8 = 2;
    pub const SS_USB_DEVICE: u8 = 3;
    pub const CONTAINER_ID: u8 = 4;
    pub const PLATFORM: u8 = 5;
}

/// A writer for USB descriptors.
pub struct DescriptorWriter<'a> {
    buf: &'a mut [u8],
    position: usize,
    num_interfaces_mark: Option<usize>,
    num_endpoints_mark: Option<usize>,
    write_iads: bool,
}

impl DescriptorWriter<'_> {
    pub(crate) fn new(buf: &mut [u8]) -> DescriptorWriter<'_> {
        DescriptorWriter {
            buf,
            position: 0,
            num_interfaces_mark: None,
            num_endpoints_mark: None,
            write_iads: false,
        }
    }

    /// Gets the current position in the buffer, i.e. the number of bytes written so far.
    pub fn position(&self) -> usize {
        self.position
    }

    /// Writes an arbitrary (usually class-specific) descriptor.
    pub fn write(&mut self, descriptor_type: u8, descriptor: &[u8]) -> Result<()> {
        self.write_with(descriptor_type, |buf| {
            if descriptor.len() > buf.len() {
                return Err(UsbError::BufferOverflow);
            }

            buf[..descriptor.len()].copy_from_slice(descriptor);

            Ok(descriptor.len())
        })
    }

    /// Writes an arbitrary (usually class-specific) descriptor by using a callback function.
    ///
    /// The callback function gets a reference to the remaining buffer space, and it should write
    /// the descriptor into it and return the number of bytes written. If the descriptor doesn't
    /// fit, the function should return `Err(UsbError::BufferOverflow)`. That and any error returned
    /// by it will be propagated up.
    pub fn write_with(
        &mut self,
        descriptor_type: u8,
        f: impl FnOnce(&mut [u8]) -> Result<usize>,
    ) -> Result<()> {
        if self.position + 2 > self.buf.len() {
            return Err(UsbError::BufferOverflow);
        }

        let data_end = min(self.buf.len(), self.position + 256);
        let data_buf = &mut self.buf[self.position + 2..data_end];

        let total_len = f(data_buf)? + 2;

        if self.position + total_len > self.buf.len() {
            return Err(UsbError::BufferOverflow);
        }

        self.buf[self.position] = total_len as u8;
        self.buf[self.position + 1] = descriptor_type;

        self.position += total_len;

        Ok(())
    }

    pub(crate) fn device(&mut self, config: &device::Config) -> Result<()> {
        self.write(
            descriptor_type::DEVICE,
            &[
                0x10,
                0x02,                     // bcdUSB 2.1
                config.device_class,      // bDeviceClass
                config.device_sub_class,  // bDeviceSubClass
                config.device_protocol,   // bDeviceProtocol
                config.max_packet_size_0, // bMaxPacketSize0
                config.vendor_id as u8,
                (config.vendor_id >> 8) as u8, // idVendor
                config.product_id as u8,
                (config.product_id >> 8) as u8, // idProduct
                config.device_release as u8,
                (config.device_release >> 8) as u8,    // bcdDevice
                config.manufacturer.map_or(0, |_| 1),  // iManufacturer
                config.product.map_or(0, |_| 2),       // iProduct
                config.serial_number.map_or(0, |_| 3), // iSerialNumber
                1,                                     // bNumConfigurations
            ],
        )
    }

    pub(crate) fn configuration(&mut self, config: &device::Config) -> Result<()> {
        self.num_interfaces_mark = Some(self.position + 4);

        self.write_iads = config.composite_with_iads;

        self.write(
            descriptor_type::CONFIGURATION,
            &[
                0,
                0,                           // wTotalLength
                0,                           // bNumInterfaces
                device::CONFIGURATION_VALUE, // bConfigurationValue
                0,                           // iConfiguration
                0x80 | if config.self_powered { 0x40 } else { 0x00 }
                    | if config.supports_remote_wakeup {
                        0x20
                    } else {
                        0x00
                    }, // bmAttributes
                config.max_power,            // bMaxPower
            ],
        )
    }

    pub(crate) fn end_class(&mut self) {
        self.num_endpoints_mark = None;
    }

    pub(crate) fn end_configuration(&mut self) {
        let position = self.position as u16;
        self.buf[2..4].copy_from_slice(&position.to_le_bytes());
    }

    /// Writes a interface association descriptor. Call from `UsbClass::get_configuration_descriptors`
    /// before writing the USB class or function's interface descriptors if your class has more than
    /// one interface and wants to play nicely with composite devices on Windows. If the USB device
    /// hosting the class was not configured as composite with IADs enabled, calling this function
    /// does nothing, so it is safe to call from libraries.
    ///
    /// # Arguments
    ///
    /// * `first_interface` - Number of the function's first interface, previously allocated with
    ///   [`UsbBusAllocator::interface`](crate::bus::UsbBusAllocator::interface).
    /// * `interface_count` - Number of interfaces in the function.
    /// * `function_class` - Class code assigned by USB.org. Use `0xff` for vendor-specific devices
    ///   that do not conform to any class.
    /// * `function_sub_class` - Sub-class code. Depends on class.
    /// * `function_protocol` - Protocol code. Depends on class and sub-class.
    pub fn iad(
        &mut self,
        first_interface: InterfaceNumber,
        interface_count: u8,
        function_class: u8,
        function_sub_class: u8,
        function_protocol: u8,
    ) -> Result<()> {
        if !self.write_iads {
            return Ok(());
        }

        self.write(
            descriptor_type::IAD,
            &[
                first_interface.into(), // bFirstInterface
                interface_count,        // bInterfaceCount
                function_class,
                function_sub_class,
                function_protocol,
                0,
            ],
        )?;

        Ok(())
    }

    /// Writes a interface descriptor.
    ///
    /// # Arguments
    ///
    /// * `number` - Interface number previously allocated with
    ///   [`UsbBusAllocator::interface`](crate::bus::UsbBusAllocator::interface).
    /// * `interface_class` - Class code assigned by USB.org. Use `0xff` for vendor-specific devices
    ///   that do not conform to any class.
    /// * `interface_sub_class` - Sub-class code. Depends on class.
    /// * `interface_protocol` - Protocol code. Depends on class and sub-class.
    pub fn interface(
        &mut self,
        number: InterfaceNumber,
        interface_class: u8,
        interface_sub_class: u8,
        interface_protocol: u8,
    ) -> Result<()> {
        self.interface_alt(
            number,
            device::DEFAULT_ALTERNATE_SETTING,
            interface_class,
            interface_sub_class,
            interface_protocol,
            None,
        )
    }

    /// Writes a interface descriptor with a specific alternate setting and
    /// interface string identifier.
    ///
    /// # Arguments
    ///
    /// * `number` - Interface number previously allocated with
    ///   [`UsbBusAllocator::interface`](crate::bus::UsbBusAllocator::interface).
    /// * `alternate_setting` - Number of the alternate setting
    /// * `interface_class` - Class code assigned by USB.org. Use `0xff` for vendor-specific devices
    ///   that do not conform to any class.
    /// * `interface_sub_class` - Sub-class code. Depends on class.
    /// * `interface_protocol` - Protocol code. Depends on class and sub-class.
    /// * `interface_string` - Index of string descriptor describing this interface

    pub fn interface_alt(
        &mut self,
        number: InterfaceNumber,
        alternate_setting: u8,
        interface_class: u8,
        interface_sub_class: u8,
        interface_protocol: u8,
        interface_string: Option<StringIndex>,
    ) -> Result<()> {
        if alternate_setting == device::DEFAULT_ALTERNATE_SETTING {
            match self.num_interfaces_mark {
                Some(mark) => self.buf[mark] += 1,
                None => return Err(UsbError::InvalidState),
            };
        }

        let str_index = interface_string.map_or(0, Into::into);

        self.num_endpoints_mark = Some(self.position + 4);

        self.write(
            descriptor_type::INTERFACE,
            &[
                number.into(),       // bInterfaceNumber
                alternate_setting,   // bAlternateSetting
                0,                   // bNumEndpoints
                interface_class,     // bInterfaceClass
                interface_sub_class, // bInterfaceSubClass
                interface_protocol,  // bInterfaceProtocol
                str_index,           // iInterface
            ],
        )?;

        Ok(())
    }

    /// Writes an endpoint descriptor.
    ///
    /// # Arguments
    ///
    /// * `endpoint` - Endpoint previously allocated with
    ///   [`UsbBusAllocator`](crate::bus::UsbBusAllocator).
    pub fn endpoint<'e, B: UsbBus, D: EndpointDirection>(
        &mut self,
        endpoint: &Endpoint<'e, B, D>,
    ) -> Result<()> {
        self.endpoint_ex(endpoint, |_| Ok(0))
    }

    /// Writes an endpoint descriptor with extra trailing data.
    ///
    /// This is rarely needed and shouldn't be used except for compatibility with standard USB
    /// classes that require it. Extra data is normally written in a separate class specific
    /// descriptor.
    ///
    /// # Arguments
    ///
    /// * `endpoint` - Endpoint previously allocated with
    ///   [`UsbBusAllocator`](crate::bus::UsbBusAllocator).
    /// * `f` - Callback for the extra data. See `write_with` for more information.
    pub fn endpoint_ex<'e, B: UsbBus, D: EndpointDirection>(
        &mut self,
        endpoint: &Endpoint<'e, B, D>,
        f: impl FnOnce(&mut [u8]) -> Result<usize>,
    ) -> Result<()> {
        match self.num_endpoints_mark {
            Some(mark) => self.buf[mark] += 1,
            None => return Err(UsbError::InvalidState),
        };

        self.write_with(descriptor_type::ENDPOINT, |buf| {
            if buf.len() < 5 {
                return Err(UsbError::BufferOverflow);
            }

            let mps = endpoint.max_packet_size();

            buf[0] = endpoint.address().into();
            buf[1] = endpoint.ep_type() as u8;
            buf[2] = mps as u8;
            buf[3] = (mps >> 8) as u8;
            buf[4] = endpoint.interval();

            Ok(f(&mut buf[5..])? + 5)
        })
    }

    /// Writes a string descriptor.
    pub(crate) fn string(&mut self, string: &str) -> Result<()> {
        let mut pos = self.position;

        if pos + 2 > self.buf.len() {
            return Err(UsbError::BufferOverflow);
        }

        self.buf[pos] = 0; // length placeholder
        self.buf[pos + 1] = descriptor_type::STRING;

        pos += 2;

        for c in string.encode_utf16() {
            if pos >= self.buf.len() {
                return Err(UsbError::BufferOverflow);
            }

            self.buf[pos..pos + 2].copy_from_slice(&c.to_le_bytes());
            pos += 2;
        }

        self.buf[self.position] = (pos - self.position) as u8;

        self.position = pos;

        Ok(())
    }
}

/// A writer for Binary Object Store descriptor.
pub struct BosWriter<'w, 'a: 'w> {
    writer: &'w mut DescriptorWriter<'a>,
    num_caps_mark: Option<usize>,
}

impl<'w, 'a: 'w> BosWriter<'w, 'a> {
    pub(crate) fn new(writer: &'w mut DescriptorWriter<'a>) -> Self {
        Self {
            writer,
            num_caps_mark: None,
        }
    }

    pub(crate) fn bos(&mut self) -> Result<()> {
        self.num_caps_mark = Some(self.writer.position + 4);
        self.writer.write(
            descriptor_type::BOS,
            &[
                0x00, 0x00, // wTotalLength
                0x00, // bNumDeviceCaps
            ],
        )?;

        self.capability(capability_type::USB_2_0_EXTENSION, &[0; 4])?;

        Ok(())
    }

    /// Writes capability descriptor to a BOS
    ///
    /// # Arguments
    ///
    /// * `capability_type` - Type of a capability
    /// * `data` - Binary data of the descriptor
    pub fn capability(&mut self, capability_type: u8, data: &[u8]) -> Result<()> {
        match self.num_caps_mark {
            Some(mark) => self.writer.buf[mark] += 1,
            None => return Err(UsbError::InvalidState),
        }

        let mut start = self.writer.position;
        let blen = data.len();

        if (start + blen + 3) > self.writer.buf.len() || (blen + 3) > 255 {
            return Err(UsbError::BufferOverflow);
        }

        self.writer.buf[start] = (blen + 3) as u8;
        self.writer.buf[start + 1] = descriptor_type::CAPABILITY;
        self.writer.buf[start + 2] = capability_type;

        start += 3;
        self.writer.buf[start..start + blen].copy_from_slice(data);
        self.writer.position = start + blen;

        Ok(())
    }

    pub(crate) fn end_bos(&mut self) {
        self.num_caps_mark = None;
        let position = self.writer.position as u16;
        self.writer.buf[2..4].copy_from_slice(&position.to_le_bytes());
    }
}