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
//! USB 2.0 data types and packet parser
//!
//! This library does not contain any I/O functionality. If you are looking for a device-side
//! USB library check out [`usb-device`](https://crates.io/crates/usb-device). 
//!
//! # References
//!
//! - (USB2) Universal Serial Bus Specification Revision 2.0 (April 27, 2000)
//! - (USBCDC1.2) Universal Serial Bus Class Definitions for Communications Devices 1.2 (Errata 1)
//! (November 3, 2010)
//! - (USBIAD) Interface Association Descriptors Engineering Change Notice
//! - (USBPTSN1.2) Universal Serial Bus Communication Class Subclass Specification for PTSN Devices
//!   Revision 1.2 (February 9, 2007)
//! - (HID1.11) Device Class Definition for Human Interface Devices (HID) version 1.11 (6/27/01)

#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![deny(missing_docs)]
#![no_std]

use core::num::NonZeroU8;

use crate::{
    bmrequesttype::{bmRequestType, Recipient},
    cdc::acm,
};

#[macro_use]
mod macros;

mod bmrequesttype;
mod brequest;
pub mod cdc;
pub mod configuration;
mod desc;
pub mod device;
pub mod endpoint;
mod feature;
pub mod hid;
pub mod ia;
pub mod interface;

/// The state of the USB device
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum State {
    /// The default state
    Default,
    /// The address-ed state
    Address(Address),
    /// The configured state
    Configured {
        /// The address of the device
        address: Address,
        /// The configuration value
        value: NonZeroU8,
    },
}

/// Device address assigned by the host; will be in the range 1..=127
pub type Address = NonZeroU8;

/// Endpoint address
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Endpoint {
    /// Endpoint direction
    pub direction: Direction,

    /// Endpoint number
    pub number: u8,
}

impl Endpoint {
    fn byte(&self) -> u8 {
        (self.number & 0b1111) | (self.direction as u8) << 7
    }
}

/// Direction from the point of view of the host
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Direction {
    /// Host to Device
    Out = 0,
    /// Device to Host
    In = 1,
}

/// Control endpoint requests
#[derive(Debug, PartialEq)]
pub enum Request {
    /// Standard device request
    Standard(StandardRequest),
    /// CDC Abstract Control Model interface request
    Acm(acm::Request),
    /// Human Interface Device (HID) request
    Hid(hid::Request),
}

impl Request {
    /// Parses a control endpoint request
    pub fn parse(
        bmrequesttype: u8,
        brequest: u8,
        wvalue: u16,
        windex: u16,
        wlength: u16,
    ) -> Result<Self, ()> {
        use bmrequesttype::Type;

        let bmrequesttype = bmRequestType::parse(bmrequesttype)?;

        match bmrequesttype.ty {
            Type::Standard => {
                StandardRequest::parse2(bmrequesttype, brequest, wvalue, windex, wlength)
                    .map(Request::Standard)
                    .or_else(|_| {
                        hid::Request::parse2(bmrequesttype, brequest, wvalue, windex, wlength)
                            .map(Request::Hid)
                    })
            }

            Type::Class => acm::Request::parse2(bmrequesttype, brequest, wvalue, windex, wlength)
                .map(Request::Acm)
                .or_else(|_| {
                    hid::Request::parse2(bmrequesttype, brequest, wvalue, windex, wlength)
                        .map(Request::Hid)
                }),

            _ => Err(()),
        }
    }
}

/// Standard device requests
///
/// See section 9.4 of (USB2)
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum StandardRequest {
    /// CLEAR_FEATURE
    ClearFeature(ClearFeature),
    /// GET_CONFIGURATION
    GetConfiguration,
    /// GET_DESCRIPTOR
    GetDescriptor {
        /// Requested descriptor
        descriptor: GetDescriptor,
        /// Maximum number of bytes to return
        length: u16,
    },
    /// GET_INTERFACE -- returns the alternate setting of the specified interface
    GetInterface {
        /// The interface
        interface: u8,
    },
    /// GET_STATUS
    GetStatus(GetStatus),
    /// SET_ADDRESS
    SetAddress {
        /// The new address -- `None` is used to return to the `Default` state
        address: Option<Address>,
    },
    /// SET_CONFIGURATION
    SetConfiguration {
        /// The new configuration -- `None` is used to return to the `Address` state
        value: Option<NonZeroU8>,
    },
    /// SET_DESCRIPTOR
    SetDescriptor {
        /// The descriptor to set or change
        descriptor: SetDescriptor,
        /// The length of the descriptor
        length: u16,
    },
    /// SET_FEATURE
    SetFeature(SetFeature),
    /// SET_INTERFACE -- changes the alternate setting of the specified interface
    SetInterface {
        /// The interface to modify
        interface: u8,
        /// The new alternate setting
        alternate: u8,
    },
    /// SYNCH_FRAME
    SynchFrame {
        /// The endpoint this synchronization frame is for
        endpoint: Endpoint,
    },
}

