diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-03-28 03:16:45 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-04-06 05:38:11 +0200 |
| commit | 3412e5dc4a771c33dc8db644ba82c071ea486d6d (patch) | |
| tree | 82348ffeba1a3f6ac10dafce39b37e4d7af463a9 /embassy-usb/src/lib.rs | |
| parent | a2f5763a67441e5e6c981407938c21c58e9eb669 (diff) | |
usb: cleanup giant matches in control code.
Diffstat (limited to 'embassy-usb/src/lib.rs')
| -rw-r--r-- | embassy-usb/src/lib.rs | 122 |
1 files changed, 55 insertions, 67 deletions
diff --git a/embassy-usb/src/lib.rs b/embassy-usb/src/lib.rs index 2c00e8817..7d631d176 100644 --- a/embassy-usb/src/lib.rs +++ b/embassy-usb/src/lib.rs | |||
| @@ -175,66 +175,53 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> { | |||
| 175 | const CONFIGURATION_VALUE_U16: u16 = CONFIGURATION_VALUE as u16; | 175 | const CONFIGURATION_VALUE_U16: u16 = CONFIGURATION_VALUE as u16; |
| 176 | const DEFAULT_ALTERNATE_SETTING_U16: u16 = DEFAULT_ALTERNATE_SETTING as u16; | 176 | const DEFAULT_ALTERNATE_SETTING_U16: u16 = DEFAULT_ALTERNATE_SETTING as u16; |
| 177 | 177 | ||
| 178 | match req.request_type { | 178 | match (req.request_type, req.recipient) { |
| 179 | RequestType::Standard => match (req.recipient, req.request, req.value) { | 179 | (RequestType::Standard, Recipient::Device) => match (req.request, req.value) { |
| 180 | ( | 180 | (Request::CLEAR_FEATURE, Request::FEATURE_DEVICE_REMOTE_WAKEUP) => { |
| 181 | Recipient::Device, | ||
| 182 | Request::CLEAR_FEATURE, | ||
| 183 | Request::FEATURE_DEVICE_REMOTE_WAKEUP, | ||
| 184 | ) => { | ||
| 185 | self.remote_wakeup_enabled = false; | 181 | self.remote_wakeup_enabled = false; |
| 186 | self.control.accept(); | 182 | self.control.accept(); |
| 187 | } | 183 | } |
| 188 | 184 | (Request::SET_FEATURE, Request::FEATURE_DEVICE_REMOTE_WAKEUP) => { | |
| 189 | (Recipient::Endpoint, Request::CLEAR_FEATURE, Request::FEATURE_ENDPOINT_HALT) => { | ||
| 190 | //self.bus.set_stalled(((req.index as u8) & 0x8f).into(), false); | ||
| 191 | self.control.accept(); | ||
| 192 | } | ||
| 193 | |||
| 194 | ( | ||
| 195 | Recipient::Device, | ||
| 196 | Request::SET_FEATURE, | ||
| 197 | Request::FEATURE_DEVICE_REMOTE_WAKEUP, | ||
| 198 | ) => { | ||
| 199 | self.remote_wakeup_enabled = true; | 185 | self.remote_wakeup_enabled = true; |
| 200 | self.control.accept(); | 186 | self.control.accept(); |
| 201 | } | 187 | } |
| 202 | 188 | (Request::SET_ADDRESS, 1..=127) => { | |
| 203 | (Recipient::Endpoint, Request::SET_FEATURE, Request::FEATURE_ENDPOINT_HALT) => { | ||
| 204 | self.bus | ||
| 205 | .set_stalled(((req.index as u8) & 0x8f).into(), true); | ||
| 206 | self.control.accept(); | ||
| 207 | } | ||
| 208 | |||
| 209 | (Recipient::Device, Request::SET_ADDRESS, 1..=127) => { | ||
| 210 | self.pending_address = req.value as u8; | 189 | self.pending_address = req.value as u8; |
| 211 | |||
| 212 | // on NRF the hardware auto-handles SET_ADDRESS. | ||
| 213 | self.control.accept(); | 190 | self.control.accept(); |
| 214 | } | 191 | } |
| 215 | 192 | (Request::SET_CONFIGURATION, CONFIGURATION_VALUE_U16) => { | |
| 216 | (Recipient::Device, Request::SET_CONFIGURATION, CONFIGURATION_VALUE_U16) => { | ||
| 217 | self.device_state = UsbDeviceState::Configured; | 193 | self.device_state = UsbDeviceState::Configured; |
| 218 | self.control.accept(); | 194 | self.control.accept(); |
| 219 | } | 195 | } |
| 220 | 196 | (Request::SET_CONFIGURATION, CONFIGURATION_NONE_U16) => match self.device_state { | |
| 221 | (Recipient::Device, Request::SET_CONFIGURATION, CONFIGURATION_NONE_U16) => { | 197 | UsbDeviceState::Default => { |
| 222 | match self.device_state { | 198 | self.control.accept(); |
| 223 | UsbDeviceState::Default => { | ||
| 224 | self.control.accept(); | ||
| 225 | } | ||
| 226 | _ => { | ||
| 227 | self.device_state = UsbDeviceState::Addressed; | ||
| 228 | self.control.accept(); | ||
| 229 | } | ||
| 230 | } | 199 | } |
| 231 | } | 200 | _ => { |
| 232 | 201 | self.device_state = UsbDeviceState::Addressed; | |
| 233 | (Recipient::Interface, Request::SET_INTERFACE, DEFAULT_ALTERNATE_SETTING_U16) => { | 202 | self.control.accept(); |
| 203 | } | ||
| 204 | }, | ||
| 205 | _ => self.control.reject(), | ||
| 206 | }, | ||
| 207 | (RequestType::Standard, Recipient::Interface) => match (req.request, req.value) { | ||
| 208 | (Request::SET_INTERFACE, DEFAULT_ALTERNATE_SETTING_U16) => { | ||
| 234 | // TODO: do something when alternate settings are implemented | 209 | // TODO: do something when alternate settings are implemented |
| 235 | self.control.accept(); | 210 | self.control.accept(); |
| 236 | } | 211 | } |
| 237 | 212 | _ => self.control.reject(), | |
| 213 | }, | ||
| 214 | (RequestType::Standard, Recipient::Endpoint) => match (req.request, req.value) { | ||
| 215 | (Request::SET_FEATURE, Request::FEATURE_ENDPOINT_HALT) => { | ||
| 216 | let ep_addr = ((req.index as u8) & 0x8f).into(); | ||
| 217 | self.bus.set_stalled(ep_addr, true); | ||
| 218 | self.control.accept(); | ||
| 219 | } | ||
| 220 | (Request::CLEAR_FEATURE, Request::FEATURE_ENDPOINT_HALT) => { | ||
| 221 | let ep_addr = ((req.index as u8) & 0x8f).into(); | ||
| 222 | self.bus.set_stalled(ep_addr, false); | ||
| 223 | self.control.accept(); | ||
| 224 | } | ||
| 238 | _ => self.control.reject(), | 225 | _ => self.control.reject(), |
| 239 | }, | 226 | }, |
| 240 | _ => self.control.reject(), | 227 | _ => self.control.reject(), |
| @@ -260,9 +247,9 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> { | |||
| 260 | } | 247 | } |
| 261 | } | 248 | } |
| 262 | 249 | ||
| 263 | match req.request_type { | 250 | match (req.request_type, req.recipient) { |
| 264 | RequestType::Standard => match (req.recipient, req.request) { | 251 | (RequestType::Standard, Recipient::Device) => match req.request { |
| 265 | (Recipient::Device, Request::GET_STATUS) => { | 252 | Request::GET_STATUS => { |
| 266 | let mut status: u16 = 0x0000; | 253 | let mut status: u16 = 0x0000; |
| 267 | if self.self_powered { | 254 | if self.self_powered { |
| 268 | status |= 0x0001; | 255 | status |= 0x0001; |
| @@ -272,40 +259,41 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> { | |||
| 272 | } | 259 | } |
| 273 | self.control.accept_in(&status.to_le_bytes()).await; | 260 | self.control.accept_in(&status.to_le_bytes()).await; |
| 274 | } | 261 | } |
| 275 | 262 | Request::GET_DESCRIPTOR => { | |
| 276 | (Recipient::Interface, Request::GET_STATUS) => { | ||
| 277 | let status: u16 = 0x0000; | ||
| 278 | self.control.accept_in(&status.to_le_bytes()).await; | ||
| 279 | } | ||
| 280 | |||
| 281 | (Recipient::Endpoint, Request::GET_STATUS) => { | ||
| 282 | let ep_addr: EndpointAddress = ((req.index as u8) & 0x8f).into(); | ||
| 283 | let mut status: u16 = 0x0000; | ||
| 284 | if self.bus.is_stalled(ep_addr) { | ||
| 285 | status |= 0x0001; | ||
| 286 | } | ||
| 287 | self.control.accept_in(&status.to_le_bytes()).await; | ||
| 288 | } | ||
| 289 | |||
| 290 | (Recipient::Device, Request::GET_DESCRIPTOR) => { | ||
| 291 | self.handle_get_descriptor(req).await; | 263 | self.handle_get_descriptor(req).await; |
| 292 | } | 264 | } |
| 293 | 265 | Request::GET_CONFIGURATION => { | |
| 294 | (Recipient::Device, Request::GET_CONFIGURATION) => { | ||
| 295 | let status = match self.device_state { | 266 | let status = match self.device_state { |
| 296 | UsbDeviceState::Configured => CONFIGURATION_VALUE, | 267 | UsbDeviceState::Configured => CONFIGURATION_VALUE, |
| 297 | _ => CONFIGURATION_NONE, | 268 | _ => CONFIGURATION_NONE, |
| 298 | }; | 269 | }; |
| 299 | self.control.accept_in(&status.to_le_bytes()).await; | 270 | self.control.accept_in(&status.to_le_bytes()).await; |
| 300 | } | 271 | } |
| 301 | 272 | _ => self.control.reject(), | |
| 302 | (Recipient::Interface, Request::GET_INTERFACE) => { | 273 | }, |
| 274 | (RequestType::Standard, Recipient::Interface) => match req.request { | ||
| 275 | Request::GET_STATUS => { | ||
| 276 | let status: u16 = 0x0000; | ||
| 277 | self.control.accept_in(&status.to_le_bytes()).await; | ||
| 278 | } | ||
| 279 | Request::GET_INTERFACE => { | ||
| 303 | // TODO: change when alternate settings are implemented | 280 | // TODO: change when alternate settings are implemented |
| 304 | let status = DEFAULT_ALTERNATE_SETTING; | 281 | let status = DEFAULT_ALTERNATE_SETTING; |
| 305 | self.control.accept_in(&status.to_le_bytes()).await; | 282 | self.control.accept_in(&status.to_le_bytes()).await; |
| 306 | } | 283 | } |
| 307 | _ => self.control.reject(), | 284 | _ => self.control.reject(), |
| 308 | }, | 285 | }, |
| 286 | (RequestType::Standard, Recipient::Endpoint) => match req.request { | ||
| 287 | Request::GET_STATUS => { | ||
| 288 | let ep_addr: EndpointAddress = ((req.index as u8) & 0x8f).into(); | ||
| 289 | let mut status: u16 = 0x0000; | ||
| 290 | if self.bus.is_stalled(ep_addr) { | ||
| 291 | status |= 0x0001; | ||
| 292 | } | ||
| 293 | self.control.accept_in(&status.to_le_bytes()).await; | ||
| 294 | } | ||
| 295 | _ => self.control.reject(), | ||
| 296 | }, | ||
| 309 | _ => self.control.reject(), | 297 | _ => self.control.reject(), |
| 310 | } | 298 | } |
| 311 | } | 299 | } |
