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 | |
| parent | 60d3d111972f462c1f38d1d4fd27e89713974fc6 (diff) | |
usb: cleanup and simplify error handling.
| -rw-r--r-- | embassy-nrf/src/usb.rs | 18 | ||||
| -rw-r--r-- | embassy-usb-serial/src/lib.rs | 117 | ||||
| -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 |
5 files changed, 91 insertions, 131 deletions
diff --git a/embassy-nrf/src/usb.rs b/embassy-nrf/src/usb.rs index d9524675d..1057d880c 100644 --- a/embassy-nrf/src/usb.rs +++ b/embassy-nrf/src/usb.rs | |||
| @@ -403,11 +403,9 @@ unsafe fn read_dma<T: Instance>(i: usize, buf: &mut [u8]) -> Result<usize, ReadE | |||
| 403 | Ok(size) | 403 | Ok(size) |
| 404 | } | 404 | } |
| 405 | 405 | ||
| 406 | unsafe fn write_dma<T: Instance>(i: usize, buf: &[u8]) -> Result<(), WriteError> { | 406 | unsafe fn write_dma<T: Instance>(i: usize, buf: &[u8]) { |
| 407 | let regs = T::regs(); | 407 | let regs = T::regs(); |
| 408 | if buf.len() > 64 { | 408 | assert!(buf.len() <= 64); |
| 409 | return Err(WriteError::BufferOverflow); | ||
| 410 | } | ||
| 411 | 409 | ||
| 412 | let mut ram_buf: MaybeUninit<[u8; 64]> = MaybeUninit::uninit(); | 410 | let mut ram_buf: MaybeUninit<[u8; 64]> = MaybeUninit::uninit(); |
| 413 | let ptr = if !slice_in_ram(buf) { | 411 | let ptr = if !slice_in_ram(buf) { |
| @@ -441,8 +439,6 @@ unsafe fn write_dma<T: Instance>(i: usize, buf: &[u8]) -> Result<(), WriteError> | |||
| 441 | regs.tasks_startepin[i].write(|w| w.bits(1)); | 439 | regs.tasks_startepin[i].write(|w| w.bits(1)); |
| 442 | while regs.events_endepin[i].read().bits() == 0 {} | 440 | while regs.events_endepin[i].read().bits() == 0 {} |
| 443 | dma_end(); | 441 | dma_end(); |
| 444 | |||
| 445 | Ok(()) | ||
| 446 | } | 442 | } |
| 447 | 443 | ||
| 448 | impl<'d, T: Instance> driver::EndpointOut for Endpoint<'d, T, Out> { | 444 | impl<'d, T: Instance> driver::EndpointOut for Endpoint<'d, T, Out> { |
| @@ -497,6 +493,8 @@ impl<'d, T: Instance> driver::EndpointIn for Endpoint<'d, T, In> { | |||
| 497 | READY_ENDPOINTS.fetch_and(!(1 << i), Ordering::AcqRel); | 493 | READY_ENDPOINTS.fetch_and(!(1 << i), Ordering::AcqRel); |
| 498 | 494 | ||
| 499 | unsafe { write_dma::<T>(i, buf) } | 495 | unsafe { write_dma::<T>(i, buf) } |
| 496 | |||
| 497 | Ok(()) | ||
| 500 | } | 498 | } |
| 501 | } | 499 | } |
| 502 | } | 500 | } |
| @@ -535,9 +533,7 @@ impl<'d, T: Instance> ControlPipe<'d, T> { | |||
| 535 | async fn write(&mut self, buf: &[u8], last_chunk: bool) { | 533 | async fn write(&mut self, buf: &[u8], last_chunk: bool) { |
| 536 | let regs = T::regs(); | 534 | let regs = T::regs(); |
| 537 | regs.events_ep0datadone.reset(); | 535 | regs.events_ep0datadone.reset(); |
| 538 | unsafe { | 536 | unsafe { write_dma::<T>(0, buf) } |
| 539 | write_dma::<T>(0, buf).unwrap(); | ||
| 540 | } | ||
| 541 | 537 | ||
| 542 | regs.shorts | 538 | regs.shorts |
| 543 | .modify(|_, w| w.ep0datadone_ep0status().bit(last_chunk)); | 539 | .modify(|_, w| w.ep0datadone_ep0status().bit(last_chunk)); |
| @@ -616,7 +612,7 @@ impl<'d, T: Instance> driver::ControlPipe for ControlPipe<'d, T> { | |||
| 616 | 612 | ||
| 617 | fn data_out<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::DataOutFuture<'a> { | 613 | fn data_out<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::DataOutFuture<'a> { |
| 618 | async move { | 614 | async move { |
| 619 | let req = self.request.unwrap(); | 615 | let req = unwrap!(self.request); |
| 620 | assert!(req.direction == UsbDirection::Out); | 616 | assert!(req.direction == UsbDirection::Out); |
| 621 | assert!(req.length > 0); | 617 | assert!(req.length > 0); |
| 622 | 618 | ||
| @@ -649,7 +645,7 @@ impl<'d, T: Instance> driver::ControlPipe for ControlPipe<'d, T> { | |||
| 649 | debug!("control in accept {:x}", buf); | 645 | debug!("control in accept {:x}", buf); |
| 650 | #[cfg(not(feature = "defmt"))] | 646 | #[cfg(not(feature = "defmt"))] |
| 651 | debug!("control in accept {:x?}", buf); | 647 | debug!("control in accept {:x?}", buf); |
| 652 | let req = self.request.unwrap(); | 648 | let req = unwrap!(self.request); |
| 653 | assert!(req.direction == UsbDirection::In); | 649 | assert!(req.direction == UsbDirection::In); |
| 654 | 650 | ||
| 655 | let req_len = usize::from(req.length); | 651 | let req_len = usize::from(req.length); |
diff --git a/embassy-usb-serial/src/lib.rs b/embassy-usb-serial/src/lib.rs index ce1fb4775..a30cdd678 100644 --- a/embassy-usb-serial/src/lib.rs +++ b/embassy-usb-serial/src/lib.rs | |||
| @@ -184,78 +184,57 @@ impl<'d, D: Driver<'d>> CdcAcmClass<'d, D> { | |||
| 184 | let read_ep = builder.alloc_bulk_endpoint_out(max_packet_size); | 184 | let read_ep = builder.alloc_bulk_endpoint_out(max_packet_size); |
| 185 | let write_ep = builder.alloc_bulk_endpoint_in(max_packet_size); | 185 | let write_ep = builder.alloc_bulk_endpoint_in(max_packet_size); |
| 186 | 186 | ||
| 187 | builder | 187 | builder.config_descriptor.iad( |
| 188 | .config_descriptor | 188 | comm_if, |
| 189 | .iad( | 189 | 2, |
| 190 | comm_if, | 190 | USB_CLASS_CDC, |
| 191 | 2, | 191 | CDC_SUBCLASS_ACM, |
| 192 | USB_CLASS_CDC, | 192 | CDC_PROTOCOL_NONE, |
| 193 | CDC_SUBCLASS_ACM, | 193 | ); |
| 194 | CDC_PROTOCOL_NONE, | 194 | builder.config_descriptor.interface( |
| 195 | ) | 195 | comm_if, |
| 196 | .unwrap(); | 196 | USB_CLASS_CDC, |
| 197 | 197 | CDC_SUBCLASS_ACM, | |
| 198 | builder | 198 | CDC_PROTOCOL_NONE, |
| 199 | .config_descriptor | 199 | ); |
| 200 | .interface(comm_if, USB_CLASS_CDC, CDC_SUBCLASS_ACM, CDC_PROTOCOL_NONE) | 200 | builder.config_descriptor.write( |
| 201 | .unwrap(); | 201 | CS_INTERFACE, |
| 202 | 202 | &[ | |
| 203 | builder | 203 | CDC_TYPE_HEADER, // bDescriptorSubtype |
| 204 | .config_descriptor | 204 | 0x10, |
| 205 | .write( | 205 | 0x01, // bcdCDC (1.10) |
| 206 | CS_INTERFACE, | 206 | ], |
| 207 | &[ | 207 | ); |
| 208 | CDC_TYPE_HEADER, // bDescriptorSubtype | 208 | builder.config_descriptor.write( |
| 209 | 0x10, | 209 | CS_INTERFACE, |
| 210 | 0x01, // bcdCDC (1.10) | 210 | &[ |
| 211 | ], | 211 | CDC_TYPE_ACM, // bDescriptorSubtype |
| 212 | ) | 212 | 0x00, // bmCapabilities |
| 213 | .unwrap(); | 213 | ], |
| 214 | 214 | ); | |
| 215 | builder | 215 | builder.config_descriptor.write( |
| 216 | .config_descriptor | 216 | CS_INTERFACE, |
| 217 | .write( | 217 | &[ |
| 218 | CS_INTERFACE, | 218 | CDC_TYPE_UNION, // bDescriptorSubtype |
| 219 | &[ | 219 | comm_if.into(), // bControlInterface |
| 220 | CDC_TYPE_ACM, // bDescriptorSubtype | 220 | data_if.into(), // bSubordinateInterface |
| 221 | 0x00, // bmCapabilities | 221 | ], |
| 222 | ], | 222 | ); |
| 223 | ) | 223 | builder.config_descriptor.write( |
| 224 | .unwrap(); | 224 | CS_INTERFACE, |
| 225 | 225 | &[ | |
| 226 | builder | 226 | CDC_TYPE_CALL_MANAGEMENT, // bDescriptorSubtype |
| 227 | .config_descriptor | 227 | 0x00, // bmCapabilities |
| 228 | .write( | 228 | data_if.into(), // bDataInterface |
| 229 | CS_INTERFACE, | 229 | ], |
| 230 | &[ | 230 | ); |
| 231 | CDC_TYPE_UNION, // bDescriptorSubtype | 231 | builder.config_descriptor.endpoint(comm_ep.info()); |
| 232 | comm_if.into(), // bControlInterface | ||
| 233 | data_if.into(), // bSubordinateInterface | ||
| 234 | ], | ||
| 235 | ) | ||
| 236 | .unwrap(); | ||
| 237 | 232 | ||
| 238 | builder | 233 | builder |
| 239 | .config_descriptor | 234 | .config_descriptor |
| 240 | .write( | 235 | .interface(data_if, USB_CLASS_CDC_DATA, 0x00, 0x00); |
| 241 | CS_INTERFACE, | 236 | builder.config_descriptor.endpoint(write_ep.info()); |
| 242 | &[ | 237 | builder.config_descriptor.endpoint(read_ep.info()); |
| 243 | CDC_TYPE_CALL_MANAGEMENT, // bDescriptorSubtype | ||
| 244 | 0x00, // bmCapabilities | ||
| 245 | data_if.into(), // bDataInterface | ||
| 246 | ], | ||
| 247 | ) | ||
| 248 | .unwrap(); | ||
| 249 | |||
| 250 | builder.config_descriptor.endpoint(comm_ep.info()).unwrap(); | ||
| 251 | |||
| 252 | builder | ||
| 253 | .config_descriptor | ||
| 254 | .interface(data_if, USB_CLASS_CDC_DATA, 0x00, 0x00) | ||
| 255 | .unwrap(); | ||
| 256 | |||
| 257 | builder.config_descriptor.endpoint(write_ep.info()).unwrap(); | ||
| 258 | builder.config_descriptor.endpoint(read_ep.info()).unwrap(); | ||
| 259 | 238 | ||
| 260 | CdcAcmClass { | 239 | CdcAcmClass { |
| 261 | comm_ep, | 240 | comm_ep, |
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 | } |
