usb_device/
control.rs

1use crate::{Result, UsbDirection, UsbError};
2use core::mem;
3
4/// Control request type.
5#[repr(u8)]
6#[derive(Copy, Clone, Eq, PartialEq, Debug)]
7#[cfg_attr(feature = "defmt", derive(defmt::Format))]
8pub enum RequestType {
9    /// Request is a USB standard request. Usually handled by
10    /// [`UsbDevice`](crate::device::UsbDevice).
11    Standard = 0,
12    /// Request is intended for a USB class.
13    Class = 1,
14    /// Request is vendor-specific.
15    Vendor = 2,
16    /// Reserved.
17    Reserved = 3,
18}
19
20/// Control request recipient.
21#[derive(Copy, Clone, Eq, PartialEq, Debug)]
22#[cfg_attr(feature = "defmt", derive(defmt::Format))]
23pub enum Recipient {
24    /// Request is intended for the entire device.
25    Device = 0,
26    /// Request is intended for an interface. Generally, the `index` field of the request specifies
27    /// the interface number.
28    Interface = 1,
29    /// Request is intended for an endpoint. Generally, the `index` field of the request specifies
30    /// the endpoint address.
31    Endpoint = 2,
32    /// None of the above.
33    Other = 3,
34    /// Reserved.
35    Reserved = 4,
36}
37
38/// A control request read from a SETUP packet.
39#[derive(Copy, Clone, Eq, PartialEq, Debug)]
40#[cfg_attr(feature = "defmt", derive(defmt::Format))]
41pub struct Request {
42    /// Direction of the request.
43    pub direction: UsbDirection,
44    /// Type of the request.
45    pub request_type: RequestType,
46    /// Recipient of the request.
47    pub recipient: Recipient,
48    /// Request code. The meaning of the value depends on the previous fields.
49    pub request: u8,
50    /// Request value. The meaning of the value depends on the previous fields.
51    pub value: u16,
52    /// Request index. The meaning of the value depends on the previous fields.
53    pub index: u16,
54    /// Length of the DATA stage. For control OUT transfers this is the exact length of the data the
55    /// host sent. For control IN transfers this is the maximum length of data the device should
56    /// return.
57    pub length: u16,
58}
59
60impl Request {
61    /// Standard USB control request Get Status
62    pub const GET_STATUS: u8 = 0;
63
64    /// Standard USB control request Clear Feature
65    pub const CLEAR_FEATURE: u8 = 1;
66
67    /// Standard USB control request Set Feature
68    pub const SET_FEATURE: u8 = 3;
69
70    /// Standard USB control request Set Address
71    pub const SET_ADDRESS: u8 = 5;
72
73    /// Standard USB control request Get Descriptor
74    pub const GET_DESCRIPTOR: u8 = 6;
75
76    /// Standard USB control request Set Descriptor
77    pub const SET_DESCRIPTOR: u8 = 7;
78
79    /// Standard USB control request Get Configuration
80    pub const GET_CONFIGURATION: u8 = 8;
81
82    /// Standard USB control request Set Configuration
83    pub const SET_CONFIGURATION: u8 = 9;
84
85    /// Standard USB control request Get Interface
86    pub const GET_INTERFACE: u8 = 10;
87
88    /// Standard USB control request Set Interface
89    pub const SET_INTERFACE: u8 = 11;
90
91    /// Standard USB control request Synch Frame
92    pub const SYNCH_FRAME: u8 = 12;
93
94    /// Standard USB feature Endpoint Halt for Set/Clear Feature
95    pub const FEATURE_ENDPOINT_HALT: u16 = 0;
96
97    /// Standard USB feature Device Remote Wakeup for Set/Clear Feature
98    pub const FEATURE_DEVICE_REMOTE_WAKEUP: u16 = 1;
99
100    pub(crate) fn parse(buf: &[u8]) -> Result<Request> {
101        if buf.len() != 8 {
102            return Err(UsbError::ParseError);
103        }
104
105        let rt = buf[0];
106        let recipient = rt & 0b11111;
107
108        Ok(Request {
109            direction: rt.into(),
110            request_type: unsafe { mem::transmute((rt >> 5) & 0b11) },
111            recipient: if recipient <= 3 {
112                unsafe { mem::transmute(recipient) }
113            } else {
114                Recipient::Reserved
115            },
116            request: buf[1],
117            value: (buf[2] as u16) | ((buf[3] as u16) << 8),
118            index: (buf[4] as u16) | ((buf[5] as u16) << 8),
119            length: (buf[6] as u16) | ((buf[7] as u16) << 8),
120        })
121    }
122
123    /// Gets the descriptor type and index from the value field of a GET_DESCRIPTOR request.
124    pub fn descriptor_type_index(&self) -> (u8, u8) {
125        ((self.value >> 8) as u8, self.value as u8)
126    }
127}