Skip to contentSkip to conversation

platform/x86: msi-ec: Report valid charge thresholds when unset

PATCH In review Daasaradhi Mannava 2 participants 4 messages Sep 16 – Oct 5

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 platform/x86: msi-ec: Report valid charge thresholds when unset

The charge thresholds are computed by subtracting a fixed offset from the raw EC value. When no charge limit has been set, the EC can hold a value below range_min (0x80 on an MSI GL65 Leopard 9SCXK), and reading the sysfs attributes returns nonsense:

charge_control_start_threshold: -10
charge_control_end_threshold: 0

Both attributes are documented to be within 0 - 100, and an end threshold of 0 wrongly suggests that charging is disabled.

The store path already rejects values outside range_min..range_max. Apply the same range to the show path and report the maximum threshold for any value outside of it.

Tested on an MSI GL65 Leopard 9SCXK (EC firmware 16U8EMS2.100) with the EC holding 0x80: the attributes now read 90 and 100.

Fixes 392cacf2aa10 ("platform/x86: Add new msi-ec driver")
Assisted-by LLM
---
 drivers/platform/x86/msi-ec.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/platform/x86/msi-ec.c +9 -0
index 566dfc73c..1762551e0 100644
@@ -1279,6 +1279,15 @@ static ssize_t charge_control_threshold_show(u8 offset,
 	if (result < 0)
 		return result;
 
+	/*
+	 * The EC may hold an out-of-range value (e.g. 0x80) when no charge
+	 * limit has been set. Report the maximum threshold instead of a
+	 * meaningless (possibly negative) percentage.
+	 */
+	if (rdata < conf.charge_control.range_min ||
+	    rdata > conf.charge_control.range_max)
+		rdata = conf.charge_control.range_max;
+
 	return sysfs_emit(buf, "%i\n", rdata - offset);
 }
 
Signature · 1 line
2.55.0
maintainer
#1 lore raw
Quoted 34 lines · Daasaradhi Mannava

The charge thresholds are computed by subtracting a fixed offset from the raw EC value. When no charge limit has been set, the EC can hold a value below range_min (0x80 on an MSI GL65 Leopard 9SCXK), and reading the sysfs attributes returns nonsense:

charge_control_start_threshold: -10
charge_control_end_threshold: 0

Both attributes are documented to be within 0 - 100, and an end threshold of 0 wrongly suggests that charging is disabled.

The store path already rejects values outside range_min..range_max. Apply the same range to the show path and report the maximum threshold for any value outside of it.

Tested on an MSI GL65 Leopard 9SCXK (EC firmware 16U8EMS2.100) with the EC holding 0x80: the attributes now read 90 and 100.

Fixes 392cacf2aa10 ("platform/x86: Add new msi-ec driver")
Assisted-by LLM
---
 drivers/platform/x86/msi-ec.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/platform/x86/msi-ec.c +1 -0
index 566dfc73c..1762551e0 100644
@@ -1279,6 +1279,15 @@ static ssize_t charge_control_threshold_show(u8 offset,
 	if (result < 0)
 		return result;
 
+	/*

drivers/platform/x86/msi-ec.c static ssize_t charge_control_threshold_show(u8 offset, · line 1292

Quoted 10 lines
drivers/platform/x86/msi-ec.c +8 -0
+	 * The EC may hold an out-of-range value (e.g. 0x80) when no charge
+	 * limit has been set. Report the maximum threshold instead of a
+	 * meaningless (possibly negative) percentage.
+	 */
+	if (rdata < conf.charge_control.range_min ||
+	    rdata > conf.charge_control.range_max)
+		rdata = conf.charge_control.range_max;
+
 	return sysfs_emit(buf, "%i\n", rdata - offset);
 }

Hi,

This seems similar to case in commits:

186bf9031666 ("platform/x86: asus-wmi: do not enforce a battery charge threshold") 78bf392ba77d ("platform/x86: asus-wmi: temporarily revert to setting a charge limit")

(The latter reverts first one as userspace could not handle -ENODATA but thought the feature was available.)

So ideally, I'd tend to think we'd want to return -ENODATA when the threshold hasn't been set. But whether userspace survives that is a question we cannot ignore.

Signature · 1 line
 i.
#2 lore raw

[Resending as plain text, the lists rejected my previous reply.]

Quoted 11 lines · Ilpo Järvinen

This seems similar to case in commits:

186bf9031666 ("platform/x86: asus-wmi: do not enforce a battery charge threshold") 78bf392ba77d ("platform/x86: asus-wmi: temporarily revert to setting a charge limit")

(The latter reverts first one as userspace could not handle -ENODATA but thought the feature was available.)

So ideally, I'd tend to think we'd want to return -ENODATA when the threshold hasn't been set. But whether userspace survives that is a question we cannot ignore.

Hi Ilpo,

Thanks for the pointers.

I agree -ENODATA describes the state better. The asus-wmi revert was caused by upower's udev rule reading the attribute to detect support (upower#347). That was fixed in upower 1.91.3 (commit 83f86c75, July 2026), but current stable distros still ship older versions, e.g. Debian 13 has 1.90.9 and Ubuntu 26.04 has 1.91.1. Returning -ENODATA from msi-ec now would hide the GNOME battery health toggle for most MSI users.

Unlike asus-wmi before 186bf9031666, msi-ec never writes the threshold at boot, so this patch only changes what is reported: today it is -10/0, which is outside the documented 0-100 range and makes the limit look set to 0%.

So I'd suggest taking this patch as is for now, and switching both drivers to -ENODATA together once a fixed upower is widely available. If you'd rather have -ENODATA right away, I'm happy to send a v2.

Thanks,
Daasaradhi

maintainer
#3 lore raw
In reply to Daasaradhi Mannava
Quoted 9 lines · Daasaradhi Mannava

The charge thresholds are computed by subtracting a fixed offset from the raw EC value. When no charge limit has been set, the EC can hold a value below range_min (0x80 on an MSI GL65 Leopard 9SCXK), and reading the sysfs attributes returns nonsense:

charge_control_start_threshold: -10
charge_control_end_threshold: 0

[...]

Thank you for your contribution, it has been applied to my local review-ilpo-next branch. Note it will show up in the public platform-drivers-x86/review-ilpo-next branch only once I've pushed my local branch there, which might take a while.

FYI [if applicable to your patch], as per Linus' policy change, also fixes are mostly routed through for-next unless the fix is for a commit introduced in the most recent cycle or is clearly a regression fix.

The list of commits applied:
[1/1] platform/x86: msi-ec: Report valid charge thresholds when unset

      commit: eabc7e36259c813dc337b48b012bbf23b380f509

--
i.