Fix the handling of of the kernel change from "quiet" to "low-power"

A coming kernel change will convert "quiet" to "low-power" due to how
platform_profile can now have multiple registered handlers.
(kernel 6.14 est)

Fixes #609
This commit is contained in:
Luke Jones
2025-02-14 19:38:02 +13:00
parent 2c006699f2
commit 4dd29952c8
13 changed files with 146 additions and 71 deletions

View File

@@ -207,6 +207,7 @@ impl From<PlatformProfile> for CPUEPP {
PlatformProfile::Balanced => CPUEPP::BalancePerformance,
PlatformProfile::Performance => CPUEPP::Performance,
PlatformProfile::Quiet => CPUEPP::Power,
PlatformProfile::LowPower => CPUEPP::Power,
}
}
}

View File

@@ -15,6 +15,7 @@ use std::path::Path;
use error::{PlatformError, Result};
use log::warn;
use platform::PlatformProfile;
use udev::Device;
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
@@ -107,6 +108,18 @@ pub fn write_attr_string(device: &mut Device, attr: &str, value: &str) -> Result
.map_err(|e| PlatformError::IoPath(attr.into(), e))
}
pub fn read_attr_string_array(device: &Device, attr_name: &str) -> Result<Vec<PlatformProfile>> {
if let Some(value) = device.attribute_value(attr_name) {
let tmp: Vec<PlatformProfile> = value
.to_string_lossy()
.split(' ')
.map(PlatformProfile::from)
.collect();
return Ok(tmp);
}
Err(PlatformError::AttrNotFound(attr_name.to_owned()))
}
#[cfg(test)]
mod tests {
#[test]

View File

@@ -154,6 +154,18 @@ macro_rules! get_attr_string {
};
}
#[macro_export]
macro_rules! get_attr_string_array {
($(#[$attr:meta])* $attr_name:literal $item:ident) => {
concat_idents::concat_idents!(fn_name = get_, $attr_name {
$(#[$attr])*
pub fn fn_name(&self) -> Result<Vec<PlatformProfile>> {
$crate::read_attr_string_array(&to_device(&self.$item)?, $attr_name)
}
});
};
}
#[macro_export]
macro_rules! set_attr_string {
($(#[$attr:meta])* $attr_name:literal $item:ident) => {
@@ -175,3 +187,12 @@ macro_rules! attr_string {
$crate::watch_attr!($attr_name $item);
};
}
#[macro_export]
macro_rules! attr_string_array {
($(#[$attr:meta])* $attr_name:literal, $item:ident) => {
$crate::has_attr!($attr_name $item);
$crate::get_attr_string_array!($attr_name $item);
$crate::watch_attr!($attr_name $item);
};
}

View File

@@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};
use zbus::zvariant::{OwnedValue, Type, Value};
use crate::error::{PlatformError, Result};
use crate::{attr_string, to_device};
use crate::{attr_string, attr_string_array, to_device};
/// The "platform" device provides access to things like:
/// - `dgpu_disable`
@@ -30,6 +30,12 @@ impl RogPlatform {
pp_path
);
attr_string_array!(
/// The acpi platform_profile support
"platform_profile_choices",
pp_path
);
pub fn new() -> Result<Self> {
let mut enumerator = udev::Enumerator::new().map_err(|err| {
warn!("{}", err);
@@ -169,7 +175,7 @@ impl Display for GpuMode {
}
}
#[repr(u32)]
#[repr(i32)]
#[derive(
Deserialize,
Serialize,
@@ -185,39 +191,40 @@ impl Display for GpuMode {
Clone,
Copy,
)]
#[zvariant(signature = "u")]
#[zvariant(signature = "i")]
/// `platform_profile` in asus_wmi
pub enum PlatformProfile {
#[default]
Balanced = 0,
Performance = 1,
Quiet = 2,
LowPower = 3,
}
impl PlatformProfile {
pub const fn next(self) -> Self {
match self {
pub fn next(current: Self, choices: &[Self]) -> Self {
match current {
Self::Balanced => Self::Performance,
Self::Performance => Self::Quiet,
Self::Performance => {
if choices.contains(&Self::LowPower) {
Self::LowPower
} else {
Self::Quiet
}
}
Self::Quiet => Self::Balanced,
Self::LowPower => Self::Balanced,
}
}
pub const fn list() -> [Self; 3] {
[
Self::Balanced,
Self::Performance,
Self::Quiet,
]
}
}
impl From<u8> for PlatformProfile {
fn from(num: u8) -> Self {
impl From<i32> for PlatformProfile {
fn from(num: i32) -> Self {
match num {
0 => Self::Balanced,
1 => Self::Performance,
2 => Self::Quiet,
3 => Self::LowPower,
_ => {
warn!("Unknown number for PlatformProfile: {}", num);
Self::Balanced
@@ -226,56 +233,38 @@ impl From<u8> for PlatformProfile {
}
}
impl From<i32> for PlatformProfile {
fn from(num: i32) -> Self {
(num as u8).into()
}
}
impl From<PlatformProfile> for u8 {
fn from(p: PlatformProfile) -> Self {
match p {
PlatformProfile::Balanced => 0,
PlatformProfile::Performance => 1,
PlatformProfile::Quiet => 2,
}
}
}
impl From<PlatformProfile> for i32 {
fn from(p: PlatformProfile) -> Self {
<u8>::from(p) as i32
p as i32
}
}
impl From<&PlatformProfile> for &str {
fn from(profile: &PlatformProfile) -> &'static str {
match profile {
PlatformProfile::Balanced => "balanced",
PlatformProfile::Performance => "performance",
PlatformProfile::Quiet => "quiet",
PlatformProfile::LowPower => "low-power",
}
}
}
impl From<PlatformProfile> for &str {
fn from(profile: PlatformProfile) -> &'static str {
match profile {
PlatformProfile::Balanced => "balanced",
PlatformProfile::Performance => "performance",
PlatformProfile::Quiet => "quiet",
}
<&str>::from(&profile)
}
}
impl From<String> for PlatformProfile {
fn from(profile: String) -> Self {
Self::from(&profile)
Self::from(profile.as_str())
}
}
impl From<&String> for PlatformProfile {
fn from(profile: &String) -> Self {
match profile.to_ascii_lowercase().trim() {
"balanced" => PlatformProfile::Balanced,
"performance" => PlatformProfile::Performance,
"quiet" => PlatformProfile::Quiet,
"low-power" => PlatformProfile::Quiet,
_ => {
warn!("{profile} is unknown, using ThrottlePolicy::Balanced");
PlatformProfile::Balanced
}
}
impl Display for PlatformProfile {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "({})", <&str>::from(self))
}
}
@@ -287,15 +276,24 @@ impl std::str::FromStr for PlatformProfile {
"balanced" => Ok(PlatformProfile::Balanced),
"performance" => Ok(PlatformProfile::Performance),
"quiet" => Ok(PlatformProfile::Quiet),
"low-power" => Ok(PlatformProfile::Quiet),
"low-power" => Ok(PlatformProfile::LowPower),
_ => Err(PlatformError::NotSupported),
}
}
}
impl Display for PlatformProfile {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
impl From<&str> for PlatformProfile {
fn from(profile: &str) -> Self {
match profile.to_ascii_lowercase().trim() {
"balanced" => PlatformProfile::Balanced,
"performance" => PlatformProfile::Performance,
"quiet" => PlatformProfile::Quiet,
"low-power" => PlatformProfile::LowPower,
_ => {
warn!("{profile} is unknown, using ThrottlePolicy::Balanced");
PlatformProfile::Balanced
}
}
}
}