aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGerhard de Clercq <[email protected]>2025-05-12 15:51:19 +0200
committerGerhard de Clercq <[email protected]>2025-05-12 15:55:11 +0200
commit3c73b497909ce5bacd16d23e54928a7f66544e09 (patch)
treec6f0937fb9e4a53cf69897a34bf069933c0d148e
parentf9f20ae2174cb26d0f8926207d179041cfec2d2e (diff)
[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.
-rw-r--r--embassy-usb-dfu/src/application.rs16
-rw-r--r--embassy-usb-dfu/src/dfu.rs16
-rw-r--r--examples/boot/application/stm32wb-dfu/src/main.rs26
-rw-r--r--examples/boot/bootloader/stm32wb-dfu/src/main.rs15
4 files changed, 65 insertions, 8 deletions
diff --git a/embassy-usb-dfu/src/application.rs b/embassy-usb-dfu/src/application.rs
index 6ad07a78c..52a7ca951 100644
--- a/embassy-usb-dfu/src/application.rs
+++ b/embassy-usb-dfu/src/application.rs
@@ -2,7 +2,7 @@ use embassy_boot::BlockingFirmwareState;
2use embassy_time::{Duration, Instant}; 2use embassy_time::{Duration, Instant};
3use embassy_usb::control::{InResponse, OutResponse, Recipient, RequestType}; 3use embassy_usb::control::{InResponse, OutResponse, Recipient, RequestType};
4use embassy_usb::driver::Driver; 4use embassy_usb::driver::Driver;
5use embassy_usb::{Builder, Handler}; 5use embassy_usb::{msos, Builder, Handler};
6use embedded_storage::nor_flash::NorFlash; 6use embedded_storage::nor_flash::NorFlash;
7 7
8use crate::consts::{ 8use crate::consts::{
@@ -130,8 +130,22 @@ pub fn usb_dfu<'d, D: Driver<'d>, MARK: DfuMarker, RST: Reset>(
130 builder: &mut Builder<'d, D>, 130 builder: &mut Builder<'d, D>,
131 handler: &'d mut Control<MARK, RST>, 131 handler: &'d mut Control<MARK, RST>,
132 timeout: Duration, 132 timeout: Duration,
133 winusb_guids: Option<&'d [&str]>,
133) { 134) {
134 let mut func = builder.function(0x00, 0x00, 0x00); 135 let mut func = builder.function(0x00, 0x00, 0x00);
136 if let Some(winusb_guids) = winusb_guids {
137 // We add MSOS headers so that the device automatically gets assigned the WinUSB driver on Windows.
138 // Otherwise users need to do this manually using a tool like Zadig.
139 //
140 // Adding them here on the function level appears to only work for compositive devices though.
141 // For non-composite devices they should be placed on the device level instead.
142 func.msos_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", ""));
143 func.msos_feature(msos::RegistryPropertyFeatureDescriptor::new(
144 "DeviceInterfaceGUIDs",
145 msos::PropertyData::RegMultiSz(winusb_guids),
146 ));
147 }
148
135 let mut iface = func.interface(); 149 let mut iface = func.interface();
136 let mut alt = iface.alt_setting(USB_CLASS_APPN_SPEC, APPN_SPEC_SUBCLASS_DFU, DFU_PROTOCOL_RT, None); 150 let mut alt = iface.alt_setting(USB_CLASS_APPN_SPEC, APPN_SPEC_SUBCLASS_DFU, DFU_PROTOCOL_RT, None);
137 let timeout = timeout.as_millis() as u16; 151 let timeout = timeout.as_millis() as u16;
diff --git a/embassy-usb-dfu/src/dfu.rs b/embassy-usb-dfu/src/dfu.rs
index a98d6ab40..83feacaf8 100644
--- a/embassy-usb-dfu/src/dfu.rs
+++ b/embassy-usb-dfu/src/dfu.rs
@@ -1,7 +1,7 @@
1use embassy_boot::{AlignedBuffer, BlockingFirmwareUpdater, FirmwareUpdaterError}; 1use embassy_boot::{AlignedBuffer, BlockingFirmwareUpdater, FirmwareUpdaterError};
2use embassy_usb::control::{InResponse, OutResponse, Recipient, RequestType}; 2use embassy_usb::control::{InResponse, OutResponse, Recipient, RequestType};
3use embassy_usb::driver::Driver; 3use embassy_usb::driver::Driver;
4use embassy_usb::{Builder, Handler}; 4use embassy_usb::{msos, Builder, Handler};
5use embedded_storage::nor_flash::{NorFlash, NorFlashErrorKind}; 5use embedded_storage::nor_flash::{NorFlash, NorFlashErrorKind};
6 6
7use crate::consts::{ 7use crate::consts::{
@@ -186,8 +186,22 @@ impl<'d, DFU: NorFlash, STATE: NorFlash, RST: Reset, const BLOCK_SIZE: usize> Ha
186pub fn usb_dfu<'d, D: Driver<'d>, DFU: NorFlash, STATE: NorFlash, RST: Reset, const BLOCK_SIZE: usize>( 186pub fn usb_dfu<'d, D: Driver<'d>, DFU: NorFlash, STATE: NorFlash, RST: Reset, const BLOCK_SIZE: usize>(
187 builder: &mut Builder<'d, D>, 187 builder: &mut Builder<'d, D>,
188 handler: &'d mut Control<'d, DFU, STATE, RST, BLOCK_SIZE>, 188 handler: &'d mut Control<'d, DFU, STATE, RST, BLOCK_SIZE>,
189 winusb_guids: Option<&'d [&str]>,
189) { 190) {
190 let mut func = builder.function(USB_CLASS_APPN_SPEC, APPN_SPEC_SUBCLASS_DFU, DFU_PROTOCOL_DFU); 191 let mut func = builder.function(USB_CLASS_APPN_SPEC, APPN_SPEC_SUBCLASS_DFU, DFU_PROTOCOL_DFU);
192 if let Some(winusb_guids) = winusb_guids {
193 // We add MSOS headers so that the device automatically gets assigned the WinUSB driver on Windows.
194 // Otherwise users need to do this manually using a tool like Zadig.
195 //
196 // Adding them here on the function level appears to only work for compositive devices though.
197 // For non-composite devices they should be placed on the device level instead.
198 func.msos_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", ""));
199 func.msos_feature(msos::RegistryPropertyFeatureDescriptor::new(
200 "DeviceInterfaceGUIDs",
201 msos::PropertyData::RegMultiSz(winusb_guids),
202 ));
203 }
204
191 let mut iface = func.interface(); 205 let mut iface = func.interface();
192 let mut alt = iface.alt_setting(USB_CLASS_APPN_SPEC, APPN_SPEC_SUBCLASS_DFU, DFU_PROTOCOL_DFU, None); 206 let mut alt = iface.alt_setting(USB_CLASS_APPN_SPEC, APPN_SPEC_SUBCLASS_DFU, DFU_PROTOCOL_DFU, None);
193 alt.descriptor( 207 alt.descriptor(
diff --git a/examples/boot/application/stm32wb-dfu/src/main.rs b/examples/boot/application/stm32wb-dfu/src/main.rs
index dda2b795b..68e9bc3f6 100644
--- a/examples/boot/application/stm32wb-dfu/src/main.rs
+++ b/examples/boot/application/stm32wb-dfu/src/main.rs
@@ -13,7 +13,7 @@ use embassy_stm32::usb::{self, Driver};
13use embassy_stm32::{bind_interrupts, peripherals}; 13use embassy_stm32::{bind_interrupts, peripherals};
14use embassy_sync::blocking_mutex::Mutex; 14use embassy_sync::blocking_mutex::Mutex;
15use embassy_time::Duration; 15use embassy_time::Duration;
16use embassy_usb::Builder; 16use embassy_usb::{msos, Builder};
17use embassy_usb_dfu::consts::DfuAttributes; 17use embassy_usb_dfu::consts::DfuAttributes;
18use embassy_usb_dfu::{usb_dfu, Control, ResetImmediate}; 18use embassy_usb_dfu::{usb_dfu, Control, ResetImmediate};
19use panic_reset as _; 19use panic_reset as _;
@@ -22,6 +22,9 @@ bind_interrupts!(struct Irqs {
22 USB_LP => usb::InterruptHandler<peripherals::USB>; 22 USB_LP => usb::InterruptHandler<peripherals::USB>;
23}); 23});
24 24
25// This is a randomly generated GUID to allow clients on Windows to find our device
26const DEVICE_INTERFACE_GUIDS: &[&str] = &["{EAA9A5DC-30BA-44BC-9232-606CDC875321}"];
27
25#[embassy_executor::main] 28#[embassy_executor::main]
26async fn main(_spawner: Spawner) { 29async fn main(_spawner: Spawner) {
27 let mut config = embassy_stm32::Config::default(); 30 let mut config = embassy_stm32::Config::default();
@@ -54,7 +57,26 @@ async fn main(_spawner: Spawner) {
54 &mut control_buf, 57 &mut control_buf,
55 ); 58 );
56 59
57 usb_dfu(&mut builder, &mut state, Duration::from_millis(2500)); 60 // We add MSOS headers so that the device automatically gets assigned the WinUSB driver on Windows.
61 // Otherwise users need to do this manually using a tool like Zadig.
62 builder.msos_descriptor(msos::windows_version::WIN8_1, 2);
63
64 // In the case of non-composite devices, it seems that feature headers need to be on the device level.
65 // (As is implemented here)
66 //
67 // For composite devices however, they should be on the function level instead.
68 // (This is achieved by passing a GUID to the "usb_dfu" function)
69 builder.msos_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", ""));
70 builder.msos_feature(msos::RegistryPropertyFeatureDescriptor::new(
71 "DeviceInterfaceGUIDs",
72 msos::PropertyData::RegMultiSz(DEVICE_INTERFACE_GUIDS),
73 ));
74
75 // For non-composite devices:
76 usb_dfu(&mut builder, &mut state, Duration::from_millis(2500), None);
77
78 // Or for composite devices:
79 // usb_dfu(&mut builder, &mut state, Duration::from_millis(2500), Some(DEVICE_INTERFACE_GUIDS));
58 80
59 let mut dev = builder.build(); 81 let mut dev = builder.build();
60 dev.run().await 82 dev.run().await
diff --git a/examples/boot/bootloader/stm32wb-dfu/src/main.rs b/examples/boot/bootloader/stm32wb-dfu/src/main.rs
index 28216806e..2cd7f859d 100644
--- a/examples/boot/bootloader/stm32wb-dfu/src/main.rs
+++ b/examples/boot/bootloader/stm32wb-dfu/src/main.rs
@@ -67,17 +67,24 @@ fn main() -> ! {
67 67
68 // We add MSOS headers so that the device automatically gets assigned the WinUSB driver on Windows. 68 // We add MSOS headers so that the device automatically gets assigned the WinUSB driver on Windows.
69 // Otherwise users need to do this manually using a tool like Zadig. 69 // Otherwise users need to do this manually using a tool like Zadig.
70 //
71 // It seems it is important for the DFU class that these headers be on the Device level.
72 //
73 builder.msos_descriptor(msos::windows_version::WIN8_1, 2); 70 builder.msos_descriptor(msos::windows_version::WIN8_1, 2);
71
72 // In the case of non-composite devices, it seems that feature headers need to be on the device level.
73 // (As is implemented here)
74 //
75 // For composite devices however, they should be on the function level instead.
76 // (This is achieved by passing a GUID to the "usb_dfu" function)
74 builder.msos_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", "")); 77 builder.msos_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", ""));
75 builder.msos_feature(msos::RegistryPropertyFeatureDescriptor::new( 78 builder.msos_feature(msos::RegistryPropertyFeatureDescriptor::new(
76 "DeviceInterfaceGUIDs", 79 "DeviceInterfaceGUIDs",
77 msos::PropertyData::RegMultiSz(DEVICE_INTERFACE_GUIDS), 80 msos::PropertyData::RegMultiSz(DEVICE_INTERFACE_GUIDS),
78 )); 81 ));
79 82
80 usb_dfu::<_, _, _, _, 4096>(&mut builder, &mut state); 83 // For non-composite devices:
84 usb_dfu::<_, _, _, _, 4096>(&mut builder, &mut state, None);
85
86 // Or for composite devices:
87 // usb_dfu::<_, _, _, _, 4096>(&mut builder, &mut state, Some(DEVICE_INTERFACE_GUIDS));
81 88
82 let mut dev = builder.build(); 89 let mut dev = builder.build();
83 embassy_futures::block_on(dev.run()); 90 embassy_futures::block_on(dev.run());