usb_if/transfer/
mod.rs

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