aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-usb/build.rs8
-rw-r--r--embassy-usb/src/builder.rs42
-rw-r--r--embassy-usb/src/class/cdc_acm.rs45
-rw-r--r--embassy-usb/src/class/cdc_ncm/mod.rs48
-rw-r--r--embassy-usb/src/class/hid.rs20
-rw-r--r--embassy-usb/src/class/midi.rs16
-rw-r--r--embassy-usb/src/control.rs2
-rw-r--r--embassy-usb/src/descriptor.rs32
-rw-r--r--embassy-usb/src/descriptor_reader.rs6
-rw-r--r--embassy-usb/src/lib.rs41
-rw-r--r--embassy-usb/src/msos.rs2
-rw-r--r--embassy-usb/src/types.rs4
12 files changed, 123 insertions, 143 deletions
diff --git a/embassy-usb/build.rs b/embassy-usb/build.rs
index 33d32f7d3..5e3bec485 100644
--- a/embassy-usb/build.rs
+++ b/embassy-usb/build.rs
@@ -70,9 +70,11 @@ fn main() {
70 70
71 // envvars take priority. 71 // envvars take priority.
72 if !cfg.seen_env { 72 if !cfg.seen_env {
73 if cfg.seen_feature { 73 assert!(
74 panic!("multiple values set for feature {}: {} and {}", name, cfg.value, value); 74 !cfg.seen_feature,
75 } 75 "multiple values set for feature {}: {} and {}",
76 name, cfg.value, value
77 );
76 78
77 cfg.value = value; 79 cfg.value = value;
78 cfg.seen_feature = true; 80 cfg.seen_feature = true;
diff --git a/embassy-usb/src/builder.rs b/embassy-usb/src/builder.rs
index 8860f9eea..b4ddccd71 100644
--- a/embassy-usb/src/builder.rs
+++ b/embassy-usb/src/builder.rs
@@ -1,17 +1,17 @@
1use heapless::Vec; 1use heapless::Vec;
2 2
3use crate::config::*; 3use crate::config::MAX_HANDLER_COUNT;
4use crate::descriptor::{BosWriter, DescriptorWriter}; 4use crate::descriptor::{BosWriter, DescriptorWriter};
5use crate::driver::{Driver, Endpoint, EndpointType}; 5use crate::driver::{Driver, Endpoint, EndpointType};
6#[cfg(feature = "msos-descriptor")] 6#[cfg(feature = "msos-descriptor")]
7use crate::msos::{DeviceLevelDescriptor, FunctionLevelDescriptor, MsOsDescriptorWriter}; 7use crate::msos::{DeviceLevelDescriptor, FunctionLevelDescriptor, MsOsDescriptorWriter};
8use crate::types::*; 8use crate::types::{InterfaceNumber, StringIndex};
9use crate::{Handler, Interface, UsbDevice, MAX_INTERFACE_COUNT, STRING_INDEX_CUSTOM_START}; 9use crate::{Handler, Interface, UsbDevice, MAX_INTERFACE_COUNT, STRING_INDEX_CUSTOM_START};
10 10
11#[derive(Debug, Copy, Clone)] 11#[derive(Debug, Copy, Clone)]
12#[cfg_attr(feature = "defmt", derive(defmt::Format))] 12#[cfg_attr(feature = "defmt", derive(defmt::Format))]
13#[non_exhaustive] 13#[non_exhaustive]
14/// Configuration used when creating [UsbDevice]. 14/// Configuration used when creating [`UsbDevice`].
15pub struct Config<'a> { 15pub struct Config<'a> {
16 pub(crate) vendor_id: u16, 16 pub(crate) vendor_id: u16,
17 pub(crate) product_id: u16, 17 pub(crate) product_id: u16,
@@ -159,9 +159,10 @@ impl<'d, D: Driver<'d>> Builder<'d, D> {
159 panic!("if composite_with_iads is set, you must set device_class = 0xEF, device_sub_class = 0x02, device_protocol = 0x01"); 159 panic!("if composite_with_iads is set, you must set device_class = 0xEF, device_sub_class = 0x02, device_protocol = 0x01");
160 } 160 }
161 161
162 if config.max_power > 500 { 162 assert!(
163 panic!("The maximum allowed value for `max_power` is 500mA"); 163 config.max_power <= 500,
164 } 164 "The maximum allowed value for `max_power` is 500mA"
165 );
165 166
166 match config.max_packet_size_0 { 167 match config.max_packet_size_0 {
167 8 | 16 | 32 | 64 => {} 168 8 | 16 | 32 | 64 => {}
@@ -260,12 +261,11 @@ impl<'d, D: Driver<'d>> Builder<'d, D> {
260 /// The Handler is called on some USB bus events, and to handle all control requests not already 261 /// The Handler is called on some USB bus events, and to handle all control requests not already
261 /// handled by the USB stack. 262 /// handled by the USB stack.
262 pub fn handler(&mut self, handler: &'d mut dyn Handler) { 263 pub fn handler(&mut self, handler: &'d mut dyn Handler) {
263 if self.handlers.push(handler).is_err() { 264 assert!(
264 panic!( 265 self.handlers.push(handler).is_ok(),
265 "embassy-usb: handler list full. Increase the `max_handler_count` compile-time setting. Current value: {}", 266 "embassy-usb: handler list full. Increase the `max_handler_count` compile-time setting. Current value: {}",
266 MAX_HANDLER_COUNT 267 MAX_HANDLER_COUNT
267 ) 268 );
268 }
269 } 269 }
270 270
271 /// Allocates a new string index. 271 /// Allocates a new string index.
@@ -332,12 +332,10 @@ impl<'a, 'd, D: Driver<'d>> FunctionBuilder<'a, 'd, D> {
332 num_alt_settings: 0, 332 num_alt_settings: 0,
333 }; 333 };
334 334
335 if self.builder.interfaces.push(iface).is_err() { 335 assert!(self.builder.interfaces.push(iface).is_ok(),
336 panic!( 336 "embassy-usb: interface list full. Increase the `max_interface_count` compile-time setting. Current value: {}",
337 "embassy-usb: interface list full. Increase the `max_interface_count` compile-time setting. Current value: {}", 337 MAX_INTERFACE_COUNT
338 MAX_INTERFACE_COUNT 338 );
339 )
340 }
341 339
342 InterfaceBuilder { 340 InterfaceBuilder {
343 builder: self.builder, 341 builder: self.builder,
@@ -371,7 +369,7 @@ pub struct InterfaceBuilder<'a, 'd, D: Driver<'d>> {
371 369
372impl<'a, 'd, D: Driver<'d>> InterfaceBuilder<'a, 'd, D> { 370impl<'a, 'd, D: Driver<'d>> InterfaceBuilder<'a, 'd, D> {
373 /// Get the interface number. 371 /// Get the interface number.
374 pub fn interface_number(&self) -> InterfaceNumber { 372 pub const fn interface_number(&self) -> InterfaceNumber {
375 self.interface_number 373 self.interface_number
376 } 374 }
377 375
@@ -422,12 +420,12 @@ pub struct InterfaceAltBuilder<'a, 'd, D: Driver<'d>> {
422 420
423impl<'a, 'd, D: Driver<'d>> InterfaceAltBuilder<'a, 'd, D> { 421impl<'a, 'd, D: Driver<'d>> InterfaceAltBuilder<'a, 'd, D> {
424 /// Get the interface number. 422 /// Get the interface number.
425 pub fn interface_number(&self) -> InterfaceNumber { 423 pub const fn interface_number(&self) -> InterfaceNumber {
426 self.interface_number 424 self.interface_number
427 } 425 }
428 426
429 /// Get the alternate setting number. 427 /// Get the alternate setting number.
430 pub fn alt_setting_number(&self) -> u8 { 428 pub const fn alt_setting_number(&self) -> u8 {
431 self.alt_setting_number 429 self.alt_setting_number
432 } 430 }
433 431
@@ -436,7 +434,7 @@ impl<'a, 'd, D: Driver<'d>> InterfaceAltBuilder<'a, 'd, D> {
436 /// Descriptors are written in the order builder functions are called. Note that some 434 /// Descriptors are written in the order builder functions are called. Note that some
437 /// classes care about the order. 435 /// classes care about the order.
438 pub fn descriptor(&mut self, descriptor_type: u8, descriptor: &[u8]) { 436 pub fn descriptor(&mut self, descriptor_type: u8, descriptor: &[u8]) {
439 self.builder.config_descriptor.write(descriptor_type, descriptor) 437 self.builder.config_descriptor.write(descriptor_type, descriptor);
440 } 438 }
441 439
442 fn endpoint_in(&mut self, ep_type: EndpointType, max_packet_size: u16, interval_ms: u8) -> D::EndpointIn { 440 fn endpoint_in(&mut self, ep_type: EndpointType, max_packet_size: u16, interval_ms: u8) -> D::EndpointIn {
diff --git a/embassy-usb/src/class/cdc_acm.rs b/embassy-usb/src/class/cdc_acm.rs
index 790f6faab..f1066d2f2 100644
--- a/embassy-usb/src/class/cdc_acm.rs
+++ b/embassy-usb/src/class/cdc_acm.rs
@@ -11,7 +11,7 @@ use embassy_sync::waitqueue::WakerRegistration;
11 11
12use crate::control::{self, InResponse, OutResponse, Recipient, Request, RequestType}; 12use crate::control::{self, InResponse, OutResponse, Recipient, Request, RequestType};
13use crate::driver::{Driver, Endpoint, EndpointError, EndpointIn, EndpointOut}; 13use crate::driver::{Driver, Endpoint, EndpointError, EndpointIn, EndpointOut};
14use crate::types::*; 14use crate::types::InterfaceNumber;
15use crate::{Builder, Handler}; 15use crate::{Builder, Handler};
16 16
17/// This should be used as `device_class` when building the `UsbDevice`. 17/// This should be used as `device_class` when building the `UsbDevice`.
@@ -50,7 +50,7 @@ impl<'a> State<'a> {
50 pub fn new() -> Self { 50 pub fn new() -> Self {
51 Self { 51 Self {
52 control: MaybeUninit::uninit(), 52 control: MaybeUninit::uninit(),
53 shared: Default::default(), 53 shared: ControlShared::default(),
54 } 54 }
55 } 55 }
56} 56}
@@ -61,9 +61,9 @@ impl<'a> State<'a> {
61/// writing USB packets with no intermediate buffers, but it will not act like a stream-like serial 61/// writing USB packets with no intermediate buffers, but it will not act like a stream-like serial
62/// port. The following constraints must be followed if you use this class directly: 62/// port. The following constraints must be followed if you use this class directly:
63/// 63///
64/// - `read_packet` must be called with a buffer large enough to hold max_packet_size bytes. 64/// - `read_packet` must be called with a buffer large enough to hold `max_packet_size` bytes.
65/// - `write_packet` must not be called with a buffer larger than max_packet_size bytes. 65/// - `write_packet` must not be called with a buffer larger than `max_packet_size` bytes.
66/// - If you write a packet that is exactly max_packet_size bytes long, it won't be processed by the 66/// - If you write a packet that is exactly `max_packet_size` bytes long, it won't be processed by the
67/// host operating system until a subsequent shorter packet is sent. A zero-length packet (ZLP) 67/// host operating system until a subsequent shorter packet is sent. A zero-length packet (ZLP)
68/// can be sent if there is no other data to send. This is because USB bulk transactions must be 68/// can be sent if there is no other data to send. This is because USB bulk transactions must be
69/// terminated with a short packet, even if the bulk endpoint is used for stream-like data. 69/// terminated with a short packet, even if the bulk endpoint is used for stream-like data.
@@ -109,17 +109,16 @@ impl Default for ControlShared {
109 109
110impl ControlShared { 110impl ControlShared {
111 async fn changed(&self) { 111 async fn changed(&self) {
112 poll_fn(|cx| match self.changed.load(Ordering::Relaxed) { 112 poll_fn(|cx| {
113 true => { 113 if self.changed.load(Ordering::Relaxed) {
114 self.changed.store(false, Ordering::Relaxed); 114 self.changed.store(false, Ordering::Relaxed);
115 Poll::Ready(()) 115 Poll::Ready(())
116 } 116 } else {
117 false => {
118 self.waker.borrow_mut().register(cx.waker()); 117 self.waker.borrow_mut().register(cx.waker());
119 Poll::Pending 118 Poll::Pending
120 } 119 }
121 }) 120 })
122 .await 121 .await;
123 } 122 }
124} 123}
125 124
@@ -198,7 +197,7 @@ impl<'d> Handler for Control<'d> {
198 // REQ_GET_ENCAPSULATED_COMMAND is not really supported - it will be rejected below. 197 // REQ_GET_ENCAPSULATED_COMMAND is not really supported - it will be rejected below.
199 REQ_GET_LINE_CODING if req.length == 7 => { 198 REQ_GET_LINE_CODING if req.length == 7 => {
200 debug!("Sending line coding"); 199 debug!("Sending line coding");
201 let coding = self.shared().line_coding.lock(|x| x.get()); 200 let coding = self.shared().line_coding.lock(Cell::get);
202 assert!(buf.len() >= 7); 201 assert!(buf.len() >= 7);
203 buf[0..4].copy_from_slice(&coding.data_rate.to_le_bytes()); 202 buf[0..4].copy_from_slice(&coding.data_rate.to_le_bytes());
204 buf[4] = coding.stop_bits as u8; 203 buf[4] = coding.stop_bits as u8;
@@ -212,8 +211,8 @@ impl<'d> Handler for Control<'d> {
212} 211}
213 212
214impl<'d, D: Driver<'d>> CdcAcmClass<'d, D> { 213impl<'d, D: Driver<'d>> CdcAcmClass<'d, D> {
215 /// Creates a new CdcAcmClass with the provided UsbBus and max_packet_size in bytes. For 214 /// Creates a new CdcAcmClass with the provided UsbBus and `max_packet_size` in bytes. For
216 /// full-speed devices, max_packet_size has to be one of 8, 16, 32 or 64. 215 /// full-speed devices, `max_packet_size` has to be one of 8, 16, 32 or 64.
217 pub fn new(builder: &mut Builder<'d, D>, state: &'d mut State<'d>, max_packet_size: u16) -> Self { 216 pub fn new(builder: &mut Builder<'d, D>, state: &'d mut State<'d>, max_packet_size: u16) -> Self {
218 assert!(builder.control_buf_len() >= 7); 217 assert!(builder.control_buf_len() >= 7);
219 218
@@ -289,7 +288,7 @@ impl<'d, D: Driver<'d>> CdcAcmClass<'d, D> {
289 /// Gets the current line coding. The line coding contains information that's mainly relevant 288 /// Gets the current line coding. The line coding contains information that's mainly relevant
290 /// for USB to UART serial port emulators, and can be ignored if not relevant. 289 /// for USB to UART serial port emulators, and can be ignored if not relevant.
291 pub fn line_coding(&self) -> LineCoding { 290 pub fn line_coding(&self) -> LineCoding {
292 self.control.line_coding.lock(|x| x.get()) 291 self.control.line_coding.lock(Cell::get)
293 } 292 }
294 293
295 /// Gets the DTR (data terminal ready) state 294 /// Gets the DTR (data terminal ready) state
@@ -314,7 +313,7 @@ impl<'d, D: Driver<'d>> CdcAcmClass<'d, D> {
314 313
315 /// Waits for the USB host to enable this interface 314 /// Waits for the USB host to enable this interface
316 pub async fn wait_connection(&mut self) { 315 pub async fn wait_connection(&mut self) {
317 self.read_ep.wait_enabled().await 316 self.read_ep.wait_enabled().await;
318 } 317 }
319 318
320 /// Split the class into a sender and receiver. 319 /// Split the class into a sender and receiver.
@@ -362,7 +361,7 @@ pub struct ControlChanged<'d> {
362impl<'d> ControlChanged<'d> { 361impl<'d> ControlChanged<'d> {
363 /// Return a future for when the control settings change 362 /// Return a future for when the control settings change
364 pub async fn control_changed(&self) { 363 pub async fn control_changed(&self) {
365 self.control.changed().await 364 self.control.changed().await;
366 } 365 }
367} 366}
368 367
@@ -384,7 +383,7 @@ impl<'d, D: Driver<'d>> Sender<'d, D> {
384 /// Gets the current line coding. The line coding contains information that's mainly relevant 383 /// Gets the current line coding. The line coding contains information that's mainly relevant
385 /// for USB to UART serial port emulators, and can be ignored if not relevant. 384 /// for USB to UART serial port emulators, and can be ignored if not relevant.
386 pub fn line_coding(&self) -> LineCoding { 385 pub fn line_coding(&self) -> LineCoding {
387 self.control.line_coding.lock(|x| x.get()) 386 self.control.line_coding.lock(Cell::get)
388 } 387 }
389 388
390 /// Gets the DTR (data terminal ready) state 389 /// Gets the DTR (data terminal ready) state
@@ -404,7 +403,7 @@ impl<'d, D: Driver<'d>> Sender<'d, D> {
404 403
405 /// Waits for the USB host to enable this interface 404 /// Waits for the USB host to enable this interface
406 pub async fn wait_connection(&mut self) { 405 pub async fn wait_connection(&mut self) {
407 self.write_ep.wait_enabled().await 406 self.write_ep.wait_enabled().await;
408 } 407 }
409} 408}
410 409
@@ -426,7 +425,7 @@ impl<'d, D: Driver<'d>> Receiver<'d, D> {
426 /// Gets the current line coding. The line coding contains information that's mainly relevant 425 /// Gets the current line coding. The line coding contains information that's mainly relevant
427 /// for USB to UART serial port emulators, and can be ignored if not relevant. 426 /// for USB to UART serial port emulators, and can be ignored if not relevant.
428 pub fn line_coding(&self) -> LineCoding { 427 pub fn line_coding(&self) -> LineCoding {
429 self.control.line_coding.lock(|x| x.get()) 428 self.control.line_coding.lock(Cell::get)
430 } 429 }
431 430
432 /// Gets the DTR (data terminal ready) state 431 /// Gets the DTR (data terminal ready) state
@@ -446,7 +445,7 @@ impl<'d, D: Driver<'d>> Receiver<'d, D> {
446 445
447 /// Waits for the USB host to enable this interface 446 /// Waits for the USB host to enable this interface
448 pub async fn wait_connection(&mut self) { 447 pub async fn wait_connection(&mut self) {
449 self.read_ep.wait_enabled().await 448 self.read_ep.wait_enabled().await;
450 } 449 }
451} 450}
452 451
@@ -520,17 +519,17 @@ impl LineCoding {
520 } 519 }
521 520
522 /// Gets the number of data bits for UART communication. 521 /// Gets the number of data bits for UART communication.
523 pub fn data_bits(&self) -> u8 { 522 pub const fn data_bits(&self) -> u8 {
524 self.data_bits 523 self.data_bits
525 } 524 }
526 525
527 /// Gets the parity type for UART communication. 526 /// Gets the parity type for UART communication.
528 pub fn parity_type(&self) -> ParityType { 527 pub const fn parity_type(&self) -> ParityType {
529 self.parity_type 528 self.parity_type
530 } 529 }
531 530
532 /// Gets the data rate in bits per second for UART communication. 531 /// Gets the data rate in bits per second for UART communication.
533 pub fn data_rate(&self) -> u32 { 532 pub const fn data_rate(&self) -> u32 {
534 self.data_rate 533 self.data_rate
535 } 534 }
536} 535}
diff --git a/embassy-usb/src/class/cdc_ncm/mod.rs b/embassy-usb/src/class/cdc_ncm/mod.rs
index 27716b37d..bea9dac27 100644
--- a/embassy-usb/src/class/cdc_ncm/mod.rs
+++ b/embassy-usb/src/class/cdc_ncm/mod.rs
@@ -16,10 +16,11 @@
16 16
17use core::intrinsics::copy_nonoverlapping; 17use core::intrinsics::copy_nonoverlapping;
18use core::mem::{size_of, MaybeUninit}; 18use core::mem::{size_of, MaybeUninit};
19use core::ptr::addr_of;
19 20
20use crate::control::{self, InResponse, OutResponse, Recipient, Request, RequestType}; 21use crate::control::{self, InResponse, OutResponse, Recipient, Request, RequestType};
21use crate::driver::{Driver, Endpoint, EndpointError, EndpointIn, EndpointOut}; 22use crate::driver::{Driver, Endpoint, EndpointError, EndpointIn, EndpointOut};
22use crate::types::*; 23use crate::types::{InterfaceNumber, StringIndex};
23use crate::{Builder, Handler}; 24use crate::{Builder, Handler};
24 25
25pub mod embassy_net; 26pub mod embassy_net;
@@ -62,9 +63,9 @@ const REQ_SET_NTB_INPUT_SIZE: u8 = 0x86;
62//const NOTIF_POLL_INTERVAL: u8 = 20; 63//const NOTIF_POLL_INTERVAL: u8 = 20;
63 64
64const NTB_MAX_SIZE: usize = 2048; 65const NTB_MAX_SIZE: usize = 2048;
65const SIG_NTH: u32 = 0x484d434e; 66const SIG_NTH: u32 = 0x484d_434e;
66const SIG_NDP_NO_FCS: u32 = 0x304d434e; 67const SIG_NDP_NO_FCS: u32 = 0x304d_434e;
67const SIG_NDP_WITH_FCS: u32 = 0x314d434e; 68const SIG_NDP_WITH_FCS: u32 = 0x314d_434e;
68 69
69const ALTERNATE_SETTING_DISABLED: u8 = 0x00; 70const ALTERNATE_SETTING_DISABLED: u8 = 0x00;
70const ALTERNATE_SETTING_ENABLED: u8 = 0x01; 71const ALTERNATE_SETTING_ENABLED: u8 = 0x01;
@@ -111,7 +112,7 @@ struct NtbParametersDir {
111 112
112fn byteify<T>(buf: &mut [u8], data: T) -> &[u8] { 113fn byteify<T>(buf: &mut [u8], data: T) -> &[u8] {
113 let len = size_of::<T>(); 114 let len = size_of::<T>();
114 unsafe { copy_nonoverlapping(&data as *const _ as *const u8, buf.as_mut_ptr(), len) } 115 unsafe { copy_nonoverlapping(addr_of!(data).cast(), buf.as_mut_ptr(), len) }
115 &buf[..len] 116 &buf[..len]
116} 117}
117 118
@@ -132,12 +133,12 @@ impl<'a> State<'a> {
132 pub fn new() -> Self { 133 pub fn new() -> Self {
133 Self { 134 Self {
134 control: MaybeUninit::uninit(), 135 control: MaybeUninit::uninit(),
135 shared: Default::default(), 136 shared: ControlShared::default(),
136 } 137 }
137 } 138 }
138} 139}
139 140
140/// Shared data between Control and CdcAcmClass 141/// Shared data between Control and `CdcAcmClass`
141#[derive(Default)] 142#[derive(Default)]
142struct ControlShared { 143struct ControlShared {
143 mac_addr: [u8; 6], 144 mac_addr: [u8; 6],
@@ -378,12 +379,12 @@ impl<'d, D: Driver<'d>> Sender<'d, D> {
378 /// 379 ///
379 /// This waits until the packet is successfully stored in the CDC-NCM endpoint buffers. 380 /// This waits until the packet is successfully stored in the CDC-NCM endpoint buffers.
380 pub async fn write_packet(&mut self, data: &[u8]) -> Result<(), EndpointError> { 381 pub async fn write_packet(&mut self, data: &[u8]) -> Result<(), EndpointError> {
381 let seq = self.seq;
382 self.seq = self.seq.wrapping_add(1);
383
384 const OUT_HEADER_LEN: usize = 28; 382 const OUT_HEADER_LEN: usize = 28;
385 const ABS_MAX_PACKET_SIZE: usize = 512; 383 const ABS_MAX_PACKET_SIZE: usize = 512;
386 384
385 let seq = self.seq;
386 self.seq = self.seq.wrapping_add(1);
387
387 let header = NtbOutHeader { 388 let header = NtbOutHeader {
388 nth_sig: SIG_NTH, 389 nth_sig: SIG_NTH,
389 nth_len: 0x0c, 390 nth_len: 0x0c,
@@ -460,12 +461,9 @@ impl<'d, D: Driver<'d>> Receiver<'d, D> {
460 let ntb = &ntb[..pos]; 461 let ntb = &ntb[..pos];
461 462
462 // Process NTB header (NTH) 463 // Process NTB header (NTH)
463 let nth = match ntb.get(..12) { 464 let Some(nth) = ntb.get(..12) else {
464 Some(x) => x, 465 warn!("Received too short NTB");
465 None => { 466 continue;
466 warn!("Received too short NTB");
467 continue;
468 }
469 }; 467 };
470 let sig = u32::from_le_bytes(nth[0..4].try_into().unwrap()); 468 let sig = u32::from_le_bytes(nth[0..4].try_into().unwrap());
471 if sig != SIG_NTH { 469 if sig != SIG_NTH {
@@ -475,12 +473,9 @@ impl<'d, D: Driver<'d>> Receiver<'d, D> {
475 let ndp_idx = u16::from_le_bytes(nth[10..12].try_into().unwrap()) as usize; 473 let ndp_idx = u16::from_le_bytes(nth[10..12].try_into().unwrap()) as usize;
476 474
477 // Process NTB Datagram Pointer (NDP) 475 // Process NTB Datagram Pointer (NDP)
478 let ndp = match ntb.get(ndp_idx..ndp_idx + 12) { 476 let Some(ndp) = ntb.get(ndp_idx..ndp_idx + 12) else {
479 Some(x) => x, 477 warn!("NTH has an NDP pointer out of range.");
480 None => { 478 continue;
481 warn!("NTH has an NDP pointer out of range.");
482 continue;
483 }
484 }; 479 };
485 let sig = u32::from_le_bytes(ndp[0..4].try_into().unwrap()); 480 let sig = u32::from_le_bytes(ndp[0..4].try_into().unwrap());
486 if sig != SIG_NDP_NO_FCS && sig != SIG_NDP_WITH_FCS { 481 if sig != SIG_NDP_NO_FCS && sig != SIG_NDP_WITH_FCS {
@@ -496,12 +491,9 @@ impl<'d, D: Driver<'d>> Receiver<'d, D> {
496 } 491 }
497 492
498 // Process actual datagram, finally. 493 // Process actual datagram, finally.
499 let datagram = match ntb.get(datagram_index..datagram_index + datagram_len) { 494 let Some(datagram) = ntb.get(datagram_index..datagram_index + datagram_len) else {
500 Some(x) => x, 495 warn!("NDP has a datagram pointer out of range.");
501 None => { 496 continue;
502 warn!("NDP has a datagram pointer out of range.");
503 continue;
504 }
505 }; 497 };
506 buf[..datagram_len].copy_from_slice(datagram); 498 buf[..datagram_len].copy_from_slice(datagram);
507 499
diff --git a/embassy-usb/src/class/hid.rs b/embassy-usb/src/class/hid.rs
index 0da29b1a6..0000b5b2b 100644
--- a/embassy-usb/src/class/hid.rs
+++ b/embassy-usb/src/class/hid.rs
@@ -63,7 +63,7 @@ pub enum ReportId {
63} 63}
64 64
65impl ReportId { 65impl ReportId {
66 fn try_from(value: u16) -> Result<Self, ()> { 66 const fn try_from(value: u16) -> Result<Self, ()> {
67 match value >> 8 { 67 match value >> 8 {
68 1 => Ok(ReportId::In(value as u8)), 68 1 => Ok(ReportId::In(value as u8)),
69 2 => Ok(ReportId::Out(value as u8)), 69 2 => Ok(ReportId::Out(value as u8)),
@@ -87,7 +87,7 @@ impl<'d> Default for State<'d> {
87 87
88impl<'d> State<'d> { 88impl<'d> State<'d> {
89 /// Create a new `State`. 89 /// Create a new `State`.
90 pub fn new() -> Self { 90 pub const fn new() -> Self {
91 State { 91 State {
92 control: MaybeUninit::uninit(), 92 control: MaybeUninit::uninit(),
93 out_report_offset: AtomicUsize::new(0), 93 out_report_offset: AtomicUsize::new(0),
@@ -154,7 +154,7 @@ fn build<'d, D: Driver<'d>>(
154} 154}
155 155
156impl<'d, D: Driver<'d>, const READ_N: usize, const WRITE_N: usize> HidReaderWriter<'d, D, READ_N, WRITE_N> { 156impl<'d, D: Driver<'d>, const READ_N: usize, const WRITE_N: usize> HidReaderWriter<'d, D, READ_N, WRITE_N> {
157 /// Creates a new HidReaderWriter. 157 /// Creates a new `HidReaderWriter`.
158 /// 158 ///
159 /// This will allocate one IN and one OUT endpoints. If you only need writing (sending) 159 /// This will allocate one IN and one OUT endpoints. If you only need writing (sending)
160 /// HID reports, consider using [`HidWriter::new`] instead, which allocates an IN endpoint only. 160 /// HID reports, consider using [`HidWriter::new`] instead, which allocates an IN endpoint only.
@@ -230,7 +230,7 @@ pub enum ReadError {
230 230
231impl From<EndpointError> for ReadError { 231impl From<EndpointError> for ReadError {
232 fn from(val: EndpointError) -> Self { 232 fn from(val: EndpointError) -> Self {
233 use EndpointError::*; 233 use EndpointError::{BufferOverflow, Disabled};
234 match val { 234 match val {
235 BufferOverflow => ReadError::BufferOverflow, 235 BufferOverflow => ReadError::BufferOverflow,
236 Disabled => ReadError::Disabled, 236 Disabled => ReadError::Disabled,
@@ -258,16 +258,15 @@ impl<'d, D: Driver<'d>, const N: usize> HidWriter<'d, D, N> {
258 258
259 /// Waits for the interrupt in endpoint to be enabled. 259 /// Waits for the interrupt in endpoint to be enabled.
260 pub async fn ready(&mut self) { 260 pub async fn ready(&mut self) {
261 self.ep_in.wait_enabled().await 261 self.ep_in.wait_enabled().await;
262 } 262 }
263 263
264 /// Writes an input report by serializing the given report structure. 264 /// Writes an input report by serializing the given report structure.
265 #[cfg(feature = "usbd-hid")] 265 #[cfg(feature = "usbd-hid")]
266 pub async fn write_serialize<IR: AsInputReport>(&mut self, r: &IR) -> Result<(), EndpointError> { 266 pub async fn write_serialize<IR: AsInputReport>(&mut self, r: &IR) -> Result<(), EndpointError> {
267 let mut buf: [u8; N] = [0; N]; 267 let mut buf: [u8; N] = [0; N];
268 let size = match serialize(&mut buf, r) { 268 let Ok(size) = serialize(&mut buf, r) else {
269 Ok(size) => size, 269 return Err(EndpointError::BufferOverflow);
270 Err(_) => return Err(EndpointError::BufferOverflow),
271 }; 270 };
272 self.write(&buf[0..size]).await 271 self.write(&buf[0..size]).await
273 } 272 }
@@ -293,7 +292,7 @@ impl<'d, D: Driver<'d>, const N: usize> HidWriter<'d, D, N> {
293impl<'d, D: Driver<'d>, const N: usize> HidReader<'d, D, N> { 292impl<'d, D: Driver<'d>, const N: usize> HidReader<'d, D, N> {
294 /// Waits for the interrupt out endpoint to be enabled. 293 /// Waits for the interrupt out endpoint to be enabled.
295 pub async fn ready(&mut self) { 294 pub async fn ready(&mut self) {
296 self.ep_out.wait_enabled().await 295 self.ep_out.wait_enabled().await;
297 } 296 }
298 297
299 /// Delivers output reports from the Interrupt Out pipe to `handler`. 298 /// Delivers output reports from the Interrupt Out pipe to `handler`.
@@ -350,9 +349,8 @@ impl<'d, D: Driver<'d>, const N: usize> HidReader<'d, D, N> {
350 if size < max_packet_size || total == N { 349 if size < max_packet_size || total == N {
351 self.offset.store(0, Ordering::Release); 350 self.offset.store(0, Ordering::Release);
352 break; 351 break;
353 } else {
354 self.offset.store(total, Ordering::Release);
355 } 352 }
353 self.offset.store(total, Ordering::Release);
356 } 354 }
357 Err(err) => { 355 Err(err) => {
358 self.offset.store(0, Ordering::Release); 356 self.offset.store(0, Ordering::Release);
diff --git a/embassy-usb/src/class/midi.rs b/embassy-usb/src/class/midi.rs
index c5cf8d876..52a96f278 100644
--- a/embassy-usb/src/class/midi.rs
+++ b/embassy-usb/src/class/midi.rs
@@ -27,9 +27,9 @@ const MIDI_OUT_SIZE: u8 = 0x09;
27/// writing USB packets with no intermediate buffers, but it will not act like a stream-like port. 27/// writing USB packets with no intermediate buffers, but it will not act like a stream-like port.
28/// The following constraints must be followed if you use this class directly: 28/// The following constraints must be followed if you use this class directly:
29/// 29///
30/// - `read_packet` must be called with a buffer large enough to hold max_packet_size bytes. 30/// - `read_packet` must be called with a buffer large enough to hold `max_packet_size` bytes.
31/// - `write_packet` must not be called with a buffer larger than max_packet_size bytes. 31/// - `write_packet` must not be called with a buffer larger than `max_packet_size` bytes.
32/// - If you write a packet that is exactly max_packet_size bytes long, it won't be processed by the 32/// - If you write a packet that is exactly `max_packet_size` bytes long, it won't be processed by the
33/// host operating system until a subsequent shorter packet is sent. A zero-length packet (ZLP) 33/// host operating system until a subsequent shorter packet is sent. A zero-length packet (ZLP)
34/// can be sent if there is no other data to send. This is because USB bulk transactions must be 34/// can be sent if there is no other data to send. This is because USB bulk transactions must be
35/// terminated with a short packet, even if the bulk endpoint is used for stream-like data. 35/// terminated with a short packet, even if the bulk endpoint is used for stream-like data.
@@ -39,8 +39,8 @@ pub struct MidiClass<'d, D: Driver<'d>> {
39} 39}
40 40
41impl<'d, D: Driver<'d>> MidiClass<'d, D> { 41impl<'d, D: Driver<'d>> MidiClass<'d, D> {
42 /// Creates a new MidiClass with the provided UsbBus, number of input and output jacks and max_packet_size in bytes. 42 /// Creates a new `MidiClass` with the provided UsbBus, number of input and output jacks and `max_packet_size` in bytes.
43 /// For full-speed devices, max_packet_size has to be one of 8, 16, 32 or 64. 43 /// For full-speed devices, `max_packet_size` has to be one of 8, 16, 32 or 64.
44 pub fn new(builder: &mut Builder<'d, D>, n_in_jacks: u8, n_out_jacks: u8, max_packet_size: u16) -> Self { 44 pub fn new(builder: &mut Builder<'d, D>, n_in_jacks: u8, n_out_jacks: u8, max_packet_size: u16) -> Self {
45 let mut func = builder.function(USB_AUDIO_CLASS, USB_AUDIOCONTROL_SUBCLASS, PROTOCOL_NONE); 45 let mut func = builder.function(USB_AUDIO_CLASS, USB_AUDIOCONTROL_SUBCLASS, PROTOCOL_NONE);
46 46
@@ -160,7 +160,7 @@ impl<'d, D: Driver<'d>> MidiClass<'d, D> {
160 160
161 /// Waits for the USB host to enable this interface 161 /// Waits for the USB host to enable this interface
162 pub async fn wait_connection(&mut self) { 162 pub async fn wait_connection(&mut self) {
163 self.read_ep.wait_enabled().await 163 self.read_ep.wait_enabled().await;
164 } 164 }
165 165
166 /// Split the class into a sender and receiver. 166 /// Split the class into a sender and receiver.
@@ -197,7 +197,7 @@ impl<'d, D: Driver<'d>> Sender<'d, D> {
197 197
198 /// Waits for the USB host to enable this interface 198 /// Waits for the USB host to enable this interface
199 pub async fn wait_connection(&mut self) { 199 pub async fn wait_connection(&mut self) {
200 self.write_ep.wait_enabled().await 200 self.write_ep.wait_enabled().await;
201 } 201 }
202} 202}
203 203
@@ -222,6 +222,6 @@ impl<'d, D: Driver<'d>> Receiver<'d, D> {
222 222
223 /// Waits for the USB host to enable this interface 223 /// Waits for the USB host to enable this interface
224 pub async fn wait_connection(&mut self) { 224 pub async fn wait_connection(&mut self) {
225 self.read_ep.wait_enabled().await 225 self.read_ep.wait_enabled().await;
226 } 226 }
227} 227}
diff --git a/embassy-usb/src/control.rs b/embassy-usb/src/control.rs
index ceccfd85b..79f736309 100644
--- a/embassy-usb/src/control.rs
+++ b/embassy-usb/src/control.rs
@@ -120,7 +120,7 @@ impl Request {
120 } 120 }
121 121
122 /// Gets the descriptor type and index from the value field of a GET_DESCRIPTOR request. 122 /// Gets the descriptor type and index from the value field of a GET_DESCRIPTOR request.
123 pub fn descriptor_type_index(&self) -> (u8, u8) { 123 pub const fn descriptor_type_index(&self) -> (u8, u8) {
124 ((self.value >> 8) as u8, self.value as u8) 124 ((self.value >> 8) as u8, self.value as u8)
125 } 125 }
126} 126}
diff --git a/embassy-usb/src/descriptor.rs b/embassy-usb/src/descriptor.rs
index 16c5f9d9b..fa83ef583 100644
--- a/embassy-usb/src/descriptor.rs
+++ b/embassy-usb/src/descriptor.rs
@@ -2,7 +2,7 @@
2 2
3use crate::builder::Config; 3use crate::builder::Config;
4use crate::driver::EndpointInfo; 4use crate::driver::EndpointInfo;
5use crate::types::*; 5use crate::types::{InterfaceNumber, StringIndex};
6use crate::CONFIGURATION_VALUE; 6use crate::CONFIGURATION_VALUE;
7 7
8/// Standard descriptor types 8/// Standard descriptor types
@@ -59,7 +59,7 @@ impl<'a> DescriptorWriter<'a> {
59 } 59 }
60 60
61 /// Gets the current position in the buffer, i.e. the number of bytes written so far. 61 /// Gets the current position in the buffer, i.e. the number of bytes written so far.
62 pub fn position(&self) -> usize { 62 pub const fn position(&self) -> usize {
63 self.position 63 self.position
64 } 64 }
65 65
@@ -67,9 +67,10 @@ impl<'a> DescriptorWriter<'a> {
67 pub fn write(&mut self, descriptor_type: u8, descriptor: &[u8]) { 67 pub fn write(&mut self, descriptor_type: u8, descriptor: &[u8]) {
68 let length = descriptor.len(); 68 let length = descriptor.len();
69 69
70 if (self.position + 2 + length) > self.buf.len() || (length + 2) > 255 { 70 assert!(
71 panic!("Descriptor buffer full"); 71 (self.position + 2 + length) <= self.buf.len() && (length + 2) <= 255,
72 } 72 "Descriptor buffer full"
73 );
73 74
74 self.buf[self.position] = (length + 2) as u8; 75 self.buf[self.position] = (length + 2) as u8;
75 self.buf[self.position + 1] = descriptor_type; 76 self.buf[self.position + 1] = descriptor_type;
@@ -102,7 +103,7 @@ impl<'a> DescriptorWriter<'a> {
102 config.serial_number.map_or(0, |_| 3), // iSerialNumber 103 config.serial_number.map_or(0, |_| 3), // iSerialNumber
103 1, // bNumConfigurations 104 1, // bNumConfigurations
104 ], 105 ],
105 ) 106 );
106 } 107 }
107 108
108 pub(crate) fn configuration(&mut self, config: &Config) { 109 pub(crate) fn configuration(&mut self, config: &Config) {
@@ -120,7 +121,7 @@ impl<'a> DescriptorWriter<'a> {
120 | if config.supports_remote_wakeup { 0x20 } else { 0x00 }, // bmAttributes 121 | if config.supports_remote_wakeup { 0x20 } else { 0x00 }, // bmAttributes
121 (config.max_power / 2) as u8, // bMaxPower 122 (config.max_power / 2) as u8, // bMaxPower
122 ], 123 ],
123 ) 124 );
124 } 125 }
125 126
126 #[allow(unused)] 127 #[allow(unused)]
@@ -248,9 +249,7 @@ impl<'a> DescriptorWriter<'a> {
248 pub(crate) fn string(&mut self, string: &str) { 249 pub(crate) fn string(&mut self, string: &str) {
249 let mut pos = self.position; 250 let mut pos = self.position;
250 251
251 if pos + 2 > self.buf.len() { 252 assert!(pos + 2 <= self.buf.len(), "Descriptor buffer full");
252 panic!("Descriptor buffer full");
253 }
254 253
255 self.buf[pos] = 0; // length placeholder 254 self.buf[pos] = 0; // length placeholder
256 self.buf[pos + 1] = descriptor_type::STRING; 255 self.buf[pos + 1] = descriptor_type::STRING;
@@ -258,9 +257,7 @@ impl<'a> DescriptorWriter<'a> {
258 pos += 2; 257 pos += 2;
259 258
260 for c in string.encode_utf16() { 259 for c in string.encode_utf16() {
261 if pos >= self.buf.len() { 260 assert!(pos < self.buf.len(), "Descriptor buffer full");
262 panic!("Descriptor buffer full");
263 }
264 261
265 self.buf[pos..pos + 2].copy_from_slice(&c.to_le_bytes()); 262 self.buf[pos..pos + 2].copy_from_slice(&c.to_le_bytes());
266 pos += 2; 263 pos += 2;
@@ -279,7 +276,7 @@ pub struct BosWriter<'a> {
279} 276}
280 277
281impl<'a> BosWriter<'a> { 278impl<'a> BosWriter<'a> {
282 pub(crate) fn new(writer: DescriptorWriter<'a>) -> Self { 279 pub(crate) const fn new(writer: DescriptorWriter<'a>) -> Self {
283 Self { 280 Self {
284 writer, 281 writer,
285 num_caps_mark: None, 282 num_caps_mark: None,
@@ -314,9 +311,10 @@ impl<'a> BosWriter<'a> {
314 let mut start = self.writer.position; 311 let mut start = self.writer.position;
315 let blen = data.len(); 312 let blen = data.len();
316 313
317 if (start + blen + 3) > self.writer.buf.len() || (blen + 3) > 255 { 314 assert!(
318 panic!("Descriptor buffer full"); 315 (start + blen + 3) <= self.writer.buf.len() && (blen + 3) <= 255,
319 } 316 "Descriptor buffer full"
317 );
320 318
321 self.writer.buf[start] = (blen + 3) as u8; 319 self.writer.buf[start] = (blen + 3) as u8;
322 self.writer.buf[start + 1] = descriptor_type::CAPABILITY; 320 self.writer.buf[start + 1] = descriptor_type::CAPABILITY;
diff --git a/embassy-usb/src/descriptor_reader.rs b/embassy-usb/src/descriptor_reader.rs
index 05adcce60..abb4b379e 100644
--- a/embassy-usb/src/descriptor_reader.rs
+++ b/embassy-usb/src/descriptor_reader.rs
@@ -11,11 +11,11 @@ pub struct Reader<'a> {
11} 11}
12 12
13impl<'a> Reader<'a> { 13impl<'a> Reader<'a> {
14 pub fn new(data: &'a [u8]) -> Self { 14 pub const fn new(data: &'a [u8]) -> Self {
15 Self { data } 15 Self { data }
16 } 16 }
17 17
18 pub fn eof(&self) -> bool { 18 pub const fn eof(&self) -> bool {
19 self.data.is_empty() 19 self.data.is_empty()
20 } 20 }
21 21
@@ -102,7 +102,7 @@ pub fn foreach_endpoint(data: &[u8], mut f: impl FnMut(EndpointInfo)) -> Result<
102 } 102 }
103 descriptor_type::ENDPOINT => { 103 descriptor_type::ENDPOINT => {
104 ep.ep_address = EndpointAddress::from(r.read_u8()?); 104 ep.ep_address = EndpointAddress::from(r.read_u8()?);
105 f(ep) 105 f(ep);
106 } 106 }
107 _ => {} 107 _ => {}
108 } 108 }
diff --git a/embassy-usb/src/lib.rs b/embassy-usb/src/lib.rs
index 2e859e3d8..88d88cad7 100644
--- a/embassy-usb/src/lib.rs
+++ b/embassy-usb/src/lib.rs
@@ -24,12 +24,12 @@ use embassy_futures::select::{select, Either};
24use heapless::Vec; 24use heapless::Vec;
25 25
26pub use crate::builder::{Builder, Config, FunctionBuilder, InterfaceAltBuilder, InterfaceBuilder}; 26pub use crate::builder::{Builder, Config, FunctionBuilder, InterfaceAltBuilder, InterfaceBuilder};
27use crate::config::*; 27use crate::config::{MAX_HANDLER_COUNT, MAX_INTERFACE_COUNT};
28use crate::control::*; 28use crate::control::{InResponse, OutResponse, Recipient, Request, RequestType};
29use crate::descriptor::*; 29use crate::descriptor::{descriptor_type, lang_id};
30use crate::descriptor_reader::foreach_endpoint; 30use crate::descriptor_reader::foreach_endpoint;
31use crate::driver::{Bus, ControlPipe, Direction, Driver, EndpointAddress, Event}; 31use crate::driver::{Bus, ControlPipe, Direction, Driver, EndpointAddress, Event};
32use crate::types::*; 32use crate::types::{InterfaceNumber, StringIndex};
33 33
34/// The global state of the USB device. 34/// The global state of the USB device.
35/// 35///
@@ -364,6 +364,8 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
364 } 364 }
365 365
366 async fn handle_control_in(&mut self, req: Request) { 366 async fn handle_control_in(&mut self, req: Request) {
367 const DEVICE_DESCRIPTOR_LEN: usize = 18;
368
367 let mut resp_length = req.length as usize; 369 let mut resp_length = req.length as usize;
368 let max_packet_size = self.control.max_packet_size(); 370 let max_packet_size = self.control.max_packet_size();
369 371
@@ -371,7 +373,6 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
371 // The host doesn't know our EP0 max packet size yet, and might assume 373 // The host doesn't know our EP0 max packet size yet, and might assume
372 // a full-length packet is a short packet, thinking we're done sending data. 374 // a full-length packet is a short packet, thinking we're done sending data.
373 // See https://github.com/hathach/tinyusb/issues/184 375 // See https://github.com/hathach/tinyusb/issues/184
374 const DEVICE_DESCRIPTOR_LEN: usize = 18;
375 if self.inner.address == 0 && max_packet_size < DEVICE_DESCRIPTOR_LEN && max_packet_size < resp_length { 376 if self.inner.address == 0 && max_packet_size < DEVICE_DESCRIPTOR_LEN && max_packet_size < resp_length {
376 trace!("received control req while not addressed: capping response to 1 packet."); 377 trace!("received control req while not addressed: capping response to 1 packet.");
377 resp_length = max_packet_size; 378 resp_length = max_packet_size;
@@ -432,7 +433,7 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
432 self.control.accept_set_address(self.inner.address).await; 433 self.control.accept_set_address(self.inner.address).await;
433 self.inner.set_address_pending = false; 434 self.inner.set_address_pending = false;
434 } else { 435 } else {
435 self.control.accept().await 436 self.control.accept().await;
436 } 437 }
437 } 438 }
438 OutResponse::Rejected => self.control.reject().await, 439 OutResponse::Rejected => self.control.reject().await,
@@ -545,9 +546,8 @@ impl<'d, D: Driver<'d>> Inner<'d, D> {
545 546
546 OutResponse::Accepted 547 OutResponse::Accepted
547 } 548 }
548 (Request::SET_CONFIGURATION, CONFIGURATION_NONE_U16) => match self.device_state { 549 (Request::SET_CONFIGURATION, CONFIGURATION_NONE_U16) => {
549 UsbDeviceState::Default => OutResponse::Accepted, 550 if self.device_state != UsbDeviceState::Default {
550 _ => {
551 debug!("SET_CONFIGURATION: unconfigured"); 551 debug!("SET_CONFIGURATION: unconfigured");
552 self.device_state = UsbDeviceState::Addressed; 552 self.device_state = UsbDeviceState::Addressed;
553 553
@@ -561,17 +561,15 @@ impl<'d, D: Driver<'d>> Inner<'d, D> {
561 for h in &mut self.handlers { 561 for h in &mut self.handlers {
562 h.configured(false); 562 h.configured(false);
563 } 563 }
564
565 OutResponse::Accepted
566 } 564 }
567 }, 565 OutResponse::Accepted
566 }
568 _ => OutResponse::Rejected, 567 _ => OutResponse::Rejected,
569 }, 568 },
570 (RequestType::Standard, Recipient::Interface) => { 569 (RequestType::Standard, Recipient::Interface) => {
571 let iface_num = InterfaceNumber::new(req.index as _); 570 let iface_num = InterfaceNumber::new(req.index as _);
572 let iface = match self.interfaces.get_mut(iface_num.0 as usize) { 571 let Some(iface) = self.interfaces.get_mut(iface_num.0 as usize) else {
573 Some(iface) => iface, 572 return OutResponse::Rejected;
574 None => return OutResponse::Rejected,
575 }; 573 };
576 574
577 match req.request { 575 match req.request {
@@ -647,9 +645,8 @@ impl<'d, D: Driver<'d>> Inner<'d, D> {
647 _ => InResponse::Rejected, 645 _ => InResponse::Rejected,
648 }, 646 },
649 (RequestType::Standard, Recipient::Interface) => { 647 (RequestType::Standard, Recipient::Interface) => {
650 let iface = match self.interfaces.get_mut(req.index as usize) { 648 let Some(iface) = self.interfaces.get_mut(req.index as usize) else {
651 Some(iface) => iface, 649 return InResponse::Rejected;
652 None => return InResponse::Rejected,
653 }; 650 };
654 651
655 match req.request { 652 match req.request {
@@ -753,16 +750,12 @@ impl<'d, D: Driver<'d>> Inner<'d, D> {
753 }; 750 };
754 751
755 if let Some(s) = s { 752 if let Some(s) = s {
756 if buf.len() < 2 { 753 assert!(buf.len() >= 2, "control buffer too small");
757 panic!("control buffer too small");
758 }
759 754
760 buf[1] = descriptor_type::STRING; 755 buf[1] = descriptor_type::STRING;
761 let mut pos = 2; 756 let mut pos = 2;
762 for c in s.encode_utf16() { 757 for c in s.encode_utf16() {
763 if pos + 2 >= buf.len() { 758 assert!(pos + 2 < buf.len(), "control buffer too small");
764 panic!("control buffer too small");
765 }
766 759
767 buf[pos..pos + 2].copy_from_slice(&c.to_le_bytes()); 760 buf[pos..pos + 2].copy_from_slice(&c.to_le_bytes());
768 pos += 2; 761 pos += 2;
diff --git a/embassy-usb/src/msos.rs b/embassy-usb/src/msos.rs
index 847338e5f..13d5d7c4b 100644
--- a/embassy-usb/src/msos.rs
+++ b/embassy-usb/src/msos.rs
@@ -6,7 +6,7 @@
6 6
7use core::mem::size_of; 7use core::mem::size_of;
8 8
9use super::{capability_type, BosWriter}; 9use crate::descriptor::{capability_type, BosWriter};
10use crate::types::InterfaceNumber; 10use crate::types::InterfaceNumber;
11 11
12/// A serialized Microsoft OS 2.0 Descriptor set. 12/// A serialized Microsoft OS 2.0 Descriptor set.
diff --git a/embassy-usb/src/types.rs b/embassy-usb/src/types.rs
index c7a47f7e4..cb9fe2576 100644
--- a/embassy-usb/src/types.rs
+++ b/embassy-usb/src/types.rs
@@ -7,7 +7,7 @@
7pub struct InterfaceNumber(pub u8); 7pub struct InterfaceNumber(pub u8);
8 8
9impl InterfaceNumber { 9impl InterfaceNumber {
10 pub(crate) fn new(index: u8) -> InterfaceNumber { 10 pub(crate) const fn new(index: u8) -> InterfaceNumber {
11 InterfaceNumber(index) 11 InterfaceNumber(index)
12 } 12 }
13} 13}
@@ -25,7 +25,7 @@ impl From<InterfaceNumber> for u8 {
25pub struct StringIndex(pub u8); 25pub struct StringIndex(pub u8);
26 26
27impl StringIndex { 27impl StringIndex {
28 pub(crate) fn new(index: u8) -> StringIndex { 28 pub(crate) const fn new(index: u8) -> StringIndex {
29 StringIndex(index) 29 StringIndex(index)
30 } 30 }
31} 31}