/// GET_DESCRIPTOR descriptor
///
/// See section 9.4.3 and table 9-5 of (USB2)
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum GetDescriptor {
    /// Configuration descriptor
    Configuration {
        /// Configuration descriptor index (`0..bNumConfigurations`)
        index: u8,
    },
    /// Device descriptor
    Device,
    /// Device qualifier descriptor
    DeviceQualifier,
    /// Other speed configuration descriptor
    OtherSpeedConfiguration {
        /// Other speed configuration descriptor index
        index: u8,
    },
    /// String descriptor
    String {
        /// String descriptor index
        index: u8,
        /// Language identifier
        lang_id: u16,
    },
}

/// SET_DESCRIPTOR descriptor
///
/// See section 9.4.8 and table 9-5 of (USB2)
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum SetDescriptor {
    /// Configuration descriptor
    Configuration {
        /// Configuration descriptor index (`0..bNumConfigurations`)
        index: u8,
    },
    /// Device descriptor
    Device,
    /// String descriptor
    String {
        /// String descriptor index
        index: u8,
        /// Language identifier
        lang_id: u16,
    },
}

const MAX_ADDRESS: u16 = 127;

/// CLEAR_FEATURE feature selector
///
/// See table 9-6 of (USB2)
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ClearFeature {
    /// Disables the device remote wake-up feature
    DeviceRemoteWakeup,
    /// "Unhalts" the specified endpoint
    EndpointHalt(Endpoint),
}

/// Argument of the GET_STATUS request
///
/// See section 9.4.5 of (USB2)
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum GetStatus {
    /// Device status
    Device,
    /// Endpoint status
    Endpoint(Endpoint),
    /// Interface status
    Interface(u8),
}

/// SET_FEATURE feature selector
///
/// See table 9-6 of (USB2)
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum SetFeature {
    /// Enables the device remote wake-up feature
    DeviceRemoteWakeup,
    /// Halts the specified endpoint
    EndpointHalt(Endpoint),
    /// Enables the specified test mode
    TestMode(Test),
}

repr!(u8,
      /// Test selector
      Test {
    /// Test_J
    J = 0x01,
    /// Test_K
    K = 0x02,
    /// Test_SE0_NAK
    SE0_NAK = 0x03,
    /// Test_Packet
    Packet = 0x04,
    /// Test_Force_Enable
    ForceEnable = 0x05,
});

impl StandardRequest {
    /// Parses a standard request
    pub fn parse(
        bmrequesttype: u8,
        brequest: u8,
        wvalue: u16,
        windex: u16,
        wlength: u16,
    ) -> Result<Self, ()> {
        let bmrequesttype = bmRequestType::parse(bmrequesttype)?;

        if bmrequesttype.ty != bmrequesttype::Type::Standard {
            return Err(());
        }

        Self::parse2(bmrequesttype, brequest, wvalue, windex, wlength)
    }

