diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-03-30 02:01:09 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-04-06 05:38:11 +0200 |
| commit | a435d78cf78deb1a93682d9ff2632706eaa1b951 (patch) | |
| tree | 580f4ac3aba6ede0a3021982c9725a6d3055b251 /embassy-usb/src | |
| parent | 60d3d111972f462c1f38d1d4fd27e89713974fc6 (diff) | |
usb: cleanup and simplify error handling.
Diffstat (limited to 'embassy-usb/src')
| -rw-r--r-- | embassy-usb/src/builder.rs | 6 | ||||
| -rw-r--r-- | embassy-usb/src/descriptor.rs | 69 | ||||
| -rw-r--r-- | embassy-usb/src/lib.rs | 12 |
3 files changed, 36 insertions, 51 deletions
diff --git a/embassy-usb/src/builder.rs b/embassy-usb/src/builder.rs index 0c118b782..c8a9db7a4 100644 --- a/embassy-usb/src/builder.rs +++ b/embassy-usb/src/builder.rs | |||
| @@ -168,9 +168,9 @@ impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D> { | |||
| 168 | let mut config_descriptor = DescriptorWriter::new(config_descriptor_buf); | 168 | let mut config_descriptor = DescriptorWriter::new(config_descriptor_buf); |
| 169 | let mut bos_descriptor = BosWriter::new(DescriptorWriter::new(bos_descriptor_buf)); | 169 | let mut bos_descriptor = BosWriter::new(DescriptorWriter::new(bos_descriptor_buf)); |
| 170 | 170 | ||
| 171 | device_descriptor.device(&config).unwrap(); | 171 | device_descriptor.device(&config); |
| 172 | config_descriptor.configuration(&config).unwrap(); | 172 | config_descriptor.configuration(&config); |
| 173 | bos_descriptor.bos().unwrap(); | 173 | bos_descriptor.bos(); |
| 174 | 174 | ||
| 175 | UsbDeviceBuilder { | 175 | UsbDeviceBuilder { |
| 176 | bus, | 176 | bus, |
diff --git a/embassy-usb/src/descriptor.rs b/embassy-usb/src/descriptor.rs index 5f8b0d560..ff971e127 100644 --- a/embassy-usb/src/descriptor.rs +++ b/embassy-usb/src/descriptor.rs | |||
| @@ -1,13 +1,6 @@ | |||
| 1 | use super::builder::Config; | 1 | use super::builder::Config; |
| 2 | use super::{types::*, CONFIGURATION_VALUE, DEFAULT_ALTERNATE_SETTING}; | 2 | use super::{types::*, CONFIGURATION_VALUE, DEFAULT_ALTERNATE_SETTING}; |
| 3 | 3 | ||
| 4 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
| 5 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 6 | pub enum Error { | ||
| 7 | BufferFull, | ||
| 8 | InvalidState, | ||
| 9 | } | ||
| 10 | |||
| 11 | /// Standard descriptor types | 4 | /// Standard descriptor types |
| 12 | #[allow(missing_docs)] | 5 | #[allow(missing_docs)] |
| 13 | pub mod descriptor_type { | 6 | pub mod descriptor_type { |
| @@ -69,11 +62,11 @@ impl<'a> DescriptorWriter<'a> { | |||
| 69 | } | 62 | } |
| 70 | 63 | ||
| 71 | /// Writes an arbitrary (usually class-specific) descriptor. | 64 | /// Writes an arbitrary (usually class-specific) descriptor. |
| 72 | pub fn write(&mut self, descriptor_type: u8, descriptor: &[u8]) -> Result<(), Error> { | 65 | pub fn write(&mut self, descriptor_type: u8, descriptor: &[u8]) { |
| 73 | let length = descriptor.len(); | 66 | let length = descriptor.len(); |
| 74 | 67 | ||
| 75 | if (self.position + 2 + length) > self.buf.len() || (length + 2) > 255 { | 68 | if (self.position + 2 + length) > self.buf.len() || (length + 2) > 255 { |
| 76 | return Err(Error::BufferFull); | 69 | panic!("Descriptor buffer full"); |
| 77 | } | 70 | } |
| 78 | 71 | ||
| 79 | self.buf[self.position] = (length + 2) as u8; | 72 | self.buf[self.position] = (length + 2) as u8; |
| @@ -84,11 +77,9 @@ impl<'a> DescriptorWriter<'a> { | |||
| 84 | self.buf[start..start + length].copy_from_slice(descriptor); | 77 | self.buf[start..start + length].copy_from_slice(descriptor); |
| 85 | 78 | ||
| 86 | self.position = start + length; | 79 | self.position = start + length; |
| 87 | |||
| 88 | Ok(()) | ||
| 89 | } | 80 | } |
| 90 | 81 | ||
| 91 | pub(crate) fn device(&mut self, config: &Config) -> Result<(), Error> { | 82 | pub(crate) fn device(&mut self, config: &Config) { |
| 92 | self.write( | 83 | self.write( |
| 93 | descriptor_type::DEVICE, | 84 | descriptor_type::DEVICE, |
| 94 | &[ | 85 | &[ |
| @@ -112,7 +103,7 @@ impl<'a> DescriptorWriter<'a> { | |||
| 112 | ) | 103 | ) |
| 113 | } | 104 | } |
| 114 | 105 | ||
| 115 | pub(crate) fn configuration(&mut self, config: &Config) -> Result<(), Error> { | 106 | pub(crate) fn configuration(&mut self, config: &Config) { |
| 116 | self.num_interfaces_mark = Some(self.position + 4); | 107 | self.num_interfaces_mark = Some(self.position + 4); |
| 117 | 108 | ||
| 118 | self.write_iads = config.composite_with_iads; | 109 | self.write_iads = config.composite_with_iads; |
| @@ -168,9 +159,9 @@ impl<'a> DescriptorWriter<'a> { | |||
| 168 | function_class: u8, | 159 | function_class: u8, |
| 169 | function_sub_class: u8, | 160 | function_sub_class: u8, |
| 170 | function_protocol: u8, | 161 | function_protocol: u8, |
| 171 | ) -> Result<(), Error> { | 162 | ) { |
| 172 | if !self.write_iads { | 163 | if !self.write_iads { |
| 173 | return Ok(()); | 164 | return; |
| 174 | } | 165 | } |
| 175 | 166 | ||
| 176 | self.write( | 167 | self.write( |
| @@ -183,9 +174,7 @@ impl<'a> DescriptorWriter<'a> { | |||
| 183 | function_protocol, | 174 | function_protocol, |
| 184 | 0, | 175 | 0, |
| 185 | ], | 176 | ], |
| 186 | )?; | 177 | ); |
| 187 | |||
| 188 | Ok(()) | ||
| 189 | } | 178 | } |
| 190 | 179 | ||
| 191 | /// Writes a interface descriptor. | 180 | /// Writes a interface descriptor. |
| @@ -204,7 +193,7 @@ impl<'a> DescriptorWriter<'a> { | |||
| 204 | interface_class: u8, | 193 | interface_class: u8, |
| 205 | interface_sub_class: u8, | 194 | interface_sub_class: u8, |
| 206 | interface_protocol: u8, | 195 | interface_protocol: u8, |
| 207 | ) -> Result<(), Error> { | 196 | ) { |
| 208 | self.interface_alt( | 197 | self.interface_alt( |
| 209 | number, | 198 | number, |
| 210 | DEFAULT_ALTERNATE_SETTING, | 199 | DEFAULT_ALTERNATE_SETTING, |
| @@ -237,11 +226,13 @@ impl<'a> DescriptorWriter<'a> { | |||
| 237 | interface_sub_class: u8, | 226 | interface_sub_class: u8, |
| 238 | interface_protocol: u8, | 227 | interface_protocol: u8, |
| 239 | interface_string: Option<StringIndex>, | 228 | interface_string: Option<StringIndex>, |
| 240 | ) -> Result<(), Error> { | 229 | ) { |
| 241 | if alternate_setting == DEFAULT_ALTERNATE_SETTING { | 230 | if alternate_setting == DEFAULT_ALTERNATE_SETTING { |
| 242 | match self.num_interfaces_mark { | 231 | match self.num_interfaces_mark { |
| 243 | Some(mark) => self.buf[mark] += 1, | 232 | Some(mark) => self.buf[mark] += 1, |
| 244 | None => return Err(Error::InvalidState), | 233 | None => { |
| 234 | panic!("you can only call `interface/interface_alt` after `configuration`.") | ||
| 235 | } | ||
| 245 | }; | 236 | }; |
| 246 | } | 237 | } |
| 247 | 238 | ||
| @@ -260,9 +251,7 @@ impl<'a> DescriptorWriter<'a> { | |||
| 260 | interface_protocol, // bInterfaceProtocol | 251 | interface_protocol, // bInterfaceProtocol |
| 261 | str_index, // iInterface | 252 | str_index, // iInterface |
| 262 | ], | 253 | ], |
| 263 | )?; | 254 | ); |
| 264 | |||
| 265 | Ok(()) | ||
| 266 | } | 255 | } |
| 267 | 256 | ||
| 268 | /// Writes an endpoint descriptor. | 257 | /// Writes an endpoint descriptor. |
| @@ -271,10 +260,10 @@ impl<'a> DescriptorWriter<'a> { | |||
| 271 | /// | 260 | /// |
| 272 | /// * `endpoint` - Endpoint previously allocated with | 261 | /// * `endpoint` - Endpoint previously allocated with |
| 273 | /// [`UsbDeviceBuilder`](crate::bus::UsbDeviceBuilder). | 262 | /// [`UsbDeviceBuilder`](crate::bus::UsbDeviceBuilder). |
| 274 | pub fn endpoint(&mut self, endpoint: &EndpointInfo) -> Result<(), Error> { | 263 | pub fn endpoint(&mut self, endpoint: &EndpointInfo) { |
| 275 | match self.num_endpoints_mark { | 264 | match self.num_endpoints_mark { |
| 276 | Some(mark) => self.buf[mark] += 1, | 265 | Some(mark) => self.buf[mark] += 1, |
| 277 | None => return Err(Error::InvalidState), | 266 | None => panic!("you can only call `endpoint` after `interface/interface_alt`."), |
| 278 | }; | 267 | }; |
| 279 | 268 | ||
| 280 | self.write( | 269 | self.write( |
| @@ -286,17 +275,15 @@ impl<'a> DescriptorWriter<'a> { | |||
| 286 | (endpoint.max_packet_size >> 8) as u8, // wMaxPacketSize | 275 | (endpoint.max_packet_size >> 8) as u8, // wMaxPacketSize |
| 287 | endpoint.interval, // bInterval | 276 | endpoint.interval, // bInterval |
| 288 | ], | 277 | ], |
| 289 | )?; | 278 | ); |
| 290 | |||
| 291 | Ok(()) | ||
| 292 | } | 279 | } |
| 293 | 280 | ||
| 294 | /// Writes a string descriptor. | 281 | /// Writes a string descriptor. |
| 295 | pub(crate) fn string(&mut self, string: &str) -> Result<(), Error> { | 282 | pub(crate) fn string(&mut self, string: &str) { |
| 296 | let mut pos = self.position; | 283 | let mut pos = self.position; |
| 297 | 284 | ||
| 298 | if pos + 2 > self.buf.len() { | 285 | if pos + 2 > self.buf.len() { |
| 299 | return Err(Error::BufferFull); | 286 | panic!("Descriptor buffer full"); |
| 300 | } | 287 | } |
| 301 | 288 | ||
| 302 | self.buf[pos] = 0; // length placeholder | 289 | self.buf[pos] = 0; // length placeholder |
| @@ -306,7 +293,7 @@ impl<'a> DescriptorWriter<'a> { | |||
| 306 | 293 | ||
| 307 | for c in string.encode_utf16() { | 294 | for c in string.encode_utf16() { |
| 308 | if pos >= self.buf.len() { | 295 | if pos >= self.buf.len() { |
| 309 | return Err(Error::BufferFull); | 296 | panic!("Descriptor buffer full"); |
| 310 | } | 297 | } |
| 311 | 298 | ||
| 312 | self.buf[pos..pos + 2].copy_from_slice(&c.to_le_bytes()); | 299 | self.buf[pos..pos + 2].copy_from_slice(&c.to_le_bytes()); |
| @@ -316,8 +303,6 @@ impl<'a> DescriptorWriter<'a> { | |||
| 316 | self.buf[self.position] = (pos - self.position) as u8; | 303 | self.buf[self.position] = (pos - self.position) as u8; |
| 317 | 304 | ||
| 318 | self.position = pos; | 305 | self.position = pos; |
| 319 | |||
| 320 | Ok(()) | ||
| 321 | } | 306 | } |
| 322 | } | 307 | } |
| 323 | 308 | ||
| @@ -335,7 +320,7 @@ impl<'a> BosWriter<'a> { | |||
| 335 | } | 320 | } |
| 336 | } | 321 | } |
| 337 | 322 | ||
| 338 | pub(crate) fn bos(&mut self) -> Result<(), Error> { | 323 | pub(crate) fn bos(&mut self) { |
| 339 | self.num_caps_mark = Some(self.writer.position + 4); | 324 | self.num_caps_mark = Some(self.writer.position + 4); |
| 340 | self.writer.write( | 325 | self.writer.write( |
| 341 | descriptor_type::BOS, | 326 | descriptor_type::BOS, |
| @@ -343,11 +328,9 @@ impl<'a> BosWriter<'a> { | |||
| 343 | 0x00, 0x00, // wTotalLength | 328 | 0x00, 0x00, // wTotalLength |
| 344 | 0x00, // bNumDeviceCaps | 329 | 0x00, // bNumDeviceCaps |
| 345 | ], | 330 | ], |
| 346 | )?; | 331 | ); |
| 347 | 332 | ||
| 348 | self.capability(capability_type::USB_2_0_EXTENSION, &[0; 4])?; | 333 | self.capability(capability_type::USB_2_0_EXTENSION, &[0; 4]); |
| 349 | |||
| 350 | Ok(()) | ||
| 351 | } | 334 | } |
| 352 | 335 | ||
| 353 | /// Writes capability descriptor to a BOS | 336 | /// Writes capability descriptor to a BOS |
| @@ -356,17 +339,17 @@ impl<'a> BosWriter<'a> { | |||
| 356 | /// | 339 | /// |
| 357 | /// * `capability_type` - Type of a capability | 340 | /// * `capability_type` - Type of a capability |
| 358 | /// * `data` - Binary data of the descriptor | 341 | /// * `data` - Binary data of the descriptor |
| 359 | pub fn capability(&mut self, capability_type: u8, data: &[u8]) -> Result<(), Error> { | 342 | pub fn capability(&mut self, capability_type: u8, data: &[u8]) { |
| 360 | match self.num_caps_mark { | 343 | match self.num_caps_mark { |
| 361 | Some(mark) => self.writer.buf[mark] += 1, | 344 | Some(mark) => self.writer.buf[mark] += 1, |
| 362 | None => return Err(Error::InvalidState), | 345 | None => panic!("called `capability` not between `bos` and `end_bos`."), |
| 363 | } | 346 | } |
| 364 | 347 | ||
| 365 | let mut start = self.writer.position; | 348 | let mut start = self.writer.position; |
| 366 | let blen = data.len(); | 349 | let blen = data.len(); |
| 367 | 350 | ||
| 368 | if (start + blen + 3) > self.writer.buf.len() || (blen + 3) > 255 { | 351 | if (start + blen + 3) > self.writer.buf.len() || (blen + 3) > 255 { |
| 369 | return Err(Error::BufferFull); | 352 | panic!("Descriptor buffer full"); |
| 370 | } | 353 | } |
| 371 | 354 | ||
| 372 | self.writer.buf[start] = (blen + 3) as u8; | 355 | self.writer.buf[start] = (blen + 3) as u8; |
| @@ -376,8 +359,6 @@ impl<'a> BosWriter<'a> { | |||
| 376 | start += 3; | 359 | start += 3; |
| 377 | self.writer.buf[start..start + blen].copy_from_slice(data); | 360 | self.writer.buf[start..start + blen].copy_from_slice(data); |
| 378 | self.writer.position = start + blen; | 361 | self.writer.position = start + blen; |
| 379 | |||
| 380 | Ok(()) | ||
| 381 | } | 362 | } |
| 382 | 363 | ||
| 383 | pub(crate) fn end_bos(&mut self) { | 364 | pub(crate) fn end_bos(&mut self) { |
diff --git a/embassy-usb/src/lib.rs b/embassy-usb/src/lib.rs index 2287267b6..32b67a766 100644 --- a/embassy-usb/src/lib.rs +++ b/embassy-usb/src/lib.rs | |||
| @@ -158,7 +158,13 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> { | |||
| 158 | 158 | ||
| 159 | // If the request has a data state, we must read it. | 159 | // If the request has a data state, we must read it. |
| 160 | let data = if req.length > 0 { | 160 | let data = if req.length > 0 { |
| 161 | let size = self.control.data_out(self.control_buf).await.unwrap(); | 161 | let size = match self.control.data_out(self.control_buf).await { |
| 162 | Ok(size) => size, | ||
| 163 | Err(_) => { | ||
| 164 | warn!("usb: failed to read CONTROL OUT data stage."); | ||
| 165 | return; | ||
| 166 | } | ||
| 167 | }; | ||
| 162 | &self.control_buf[0..size] | 168 | &self.control_buf[0..size] |
| 163 | } else { | 169 | } else { |
| 164 | &[] | 170 | &[] |
| @@ -311,7 +317,6 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> { | |||
| 311 | if index == 0 { | 317 | if index == 0 { |
| 312 | self.control_in_accept_writer(req, |w| { | 318 | self.control_in_accept_writer(req, |w| { |
| 313 | w.write(descriptor_type::STRING, &lang_id::ENGLISH_US.to_le_bytes()) | 319 | w.write(descriptor_type::STRING, &lang_id::ENGLISH_US.to_le_bytes()) |
| 314 | .unwrap(); | ||
| 315 | }) | 320 | }) |
| 316 | .await | 321 | .await |
| 317 | } else { | 322 | } else { |
| @@ -328,8 +333,7 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> { | |||
| 328 | }; | 333 | }; |
| 329 | 334 | ||
| 330 | if let Some(s) = s { | 335 | if let Some(s) = s { |
| 331 | self.control_in_accept_writer(req, |w| w.string(s).unwrap()) | 336 | self.control_in_accept_writer(req, |w| w.string(s)).await; |
| 332 | .await; | ||
| 333 | } else { | 337 | } else { |
| 334 | self.control.reject() | 338 | self.control.reject() |
| 335 | } | 339 | } |
