usb_if/descriptor/
class_code.rs

1/// USB Device Class Codes as defined by USB-IF
2/// https://www.usb.org/defined-class-codes
3#[derive(Debug, Clone, Copy)]
4pub enum Class {
5    /// Use class information in the Interface Descriptors
6    ClassInInterface,
7    /// Audio device
8    Audio,
9    /// Communications and CDC Control
10    Communication,
11    /// HID (Human Interface Device)
12    Hid,
13    /// Physical device
14    Physical,
15    /// Still Imaging device
16    StillImaging,
17    /// Printer device
18    Printer,
19    /// Mass Storage device
20    MassStorage,
21    /// Hub device
22    Hub(HubSpeed),
23    /// CDC-Data
24    CdcData,
25    /// Smart Card device
26    SmartCard,
27    /// Content Security device
28    ContentSecurity,
29    /// Video device
30    Video,
31    /// Personal Healthcare device
32    PersonalHealthcare,
33    /// Audio/Video Devices
34    AudioVideo(AudioVideoType),
35    /// Billboard Device
36    Billboard,
37    /// USB Type-C Bridge Device
38    TypeCBridge,
39    /// USB Bulk Display Protocol Device
40    BulkDisplayProtocol,
41    /// MCTP over USB Protocol Endpoint Device
42    MctpOverUsb(MctpType),
43    /// I3C Device
44    I3c,
45    /// Diagnostic Device
46    Diagnostic(DiagnosticType),
47    /// Wireless Controller
48    Wireless(WirelessType),
49    /// Miscellaneous
50    Miscellaneous(MiscellaneousType),
51    /// Application Specific
52    Application(ApplicationType),
53    /// Vendor Specific
54    Vendor,
55    /// Unknown/Other class codes
56    Unknown {
57        class: u8,
58        subclass: u8,
59        protocol: u8,
60    },
61}
62
63#[derive(Debug, Clone, Copy)]
64pub enum HubSpeed {
65    Full,
66    HiSpeedSignalTT,
67    HiSpeedMultipleTTs,
68    Unknown,
69}
70
71#[derive(Debug, Clone, Copy)]
72pub enum AudioVideoType {
73    AvControl,
74    AvDataVideoStreaming,
75    AvDataAudioStreaming,
76}
77
78#[derive(Debug, Clone, Copy)]
79pub enum MctpType {
80    ManagementControllerEndpoint(MctpVersion),
81    HostInterfaceEndpoint(MctpVersion),
82}
83
84#[derive(Debug, Clone, Copy)]
85pub enum MctpVersion {
86    V1x,
87    V2x,
88}
89
90#[derive(Debug, Clone, Copy)]
91pub enum DiagnosticType {
92    Usb2Compliance,
93    DebugTarget(DebugProtocol),
94    Trace(TraceProtocol),
95    Dfx(DfxProtocol),
96    Unknown(u8, u8), // (subclass, protocol)
97}
98
99#[derive(Debug, Clone, Copy)]
100pub enum DebugProtocol {
101    VendorDefined,
102    GnuRemoteDebug,
103}
104
105#[derive(Debug, Clone, Copy)]
106pub enum TraceProtocol {
107    VendorDefined,
108}
109
110#[derive(Debug, Clone, Copy)]
111pub enum DfxProtocol {
112    VendorDefined,
113}
114
115#[derive(Debug, Clone, Copy)]
116pub enum WirelessType {
117    BluetoothProgramming,
118    UwbRadioControl,
119    RemoteNdis,
120    BluetoothAmp,
121    HostWireAdapter(WireAdapterInterface),
122    DeviceWireAdapter(WireAdapterInterface),
123}
124
125#[derive(Debug, Clone, Copy)]
126pub enum WireAdapterInterface {
127    ControlData,
128    Isochronous,
129}
130
131#[derive(Debug, Clone, Copy)]
132pub enum MiscellaneousType {
133    ActiveSync,
134    PalmSync,
135    InterfaceAssociation,
136    WireAdapterMultifunction,
137    CableBasedAssociation,
138    Rndis(RndisType),
139    Usb3Vision(VisionInterface),
140    Step(StepType),
141    DvbCi(DvbInterface),
142}
143
144#[derive(Debug, Clone, Copy)]
145pub enum RndisType {
146    Ethernet,
147    Wifi,
148    Wimax,
149    Wwan,
150    RawIpv4,
151    RawIpv6,
152    Gprs,
153}
154
155#[derive(Debug, Clone, Copy)]
156pub enum VisionInterface {
157    Control,
158    Event,
159    Streaming,
160}
161
162#[derive(Debug, Clone, Copy)]
163pub enum StepType {
164    Step,
165    StepRaw,
166}
167
168#[derive(Debug, Clone, Copy)]
169pub enum DvbInterface {
170    CommandInIad,
171    CommandInInterface,
172    MediaInInterface,
173}
174
175#[derive(Debug, Clone, Copy)]
176pub enum ApplicationType {
177    DeviceFirmwareUpgrade,
178    IrdaBridge,
179    TestMeasurement(TestMeasurementType),
180}
181
182#[derive(Debug, Clone, Copy)]
183pub enum TestMeasurementType {
184    Standard,
185    Usb488Subclass,
186}
187
188impl Class {
189    pub fn from_class_and_subclass(class: u8, subclass: u8, protocol: u8) -> Self {
190        match (class, subclass, protocol) {
191            // Base Class 00h - Use class information in Interface Descriptors
192            (0x00, 0x00, 0x00) => Self::ClassInInterface,
193
194            // Base Class 01h - Audio
195            (0x01, _, _) => Self::Audio,
196
197            // Base Class 02h - Communications and CDC Control
198            (0x02, _, _) => Self::Communication,
199
200            // Base Class 03h - HID
201            (0x03, _, _) => Self::Hid,
202
203            // Base Class 05h - Physical
204            (0x05, _, _) => Self::Physical,
205
206            // Base Class 06h - Still Imaging
207            (0x06, 0x01, 0x01) => Self::StillImaging,
208
209            // Base Class 07h - Printer
210            (0x07, _, _) => Self::Printer,
211
212            // Base Class 08h - Mass Storage
213            (0x08, _, _) => Self::MassStorage,
214
215            // Base Class 09h - Hub
216            (0x09, _, 0x00) => Self::Hub(HubSpeed::Full),
217            (0x09, _, 0x01) => Self::Hub(HubSpeed::HiSpeedSignalTT),
218            (0x09, _, 0x02) => Self::Hub(HubSpeed::HiSpeedMultipleTTs),
219            (0x09, _, _) => Self::Hub(HubSpeed::Unknown),
220
221            // Base Class 0Ah - CDC-Data
222            (0x0A, _, _) => Self::CdcData,
223
224            // Base Class 0Bh - Smart Card
225            (0x0B, _, _) => Self::SmartCard,
226
227            // Base Class 0Dh - Content Security
228            (0x0D, _, _) => Self::ContentSecurity,
229
230            // Base Class 0Eh - Video
231            (0x0E, _, _) => Self::Video,
232
233            // Base Class 0Fh - Personal Healthcare
234            (0x0F, _, _) => Self::PersonalHealthcare,
235
236            // Base Class 10h - Audio/Video Devices
237            (0x10, 0x01, _) => Self::AudioVideo(AudioVideoType::AvControl),
238            (0x10, 0x02, _) => Self::AudioVideo(AudioVideoType::AvDataVideoStreaming),
239            (0x10, 0x03, _) => Self::AudioVideo(AudioVideoType::AvDataAudioStreaming),
240
241            // Base Class 11h - Billboard Device
242            (0x11, _, _) => Self::Billboard,
243
244            // Base Class 12h - USB Type-C Bridge
245            (0x12, _, _) => Self::TypeCBridge,
246
247            // Base Class 13h - USB Bulk Display Protocol
248            (0x13, _, _) => Self::BulkDisplayProtocol,
249
250            // Base Class 14h - MCTP over USB
251            (0x14, 0x00, 0x01) => {
252                Self::MctpOverUsb(MctpType::ManagementControllerEndpoint(MctpVersion::V1x))
253            }
254            (0x14, 0x00, 0x02) => {
255                Self::MctpOverUsb(MctpType::ManagementControllerEndpoint(MctpVersion::V2x))
256            }
257            (0x14, 0x01, 0x01) => {
258                Self::MctpOverUsb(MctpType::HostInterfaceEndpoint(MctpVersion::V1x))
259            }
260            (0x14, 0x01, 0x02) => {
261                Self::MctpOverUsb(MctpType::HostInterfaceEndpoint(MctpVersion::V2x))
262            }
263
264            // Base Class 3Ch - I3C Device
265            (0x3C, _, _) => Self::I3c,
266
267            // Base Class DCh - Diagnostic Device
268            (0xDC, 0x01, 0x01) => Self::Diagnostic(DiagnosticType::Usb2Compliance),
269            (0xDC, 0x02, 0x00) => {
270                Self::Diagnostic(DiagnosticType::DebugTarget(DebugProtocol::VendorDefined))
271            }
272            (0xDC, 0x02, 0x01) => {
273                Self::Diagnostic(DiagnosticType::DebugTarget(DebugProtocol::GnuRemoteDebug))
274            }
275            (0xDC, 0x03, 0x01) => {
276                Self::Diagnostic(DiagnosticType::Trace(TraceProtocol::VendorDefined))
277            }
278            (0xDC, 0x04, 0x01) => Self::Diagnostic(DiagnosticType::Dfx(DfxProtocol::VendorDefined)),
279            (0xDC, subclass, protocol) => {
280                Self::Diagnostic(DiagnosticType::Unknown(subclass, protocol))
281            }
282
283            // Base Class E0h - Wireless Controller
284            (0xE0, 0x01, 0x01) => Self::Wireless(WirelessType::BluetoothProgramming),
285            (0xE0, 0x01, 0x02) => Self::Wireless(WirelessType::UwbRadioControl),
286            (0xE0, 0x01, 0x03) => Self::Wireless(WirelessType::RemoteNdis),
287            (0xE0, 0x01, 0x04) => Self::Wireless(WirelessType::BluetoothAmp),
288            (0xE0, 0x02, 0x01) => Self::Wireless(WirelessType::HostWireAdapter(
289                WireAdapterInterface::ControlData,
290            )),
291            (0xE0, 0x02, 0x02) => Self::Wireless(WirelessType::DeviceWireAdapter(
292                WireAdapterInterface::ControlData,
293            )),
294            (0xE0, 0x02, 0x03) => Self::Wireless(WirelessType::DeviceWireAdapter(
295                WireAdapterInterface::Isochronous,
296            )),
297
298            // Base Class EFh - Miscellaneous
299            (0xEF, 0x01, 0x01) => Self::Miscellaneous(MiscellaneousType::ActiveSync),
300            (0xEF, 0x01, 0x02) => Self::Miscellaneous(MiscellaneousType::PalmSync),
301            (0xEF, 0x02, 0x01) => Self::Miscellaneous(MiscellaneousType::InterfaceAssociation),
302            (0xEF, 0x02, 0x02) => Self::Miscellaneous(MiscellaneousType::WireAdapterMultifunction),
303            (0xEF, 0x03, 0x01) => Self::Miscellaneous(MiscellaneousType::CableBasedAssociation),
304            (0xEF, 0x04, 0x01) => {
305                Self::Miscellaneous(MiscellaneousType::Rndis(RndisType::Ethernet))
306            }
307            (0xEF, 0x04, 0x02) => Self::Miscellaneous(MiscellaneousType::Rndis(RndisType::Wifi)),
308            (0xEF, 0x04, 0x03) => Self::Miscellaneous(MiscellaneousType::Rndis(RndisType::Wimax)),
309            (0xEF, 0x04, 0x04) => Self::Miscellaneous(MiscellaneousType::Rndis(RndisType::Wwan)),
310            (0xEF, 0x04, 0x05) => Self::Miscellaneous(MiscellaneousType::Rndis(RndisType::RawIpv4)),
311            (0xEF, 0x04, 0x06) => Self::Miscellaneous(MiscellaneousType::Rndis(RndisType::RawIpv6)),
312            (0xEF, 0x04, 0x07) => Self::Miscellaneous(MiscellaneousType::Rndis(RndisType::Gprs)),
313            (0xEF, 0x05, 0x00) => {
314                Self::Miscellaneous(MiscellaneousType::Usb3Vision(VisionInterface::Control))
315            }
316            (0xEF, 0x05, 0x01) => {
317                Self::Miscellaneous(MiscellaneousType::Usb3Vision(VisionInterface::Event))
318            }
319            (0xEF, 0x05, 0x02) => {
320                Self::Miscellaneous(MiscellaneousType::Usb3Vision(VisionInterface::Streaming))
321            }
322            (0xEF, 0x06, 0x01) => Self::Miscellaneous(MiscellaneousType::Step(StepType::Step)),
323            (0xEF, 0x06, 0x02) => Self::Miscellaneous(MiscellaneousType::Step(StepType::StepRaw)),
324            (0xEF, 0x07, 0x01) => {
325                Self::Miscellaneous(MiscellaneousType::DvbCi(DvbInterface::CommandInIad))
326            }
327            (0xEF, 0x07, 0x02) => {
328                Self::Miscellaneous(MiscellaneousType::DvbCi(DvbInterface::CommandInInterface))
329            }
330            (0xEF, 0x07, 0x03) => {
331                Self::Miscellaneous(MiscellaneousType::DvbCi(DvbInterface::MediaInInterface))
332            }
333
334            // Base Class FEh - Application Specific
335            (0xFE, 0x01, 0x01) => Self::Application(ApplicationType::DeviceFirmwareUpgrade),
336            (0xFE, 0x02, 0x00) => Self::Application(ApplicationType::IrdaBridge),
337            (0xFE, 0x03, 0x00) => Self::Application(ApplicationType::TestMeasurement(
338                TestMeasurementType::Standard,
339            )),
340            (0xFE, 0x03, 0x01) => Self::Application(ApplicationType::TestMeasurement(
341                TestMeasurementType::Usb488Subclass,
342            )),
343
344            // Base Class FFh - Vendor Specific
345            (0xFF, _, _) => Self::Vendor,
346
347            _ => Self::Unknown {
348                class,
349                subclass,
350                protocol,
351            },
352        }
353    }
354}