From 9912b35c5d7ad0253a60219c1555084eab3844d7 Mon Sep 17 00:00:00 2001 From: Markus Meissner Date: Mon, 20 Jul 2026 14:29:47 +0200 Subject: [PATCH] Add usb-device v0.3 behind feature flag --- Cargo.toml | 10 ++++++++-- src/class.rs | 14 ++++++++++---- src/lib.rs | 9 +++++++++ src/pipe.rs | 2 +- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 557a476..89d6f6d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,12 +15,18 @@ embedded-time = "0.12" heapless = "0.9" # heapless-bytes = "0.3" interchange = "0.3" -usb-device = { version = "0.2.3", features = ["control-buffer-256"] } +# Selects the usb-device version; see the `usb_device` alias in src/lib.rs. +# control-buffer-256 is needed either way: CCID descriptors exceed 128 bytes. +usb-device-0-2 = { package = "usb-device", version = "0.2.3", features = ["control-buffer-256"], optional = true } +usb-device-0-3 = { package = "usb-device", version = "0.3.2", features = ["control-buffer-256"], optional = true } [features] -default = [] +default = ["usb-device-0-2"] highspeed-usb = [] +"usb-device-0-2" = ["dep:usb-device-0-2"] +"usb-device-0-3" = ["dep:usb-device-0-3"] + log-all = [] log-none = [] log-trace = [] diff --git a/src/class.rs b/src/class.rs index 789c1b3..5d61fbe 100644 --- a/src/class.rs +++ b/src/class.rs @@ -9,9 +9,15 @@ use crate::{ types::{packet::RawPacket, ClassRequest, Status}, }; -use usb_device::class_prelude::*; +use crate::usb_device::class_prelude::*; type Result = core::result::Result; +/// usb-device 0.3 changed the `get_string` lang id from `u16` to `LangID`. +#[cfg(feature = "usb-device-0-3")] +type LangId = LangID; +#[cfg(not(feature = "usb-device-0-3"))] +type LangId = u16; + pub struct Ccid<'bus, 'pipe, Bus, const N: usize> where Bus: 'static + UsbBus, @@ -101,7 +107,7 @@ where Ok(()) } - fn get_string(&self, index: StringIndex, _lang_id: u16) -> Option<&str> { + fn get_string(&self, index: StringIndex, _lang_id: LangId) -> Option<&str> { (self.string_index == index).then_some(FUNCTIONAL_INTERFACE_STRING) } @@ -150,7 +156,7 @@ where } fn control_in(&mut self, transfer: ControlIn) { - use usb_device::control::*; + use crate::usb_device::control::*; let Request { request_type, recipient, @@ -191,7 +197,7 @@ where } fn control_out(&mut self, transfer: ControlOut) { - use usb_device::control::*; + use crate::usb_device::control::*; let Request { request_type, recipient, diff --git a/src/lib.rs b/src/lib.rs index ff86407..5499ac0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,15 @@ extern crate delog; generate_macros!(); +// The selected `usb-device` version; 0.3 wins if both features are enabled. +#[cfg(all(feature = "usb-device-0-2", not(feature = "usb-device-0-3")))] +pub use usb_device_0_2 as usb_device; +#[cfg(feature = "usb-device-0-3")] +pub use usb_device_0_3 as usb_device; + +#[cfg(not(any(feature = "usb-device-0-2", feature = "usb-device-0-3")))] +compile_error!("No usb-device version chosen! Enable `usb-device-0-2` or `usb-device-0-3`."); + mod class; mod constants; mod pipe; diff --git a/src/pipe.rs b/src/pipe.rs index ba2ff71..a55fdf0 100644 --- a/src/pipe.rs +++ b/src/pipe.rs @@ -9,7 +9,7 @@ use crate::{ }, }; -use usb_device::class_prelude::*; +use crate::usb_device::class_prelude::*; #[allow(clippy::assertions_on_constants)] const _: () = assert!(MAX_MSG_LENGTH >= PACKET_SIZE);