Skip to content

Commit

Permalink
usb: remove device_descriptor buffer, store bytes in UsbDevice.inner …
Browse files Browse the repository at this point in the history
…instead
  • Loading branch information
adamgreig committed Mar 23, 2024
1 parent c2aa950 commit 7cf6490
Show file tree
Hide file tree
Showing 40 changed files with 33 additions and 113 deletions.
3 changes: 0 additions & 3 deletions embassy-usb-logger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type CS = embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
/// The logger state containing buffers that must live as long as the USB peripheral.
pub struct LoggerState<'d> {
state: State<'d>,
device_descriptor: [u8; 32],
config_descriptor: [u8; 128],
bos_descriptor: [u8; 16],
msos_descriptor: [u8; 256],
Expand All @@ -28,7 +27,6 @@ impl<'d> LoggerState<'d> {
pub fn new() -> Self {
Self {
state: State::new(),
device_descriptor: [0; 32],
config_descriptor: [0; 128],
bos_descriptor: [0; 16],
msos_descriptor: [0; 256],
Expand Down Expand Up @@ -74,7 +72,6 @@ impl<const N: usize> UsbLogger<N> {
let mut builder = Builder::new(
driver,
config,
&mut state.device_descriptor,
&mut state.config_descriptor,
&mut state.bos_descriptor,
&mut state.msos_descriptor,
Expand Down
7 changes: 0 additions & 7 deletions embassy-usb/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ pub struct Builder<'d, D: Driver<'d>> {
driver: D,
next_string_index: u8,

device_descriptor: DescriptorWriter<'d>,
config_descriptor: DescriptorWriter<'d>,
bos_descriptor: BosWriter<'d>,

Expand All @@ -144,7 +143,6 @@ impl<'d, D: Driver<'d>> Builder<'d, D> {
pub fn new(
driver: D,
config: Config<'d>,
device_descriptor_buf: &'d mut [u8],
config_descriptor_buf: &'d mut [u8],
bos_descriptor_buf: &'d mut [u8],
msos_descriptor_buf: &'d mut [u8],
Expand All @@ -167,11 +165,9 @@ impl<'d, D: Driver<'d>> Builder<'d, D> {
_ => panic!("invalid max_packet_size_0, the allowed values are 8, 16, 32 or 64"),
}

let mut device_descriptor = DescriptorWriter::new(device_descriptor_buf);
let mut config_descriptor = DescriptorWriter::new(config_descriptor_buf);
let mut bos_descriptor = BosWriter::new(DescriptorWriter::new(bos_descriptor_buf));

device_descriptor.device(&config);
config_descriptor.configuration(&config);
bos_descriptor.bos();

Expand All @@ -183,7 +179,6 @@ impl<'d, D: Driver<'d>> Builder<'d, D> {
control_buf,
next_string_index: STRING_INDEX_CUSTOM_START,

device_descriptor,
config_descriptor,
bos_descriptor,

Expand All @@ -199,7 +194,6 @@ impl<'d, D: Driver<'d>> Builder<'d, D> {
self.bos_descriptor.end_bos();

// Log the number of allocator bytes actually used in descriptor buffers
info!("USB: device_descriptor used: {}", self.device_descriptor.position());
info!("USB: config_descriptor used: {}", self.config_descriptor.position());
info!("USB: bos_descriptor used: {}", self.bos_descriptor.writer.position());
info!("USB: msos_descriptor used: {}", msos_descriptor.len());
Expand All @@ -209,7 +203,6 @@ impl<'d, D: Driver<'d>> Builder<'d, D> {
self.driver,
self.config,
self.handlers,
self.device_descriptor.into_buf(),
self.config_descriptor.into_buf(),
self.bos_descriptor.writer.into_buf(),
msos_descriptor,
Expand Down
51 changes: 27 additions & 24 deletions embassy-usb/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,30 +82,6 @@ impl<'a> DescriptorWriter<'a> {
self.position = start + length;
}

pub(crate) fn device(&mut self, config: &Config) {
self.write(
descriptor_type::DEVICE,
&[
0x10,
0x02, // bcdUSB 2.1
config.device_class, // bDeviceClass
config.device_sub_class, // bDeviceSubClass
config.device_protocol, // bDeviceProtocol
config.max_packet_size_0, // bMaxPacketSize0
config.vendor_id as u8,
(config.vendor_id >> 8) as u8, // idVendor
config.product_id as u8,
(config.product_id >> 8) as u8, // idProduct
config.device_release as u8,
(config.device_release >> 8) as u8, // bcdDevice
config.manufacturer.map_or(0, |_| 1), // iManufacturer
config.product.map_or(0, |_| 2), // iProduct
config.serial_number.map_or(0, |_| 3), // iSerialNumber
1, // bNumConfigurations
],
);
}

pub(crate) fn configuration(&mut self, config: &Config) {
self.num_interfaces_mark = Some(self.position + 4);

Expand Down Expand Up @@ -269,6 +245,33 @@ impl<'a> DescriptorWriter<'a> {
}
}

/// Create a new Device Descriptor array.
///
/// All device descriptors are always 18 bytes, so there's no need for
/// a variable-length buffer or DescriptorWriter.
pub(crate) fn device_descriptor(config: &Config) -> [u8; 18] {
[
18, // bLength
0x01, // bDescriptorType
0x10,
0x02, // bcdUSB 2.1
config.device_class, // bDeviceClass
config.device_sub_class, // bDeviceSubClass
config.device_protocol, // bDeviceProtocol
config.max_packet_size_0, // bMaxPacketSize0
config.vendor_id as u8,
(config.vendor_id >> 8) as u8, // idVendor
config.product_id as u8,
(config.product_id >> 8) as u8, // idProduct
config.device_release as u8,
(config.device_release >> 8) as u8, // bcdDevice
config.manufacturer.map_or(0, |_| 1), // iManufacturer
config.product.map_or(0, |_| 2), // iProduct
config.serial_number.map_or(0, |_| 3), // iSerialNumber
1, // bNumConfigurations
]
}

/// A writer for Binary Object Store descriptor.
pub struct BosWriter<'a> {
pub(crate) writer: DescriptorWriter<'a>,
Expand Down
9 changes: 3 additions & 6 deletions embassy-usb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,6 @@ struct Interface {
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct UsbBufferReport {
/// Number of device descriptor bytes used
pub device_descriptor_used: usize,
/// Number of config descriptor bytes used
pub config_descriptor_used: usize,
/// Number of bos descriptor bytes used
Expand All @@ -191,7 +189,7 @@ struct Inner<'d, D: Driver<'d>> {
bus: D::Bus,

config: Config<'d>,
device_descriptor: &'d [u8],
device_descriptor: [u8; 18],
config_descriptor: &'d [u8],
bos_descriptor: &'d [u8],
msos_descriptor: crate::msos::MsOsDescriptorSet<'d>,
Expand All @@ -217,7 +215,6 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
driver: D,
config: Config<'d>,
handlers: Vec<&'d mut dyn Handler, MAX_HANDLER_COUNT>,
device_descriptor: &'d [u8],
config_descriptor: &'d [u8],
bos_descriptor: &'d [u8],
msos_descriptor: crate::msos::MsOsDescriptorSet<'d>,
Expand All @@ -227,6 +224,7 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
// Start the USB bus.
// This prevent further allocation by consuming the driver.
let (bus, control) = driver.start(config.max_packet_size_0 as u16);
let device_descriptor = descriptor::device_descriptor(&config);

Self {
control_buf,
Expand Down Expand Up @@ -256,7 +254,6 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
/// Useful for tuning buffer sizes for actual usage
pub fn buffer_usage(&self) -> UsbBufferReport {
UsbBufferReport {
device_descriptor_used: self.inner.device_descriptor.len(),
config_descriptor_used: self.inner.config_descriptor.len(),
bos_descriptor_used: self.inner.bos_descriptor.len(),
msos_descriptor_used: self.inner.msos_descriptor.len(),
Expand Down Expand Up @@ -720,7 +717,7 @@ impl<'d, D: Driver<'d>> Inner<'d, D> {

match dtype {
descriptor_type::BOS => InResponse::Accepted(self.bos_descriptor),
descriptor_type::DEVICE => InResponse::Accepted(self.device_descriptor),
descriptor_type::DEVICE => InResponse::Accepted(&self.device_descriptor),
descriptor_type::CONFIGURATION => InResponse::Accepted(self.config_descriptor),
descriptor_type::STRING => {
if index == 0 {
Expand Down
6 changes: 3 additions & 3 deletions embassy-usb/src/msos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ use sealed::*;
unsafe fn transmute_write_to<T: Sized>(t: &T, buf: &mut [u8]) {
let bytes = core::slice::from_raw_parts((t as *const T) as *const u8, size_of::<T>());
assert!(buf.len() >= bytes.len(), "MS OS descriptor buffer full");
(&mut buf[..bytes.len()]).copy_from_slice(bytes);
buf[..bytes.len()].copy_from_slice(bytes);
}

/// Table 9. Microsoft OS 2.0 descriptor wDescriptorType values.
Expand Down Expand Up @@ -444,9 +444,9 @@ impl CompatibleIdFeatureDescriptor {
pub fn new(compatible_id: &str, sub_compatible_id: &str) -> Self {
assert!(compatible_id.len() <= 8 && sub_compatible_id.len() <= 8);
let mut cid = [0u8; 8];
(&mut cid[..compatible_id.len()]).copy_from_slice(compatible_id.as_bytes());
cid[..compatible_id.len()].copy_from_slice(compatible_id.as_bytes());
let mut scid = [0u8; 8];
(&mut scid[..sub_compatible_id.len()]).copy_from_slice(sub_compatible_id.as_bytes());
scid[..sub_compatible_id.len()].copy_from_slice(sub_compatible_id.as_bytes());
Self::new_raw(cid, scid)
}

Expand Down
2 changes: 0 additions & 2 deletions examples/boot/application/stm32wb-dfu/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ async fn main(_spawner: Spawner) {
config.product = Some("USB-DFU Runtime example");
config.serial_number = Some("1235678");

let mut device_descriptor = [0; 256];
let mut config_descriptor = [0; 256];
let mut bos_descriptor = [0; 256];
let mut control_buf = [0; 64];
let mut state = Control::new(firmware_state, DfuAttributes::CAN_DOWNLOAD);
let mut builder = Builder::new(
driver,
config,
&mut device_descriptor,
&mut config_descriptor,
&mut bos_descriptor,
&mut [],
Expand Down
2 changes: 0 additions & 2 deletions examples/boot/bootloader/stm32wb-dfu/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,13 @@ fn main() -> ! {
let mut buffer = AlignedBuffer([0; WRITE_SIZE]);
let updater = BlockingFirmwareUpdater::new(fw_config, &mut buffer.0[..]);

let mut device_descriptor = [0; 256];
let mut config_descriptor = [0; 256];
let mut bos_descriptor = [0; 256];
let mut control_buf = [0; 4096];
let mut state = Control::new(updater, DfuAttributes::CAN_DOWNLOAD);
let mut builder = Builder::new(
driver,
config,
&mut device_descriptor,
&mut config_descriptor,
&mut bos_descriptor,
&mut [],
Expand Down
2 changes: 0 additions & 2 deletions examples/nrf52840/src/bin/usb_ethernet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,13 @@ async fn main(spawner: Spawner) {
config.device_protocol = 0x01;

// Create embassy-usb DeviceBuilder using the driver and config.
static DEVICE_DESC: StaticCell<[u8; 256]> = StaticCell::new();
static CONFIG_DESC: StaticCell<[u8; 256]> = StaticCell::new();
static BOS_DESC: StaticCell<[u8; 256]> = StaticCell::new();
static MSOS_DESC: StaticCell<[u8; 128]> = StaticCell::new();
static CONTROL_BUF: StaticCell<[u8; 128]> = StaticCell::new();
let mut builder = Builder::new(
driver,
config,
&mut DEVICE_DESC.init([0; 256])[..],
&mut CONFIG_DESC.init([0; 256])[..],
&mut BOS_DESC.init([0; 256])[..],
&mut MSOS_DESC.init([0; 128])[..],
Expand Down
2 changes: 0 additions & 2 deletions examples/nrf52840/src/bin/usb_hid_keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ async fn main(_spawner: Spawner) {

// Create embassy-usb DeviceBuilder using the driver and config.
// It needs some buffers for building the descriptors.
let mut device_descriptor = [0; 256];
let mut config_descriptor = [0; 256];
let mut bos_descriptor = [0; 256];
let mut msos_descriptor = [0; 256];
Expand All @@ -63,7 +62,6 @@ async fn main(_spawner: Spawner) {
let mut builder = Builder::new(
driver,
config,
&mut device_descriptor,
&mut config_descriptor,
&mut bos_descriptor,
&mut msos_descriptor,
Expand Down
2 changes: 0 additions & 2 deletions examples/nrf52840/src/bin/usb_hid_mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ async fn main(_spawner: Spawner) {

// Create embassy-usb DeviceBuilder using the driver and config.
// It needs some buffers for building the descriptors.
let mut device_descriptor = [0; 256];
let mut config_descriptor = [0; 256];
let mut bos_descriptor = [0; 256];
let mut msos_descriptor = [0; 256];
Expand All @@ -55,7 +54,6 @@ async fn main(_spawner: Spawner) {
let mut builder = Builder::new(
driver,
config,
&mut device_descriptor,
&mut config_descriptor,
&mut bos_descriptor,
&mut msos_descriptor,
Expand Down
2 changes: 0 additions & 2 deletions examples/nrf52840/src/bin/usb_serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ async fn main(_spawner: Spawner) {

// Create embassy-usb DeviceBuilder using the driver and config.
// It needs some buffers for building the descriptors.
let mut device_descriptor = [0; 256];
let mut config_descriptor = [0; 256];
let mut bos_descriptor = [0; 256];
let mut msos_descriptor = [0; 256];
Expand All @@ -59,7 +58,6 @@ async fn main(_spawner: Spawner) {
let mut builder = Builder::new(
driver,
config,
&mut device_descriptor,
&mut config_descriptor,
&mut bos_descriptor,
&mut msos_descriptor,
Expand Down
2 changes: 0 additions & 2 deletions examples/nrf52840/src/bin/usb_serial_multitask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,13 @@ async fn main(spawner: Spawner) {
let state = STATE.init(State::new());

// Create embassy-usb DeviceBuilder using the driver and config.
static DEVICE_DESC: StaticCell<[u8; 256]> = StaticCell::new();
static CONFIG_DESC: StaticCell<[u8; 256]> = StaticCell::new();
static BOS_DESC: StaticCell<[u8; 256]> = StaticCell::new();
static MSOS_DESC: StaticCell<[u8; 128]> = StaticCell::new();
static CONTROL_BUF: StaticCell<[u8; 128]> = StaticCell::new();
let mut builder = Builder::new(
driver,
config,
&mut DEVICE_DESC.init([0; 256])[..],
&mut CONFIG_DESC.init([0; 256])[..],
&mut BOS_DESC.init([0; 256])[..],
&mut MSOS_DESC.init([0; 128])[..],
Expand Down
2 changes: 0 additions & 2 deletions examples/nrf52840/src/bin/usb_serial_winusb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ async fn main(_spawner: Spawner) {

// Create embassy-usb DeviceBuilder using the driver and config.
// It needs some buffers for building the descriptors.
let mut device_descriptor = [0; 256];
let mut config_descriptor = [0; 256];
let mut bos_descriptor = [0; 256];
let mut msos_descriptor = [0; 256];
Expand All @@ -64,7 +63,6 @@ async fn main(_spawner: Spawner) {
let mut builder = Builder::new(
driver,
config,
&mut device_descriptor,
&mut config_descriptor,
&mut bos_descriptor,
&mut msos_descriptor,
Expand Down
2 changes: 0 additions & 2 deletions examples/rp/src/bin/pio_uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ async fn main(_spawner: Spawner) {

// Create embassy-usb DeviceBuilder using the driver and config.
// It needs some buffers for building the descriptors.
let mut device_descriptor = [0; 256];
let mut config_descriptor = [0; 256];
let mut bos_descriptor = [0; 256];
let mut control_buf = [0; 64];
Expand All @@ -70,7 +69,6 @@ async fn main(_spawner: Spawner) {
let mut builder = Builder::new(
driver,
config,
&mut device_descriptor,
&mut config_descriptor,
&mut bos_descriptor,
&mut [], // no msos descriptors
Expand Down
2 changes: 0 additions & 2 deletions examples/rp/src/bin/usb_ethernet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,12 @@ async fn main(spawner: Spawner) {
config.device_protocol = 0x01;

// Create embassy-usb DeviceBuilder using the driver and config.
static DEVICE_DESC: StaticCell<[u8; 256]> = StaticCell::new();
static CONFIG_DESC: StaticCell<[u8; 256]> = StaticCell::new();
static BOS_DESC: StaticCell<[u8; 256]> = StaticCell::new();
static CONTROL_BUF: StaticCell<[u8; 128]> = StaticCell::new();
let mut builder = Builder::new(
driver,
config,
&mut DEVICE_DESC.init([0; 256])[..],
&mut CONFIG_DESC.init([0; 256])[..],
&mut BOS_DESC.init([0; 256])[..],
&mut [], // no msos descriptors
Expand Down
2 changes: 0 additions & 2 deletions examples/rp/src/bin/usb_hid_keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ async fn main(_spawner: Spawner) {

// Create embassy-usb DeviceBuilder using the driver and config.
// It needs some buffers for building the descriptors.
let mut device_descriptor = [0; 256];
let mut config_descriptor = [0; 256];
let mut bos_descriptor = [0; 256];
// You can also add a Microsoft OS descriptor.
Expand All @@ -50,7 +49,6 @@ async fn main(_spawner: Spawner) {
let mut builder = Builder::new(
driver,
config,
&mut device_descriptor,
&mut config_descriptor,
&mut bos_descriptor,
&mut msos_descriptor,
Expand Down
Loading

0 comments on commit 7cf6490

Please sign in to comment.