Skip to content

Commit ace46f9

Browse files
committed
--versions: Return any number of PD versions
Based on EC host command. Signed-off-by: Daniel Schaefer <[email protected]>
1 parent db61c36 commit ace46f9

File tree

3 files changed

+60
-14
lines changed

3 files changed

+60
-14
lines changed

framework_lib/src/ccgx/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use alloc::format;
44
use alloc::string::String;
55
use alloc::string::ToString;
6+
use alloc::vec::Vec;
67
#[cfg(feature = "uefi")]
78
use core::prelude::rust_2021::derive;
89
use num_derive::FromPrimitive;
@@ -241,9 +242,10 @@ pub struct PdVersions {
241242

242243
/// Same as PdVersions but only the main FW
243244
#[derive(Debug)]
244-
pub struct MainPdVersions {
245-
pub controller01: ControllerVersion,
246-
pub controller23: ControllerVersion,
245+
pub enum MainPdVersions {
246+
RightLeft((ControllerVersion, ControllerVersion)),
247+
Single(ControllerVersion),
248+
Many(Vec<ControllerVersion>),
247249
}
248250

249251
pub fn get_pd_controller_versions(ec: &CrosEc) -> EcResult<PdVersions> {

framework_lib/src/commandline/mod.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::capsule_content::{
3333
use crate::ccgx::device::{FwMode, PdController, PdPort};
3434
#[cfg(feature = "hidapi")]
3535
use crate::ccgx::hid::{check_ccg_fw_version, find_devices, DP_CARD_PID, HDMI_CARD_PID};
36-
use crate::ccgx::{self, SiliconId::*};
36+
use crate::ccgx::{self, MainPdVersions, SiliconId::*};
3737
use crate::chromium_ec;
3838
use crate::chromium_ec::commands::DeckStateMode;
3939
use crate::chromium_ec::commands::FpLedBrightnessLevel;
@@ -478,8 +478,25 @@ fn print_versions(ec: &CrosEc) {
478478
}
479479
} else if let Ok(pd_versions) = power::read_pd_version(ec) {
480480
// As fallback try to get it from the EC. But not all EC versions have this command
481-
println!(" Right (01): {}", pd_versions.controller01.app);
482-
println!(" Left (23): {}", pd_versions.controller23.app);
481+
match pd_versions {
482+
MainPdVersions::RightLeft((controller01, controller23)) => {
483+
if let Some(Platform::IntelGen11) = smbios::get_platform() {
484+
println!(" Right (01): {}", controller01.base);
485+
println!(" Left (23): {}", controller23.base);
486+
} else {
487+
println!(" Right (01): {}", controller01.app);
488+
println!(" Left (23): {}", controller23.app);
489+
}
490+
}
491+
MainPdVersions::Single(version) => {
492+
println!(" Version: {}", version.app);
493+
}
494+
MainPdVersions::Many(versions) => {
495+
for (i, version) in versions.into_iter().enumerate() {
496+
println!(" PD {}: {}", i, version.app);
497+
}
498+
}
499+
}
483500
} else {
484501
println!(" Unknown")
485502
}

framework_lib/src/power.rs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Get information about system power (battery, AC, PD ports)
22
33
use alloc::format;
4-
use alloc::string::String;
4+
use alloc::string::{String, ToString};
55
use alloc::vec;
66
use alloc::vec::Vec;
77
use core::convert::TryInto;
@@ -798,6 +798,11 @@ pub fn is_charging(ec: &CrosEc) -> EcResult<(bool, bool)> {
798798
Ok((port0 || port1, port2 || port3))
799799
}
800800

801+
fn parse_pd_ver_slice(data: &[u8]) -> ControllerVersion {
802+
parse_pd_ver(&[
803+
data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
804+
])
805+
}
801806
fn parse_pd_ver(data: &[u8; 8]) -> ControllerVersion {
802807
ControllerVersion {
803808
base: BaseVersion {
@@ -815,15 +820,37 @@ fn parse_pd_ver(data: &[u8; 8]) -> ControllerVersion {
815820
}
816821
}
817822

818-
// NOTE: Only works on ADL at the moment!
819-
// TODO: Not on TGL, need to check if RPL and later have it.
823+
// NOTE: TGL (hx20) does not have this host command
820824
pub fn read_pd_version(ec: &CrosEc) -> EcResult<MainPdVersions> {
821-
let info = EcRequestReadPdVersion {}.send_command(ec)?;
825+
let info = EcRequestReadPdVersionV1 {}.send_command_vec(ec);
822826

823-
Ok(MainPdVersions {
824-
controller01: parse_pd_ver(&info.controller01),
825-
controller23: parse_pd_ver(&info.controller23),
826-
})
827+
// If v1 not available, fall back
828+
if let Err(EcError::Response(EcResponseStatus::InvalidVersion)) = info {
829+
let info = EcRequestReadPdVersionV0 {}.send_command(ec)?;
830+
831+
return Ok(if info.controller23 == [0, 0, 0, 0, 0, 0, 0, 0] {
832+
MainPdVersions::Single(parse_pd_ver(&info.controller01))
833+
} else {
834+
MainPdVersions::RightLeft((
835+
parse_pd_ver(&info.controller01),
836+
parse_pd_ver(&info.controller23),
837+
))
838+
});
839+
}
840+
// If any other error, exit
841+
let info = info?;
842+
843+
let mut versions = vec![];
844+
let pd_count = info[0] as usize;
845+
for i in 0..pd_count {
846+
// TODO: Is there a safer way to check the range?
847+
if info.len() < 1 + 8 * (i + 1) {
848+
return Err(EcError::DeviceError("Not enough data returned".to_string()));
849+
}
850+
versions.push(parse_pd_ver_slice(&info[1 + 8 * i..1 + 8 * (i + 1)]));
851+
}
852+
853+
Ok(MainPdVersions::Many(versions))
827854
}
828855

829856
pub fn standalone_mode(ec: &CrosEc) -> bool {

0 commit comments

Comments
 (0)