Skip to main content

netget/server/usb/
common.rs

1//! USB constant tables shared across the USB device protocols
2//!
3//! These are the USB specification values that `descriptors.rs` and the
4//! individual device implementations build their descriptors and control
5//! request handling from.
6//!
7//! The module is compiled under the `usb-common` feature, which every USB
8//! device protocol (`usb-keyboard`, `usb-mouse`, `usb-serial`, `usb-msc`,
9//! `usb-fido2`, `usb-smartcard`) enables. USB/IP framing - opcodes, protocol
10//! version, device paths, connection state - is the `usbip` crate's job and
11//! deliberately has no counterpart here.
12
13/// USB device class codes
14pub mod device_class {
15    pub const USE_INTERFACE: u8 = 0x00;
16    pub const AUDIO: u8 = 0x01;
17    pub const COMM: u8 = 0x02; // CDC (Communication Device Class)
18    pub const HID: u8 = 0x03; // Human Interface Device
19    pub const PHYSICAL: u8 = 0x05;
20    pub const IMAGE: u8 = 0x06;
21    pub const PRINTER: u8 = 0x07;
22    pub const MASS_STORAGE: u8 = 0x08;
23    pub const HUB: u8 = 0x09;
24    pub const CDC_DATA: u8 = 0x0a;
25    pub const SMART_CARD: u8 = 0x0b;
26    pub const CONTENT_SECURITY: u8 = 0x0d;
27    pub const VIDEO: u8 = 0x0e;
28    pub const PERSONAL_HEALTHCARE: u8 = 0x0f;
29    pub const AUDIO_VIDEO: u8 = 0x10;
30    pub const DIAGNOSTIC: u8 = 0xdc;
31    pub const WIRELESS: u8 = 0xe0;
32    pub const MISCELLANEOUS: u8 = 0xef;
33    pub const APPLICATION_SPECIFIC: u8 = 0xfe;
34    pub const VENDOR_SPECIFIC: u8 = 0xff;
35}
36
37/// USB descriptor types
38pub mod descriptor_type {
39    pub const DEVICE: u8 = 0x01;
40    pub const CONFIGURATION: u8 = 0x02;
41    pub const STRING: u8 = 0x03;
42    pub const INTERFACE: u8 = 0x04;
43    pub const ENDPOINT: u8 = 0x05;
44    pub const DEVICE_QUALIFIER: u8 = 0x06;
45    pub const OTHER_SPEED_CONFIGURATION: u8 = 0x07;
46    pub const INTERFACE_POWER: u8 = 0x08;
47    pub const HID: u8 = 0x21;
48    pub const HID_REPORT: u8 = 0x22;
49    pub const HID_PHYSICAL: u8 = 0x23;
50}
51
52/// USB standard requests (bmRequestType)
53pub mod request_type {
54    // Direction
55    pub const HOST_TO_DEVICE: u8 = 0x00;
56    pub const DEVICE_TO_HOST: u8 = 0x80;
57
58    // Type
59    pub const STANDARD: u8 = 0x00;
60    pub const CLASS: u8 = 0x20;
61    pub const VENDOR: u8 = 0x40;
62
63    // Recipient
64    pub const DEVICE: u8 = 0x00;
65    pub const INTERFACE: u8 = 0x01;
66    pub const ENDPOINT: u8 = 0x02;
67    pub const OTHER: u8 = 0x03;
68}
69
70/// USB standard request codes (bRequest)
71pub mod request {
72    pub const GET_STATUS: u8 = 0x00;
73    pub const CLEAR_FEATURE: u8 = 0x01;
74    pub const SET_FEATURE: u8 = 0x03;
75    pub const SET_ADDRESS: u8 = 0x05;
76    pub const GET_DESCRIPTOR: u8 = 0x06;
77    pub const SET_DESCRIPTOR: u8 = 0x07;
78    pub const GET_CONFIGURATION: u8 = 0x08;
79    pub const SET_CONFIGURATION: u8 = 0x09;
80    pub const GET_INTERFACE: u8 = 0x0a;
81    pub const SET_INTERFACE: u8 = 0x0b;
82    pub const SYNCH_FRAME: u8 = 0x0c;
83}
84
85/// HID-specific request codes
86pub mod hid_request {
87    pub const GET_REPORT: u8 = 0x01;
88    pub const GET_IDLE: u8 = 0x02;
89    pub const GET_PROTOCOL: u8 = 0x03;
90    pub const SET_REPORT: u8 = 0x09;
91    pub const SET_IDLE: u8 = 0x0a;
92    pub const SET_PROTOCOL: u8 = 0x0b;
93}
94
95/// CDC-specific request codes.
96///
97/// This carried a note saying `usb-serial` "does not implement CDC class control
98/// requests yet", which stopped being true some time ago and is worth correcting
99/// rather than leaving: `UsbCdcAcmSerialHandler::handle_control`
100/// (`src/server/usb/serial/handler.rs`) matches on these constants and answers
101/// SET_LINE_CODING, GET_LINE_CODING, SET_CONTROL_LINE_STATE and SEND_BREAK
102/// itself. `SEND_ENCAPSULATED_COMMAND` and `GET_ENCAPSULATED_RESPONSE` fall
103/// through to the catch-all, which answers empty on purpose — an error would
104/// abort the USB/IP session for the whole device, and a host probing an optional
105/// request must not unplug the port.
106pub mod cdc_request {
107    pub const SEND_ENCAPSULATED_COMMAND: u8 = 0x00;
108    pub const GET_ENCAPSULATED_RESPONSE: u8 = 0x01;
109    pub const SET_LINE_CODING: u8 = 0x20;
110    pub const GET_LINE_CODING: u8 = 0x21;
111    pub const SET_CONTROL_LINE_STATE: u8 = 0x22;
112    pub const SEND_BREAK: u8 = 0x23;
113}
114
115/// Endpoint transfer types
116pub mod transfer_type {
117    pub const CONTROL: u8 = 0;
118    pub const ISOCHRONOUS: u8 = 1;
119    pub const BULK: u8 = 2;
120    pub const INTERRUPT: u8 = 3;
121}