    fn parse2(
        bmRequestType {
            direction,
            recipient,
            // ty must be `Standard`
            ..
        }: bmRequestType,
        brequest: u8,
        wvalue: u16,
        windex: u16,
        wlength: u16,
    ) -> Result<Self, ()> {
        // See table 9-3 of (USB2)
        match (brequest, direction) {
            // see section 9.4.1 of (USB2)
            (brequest::CLEAR_FEATURE, bmrequesttype::Direction::HostToDevice)
                if recipient != Recipient::Other && wlength == 0 =>
            {
                if wvalue == feature::DEVICE_REMOTE_WAKEUP
                    && recipient == Recipient::Device
                    && windex == 0
                {
                    Ok(StandardRequest::ClearFeature(
                        ClearFeature::DeviceRemoteWakeup,
                    ))
                } else if wvalue == feature::ENDPOINT_HALT && recipient == Recipient::Endpoint {
                    Ok(StandardRequest::ClearFeature(ClearFeature::EndpointHalt(
                        windex2endpoint(windex)?,
                    )))
                } else {
                    Err(())
                }
            }

            // see section 9.4.2 of (USB2)
            (brequest::GET_CONFIGURATION, bmrequesttype::Direction::DeviceToHost)
                if recipient == Recipient::Device && wvalue == 0 && windex == 0 && wlength == 1 =>
            {
                Ok(StandardRequest::GetConfiguration)
            }

            // see section 9.4.3 of (USB2)
            (brequest::GET_DESCRIPTOR, bmrequesttype::Direction::DeviceToHost)
                if recipient == Recipient::Device =>
            {
                let desc_ty = (wvalue >> 8) as u8;
                let desc_idx = wvalue as u8;

                let ty = desc::Type::_from(desc_ty).ok_or(())?;

                let desc = match ty {
                    desc::Type::Device if desc_idx == 0 && windex == 0 => GetDescriptor::Device,
                    desc::Type::DeviceQualifier if desc_idx == 0 && windex == 0 => {
                        GetDescriptor::DeviceQualifier
                    }
                    desc::Type::Configuration if windex == 0 => {
                        GetDescriptor::Configuration { index: desc_idx }
                    }
                    desc::Type::OtherSpeedConfiguration if windex == 0 => {
                        GetDescriptor::OtherSpeedConfiguration { index: desc_idx }
                    }
                    desc::Type::String => GetDescriptor::String {
                        index: desc_idx,
                        lang_id: windex,
                    },
                    // other types cannot appear in a GET_DESCRIPTOR request
                    _ => return Err(()),
                };

                Ok(StandardRequest::GetDescriptor {
                    descriptor: desc,
                    length: wlength,
                })
            }

            // see section 9.4.4 of (USB2)
            (brequest::GET_INTERFACE, bmrequesttype::Direction::DeviceToHost)
                if recipient == Recipient::Interface && wvalue == 0 && wlength == 1 =>
            {
                Ok(StandardRequest::GetInterface {
                    interface: windex2interface(windex)?,
                })
            }

            // see section 9.4.5 of (USB2)
            (brequest::GET_STATUS, bmrequesttype::Direction::DeviceToHost)
                if wvalue == 0 && wlength == 2 =>
            {
                let status = match recipient {
                    Recipient::Device if windex == 0 => GetStatus::Device,
                    Recipient::Endpoint => GetStatus::Endpoint(windex2endpoint(windex)?),
                    Recipient::Interface => GetStatus::Interface(windex2interface(windex)?),
                    _ => return Err(()),
                };

                Ok(StandardRequest::GetStatus(status))
            }

            // see section 9.4.6 of (USB2)
            (brequest::SET_ADDRESS, bmrequesttype::Direction::HostToDevice)
                if recipient == Recipient::Device
                    && windex == 0
                    && wlength == 0
                    && wvalue <= MAX_ADDRESS =>
            {
                let address = NonZeroU8::new(wvalue as u8);
                Ok(StandardRequest::SetAddress { address })
            }

            // see section 9.4.7 of (USB2)
            (brequest::SET_CONFIGURATION, bmrequesttype::Direction::HostToDevice)
                if recipient == Recipient::Device
                    && windex == 0
                    && wlength == 0
                    && wvalue >> 8 == 0 =>
            {
                Ok(StandardRequest::SetConfiguration {
                    value: NonZeroU8::new(wvalue as u8),
                })
            }

            (brequest::SET_DESCRIPTOR, bmrequesttype::Direction::HostToDevice)
                if recipient == Recipient::Device =>
            {
                let desc_ty = (wvalue >> 8) as u8;
                let desc_idx = wvalue as u8;

                let ty = desc::Type::_from(desc_ty).ok_or(())?;

                let desc = match ty {
                    desc::Type::Device if desc_idx == 0 && windex == 0 => SetDescriptor::Device,
                    desc::Type::Configuration if windex == 0 => {
                        SetDescriptor::Configuration { index: desc_idx }
                    }
                    desc::Type::String => SetDescriptor::String {
                        index: desc_idx,
                        lang_id: windex,
                    },
                    // other types cannot appear in a SET_DESCRIPTOR request
                    _ => return Err(()),
                };

                Ok(StandardRequest::SetDescriptor {
                    descriptor: desc,
                    length: wlength,
                })
            }

            (brequest::SET_FEATURE, bmrequesttype::Direction::HostToDevice) if wlength == 0 => {
                let feature = if wvalue == feature::DEVICE_REMOTE_WAKEUP
                    && recipient == Recipient::Device
                    && windex == 0
                {
                    SetFeature::DeviceRemoteWakeup
                } else if wvalue == feature::TEST_MODE
                    && recipient == Recipient::Device
                    && windex as u8 == 0
                {
                    SetFeature::TestMode(Test::_from((windex >> 8) as u8).ok_or(())?)
                } else if wvalue == feature::ENDPOINT_HALT && recipient == Recipient::Endpoint {
                    SetFeature::EndpointHalt(windex2endpoint(windex)?)
                } else {
                    return Err(());
                };

                Ok(StandardRequest::SetFeature(feature))
            }

            (brequest::SET_INTERFACE, bmrequesttype::Direction::HostToDevice)
                if recipient == Recipient::Interface && wlength == 0 =>
            {
                let interface = windex2interface(windex)?;
                let alternate = windex2interface(wvalue)?;

                Ok(StandardRequest::SetInterface {
                    interface,
                    alternate,
                })
            }

            (brequest::SYNCH_FRAME, bmrequesttype::Direction::DeviceToHost)
                if recipient == Recipient::Endpoint && wvalue == 0 && wlength == 2 =>
            {
                Ok(StandardRequest::SynchFrame {
                    endpoint: windex2endpoint(windex)?,
                })
            }

            _ => Err(()),
        }
    }
}

