| 1 | use super::{ResponseBuffer, TransferRequest}; |
| 2 | |
| 3 | /// Transfer direction |
| 4 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 5 | #[repr(u8)] |
| 6 | pub enum Direction { |
| 7 | /// Host to device |
| 8 | Out = 0, |
| 9 | |
| 10 | /// Device to host |
| 11 | In = 1, |
| 12 | } |
| 13 | |
| 14 | /// Specification defining the request. |
| 15 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 16 | #[repr(u8)] |
| 17 | pub enum ControlType { |
| 18 | /// Request defined by the USB standard. |
| 19 | Standard = 0, |
| 20 | |
| 21 | /// Request defined by the standard USB class specification. |
| 22 | Class = 1, |
| 23 | |
| 24 | /// Non-standard request. |
| 25 | Vendor = 2, |
| 26 | } |
| 27 | |
| 28 | /// Entity targeted by the request. |
| 29 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 30 | #[repr(u8)] |
| 31 | pub enum Recipient { |
| 32 | /// Request made to device as a whole. |
| 33 | Device = 0, |
| 34 | |
| 35 | /// Request made to specific interface. |
| 36 | Interface = 1, |
| 37 | |
| 38 | /// Request made to specific endpoint. |
| 39 | Endpoint = 2, |
| 40 | |
| 41 | /// Other request. |
| 42 | Other = 3, |
| 43 | } |
| 44 | |
| 45 | /// SETUP packet without direction or buffers |
| 46 | pub struct Control { |
| 47 | /// Request type used for the `bmRequestType` field sent in the SETUP packet. |
| 48 | #[doc(alias = "bmRequestType")] |
| 49 | pub control_type: ControlType, |
| 50 | |
| 51 | /// Recipient used for the `bmRequestType` field sent in the SETUP packet. |
| 52 | #[doc(alias = "bmRequestType")] |
| 53 | pub recipient: Recipient, |
| 54 | |
| 55 | /// `bRequest` field sent in the SETUP packet. |
| 56 | #[doc(alias = "bRequest")] |
| 57 | pub request: u8, |
| 58 | |
| 59 | /// `wValue` field sent in the SETUP packet. |
| 60 | #[doc(alias = "wValue")] |
| 61 | pub value: u16, |
| 62 | |
| 63 | /// `wIndex` field sent in the SETUP packet. |
| 64 | /// |
| 65 | /// For [`Recipient::Interface`] this is the interface number. For [`Recipient::Endpoint`] this is the endpoint number. |
| 66 | #[doc(alias = "wIndex")] |
| 67 | pub index: u16, |
| 68 | } |
| 69 | |
| 70 | impl Control { |
| 71 | pub(crate) fn request_type(&self, direction: Direction) -> u8 { |
| 72 | request_type(direction, self.control_type, self.recipient) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /// SETUP packet and associated data to make an **OUT** request on a control endpoint. |
| 77 | pub struct ControlOut<'a> { |
| 78 | /// Request type used for the `bmRequestType` field sent in the SETUP packet. |
| 79 | #[doc(alias = "bmRequestType")] |
| 80 | pub control_type: ControlType, |
| 81 | |
| 82 | /// Recipient used for the `bmRequestType` field sent in the SETUP packet. |
| 83 | #[doc(alias = "bmRequestType")] |
| 84 | pub recipient: Recipient, |
| 85 | |
| 86 | /// `bRequest` field sent in the SETUP packet. |
| 87 | #[doc(alias = "bRequest")] |
| 88 | pub request: u8, |
| 89 | |
| 90 | /// `wValue` field sent in the SETUP packet. |
| 91 | #[doc(alias = "wValue")] |
| 92 | pub value: u16, |
| 93 | |
| 94 | /// `wIndex` field sent in the SETUP packet. |
| 95 | /// |
| 96 | /// For [`Recipient::Interface`] this is the interface number. For [`Recipient::Endpoint`] this is the endpoint number. |
| 97 | #[doc(alias = "wIndex")] |
| 98 | pub index: u16, |
| 99 | |
| 100 | /// Data to be sent in the data stage. |
| 101 | #[doc(alias = "wLength")] |
| 102 | pub data: &'a [u8], |
| 103 | } |
| 104 | |
| 105 | impl<'a> ControlOut<'a> { |
| 106 | #[allow(unused)] |
| 107 | pub(crate) fn setup_packet(&self) -> Result<[u8; SETUP_PACKET_SIZE], ()> { |
| 108 | Ok(pack_setup( |
| 109 | Direction::Out, |
| 110 | self.control_type, |
| 111 | self.recipient, |
| 112 | self.request, |
| 113 | self.value, |
| 114 | self.index, |
| 115 | self.data.len().try_into().map_err(|_| ())?, |
| 116 | )) |
| 117 | } |
| 118 | |
| 119 | #[allow(unused)] |
| 120 | pub(crate) fn request_type(&self) -> u8 { |
| 121 | request_type(Direction::Out, self.control_type, self.recipient) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | impl TransferRequest for ControlOut<'_> { |
| 126 | type Response = ResponseBuffer; |
| 127 | } |
| 128 | |
| 129 | /// SETUP packet to make an **IN** request on a control endpoint. |
| 130 | pub struct ControlIn { |
| 131 | /// Request type used for the `bmRequestType` field sent in the SETUP packet. |
| 132 | #[doc(alias = "bmRequestType")] |
| 133 | pub control_type: ControlType, |
| 134 | |
| 135 | /// Recipient used for the `bmRequestType` field sent in the SETUP packet. |
| 136 | #[doc(alias = "bmRequestType")] |
| 137 | pub recipient: Recipient, |
| 138 | |
| 139 | /// `bRequest` field sent in the SETUP packet. |
| 140 | #[doc(alias = "bRequest")] |
| 141 | pub request: u8, |
| 142 | |
| 143 | /// `wValue` field sent in the SETUP packet. |
| 144 | #[doc(alias = "wValue")] |
| 145 | pub value: u16, |
| 146 | |
| 147 | /// `wIndex` field sent in the SETUP packet. |
| 148 | /// |
| 149 | /// For [`Recipient::Interface`] this is the interface number. For [`Recipient::Endpoint`] this is the endpoint number. |
| 150 | #[doc(alias = "wIndex")] |
| 151 | pub index: u16, |
| 152 | |
| 153 | /// Number of bytes to be read in the data stage. |
| 154 | #[doc(alias = "wLength")] |
| 155 | pub length: u16, |
| 156 | } |
| 157 | |
| 158 | impl ControlIn { |
| 159 | #[allow(unused)] |
| 160 | pub(crate) fn setup_packet(&self) -> [u8; SETUP_PACKET_SIZE] { |
| 161 | pack_setup( |
| 162 | Direction::In, |
| 163 | self.control_type, |
| 164 | self.recipient, |
| 165 | self.request, |
| 166 | self.value, |
| 167 | self.index, |
| 168 | self.length, |
| 169 | ) |
| 170 | } |
| 171 | |
| 172 | #[allow(unused)] |
| 173 | pub(crate) fn request_type(&self) -> u8 { |
| 174 | request_type(Direction::In, self.control_type, self.recipient) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | pub(crate) const SETUP_PACKET_SIZE: usize = 8; |
| 179 | |
| 180 | fn pack_setup( |
| 181 | direction: Direction, |
| 182 | control_type: ControlType, |
| 183 | recipient: Recipient, |
| 184 | request: u8, |
| 185 | value: u16, |
| 186 | index: u16, |
| 187 | length: u16, |
| 188 | ) -> [u8; SETUP_PACKET_SIZE] { |
| 189 | let bmrequesttype = request_type(direction, control_type, recipient); |
| 190 | |
| 191 | [ |
| 192 | bmrequesttype, |
| 193 | request, |
| 194 | (value & 0xFF) as u8, |
| 195 | (value >> 8) as u8, |
| 196 | (index & 0xFF) as u8, |
| 197 | (index >> 8) as u8, |
| 198 | (length & 0xFF) as u8, |
| 199 | (length >> 8) as u8, |
| 200 | ] |
| 201 | } |
| 202 | |
| 203 | fn request_type(direction: Direction, control_type: ControlType, recipient: Recipient) -> u8 { |
| 204 | ((direction as u8) << 7) | ((control_type as u8) << 5) | (recipient as u8) |
| 205 | } |
| 206 | |
| 207 | impl TransferRequest for ControlIn { |
| 208 | type Response = Vec<u8>; |
| 209 | } |