[embassy-usb-dfu] support function level WinUSB GUIDs

This commit makes it possible to provide function level msos GUIDs to usb_dfu. This helps to ensure that composite DFU devices automatically get assigned the WinUSB driver on Windows.
This commit is contained in:
Gerhard de Clercq 2025-05-12 15:51:19 +02:00
parent f9f20ae217
commit 3c73b49790
4 changed files with 65 additions and 8 deletions

View File

@ -2,7 +2,7 @@ use embassy_boot::BlockingFirmwareState;
use embassy_time::{Duration, Instant};
use embassy_usb::control::{InResponse, OutResponse, Recipient, RequestType};
use embassy_usb::driver::Driver;
use embassy_usb::{Builder, Handler};
use embassy_usb::{msos, Builder, Handler};
use embedded_storage::nor_flash::NorFlash;
use crate::consts::{
@ -130,8 +130,22 @@ pub fn usb_dfu<'d, D: Driver<'d>, MARK: DfuMarker, RST: Reset>(
builder: &mut Builder<'d, D>,
handler: &'d mut Control<MARK, RST>,
timeout: Duration,
winusb_guids: Option<&'d [&str]>,
) {
let mut func = builder.function(0x00, 0x00, 0x00);
if let Some(winusb_guids) = winusb_guids {
// We add MSOS headers so that the device automatically gets assigned the WinUSB driver on Windows.
// Otherwise users need to do this manually using a tool like Zadig.
//
// Adding them here on the function level appears to only work for compositive devices though.
// For non-composite devices they should be placed on the device level instead.
func.msos_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", ""));
func.msos_feature(msos::RegistryPropertyFeatureDescriptor::new(
"DeviceInterfaceGUIDs",
msos::PropertyData::RegMultiSz(winusb_guids),
));
}
let mut iface = func.interface();
let mut alt = iface.alt_setting(USB_CLASS_APPN_SPEC, APPN_SPEC_SUBCLASS_DFU, DFU_PROTOCOL_RT, None);
let timeout = timeout.as_millis() as u16;

View File

@ -1,7 +1,7 @@
use embassy_boot::{AlignedBuffer, BlockingFirmwareUpdater, FirmwareUpdaterError};
use embassy_usb::control::{InResponse, OutResponse, Recipient, RequestType};
use embassy_usb::driver::Driver;
use embassy_usb::{Builder, Handler};
use embassy_usb::{msos, Builder, Handler};
use embedded_storage::nor_flash::{NorFlash, NorFlashErrorKind};
use crate::consts::{
@ -186,8 +186,22 @@ impl<'d, DFU: NorFlash, STATE: NorFlash, RST: Reset, const BLOCK_SIZE: usize> Ha
pub fn usb_dfu<'d, D: Driver<'d>, DFU: NorFlash, STATE: NorFlash, RST: Reset, const BLOCK_SIZE: usize>(
builder: &mut Builder<'d, D>,
handler: &'d mut Control<'d, DFU, STATE, RST, BLOCK_SIZE>,
winusb_guids: Option<&'d [&str]>,
) {
let mut func = builder.function(USB_CLASS_APPN_SPEC, APPN_SPEC_SUBCLASS_DFU, DFU_PROTOCOL_DFU);
if let Some(winusb_guids) = winusb_guids {
// We add MSOS headers so that the device automatically gets assigned the WinUSB driver on Windows.
// Otherwise users need to do this manually using a tool like Zadig.
//
// Adding them here on the function level appears to only work for compositive devices though.
// For non-composite devices they should be placed on the device level instead.
func.msos_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", ""));
func.msos_feature(msos::RegistryPropertyFeatureDescriptor::new(
"DeviceInterfaceGUIDs",
msos::PropertyData::RegMultiSz(winusb_guids),
));
}
let mut iface = func.interface();
let mut alt = iface.alt_setting(USB_CLASS_APPN_SPEC, APPN_SPEC_SUBCLASS_DFU, DFU_PROTOCOL_DFU, None);
alt.descriptor(

View File

@ -13,7 +13,7 @@ use embassy_stm32::usb::{self, Driver};
use embassy_stm32::{bind_interrupts, peripherals};
use embassy_sync::blocking_mutex::Mutex;
use embassy_time::Duration;
use embassy_usb::Builder;
use embassy_usb::{msos, Builder};
use embassy_usb_dfu::consts::DfuAttributes;
use embassy_usb_dfu::{usb_dfu, Control, ResetImmediate};
use panic_reset as _;
@ -22,6 +22,9 @@ bind_interrupts!(struct Irqs {
USB_LP => usb::InterruptHandler<peripherals::USB>;
});
// This is a randomly generated GUID to allow clients on Windows to find our device
const DEVICE_INTERFACE_GUIDS: &[&str] = &["{EAA9A5DC-30BA-44BC-9232-606CDC875321}"];
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let mut config = embassy_stm32::Config::default();
@ -54,7 +57,26 @@ async fn main(_spawner: Spawner) {
&mut control_buf,
);
usb_dfu(&mut builder, &mut state, Duration::from_millis(2500));
// We add MSOS headers so that the device automatically gets assigned the WinUSB driver on Windows.
// Otherwise users need to do this manually using a tool like Zadig.
builder.msos_descriptor(msos::windows_version::WIN8_1, 2);
// In the case of non-composite devices, it seems that feature headers need to be on the device level.
// (As is implemented here)
//
// For composite devices however, they should be on the function level instead.
// (This is achieved by passing a GUID to the "usb_dfu" function)
builder.msos_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", ""));
builder.msos_feature(msos::RegistryPropertyFeatureDescriptor::new(
"DeviceInterfaceGUIDs",
msos::PropertyData::RegMultiSz(DEVICE_INTERFACE_GUIDS),
));
// For non-composite devices:
usb_dfu(&mut builder, &mut state, Duration::from_millis(2500), None);
// Or for composite devices:
// usb_dfu(&mut builder, &mut state, Duration::from_millis(2500), Some(DEVICE_INTERFACE_GUIDS));
let mut dev = builder.build();
dev.run().await

View File

@ -67,17 +67,24 @@ fn main() -> ! {
// We add MSOS headers so that the device automatically gets assigned the WinUSB driver on Windows.
// Otherwise users need to do this manually using a tool like Zadig.
//
// It seems it is important for the DFU class that these headers be on the Device level.
//
builder.msos_descriptor(msos::windows_version::WIN8_1, 2);
// In the case of non-composite devices, it seems that feature headers need to be on the device level.
// (As is implemented here)
//
// For composite devices however, they should be on the function level instead.
// (This is achieved by passing a GUID to the "usb_dfu" function)
builder.msos_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", ""));
builder.msos_feature(msos::RegistryPropertyFeatureDescriptor::new(
"DeviceInterfaceGUIDs",
msos::PropertyData::RegMultiSz(DEVICE_INTERFACE_GUIDS),
));
usb_dfu::<_, _, _, _, 4096>(&mut builder, &mut state);
// For non-composite devices:
usb_dfu::<_, _, _, _, 4096>(&mut builder, &mut state, None);
// Or for composite devices:
// usb_dfu::<_, _, _, _, 4096>(&mut builder, &mut state, Some(DEVICE_INTERFACE_GUIDS));
let mut dev = builder.build();
embassy_futures::block_on(dev.run());