Skip to content

Commit db5fbe1

Browse files
authored
Merge pull request #116 from ithinuel/selectable-usb-version
Add the ability to select USB revision
2 parents 46a354c + 6deff28 commit db5fbe1

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

src/descriptor.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ impl DescriptorWriter<'_> {
110110
self.write(
111111
descriptor_type::DEVICE,
112112
&[
113-
0x10,
114-
0x02, // bcdUSB 2.1
115-
config.device_class, // bDeviceClass
116-
config.device_sub_class, // bDeviceSubClass
117-
config.device_protocol, // bDeviceProtocol
118-
config.max_packet_size_0, // bMaxPacketSize0
113+
(config.usb_rev as u16) as u8,
114+
(config.usb_rev as u16 >> 8) as u8, // bcdUSB
115+
config.device_class, // bDeviceClass
116+
config.device_sub_class, // bDeviceSubClass
117+
config.device_protocol, // bDeviceProtocol
118+
config.max_packet_size_0, // bMaxPacketSize0
119119
config.vendor_id as u8,
120120
(config.vendor_id >> 8) as u8, // idVendor
121121
config.product_id as u8,

src/device.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ pub enum UsbDeviceState {
3030
// Maximum number of endpoints in one direction. Specified by the USB specification.
3131
const MAX_ENDPOINTS: usize = 16;
3232

33+
/// Usb spec revision.
34+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
35+
#[repr(u16)]
36+
pub enum UsbRev {
37+
/// USB 2.0 compliance
38+
Usb200 = 0x200,
39+
/// USB 2.1 compliance.
40+
///
41+
/// Typically adds support for BOS requests.
42+
Usb210 = 0x210,
43+
}
44+
3345
/// A USB device consisting of one or more device classes.
3446
pub struct UsbDevice<'a, B: UsbBus> {
3547
bus: &'a B,
@@ -49,6 +61,7 @@ pub(crate) struct Config<'a> {
4961
pub max_packet_size_0: u8,
5062
pub vendor_id: u16,
5163
pub product_id: u16,
64+
pub usb_rev: UsbRev,
5265
pub device_release: u16,
5366
pub manufacturer: Option<&'a str>,
5467
pub product: Option<&'a str>,
@@ -469,7 +482,7 @@ impl<B: UsbBus> UsbDevice<'_, B> {
469482
}
470483

471484
match dtype {
472-
descriptor_type::BOS => accept_writer(xfer, |w| {
485+
descriptor_type::BOS if config.usb_rev > UsbRev::Usb200 => accept_writer(xfer, |w| {
473486
let mut bw = BosWriter::new(w);
474487
bw.bos()?;
475488

src/device_builder.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::bus::{UsbBus, UsbBusAllocator};
2-
use crate::device::{Config, UsbDevice};
2+
use crate::device::{Config, UsbDevice, UsbRev};
33

44
/// A USB vendor ID and product ID pair.
55
pub struct UsbVidPid(pub u16, pub u16);
@@ -34,6 +34,7 @@ impl<'a, B: UsbBus> UsbDeviceBuilder<'a, B> {
3434
max_packet_size_0: 8,
3535
vendor_id: vid_pid.0,
3636
product_id: vid_pid.1,
37+
usb_rev: UsbRev::Usb210,
3738
device_release: 0x0010,
3839
manufacturer: None,
3940
product: None,
@@ -87,6 +88,11 @@ impl<'a, B: UsbBus> UsbDeviceBuilder<'a, B> {
8788
///
8889
/// Default: `false`
8990
supports_remote_wakeup: bool,
91+
92+
/// Sets which Usb 2 revision to comply to.
93+
///
94+
/// Default: `UsbRev::Usb210`
95+
usb_rev: UsbRev,
9096
}
9197

9298
/// Configures the device as a composite device with interface association descriptors.

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ pub mod class_prelude {
198198
}
199199

200200
fn _ensure_sync() {
201-
use crate::bus::{PollResult, UsbBus, UsbBusAllocator};
201+
use crate::bus::PollResult;
202202
use crate::class_prelude::*;
203203

204204
struct DummyBus<'a> {

0 commit comments

Comments
 (0)