From 594f69b24c4424049ce3e7bac54a23d112495be2 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Mon, 7 Aug 2023 20:08:49 -0400 Subject: [PATCH] Improve battery hwmon searching for newer kernels --- backend/Cargo.lock | 2 +- backend/src/settings/steam_deck/battery.rs | 31 ++++++++++++++++++++-- backend/src/settings/steam_deck/util.rs | 1 + 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/backend/Cargo.lock b/backend/Cargo.lock index b39eac7..85199ce 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -1043,7 +1043,7 @@ dependencies = [ [[package]] name = "powertools" -version = "1.4.0-alpha2" +version = "1.4.0-beta1" dependencies = [ "async-trait", "libryzenadj", diff --git a/backend/src/settings/steam_deck/battery.rs b/backend/src/settings/steam_deck/battery.rs index e57662f..4d521cc 100644 --- a/backend/src/settings/steam_deck/battery.rs +++ b/backend/src/settings/steam_deck/battery.rs @@ -285,8 +285,19 @@ impl Battery { hwmon }, Err(e) => { - log::error!("Failed to find SteamDeck battery hwmon in sysfs ({}), using naive fallback", e); - root.hwmon_by_index(5) + log::warn!("Failed to find SteamDeck battery hwmon {} in sysfs ({}), trying alternate name", + super::util::JUPITER_HWMON_NAME, e); + match root.hwmon_by_name(super::util::STEAMDECK_HWMON_NAME) { + Ok(hwmon) => { + if !hwmon.capable(attributes(HWMON_NEEDS.into_iter().copied())) { + log::warn!("Found incapable SteamDeck battery hwmon in sysfs (hwmon by name {} exists but missing attributes), persevering because ignorance is bliss", super::util::STEAMDECK_HWMON_NAME); + } + hwmon + }, + Err(e) => { + log::error!("Failed to find SteamDeck battery hwmon in sysfs ({}), using naive fallback", e); + root.hwmon_by_index(5) + } } } } @@ -550,7 +561,23 @@ impl OnPowerEvent for Battery { PowerMode::BatteryCharge(_) => Ok(()), } .unwrap_or_else(|mut e| errors.append(&mut e)); + let new_charge_control_attr = HwMonAttribute::custom("max_battery_charge_level"); + let attr_exists = new_charge_control_attr.exists(&*self.sysfs_hwmon); + log::info!("Does battery limit attribute (max_battery_charge_level) exist? {}", attr_exists); for ev in &mut self.events { + if attr_exists { + if let EventTrigger::BatteryAbove(level) = ev.trigger { + if let Some(ChargeMode::Idle) = ev.charge_mode { + self.sysfs_hwmon.set(new_charge_control_attr, (level * 100.0).round() as u64) + .unwrap_or_else(|e| errors.push( + SettingError { + msg: format!("Failed to write to {:?}: {}", new_charge_control_attr, e), + setting: crate::settings::SettingVariant::Battery, + } + )); + } + } + } ev.on_power_event(new_mode) .unwrap_or_else(|mut e| errors.append(&mut e)); } diff --git a/backend/src/settings/steam_deck/util.rs b/backend/src/settings/steam_deck/util.rs index 84dfd9c..471e2be 100644 --- a/backend/src/settings/steam_deck/util.rs +++ b/backend/src/settings/steam_deck/util.rs @@ -9,6 +9,7 @@ use std::fs::OpenOptions; use std::io::{Error, Read, Seek, SeekFrom, Write}; pub const JUPITER_HWMON_NAME: &'static str = "jupiter"; +pub const STEAMDECK_HWMON_NAME: &'static str = "steamdeck_hwmon"; pub const GPU_HWMON_NAME: &'static str = "amdgpu"; #[inline]