fn windex2endpoint(windex: u16) -> Result<Endpoint, ()> {
    if windex >> 8 != 0 {
        return Err(());
    }

    let windex = windex as u8;
    let direction = windex >> 4;
    let direction = if direction == 0b0000 {
        Direction::Out
    } else if direction == 0b1000 {
        Direction::In
    } else {
        return Err(());
    };

    Ok(Endpoint {
        direction,
        number: windex & 0b1111,
    })
}

fn windex2interface(windex: u16) -> Result<u8, ()> {
    if windex >> 8 != 0 {
        Err(())
    } else {
        Ok(windex as u8)
    }
}

#[cfg(test)]
mod tests {
    use core::num::NonZeroU8;

    use crate::{Direction, Endpoint, GetDescriptor, StandardRequest};

    #[test]
    fn endpoint() {
        assert_eq!(
            crate::windex2endpoint(0x0080),
            Ok(Endpoint {
                direction: Direction::In,
                number: 0
            })
        );

        assert_eq!(
            crate::windex2endpoint(0x0000),
            Ok(Endpoint {
                direction: Direction::Out,
                number: 0
            })
        );

        assert!(crate::windex2endpoint(0x0010).is_err());
        assert!(crate::windex2endpoint(0x0090).is_err());
    }

    #[test]
    fn get_descriptor_device() {
        assert_eq!(
            StandardRequest::parse(0b1000_0000, 0x06, 0x01_00, 0, 18),
            Ok(StandardRequest::GetDescriptor {
                descriptor: GetDescriptor::Device,
                length: 18
            })
        );

        // wrong descriptor index
        assert!(StandardRequest::parse(0b1000_0000, 0x06, 0x01_01, 0, 18).is_err(),);

        // language ID
        assert!(StandardRequest::parse(0b1000_0000, 0x06, 0x01_00, 1033, 18).is_err(),);
    }

    #[test]
    fn get_descriptor_configuration() {
        // GET_DESCRIPTOR Configuration 0
        assert_eq!(
            StandardRequest::parse(0b1000_0000, 0x06, 0x02_00, 0, 9),
            Ok(StandardRequest::GetDescriptor {
                descriptor: GetDescriptor::Configuration { index: 0 },
                length: 9
            })
        );

        assert!(StandardRequest::parse(0b1000_0000, 0x06, 0x02_00, 1033, 9).is_err());
    }

    #[test]
    fn set_address() {
        // SET_ADDRESS 16
        assert_eq!(
            StandardRequest::parse(0b0000_0000, 0x05, 0x00_10, 0, 0),
            Ok(StandardRequest::SetAddress {
                address: NonZeroU8::new(16)
            })
        );

        // has language id but shouldn't
        assert!(StandardRequest::parse(0b0000_0000, 0x05, 0x00_10, 1033, 0).is_err());

        // length should be zero
        assert!(StandardRequest::parse(0b0000_0000, 0x05, 0x00_10, 0, 1).is_err());
    }

    #[test]
    fn set_configuration() {
        // SET_CONFIGURATION 1
        assert_eq!(
            StandardRequest::parse(0b0000_0000, 0x09, 0x00_01, 0, 0),
            Ok(StandardRequest::SetConfiguration {
                value: NonZeroU8::new(1)
            })
        );

        // has language id but shouldn't
        assert!(StandardRequest::parse(0b0000_0000, 0x09, 0x00_01, 1033, 0).is_err());

        // length should be zero
        assert!(StandardRequest::parse(0b0000_0000, 0x09, 0x00_01, 0, 1).is_err());
    }
}