Skip to content

Commit

Permalink
Fix windows driver
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Schaefer <[email protected]>
  • Loading branch information
JohnAZoidberg committed Oct 28, 2023
1 parent 9c6a2c8 commit 055cec1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
14 changes: 5 additions & 9 deletions framework_lib/src/ccgx/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use core::prelude::rust_2021::derive;
use crate::ccgx::{AppVersion, BaseVersion, ControllerVersion};
use crate::chromium_ec::command::EcCommands;
use crate::chromium_ec::{CrosEc, CrosEcDriver, EcError, EcResponseStatus, EcResult};
use crate::util::{self, Config, Platform};
use crate::util::{self, assert_win_len, Config, Platform};
use std::mem::size_of;

use super::*;
Expand Down Expand Up @@ -245,8 +245,7 @@ impl PdController {

pub fn get_silicon_id(&self) -> EcResult<u16> {
let data = self.ccgx_read(ControlRegisters::SiliconId, 2)?;
assert!(data.len() >= 2);
debug_assert_eq!(data.len(), 2);
assert_win_len(data.len(), 2);
Ok(u16::from_le_bytes([data[0], data[1]]))
}

Expand Down Expand Up @@ -306,8 +305,7 @@ impl PdController {
}
};

assert!(data.len() >= 8);
debug_assert_eq!(data.len(), 8);
assert_win_len(data.len(), 8);
let base_ver = BaseVersion::from(&data[..4]);
let app_ver = AppVersion::from(&data[4..]);
println!(
Expand All @@ -317,8 +315,7 @@ impl PdController {

let data = self.ccgx_read(ControlRegisters::Firmware1Version, 8);
let data = data.unwrap();
assert!(data.len() >= 8);
debug_assert_eq!(data.len(), 8);
assert_win_len(data.len(), 8);
let base_ver = BaseVersion::from(&data[..4]);
let app_ver = AppVersion::from(&data[4..]);
println!(
Expand All @@ -328,8 +325,7 @@ impl PdController {

let data = self.ccgx_read(ControlRegisters::Firmware2Version, 8);
let data = data.unwrap();
assert!(data.len() >= 8);
debug_assert_eq!(data.len(), 8);
assert_win_len(data.len(), 8);
let base_ver = BaseVersion::from(&data[..4]);
let app_ver = AppVersion::from(&data[4..]);
println!(
Expand Down
9 changes: 7 additions & 2 deletions framework_lib/src/chromium_ec/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,14 @@ pub trait EcRequestRaw<R> {
Self: Sized,
{
let response = self.send_command_vec_extra(ec, extra_data)?;
if response.len() != std::mem::size_of::<R>() {
// TODO: The Windows driver seems to return 20 more bytes than expected
#[cfg(feature = "win_driver")]
let expected = response.len() != std::mem::size_of::<R>() + 20;
#[cfg(not(feature = "win_driver"))]
let expected = response.len() != std::mem::size_of::<R>();
if expected {
return Err(EcError::DeviceError(format!(
"Returned data is not the expected ({}) size: {}",
"Returned data size {} is not the expted size: {}",
response.len(),
std::mem::size_of::<R>()
)));
Expand Down
7 changes: 5 additions & 2 deletions framework_lib/src/chromium_ec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::os_specific;
use crate::smbios;
#[cfg(feature = "uefi")]
use crate::uefi::shell_get_execution_break_flag;
use crate::util::assert_win_len;

use num_derive::FromPrimitive;

Expand Down Expand Up @@ -235,7 +236,8 @@ impl CrosEc {
// does not return any data
let limits = &[ChargeLimitControlModes::Set as u8, max, min];
let data = self.send_command(EcCommands::ChargeLimitControl as u16, 0, limits)?;
assert_eq!(data.len(), 0);

assert_win_len(data.len(), 0);

Ok(())
}
Expand All @@ -262,7 +264,8 @@ impl CrosEc {
// does not return any data
let limits = &[level as u8, 0x00];
let data = self.send_command(EcCommands::FpLedLevelControl as u16, 0, limits)?;
assert_eq!(data.len(), 0);

assert_win_len(data.len(), 0);

Ok(())
}
Expand Down
5 changes: 4 additions & 1 deletion framework_lib/src/chromium_ec/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ pub fn send_command(command: u16, command_version: u8, data: &[u8]) -> EcResult<
Some(status) => return Err(EcError::Response(status)),
}

let out_buffer = &cmd.buffer[..(returned as usize)];
// TODO: Figure out why that's sometimes bigger
let end = std::cmp::min(returned, 256);

let out_buffer = &cmd.buffer[..(end as usize)];
Ok(out_buffer.to_vec())
}

Expand Down
10 changes: 10 additions & 0 deletions framework_lib/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Miscellaneous utility functions to use across modules
use num::{Num, NumCast};
use std::prelude::v1::*;

#[cfg(feature = "uefi")]
Expand Down Expand Up @@ -151,3 +152,12 @@ pub fn find_sequence(haystack: &[u8], needle: &[u8]) -> Option<usize> {
.windows(needle.len())
.position(|window| window == needle)
}

/// Assert length of an EC response from the windows driver
/// It's always 20 more than expected. TODO: Figure out why
pub fn assert_win_len<N: Num + std::fmt::Debug + Ord + NumCast + Copy>(left: N, right: N) {
#[cfg(feature = "win_driver")]
assert_eq!(left, right + NumCast::from(20).unwrap());
#[cfg(not(feature = "win_driver"))]
assert_eq!(left, right);
}

0 comments on commit 055cec1

Please sign in to comment.