Skip to main content

usb_stack/
descriptor.rs

1//! USB descriptor vocabulary shared by both directions of the stack.
2//!
3//! [`descriptor_type`] holds the `bDescriptorType` codes, [`class_code`] the
4//! `bDeviceClass`/`bInterfaceClass` codes and the `cdc_*` modules the CDC
5//! function descriptors. The device-side builder that writes descriptors into a
6//! configuration buffer lives in [`crate::device::descriptor`].
7
8// ─── Standard descriptor type codes ──────────────────────────────────────────
9
10/// `bDescriptorType` values (USB 2.0 §9.4).
11#[allow(missing_docs)]
12pub mod descriptor_type {
13    pub const DEVICE: u8 = 1;
14    pub const CONFIGURATION: u8 = 2;
15    pub const STRING: u8 = 3;
16    pub const INTERFACE: u8 = 4;
17    pub const ENDPOINT: u8 = 5;
18    pub const DEVICE_QUALIFIER: u8 = 6;
19    pub const OTHER_SPEED_CONFIGURATION: u8 = 7;
20    pub const INTERFACE_ASSOCIATION: u8 = 11;
21    /// Alias of [`INTERFACE_ASSOCIATION`].
22    pub const IAD: u8 = INTERFACE_ASSOCIATION;
23    pub const BOS: u8 = 15;
24    pub const DEVICE_CAPABILITY: u8 = 16;
25    /// Alias of [`DEVICE_CAPABILITY`].
26    pub const CAPABILITY: u8 = DEVICE_CAPABILITY;
27    /// Hub class descriptor, read with a class-specific `GET_DESCRIPTOR`.
28    pub const HUB: u8 = 0x29;
29}
30
31/// Hub class constants (USB 2.0 §11.24).
32pub mod hub {
33    /// Port feature selectors, used with `SET_FEATURE` / `CLEAR_FEATURE`
34    /// addressed to [`Recipient::Other`](crate::request::Recipient::Other).
35    #[allow(missing_docs)]
36    pub mod feature {
37        pub const PORT_CONNECTION: u16 = 0;
38        pub const PORT_ENABLE: u16 = 1;
39        pub const PORT_SUSPEND: u16 = 2;
40        pub const PORT_OVER_CURRENT: u16 = 3;
41        pub const PORT_RESET: u16 = 4;
42        pub const PORT_POWER: u16 = 8;
43        pub const PORT_LOW_SPEED: u16 = 9;
44        pub const C_PORT_CONNECTION: u16 = 16;
45        pub const C_PORT_ENABLE: u16 = 17;
46        pub const C_PORT_SUSPEND: u16 = 18;
47        pub const C_PORT_OVER_CURRENT: u16 = 19;
48        pub const C_PORT_RESET: u16 = 20;
49        pub const PORT_TEST: u16 = 21;
50        pub const PORT_INDICATOR: u16 = 22;
51    }
52
53    /// `wPortStatus` bits.
54    #[allow(missing_docs)]
55    pub mod port_status {
56        pub const CONNECTION: u16 = 1 << 0;
57        pub const ENABLE: u16 = 1 << 1;
58        pub const SUSPEND: u16 = 1 << 2;
59        pub const OVER_CURRENT: u16 = 1 << 3;
60        pub const RESET: u16 = 1 << 4;
61        pub const POWER: u16 = 1 << 8;
62        pub const LOW_SPEED: u16 = 1 << 9;
63        pub const HIGH_SPEED: u16 = 1 << 10;
64        pub const TEST_MODE: u16 = 1 << 11;
65        pub const INDICATOR: u16 = 1 << 12;
66    }
67
68    /// `wPortChange` bits.
69    #[allow(missing_docs)]
70    pub mod port_change {
71        pub const CONNECTION: u16 = 1 << 0;
72        pub const ENABLE: u16 = 1 << 1;
73        pub const SUSPEND: u16 = 1 << 2;
74        pub const OVER_CURRENT: u16 = 1 << 3;
75        pub const RESET: u16 = 1 << 4;
76    }
77
78    /// `wHubCharacteristics` bits.
79    #[allow(missing_docs)]
80    pub mod characteristics {
81        /// Logical power switching mode, `[1:0]`.
82        pub const POWER_SWITCHING_MASK: u16 = 0x3;
83        pub const POWER_SWITCHING_GANGED: u16 = 0;
84        pub const POWER_SWITCHING_INDIVIDUAL: u16 = 1;
85        pub const POWER_SWITCHING_NONE: u16 = 2;
86        /// Over-current protection mode, `[4:3]`.
87        pub const OVER_CURRENT_MASK: u16 = 0x3 << 3;
88        pub const OVER_CURRENT_GLOBAL: u16 = 0;
89        pub const OVER_CURRENT_INDIVIDUAL: u16 = 1;
90        pub const OVER_CURRENT_NONE: u16 = 2;
91        /// Transaction translator think time, `[6:5]`, in high-speed hubs.
92        pub const TT_THINK_TIME_MASK: u16 = 0x3 << 5;
93        pub const TT_THINK_TIME_SHIFT: u16 = 5;
94        /// The hub has per-port status indicators.
95        pub const PORT_INDICATORS: u16 = 1 << 7;
96    }
97}
98
99/// USB language IDs (ISO 639).
100pub mod lang_id {
101    /// English (United States).
102    pub const EN_US: u16 = 0x0409;
103}
104
105// ─── USB class / subclass / protocol constants ────────────────────────────────
106
107/// USB class codes (`bDeviceClass` / `bInterfaceClass`).
108#[allow(missing_docs)]
109pub mod class_code {
110    pub const CDC: u8 = 0x02;
111    pub const CDC_DATA: u8 = 0x0A;
112    pub const HID: u8 = 0x03;
113    pub const HUB: u8 = 0x09;
114    pub const MISC: u8 = 0xEF;
115    pub const NONE: u8 = 0x00;
116    /// Vendor-specific interface/device class.
117    pub const VENDOR: u8 = 0xFF;
118}
119
120/// CDC interface subclasses (`bInterfaceSubClass`).
121#[allow(missing_docs)]
122pub mod cdc_subclass {
123    pub const ACM: u8 = 0x02;
124}
125
126/// CDC interface protocols (`bInterfaceProtocol`).
127#[allow(missing_docs)]
128pub mod cdc_protocol {
129    pub const AT_COMMANDS: u8 = 0x01;
130    pub const NONE: u8 = 0x00;
131}
132
133/// CDC functional descriptors subtypes.
134#[allow(missing_docs)]
135pub mod cdc_descriptor {
136    /// bDescriptorType for class-specific interface descriptors.
137    pub const CS_INTERFACE: u8 = 0x24;
138    pub const HEADER: u8 = 0x00;
139    pub const CALL_MANAGEMENT: u8 = 0x01;
140    pub const ACM: u8 = 0x02;
141    pub const ABSTRACT_CONTROL_MANAGEMENT: u8 = 0x02;
142    pub const UNION: u8 = 0x06;
143}