Skip to main content

usb_if/transfer/
mod.rs

1use num_enum::{FromPrimitive, IntoPrimitive};
2
3#[repr(u8)]
4/// The direction of the data transfer.
5#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
6pub enum Direction {
7    /// Out (Write Data)
8    Out = 0,
9    /// In (Read Data)
10    In = 1,
11}
12
13impl Direction {
14    pub fn from_address(addr: u8) -> Direction {
15        match addr & Self::MASK {
16            0 => Self::Out,
17            _ => Self::In,
18        }
19    }
20    const MASK: u8 = 0x80;
21
22    pub fn from_raw(raw: u8) -> Direction {
23        match raw {
24            0 => Self::Out,
25            _ => Self::In,
26        }
27    }
28}
29
30#[repr(C)]
31#[derive(Debug, Clone)]
32pub struct BmRequestType {
33    pub direction: Direction,
34    pub request_type: RequestType,
35    pub recipient: Recipient,
36}
37
38impl BmRequestType {
39    pub const fn new(
40        direction: Direction,
41        transfer_type: RequestType,
42        recipient: Recipient,
43    ) -> BmRequestType {
44        BmRequestType {
45            direction,
46            request_type: transfer_type,
47            recipient,
48        }
49    }
50}
51
52impl From<BmRequestType> for u8 {
53    fn from(value: BmRequestType) -> Self {
54        ((value.direction as u8) << 7) | ((value.request_type as u8) << 5) | value.recipient as u8
55    }
56}
57
58#[derive(Copy, Clone, Debug)]
59#[repr(u8)]
60pub enum RequestType {
61    Standard = 0,
62    Class = 1,
63    Vendor = 2,
64    Reserved = 3,
65}
66
67#[derive(Copy, Clone, Debug)]
68#[repr(u8)]
69pub enum Recipient {
70    Device = 0,
71    Interface = 1,
72    Endpoint = 2,
73    Other = 3,
74}
75
76#[derive(Debug, Clone, FromPrimitive, IntoPrimitive, Copy)]
77#[repr(u8)]
78pub enum Request {
79    GetStatus = 0,
80    ClearFeature = 1,
81    SetFeature = 3,
82    SetAddress = 5,
83    GetDescriptor = 6,
84    SetDescriptor = 7,
85    GetConfiguration = 8,
86    SetConfiguration = 9,
87    GetInterface = 10,
88    SetInterface = 11,
89    SynchFrame = 12,
90    SetEncryption = 13,
91    GetEncryption = 14,
92    SetHandshake = 15,
93    GetHandshake = 16,
94    SetConnection = 17,
95    SetSecurityData = 18,
96    GetSecurityData = 19,
97    SetWusbData = 20,
98    LoopbackDataWrite = 21,
99    LoopbackDataRead = 22,
100    SetInterfaceDs = 23,
101    GetFwStatus = 26,
102    SetFwStatus = 27,
103    SetSel = 48,
104    SetIsochDelay = 49,
105    #[num_enum(catch_all)]
106    Other(u8),
107}