From f900821854986abca90d6df1f14a4bcab4609d0e Mon Sep 17 00:00:00 2001 From: Sven Rademakers Date: Thu, 23 Nov 2023 14:05:57 +0000 Subject: [PATCH] USB route api change The boot pin state is added to the USB state, as its closely related to the different roles a given node forfills. THIS BREAKS the storage layout. -> reset of storage required --- src/api/legacy.rs | 12 ++++++++---- src/app/bmc_application.rs | 18 ++++++++++-------- src/hal.rs | 2 ++ src/hal/pin_controller.rs | 10 ++++++++-- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/api/legacy.rs b/src/api/legacy.rs index 4699dbe..5fc8109 100644 --- a/src/api/legacy.rs +++ b/src/api/legacy.rs @@ -494,8 +494,10 @@ async fn set_usb_mode(bmc: &BmcApplication, query: Query) -> LegacyResult<()> { }; let cfg = match (mode, route) { - (UsbMode::Device, UsbRoute::UsbA) => UsbConfig::UsbA(node), - (UsbMode::Device, UsbRoute::Bmc) => UsbConfig::Bmc(node), + (UsbMode::Device, UsbRoute::UsbA) => UsbConfig::UsbA(node, false), + (UsbMode::Device, UsbRoute::Bmc) => UsbConfig::Bmc(node, false), + (UsbMode::Recovery, UsbRoute::UsbA) => UsbConfig::UsbA(node, true), + (UsbMode::Recovery, UsbRoute::Bmc) => UsbConfig::Bmc(node, true), (UsbMode::Host, route) => UsbConfig::Node(node, route), }; @@ -510,8 +512,10 @@ async fn get_usb_mode(bmc: &BmcApplication) -> impl Into { let config = bmc.get_usb_mode().await; let (node, mode, route) = match config { - UsbConfig::UsbA(node) => (node, UsbMode::Device, UsbRoute::UsbA), - UsbConfig::Bmc(node) => (node, UsbMode::Device, UsbRoute::Bmc), + UsbConfig::UsbA(node, false) => (node, UsbMode::Device, UsbRoute::UsbA), + UsbConfig::Bmc(node, false) => (node, UsbMode::Device, UsbRoute::Bmc), + UsbConfig::UsbA(node, true) => (node, UsbMode::Recovery, UsbRoute::UsbA), + UsbConfig::Bmc(node, true) => (node, UsbMode::Recovery, UsbRoute::Bmc), UsbConfig::Node(node, route) => (node, UsbMode::Host, route), }; diff --git a/src/app/bmc_application.rs b/src/app/bmc_application.rs index 8168b8c..a90e590 100644 --- a/src/app/bmc_application.rs +++ b/src/app/bmc_application.rs @@ -37,9 +37,9 @@ const REBOOT_DELAY: Duration = Duration::from_millis(500); #[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)] pub enum UsbConfig { /// USB-A port is host, NodeId is the device. - UsbA(NodeId), + UsbA(NodeId, bool), /// BMC is host, NodeId is the device. - Bmc(NodeId), + Bmc(NodeId, bool), /// NodeId is host, [UsbRoute] is configured for device Node(NodeId, UsbRoute), } @@ -65,7 +65,7 @@ impl BmcApplication { let power_controller = PowerController::new().context("power_controller")?; let app_db = PersistencyBuilder::default() .register_key(ACTIVATED_NODES_KEY, &0u8) - .register_key(USB_CONFIG, &UsbConfig::UsbA(NodeId::Node1)) + .register_key(USB_CONFIG, &UsbConfig::UsbA(NodeId::Node1, false)) .write_timeout(database_write_timeout) .build() .await?; @@ -169,13 +169,16 @@ impl BmcApplication { async fn configure_usb_internal(&self, config: UsbConfig) -> anyhow::Result<()> { log::debug!("changing usb config to {:?}", config); let (mode, dest, route) = match config { - UsbConfig::UsbA(device) => (UsbMode::Device, device, UsbRoute::UsbA), - UsbConfig::Bmc(device) => (UsbMode::Device, device, UsbRoute::Bmc), + UsbConfig::UsbA(device, false) => (UsbMode::Device, device, UsbRoute::UsbA), + UsbConfig::UsbA(device, true) => (UsbMode::Recovery, device, UsbRoute::UsbA), + UsbConfig::Bmc(device, false) => (UsbMode::Device, device, UsbRoute::Bmc), + UsbConfig::Bmc(device, true) => (UsbMode::Recovery, device, UsbRoute::Bmc), UsbConfig::Node(host, route) => (UsbMode::Host, host, route), }; self.pin_controller.set_usb_route(route).await?; self.pin_controller.select_usb(dest, mode)?; + Ok(()) } @@ -219,10 +222,9 @@ impl BmcApplication { sleep(REBOOT_DELAY).await; let config = match router { - UsbRoute::Bmc => UsbConfig::Bmc(node), - UsbRoute::UsbA => UsbConfig::UsbA(node), + UsbRoute::Bmc => UsbConfig::Bmc(node, true), + UsbRoute::UsbA => UsbConfig::UsbA(node, true), }; - self.usb_boot(node, true).await?; self.configure_usb_internal(config).await?; log::info!("Prerequisite settings toggled, powering on..."); diff --git a/src/hal.rs b/src/hal.rs index fcec1c7..f79e024 100644 --- a/src/hal.rs +++ b/src/hal.rs @@ -108,6 +108,7 @@ pub enum UsbRoute { pub enum UsbMode { Host, Device, + Recovery, } impl UsbMode { @@ -115,6 +116,7 @@ impl UsbMode { match value & 0x1 { 0 => UsbMode::Host, 1 => UsbMode::Device, + 2 => UsbMode::Recovery, _ => unreachable!(), } } diff --git a/src/hal/pin_controller.rs b/src/hal/pin_controller.rs index e610f5c..5b480c6 100644 --- a/src/hal/pin_controller.rs +++ b/src/hal/pin_controller.rs @@ -87,9 +87,15 @@ impl PinController { let vbus = match mode { UsbMode::Host => node.to_inverse_bitfield(), - UsbMode::Device => 0b1111, + UsbMode::Device | UsbMode::Recovery => 0b1111, }; - self.usb_vbus.set_values(vbus) + self.usb_vbus.set_values(vbus)?; + + if UsbMode::Recovery == mode { + self.set_usb_boot(node.to_bitfield(), node.to_bitfield())?; + } + + Ok(()) } /// Set which way the USB is routed: USB-A ↔ PORTx (`UsbRoute::UsbA`) or BMC ↔ PORTx