Skip to content

Commit

Permalink
USB route api change
Browse files Browse the repository at this point in the history
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
  • Loading branch information
svenrademakers committed Nov 23, 2023
1 parent ebb4240 commit f900821
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
12 changes: 8 additions & 4 deletions src/api/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};

Expand All @@ -510,8 +512,10 @@ async fn get_usb_mode(bmc: &BmcApplication) -> impl Into<LegacyResponse> {
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),
};

Expand Down
18 changes: 10 additions & 8 deletions src/app/bmc_application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand All @@ -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?;
Expand Down Expand Up @@ -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(())
}

Expand Down Expand Up @@ -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...");
Expand Down
2 changes: 2 additions & 0 deletions src/hal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,15 @@ pub enum UsbRoute {
pub enum UsbMode {
Host,
Device,
Recovery,
}

impl UsbMode {
pub fn from_api_mode(value: i32) -> Self {
match value & 0x1 {
0 => UsbMode::Host,
1 => UsbMode::Device,
2 => UsbMode::Recovery,
_ => unreachable!(),
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/hal/pin_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f900821

Please sign in to comment.