aboutsummaryrefslogtreecommitdiff
path: root/embassy-usb/src
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-usb/src')
-rw-r--r--embassy-usb/src/builder.rs7
-rw-r--r--embassy-usb/src/descriptor.rs51
-rw-r--r--embassy-usb/src/lib.rs9
-rw-r--r--embassy-usb/src/msos.rs6
4 files changed, 33 insertions, 40 deletions
diff --git a/embassy-usb/src/builder.rs b/embassy-usb/src/builder.rs
index c4705d041..c06107396 100644
--- a/embassy-usb/src/builder.rs
+++ b/embassy-usb/src/builder.rs
@@ -128,7 +128,6 @@ pub struct Builder<'d, D: Driver<'d>> {
128 driver: D, 128 driver: D,
129 next_string_index: u8, 129 next_string_index: u8,
130 130
131 device_descriptor: DescriptorWriter<'d>,
132 config_descriptor: DescriptorWriter<'d>, 131 config_descriptor: DescriptorWriter<'d>,
133 bos_descriptor: BosWriter<'d>, 132 bos_descriptor: BosWriter<'d>,
134 133
@@ -144,7 +143,6 @@ impl<'d, D: Driver<'d>> Builder<'d, D> {
144 pub fn new( 143 pub fn new(
145 driver: D, 144 driver: D,
146 config: Config<'d>, 145 config: Config<'d>,
147 device_descriptor_buf: &'d mut [u8],
148 config_descriptor_buf: &'d mut [u8], 146 config_descriptor_buf: &'d mut [u8],
149 bos_descriptor_buf: &'d mut [u8], 147 bos_descriptor_buf: &'d mut [u8],
150 msos_descriptor_buf: &'d mut [u8], 148 msos_descriptor_buf: &'d mut [u8],
@@ -167,11 +165,9 @@ impl<'d, D: Driver<'d>> Builder<'d, D> {
167 _ => panic!("invalid max_packet_size_0, the allowed values are 8, 16, 32 or 64"), 165 _ => panic!("invalid max_packet_size_0, the allowed values are 8, 16, 32 or 64"),
168 } 166 }
169 167
170 let mut device_descriptor = DescriptorWriter::new(device_descriptor_buf);
171 let mut config_descriptor = DescriptorWriter::new(config_descriptor_buf); 168 let mut config_descriptor = DescriptorWriter::new(config_descriptor_buf);
172 let mut bos_descriptor = BosWriter::new(DescriptorWriter::new(bos_descriptor_buf)); 169 let mut bos_descriptor = BosWriter::new(DescriptorWriter::new(bos_descriptor_buf));
173 170
174 device_descriptor.device(&config);
175 config_descriptor.configuration(&config); 171 config_descriptor.configuration(&config);
176 bos_descriptor.bos(); 172 bos_descriptor.bos();
177 173
@@ -183,7 +179,6 @@ impl<'d, D: Driver<'d>> Builder<'d, D> {
183 control_buf, 179 control_buf,
184 next_string_index: STRING_INDEX_CUSTOM_START, 180 next_string_index: STRING_INDEX_CUSTOM_START,
185 181
186 device_descriptor,
187 config_descriptor, 182 config_descriptor,
188 bos_descriptor, 183 bos_descriptor,
189 184
@@ -199,7 +194,6 @@ impl<'d, D: Driver<'d>> Builder<'d, D> {
199 self.bos_descriptor.end_bos(); 194 self.bos_descriptor.end_bos();
200 195
201 // Log the number of allocator bytes actually used in descriptor buffers 196 // Log the number of allocator bytes actually used in descriptor buffers
202 info!("USB: device_descriptor used: {}", self.device_descriptor.position());
203 info!("USB: config_descriptor used: {}", self.config_descriptor.position()); 197 info!("USB: config_descriptor used: {}", self.config_descriptor.position());
204 info!("USB: bos_descriptor used: {}", self.bos_descriptor.writer.position()); 198 info!("USB: bos_descriptor used: {}", self.bos_descriptor.writer.position());
205 info!("USB: msos_descriptor used: {}", msos_descriptor.len()); 199 info!("USB: msos_descriptor used: {}", msos_descriptor.len());
@@ -209,7 +203,6 @@ impl<'d, D: Driver<'d>> Builder<'d, D> {
209 self.driver, 203 self.driver,
210 self.config, 204 self.config,
211 self.handlers, 205 self.handlers,
212 self.device_descriptor.into_buf(),
213 self.config_descriptor.into_buf(), 206 self.config_descriptor.into_buf(),
214 self.bos_descriptor.writer.into_buf(), 207 self.bos_descriptor.writer.into_buf(),
215 msos_descriptor, 208 msos_descriptor,
diff --git a/embassy-usb/src/descriptor.rs b/embassy-usb/src/descriptor.rs
index fa83ef583..eb3d1f53a 100644
--- a/embassy-usb/src/descriptor.rs
+++ b/embassy-usb/src/descriptor.rs
@@ -82,30 +82,6 @@ impl<'a> DescriptorWriter<'a> {
82 self.position = start + length; 82 self.position = start + length;
83 } 83 }
84 84
85 pub(crate) fn device(&mut self, config: &Config) {
86 self.write(
87 descriptor_type::DEVICE,
88 &[
89 0x10,
90 0x02, // bcdUSB 2.1
91 config.device_class, // bDeviceClass
92 config.device_sub_class, // bDeviceSubClass
93 config.device_protocol, // bDeviceProtocol
94 config.max_packet_size_0, // bMaxPacketSize0
95 config.vendor_id as u8,
96 (config.vendor_id >> 8) as u8, // idVendor
97 config.product_id as u8,
98 (config.product_id >> 8) as u8, // idProduct
99 config.device_release as u8,
100 (config.device_release >> 8) as u8, // bcdDevice
101 config.manufacturer.map_or(0, |_| 1), // iManufacturer
102 config.product.map_or(0, |_| 2), // iProduct
103 config.serial_number.map_or(0, |_| 3), // iSerialNumber
104 1, // bNumConfigurations
105 ],
106 );
107 }
108
109 pub(crate) fn configuration(&mut self, config: &Config) { 85 pub(crate) fn configuration(&mut self, config: &Config) {
110 self.num_interfaces_mark = Some(self.position + 4); 86 self.num_interfaces_mark = Some(self.position + 4);
111 87
@@ -269,6 +245,33 @@ impl<'a> DescriptorWriter<'a> {
269 } 245 }
270} 246}
271 247
248/// Create a new Device Descriptor array.
249///
250/// All device descriptors are always 18 bytes, so there's no need for
251/// a variable-length buffer or DescriptorWriter.
252pub(crate) fn device_descriptor(config: &Config) -> [u8; 18] {
253 [
254 18, // bLength
255 0x01, // bDescriptorType
256 0x10,
257 0x02, // bcdUSB 2.1
258 config.device_class, // bDeviceClass
259 config.device_sub_class, // bDeviceSubClass
260 config.device_protocol, // bDeviceProtocol
261 config.max_packet_size_0, // bMaxPacketSize0
262 config.vendor_id as u8,
263 (config.vendor_id >> 8) as u8, // idVendor
264 config.product_id as u8,
265 (config.product_id >> 8) as u8, // idProduct
266 config.device_release as u8,
267 (config.device_release >> 8) as u8, // bcdDevice
268 config.manufacturer.map_or(0, |_| 1), // iManufacturer
269 config.product.map_or(0, |_| 2), // iProduct
270 config.serial_number.map_or(0, |_| 3), // iSerialNumber
271 1, // bNumConfigurations
272 ]
273}
274
272/// A writer for Binary Object Store descriptor. 275/// A writer for Binary Object Store descriptor.
273pub struct BosWriter<'a> { 276pub struct BosWriter<'a> {
274 pub(crate) writer: DescriptorWriter<'a>, 277 pub(crate) writer: DescriptorWriter<'a>,
diff --git a/embassy-usb/src/lib.rs b/embassy-usb/src/lib.rs
index 241e33a78..d58950838 100644
--- a/embassy-usb/src/lib.rs
+++ b/embassy-usb/src/lib.rs
@@ -168,8 +168,6 @@ struct Interface {
168#[derive(PartialEq, Eq, Copy, Clone, Debug)] 168#[derive(PartialEq, Eq, Copy, Clone, Debug)]
169#[cfg_attr(feature = "defmt", derive(defmt::Format))] 169#[cfg_attr(feature = "defmt", derive(defmt::Format))]
170pub struct UsbBufferReport { 170pub struct UsbBufferReport {
171 /// Number of device descriptor bytes used
172 pub device_descriptor_used: usize,
173 /// Number of config descriptor bytes used 171 /// Number of config descriptor bytes used
174 pub config_descriptor_used: usize, 172 pub config_descriptor_used: usize,
175 /// Number of bos descriptor bytes used 173 /// Number of bos descriptor bytes used
@@ -191,7 +189,7 @@ struct Inner<'d, D: Driver<'d>> {
191 bus: D::Bus, 189 bus: D::Bus,
192 190
193 config: Config<'d>, 191 config: Config<'d>,
194 device_descriptor: &'d [u8], 192 device_descriptor: [u8; 18],
195 config_descriptor: &'d [u8], 193 config_descriptor: &'d [u8],
196 bos_descriptor: &'d [u8], 194 bos_descriptor: &'d [u8],
197 msos_descriptor: crate::msos::MsOsDescriptorSet<'d>, 195 msos_descriptor: crate::msos::MsOsDescriptorSet<'d>,
@@ -217,7 +215,6 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
217 driver: D, 215 driver: D,
218 config: Config<'d>, 216 config: Config<'d>,
219 handlers: Vec<&'d mut dyn Handler, MAX_HANDLER_COUNT>, 217 handlers: Vec<&'d mut dyn Handler, MAX_HANDLER_COUNT>,
220 device_descriptor: &'d [u8],
221 config_descriptor: &'d [u8], 218 config_descriptor: &'d [u8],
222 bos_descriptor: &'d [u8], 219 bos_descriptor: &'d [u8],
223 msos_descriptor: crate::msos::MsOsDescriptorSet<'d>, 220 msos_descriptor: crate::msos::MsOsDescriptorSet<'d>,
@@ -227,6 +224,7 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
227 // Start the USB bus. 224 // Start the USB bus.
228 // This prevent further allocation by consuming the driver. 225 // This prevent further allocation by consuming the driver.
229 let (bus, control) = driver.start(config.max_packet_size_0 as u16); 226 let (bus, control) = driver.start(config.max_packet_size_0 as u16);
227 let device_descriptor = descriptor::device_descriptor(&config);
230 228
231 Self { 229 Self {
232 control_buf, 230 control_buf,
@@ -256,7 +254,6 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
256 /// Useful for tuning buffer sizes for actual usage 254 /// Useful for tuning buffer sizes for actual usage
257 pub fn buffer_usage(&self) -> UsbBufferReport { 255 pub fn buffer_usage(&self) -> UsbBufferReport {
258 UsbBufferReport { 256 UsbBufferReport {
259 device_descriptor_used: self.inner.device_descriptor.len(),
260 config_descriptor_used: self.inner.config_descriptor.len(), 257 config_descriptor_used: self.inner.config_descriptor.len(),
261 bos_descriptor_used: self.inner.bos_descriptor.len(), 258 bos_descriptor_used: self.inner.bos_descriptor.len(),
262 msos_descriptor_used: self.inner.msos_descriptor.len(), 259 msos_descriptor_used: self.inner.msos_descriptor.len(),
@@ -720,7 +717,7 @@ impl<'d, D: Driver<'d>> Inner<'d, D> {
720 717
721 match dtype { 718 match dtype {
722 descriptor_type::BOS => InResponse::Accepted(self.bos_descriptor), 719 descriptor_type::BOS => InResponse::Accepted(self.bos_descriptor),
723 descriptor_type::DEVICE => InResponse::Accepted(self.device_descriptor), 720 descriptor_type::DEVICE => InResponse::Accepted(&self.device_descriptor),
724 descriptor_type::CONFIGURATION => InResponse::Accepted(self.config_descriptor), 721 descriptor_type::CONFIGURATION => InResponse::Accepted(self.config_descriptor),
725 descriptor_type::STRING => { 722 descriptor_type::STRING => {
726 if index == 0 { 723 if index == 0 {
diff --git a/embassy-usb/src/msos.rs b/embassy-usb/src/msos.rs
index 3858c0f51..a285a3ccd 100644
--- a/embassy-usb/src/msos.rs
+++ b/embassy-usb/src/msos.rs
@@ -255,7 +255,7 @@ use sealed::*;
255unsafe fn transmute_write_to<T: Sized>(t: &T, buf: &mut [u8]) { 255unsafe fn transmute_write_to<T: Sized>(t: &T, buf: &mut [u8]) {
256 let bytes = core::slice::from_raw_parts((t as *const T) as *const u8, size_of::<T>()); 256 let bytes = core::slice::from_raw_parts((t as *const T) as *const u8, size_of::<T>());
257 assert!(buf.len() >= bytes.len(), "MS OS descriptor buffer full"); 257 assert!(buf.len() >= bytes.len(), "MS OS descriptor buffer full");
258 (&mut buf[..bytes.len()]).copy_from_slice(bytes); 258 buf[..bytes.len()].copy_from_slice(bytes);
259} 259}
260 260
261/// Table 9. Microsoft OS 2.0 descriptor wDescriptorType values. 261/// Table 9. Microsoft OS 2.0 descriptor wDescriptorType values.
@@ -444,9 +444,9 @@ impl CompatibleIdFeatureDescriptor {
444 pub fn new(compatible_id: &str, sub_compatible_id: &str) -> Self { 444 pub fn new(compatible_id: &str, sub_compatible_id: &str) -> Self {
445 assert!(compatible_id.len() <= 8 && sub_compatible_id.len() <= 8); 445 assert!(compatible_id.len() <= 8 && sub_compatible_id.len() <= 8);
446 let mut cid = [0u8; 8]; 446 let mut cid = [0u8; 8];
447 (&mut cid[..compatible_id.len()]).copy_from_slice(compatible_id.as_bytes()); 447 cid[..compatible_id.len()].copy_from_slice(compatible_id.as_bytes());
448 let mut scid = [0u8; 8]; 448 let mut scid = [0u8; 8];
449 (&mut scid[..sub_compatible_id.len()]).copy_from_slice(sub_compatible_id.as_bytes()); 449 scid[..sub_compatible_id.len()].copy_from_slice(sub_compatible_id.as_bytes());
450 Self::new_raw(cid, scid) 450 Self::new_raw(cid, scid)
451 } 451 }
452 452