Skip to contentSkip to conversation

platform/x86: lenovo-wmi-other: Deconflict battery with thinkpad_acpi

PATCH Superseded Aaron Ma 3 participants 5 messages Nov 13 – Nov 18

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Aaron Ma

author
#0 lore raw

PATCH v3 1/1 platform/x86: lenovo-wmi-other: Deconflict battery with thinkpad_acpi

ThinkPads expose battery charge thresholds and charge_behaviour through thinkpad_acpi's ACPI battery hook using raw sysfs attribute groups. The Lenovo WMI "Other Mode" driver also registers a power_supply_ext for charge_behaviour and charge_types on systems advertising those WMI capabilities.

When lenovo-wmi-other registers first, thinkpad_acpi fails to create a duplicate charge_behaviour sysfs attribute and unregisters its battery extension. Conversely, registering or updating any power_supply_ext on the same battery runs sysfs_update_groups(), which removes standard power-supply attributes not owned by power_supply_core and wipes out thinkpad_acpi's charge_control_start_threshold, charge_control_end_threshold, and charge_behaviour files.

Deconflict both ideapad_laptop (VPC2004 with GBMD and SBMC) and thinkpad_acpi (LEN0268, LEN0068, or IBM0068 with BDSG and BICG) via a per-driver table guarded by IS_ENABLED() in Kconfig, and update the force_load_psy_ext description accordingly.

Fixes 9ca8fc065b88b ("platform/x86: lenovo-wmi-other: Add WMI battery charge limiting")

---
Changes in v3:
- Wrap deconfliction table entries in #if IS_ENABLED(CONFIG_*) and drop driver_enabled, as suggested by Rong Zhang.
- Group deconfliction entries per driver (HIDs list + methods list).

Changes in v2:
- Consolidate IdeaPad and ThinkPad deconfliction into a table of ACPI device IDs, method pairs, and driver configuration checks, as suggested by Derek J. Clark.
- Check GBMD and SBMC for IdeaPads, and BDSG and BICG for ThinkPads, instead of relying only on ThinkPad ACPI device presence.
- Check whether the corresponding driver is enabled before looking up its ACPI device, including for ideapad_laptop.
- Update the force_load_psy_ext description to cover both drivers.

v2: https://lore.kernel.org/platform-driver-x86/[email protected]/
v1: https://lore.kernel.org/platform-driver-x86/[email protected]/

 drivers/platform/x86/lenovo/wmi-other.c | 66 ++++++++++++++++++++-----
 1 file changed, 53 insertions(+), 13 deletions(-)
drivers/platform/x86/lenovo/wmi-other.c +53 -13
index bd64e7b44669d..340159df5f7cc 100644
@@ -189,10 +189,10 @@ MODULE_PARM_DESC(relax_fan_constraint,
 static bool force_load_psy_ext;
 module_param(force_load_psy_ext, bool, 0444);
 MODULE_PARM_DESC(force_load_psy_ext,
-	"This option will skip checking if the ideapad_laptop driver will conflict "
+	"This option will skip checking if the ideapad_laptop or thinkpad_acpi driver will conflict "
 	"with adding an extension to set the battery charge behavior and battery charge "
 	"control end threshold. It will also skip checking if the BIOS reports that "
-	"those features are fully supported. It is recommended to blacklist the ideapad "
+	"those features are fully supported. It is recommended to blacklist the conflicting "
 	"driver before using this option.");
 
 /* ======== HWMON (component: lenovo-wmi-capdata 00 & fan) ======== */
@@ -905,17 +905,41 @@ static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl,
 	return AE_CTRL_TERMINATE;
 }
 
+struct lwmi_psy_deconflict {
+	char * const *hids;
+	char * const *methods;
+};
+
+#if IS_ENABLED(CONFIG_IDEAPAD_LAPTOP) || IS_ENABLED(CONFIG_THINKPAD_ACPI)
+#define LWMI_PSY_DECONFLICT_STRS(...) \
+	((char * const[]) { __VA_ARGS__, NULL })
+#endif
+
+static const struct lwmi_psy_deconflict lwmi_psy_deconflict_list[] = {
+#if IS_ENABLED(CONFIG_IDEAPAD_LAPTOP)
+	{
+		.hids = LWMI_PSY_DECONFLICT_STRS("VPC2004"),
+		.methods = LWMI_PSY_DECONFLICT_STRS("GBMD", "SBMC"),
+	},
+#endif /* IS_ENABLED(CONFIG_IDEAPAD_LAPTOP) */
+#if IS_ENABLED(CONFIG_THINKPAD_ACPI)
+	{
+		.hids = LWMI_PSY_DECONFLICT_STRS("LEN0268", "LEN0068", "IBM0068"),
+		.methods = LWMI_PSY_DECONFLICT_STRS("BDSG", "BICG"),
+	},
+#endif /* IS_ENABLED(CONFIG_THINKPAD_ACPI) */
+};
+
 /**
  * lwmi_om_psy_ext_init() - Hooks power supply extension to device battery
  * @priv: Pointer to the lwmi_om_priv drvdata.
  *
- * Checks if the ideapad_laptop driver is going to manage charge attributes first,
- * then if not, hooks the battery to our WMI methods if they are supported.
+ * Checks if the ideapad_laptop or thinkpad_acpi driver is going to manage
+ * charge attributes first, then if not, hooks the battery to our WMI methods
+ * if they are supported.
  */
 static void lwmi_om_psy_ext_init(struct lwmi_om_priv *priv)
 {
-	static const char * const ideapad_hid = "VPC2004";
-	acpi_handle handle = NULL;
 	unsigned int props = 0;
 	int ret;
 
@@ -928,14 +952,30 @@ static void lwmi_om_psy_ext_init(struct lwmi_om_priv *priv)
 	if (force_load_psy_ext)
 		goto load_psy_ext;
 
-	/* Deconflict ideapad_laptop driver */
-	ret = acpi_get_devices(ideapad_hid, lwmi_acpi_match, &handle, NULL);
-	if (ret)
-		return;
+	for (unsigned int i = 0; i < ARRAY_SIZE(lwmi_psy_deconflict_list); i++) {
+		const struct lwmi_psy_deconflict *entry = &lwmi_psy_deconflict_list[i];
 
-	if (handle && acpi_has_method(handle, "GBMD") && acpi_has_method(handle, "SBMC")) {
-		dev_dbg(&priv->wdev->dev, "ideapad_laptop driver manages battery for device\n");
-		return;
+		for (unsigned int j = 0; entry->hids[j]; j++) {
+			const char *hid = entry->hids[j];
+			acpi_handle handle = NULL;
+			bool has_methods = true;
+
+			ret = acpi_get_devices(hid, lwmi_acpi_match, &handle, NULL);
+			if (ret || !handle)
+				continue;
+
+			for (unsigned int k = 0; entry->methods[k]; k++) {
+				has_methods = acpi_has_method(handle, entry->methods[k]);
+				if (!has_methods)
+					break;
+			}
+			if (!has_methods)
+				continue;
+
+			dev_dbg(&priv->wdev->dev, "ACPI device %s manages battery for device\n",
+				hid);
+			return;
+		}
 	}
 
 load_psy_ext:
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 64 lines · Aaron Ma

ThinkPads expose battery charge thresholds and charge_behaviour through thinkpad_acpi's ACPI battery hook using raw sysfs attribute groups. The Lenovo WMI "Other Mode" driver also registers a power_supply_ext for charge_behaviour and charge_types on systems advertising those WMI capabilities.

When lenovo-wmi-other registers first, thinkpad_acpi fails to create a duplicate charge_behaviour sysfs attribute and unregisters its battery extension. Conversely, registering or updating any power_supply_ext on the same battery runs sysfs_update_groups(), which removes standard power-supply attributes not owned by power_supply_core and wipes out thinkpad_acpi's charge_control_start_threshold, charge_control_end_threshold, and charge_behaviour files.

Deconflict both ideapad_laptop (VPC2004 with GBMD and SBMC) and thinkpad_acpi (LEN0268, LEN0068, or IBM0068 with BDSG and BICG) via a per-driver table guarded by IS_ENABLED() in Kconfig, and update the force_load_psy_ext description accordingly.

Fixes 9ca8fc065b88b ("platform/x86: lenovo-wmi-other: Add WMI battery charge limiting")

---
Changes in v3:
- Wrap deconfliction table entries in #if IS_ENABLED(CONFIG_*) and drop driver_enabled, as suggested by Rong Zhang.
- Group deconfliction entries per driver (HIDs list + methods list).

Changes in v2:
- Consolidate IdeaPad and ThinkPad deconfliction into a table of ACPI device IDs, method pairs, and driver configuration checks, as suggested by Derek J. Clark.
- Check GBMD and SBMC for IdeaPads, and BDSG and BICG for ThinkPads, instead of relying only on ThinkPad ACPI device presence.
- Check whether the corresponding driver is enabled before looking up its ACPI device, including for ideapad_laptop.
- Update the force_load_psy_ext description to cover both drivers.

v2: https://lore.kernel.org/platform-driver-x86/[email protected]/
v1: https://lore.kernel.org/platform-driver-x86/[email protected]/

 drivers/platform/x86/lenovo/wmi-other.c | 66 ++++++++++++++++++++-----
 1 file changed, 53 insertions(+), 13 deletions(-)
drivers/platform/x86/lenovo/wmi-other.c +2 -2
index bd64e7b44669d..340159df5f7cc 100644
@@ -189,10 +189,10 @@ MODULE_PARM_DESC(relax_fan_constraint,
 static bool force_load_psy_ext;
 module_param(force_load_psy_ext, bool, 0444);
 MODULE_PARM_DESC(force_load_psy_ext,
-	"This option will skip checking if the ideapad_laptop driver will conflict "
+	"This option will skip checking if the ideapad_laptop or thinkpad_acpi driver will conflict "
 	"with adding an extension to set the battery charge behavior and battery charge "
 	"control end threshold. It will also skip checking if the BIOS reports that "
-	"those features are fully supported. It is recommended to blacklist the ideapad "
+	"those features are fully supported. It is recommended to blacklist the conflicting "
 	"driver before using this option.");
 
 /* ======== HWMON (component: lenovo-wmi-capdata 00 & fan) ======== */

drivers/platform/x86/lenovo/wmi-other.c static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl, · line 909

Quoted 5 lines
drivers/platform/x86/lenovo/wmi-other.c +2 -0
@@ -905,17 +905,41 @@ static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl,
 	return AE_CTRL_TERMINATE;
 }
 
+struct lwmi_psy_deconflict {
+	char * const *hids;

Who is only the pointer const, not the char?

drivers/platform/x86/lenovo/wmi-other.c static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl, · line 910

Quoted 1 line
drivers/platform/x86/lenovo/wmi-other.c +1 -0
+	char * const *methods;

If acpi_has_method would input const char * (to me it seems convertable as it only calls acpi_get_handle that takes const char *), this too could be const char.

drivers/platform/x86/lenovo/wmi-other.c static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl, · line 916

Quoted 6 lines
drivers/platform/x86/lenovo/wmi-other.c +6 -0
+};
+
+#if IS_ENABLED(CONFIG_IDEAPAD_LAPTOP) || IS_ENABLED(CONFIG_THINKPAD_ACPI)
+#define LWMI_PSY_DECONFLICT_STRS(...) \
+	((char * const[]) { __VA_ARGS__, NULL })
+#endif

What exactly does ifdef'ing this construct achieve? Does something hunt unused macros?

Quoted 50 lines
drivers/platform/x86/lenovo/wmi-other.c +24 -11
+
+static const struct lwmi_psy_deconflict lwmi_psy_deconflict_list[] = {
+#if IS_ENABLED(CONFIG_IDEAPAD_LAPTOP)
+	{
+		.hids = LWMI_PSY_DECONFLICT_STRS("VPC2004"),
+		.methods = LWMI_PSY_DECONFLICT_STRS("GBMD", "SBMC"),
+	},
+#endif /* IS_ENABLED(CONFIG_IDEAPAD_LAPTOP) */
+#if IS_ENABLED(CONFIG_THINKPAD_ACPI)
+	{
+		.hids = LWMI_PSY_DECONFLICT_STRS("LEN0268", "LEN0068", "IBM0068"),
+		.methods = LWMI_PSY_DECONFLICT_STRS("BDSG", "BICG"),
+	},
+#endif /* IS_ENABLED(CONFIG_THINKPAD_ACPI) */
+};
+
 /**
  * lwmi_om_psy_ext_init() - Hooks power supply extension to device battery
  * @priv: Pointer to the lwmi_om_priv drvdata.
  *
- * Checks if the ideapad_laptop driver is going to manage charge attributes first,
- * then if not, hooks the battery to our WMI methods if they are supported.
+ * Checks if the ideapad_laptop or thinkpad_acpi driver is going to manage
+ * charge attributes first, then if not, hooks the battery to our WMI methods
+ * if they are supported.
  */
 static void lwmi_om_psy_ext_init(struct lwmi_om_priv *priv)
 {
-	static const char * const ideapad_hid = "VPC2004";
-	acpi_handle handle = NULL;
 	unsigned int props = 0;
 	int ret;
 
@@ -928,14 +952,30 @@ static void lwmi_om_psy_ext_init(struct lwmi_om_priv *priv)
 	if (force_load_psy_ext)
 		goto load_psy_ext;
 
-	/* Deconflict ideapad_laptop driver */
-	ret = acpi_get_devices(ideapad_hid, lwmi_acpi_match, &handle, NULL);
-	if (ret)
-		return;
+	for (unsigned int i = 0; i < ARRAY_SIZE(lwmi_psy_deconflict_list); i++) {
+		const struct lwmi_psy_deconflict *entry = &lwmi_psy_deconflict_list[i];
 
-	if (handle && acpi_has_method(handle, "GBMD") && acpi_has_method(handle, "SBMC")) {
-		dev_dbg(&priv->wdev->dev, "ideapad_laptop driver manages battery for device\n");
-		return;
+		for (unsigned int j = 0; entry->hids[j]; j++) {
+			const char *hid = entry->hids[j];
+			acpi_handle handle = NULL;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_psy_ext_init(struct lwmi_om_priv *priv) · line 970

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+			bool has_methods = true;
+
+			ret = acpi_get_devices(hid, lwmi_acpi_match, &handle, NULL);
+			if (ret || !handle)
+				continue;
+
+			for (unsigned int k = 0; entry->methods[k]; k++) {
+				has_methods = acpi_has_method(handle, entry->methods[k]);
+				if (!has_methods)
+					break;

So this goes to dev_dbg + return, why complicate the code flow with extra break + check that is never true?

If indentation is the worry here, the innermost bodies don't seem to require much from the outer ones so they could be moved to a function (j body + k loop).

Quoted 12 lines
drivers/platform/x86/lenovo/wmi-other.c +8 -0
+			}
+			if (!has_methods)
+				continue;
+
+			dev_dbg(&priv->wdev->dev, "ACPI device %s manages battery for device\n",
+				hid);
+			return;
+		}
 	}
 
 load_psy_ext:
Signature · 1 line
 i.
author
#2 lore raw

Hi,

Quoted 70 lines · Ilpo Järvinen
Quoted 67 lines · Aaron Ma

ThinkPads expose battery charge thresholds and charge_behaviour through thinkpad_acpi's ACPI battery hook using raw sysfs attribute groups. The Lenovo WMI "Other Mode" driver also registers a power_supply_ext for charge_behaviour and charge_types on systems advertising those WMI capabilities.

When lenovo-wmi-other registers first, thinkpad_acpi fails to create a duplicate charge_behaviour sysfs attribute and unregisters its battery extension. Conversely, registering or updating any power_supply_ext on the same battery runs sysfs_update_groups(), which removes standard power-supply attributes not owned by power_supply_core and wipes out thinkpad_acpi's charge_control_start_threshold, charge_control_end_threshold, and charge_behaviour files.

Deconflict both ideapad_laptop (VPC2004 with GBMD and SBMC) and thinkpad_acpi (LEN0268, LEN0068, or IBM0068 with BDSG and BICG) via a per-driver table guarded by IS_ENABLED() in Kconfig, and update the force_load_psy_ext description accordingly.

Fixes 9ca8fc065b88b ("platform/x86: lenovo-wmi-other: Add WMI battery charge limiting")

---
Changes in v3:
- Wrap deconfliction table entries in #if IS_ENABLED(CONFIG_*) and drop driver_enabled, as suggested by Rong Zhang.
- Group deconfliction entries per driver (HIDs list + methods list).

Changes in v2:
- Consolidate IdeaPad and ThinkPad deconfliction into a table of ACPI device IDs, method pairs, and driver configuration checks, as suggested by Derek J. Clark.
- Check GBMD and SBMC for IdeaPads, and BDSG and BICG for ThinkPads, instead of relying only on ThinkPad ACPI device presence.
- Check whether the corresponding driver is enabled before looking up its ACPI device, including for ideapad_laptop.
- Update the force_load_psy_ext description to cover both drivers.

v2: https://lore.kernel.org/platform-driver-x86/[email protected]/
v1: https://lore.kernel.org/platform-driver-x86/[email protected]/

 drivers/platform/x86/lenovo/wmi-other.c | 66 ++++++++++++++++++++-----
 1 file changed, 53 insertions(+), 13 deletions(-)
drivers/platform/x86/lenovo/wmi-other.c +2 -2
index bd64e7b44669d..340159df5f7cc 100644
@@ -189,10 +189,10 @@ MODULE_PARM_DESC(relax_fan_constraint,
 static bool force_load_psy_ext;
 module_param(force_load_psy_ext, bool, 0444);
 MODULE_PARM_DESC(force_load_psy_ext,
-     "This option will skip checking if the ideapad_laptop driver will conflict "
+     "This option will skip checking if the ideapad_laptop or thinkpad_acpi driver will conflict "
      "with adding an extension to set the battery charge behavior and battery charge "
      "control end threshold. It will also skip checking if the BIOS reports that "
-     "those features are fully supported. It is recommended to blacklist the ideapad "
+     "those features are fully supported. It is recommended to blacklist the conflicting "
      "driver before using this option.");
 
 /* ======== HWMON (component: lenovo-wmi-capdata 00 & fan) ======== */
@@ -905,17 +905,41 @@ static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl,
      return AE_CTRL_TERMINATE;
 }
 
Quoted 5 lines

drivers/platform/x86/lenovo/wmi-other.c static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl, · line 909

Quoted 2 lines
drivers/platform/x86/lenovo/wmi-other.c +2 -0
+struct lwmi_psy_deconflict {
+     char * const *hids;

Who is only the pointer const, not the char?

It only lacked const because it shared LWMI_PSY_DECONFLICT_STRS() with methods. I made hids const char * const * in v4.

Quoted 6 lines

drivers/platform/x86/lenovo/wmi-other.c static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl, · line 910

Quoted 1 line
drivers/platform/x86/lenovo/wmi-other.c +1 -0
+     char * const *methods;

If acpi_has_method would input const char * (to me it seems convertable as it only calls acpi_get_handle that takes const char *), this too could be const char.

Agreed. acpi_has_method() still takes char *, so I kept methods as char * const * in v4.

Quoted 10 lines

drivers/platform/x86/lenovo/wmi-other.c static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl, · line 916

Quoted 6 lines
drivers/platform/x86/lenovo/wmi-other.c +6 -0
+};
+
+#if IS_ENABLED(CONFIG_IDEAPAD_LAPTOP) || IS_ENABLED(CONFIG_THINKPAD_ACPI)
+#define LWMI_PSY_DECONFLICT_STRS(...) \
+     ((char * const[]) { __VA_ARGS__, NULL })
+#endif

What exactly does ifdef'ing this construct achieve? Does something hunt unused macros?

Yes, W=2 enables -Wunused-macros. The guard avoids the warning when both drivers are disabled.

Quoted 64 lines
Quoted 50 lines
drivers/platform/x86/lenovo/wmi-other.c +24 -11
+
+static const struct lwmi_psy_deconflict lwmi_psy_deconflict_list[] = {
+#if IS_ENABLED(CONFIG_IDEAPAD_LAPTOP)
+     {
+             .hids = LWMI_PSY_DECONFLICT_STRS("VPC2004"),
+             .methods = LWMI_PSY_DECONFLICT_STRS("GBMD", "SBMC"),
+     },
+#endif /* IS_ENABLED(CONFIG_IDEAPAD_LAPTOP) */
+#if IS_ENABLED(CONFIG_THINKPAD_ACPI)
+     {
+             .hids = LWMI_PSY_DECONFLICT_STRS("LEN0268", "LEN0068", "IBM0068"),
+             .methods = LWMI_PSY_DECONFLICT_STRS("BDSG", "BICG"),
+     },
+#endif /* IS_ENABLED(CONFIG_THINKPAD_ACPI) */
+};
+
 /**
  * lwmi_om_psy_ext_init() - Hooks power supply extension to device battery
  * @priv: Pointer to the lwmi_om_priv drvdata.
  *
- * Checks if the ideapad_laptop driver is going to manage charge attributes first,
- * then if not, hooks the battery to our WMI methods if they are supported.
+ * Checks if the ideapad_laptop or thinkpad_acpi driver is going to manage
+ * charge attributes first, then if not, hooks the battery to our WMI methods
+ * if they are supported.
  */
 static void lwmi_om_psy_ext_init(struct lwmi_om_priv *priv)
 {
-     static const char * const ideapad_hid = "VPC2004";
-     acpi_handle handle = NULL;
      unsigned int props = 0;
      int ret;
 
@@ -928,14 +952,30 @@ static void lwmi_om_psy_ext_init(struct lwmi_om_priv *priv)
      if (force_load_psy_ext)
              goto load_psy_ext;
 
-     /* Deconflict ideapad_laptop driver */
-     ret = acpi_get_devices(ideapad_hid, lwmi_acpi_match, &handle, NULL);
-     if (ret)
-             return;
+     for (unsigned int i = 0; i < ARRAY_SIZE(lwmi_psy_deconflict_list); i++) {
+             const struct lwmi_psy_deconflict *entry = &lwmi_psy_deconflict_list[i];
 
-     if (handle && acpi_has_method(handle, "GBMD") && acpi_has_method(handle, "SBMC")) {
-             dev_dbg(&priv->wdev->dev, "ideapad_laptop driver manages battery for device\n");
-             return;
+             for (unsigned int j = 0; entry->hids[j]; j++) {
+                     const char *hid = entry->hids[j];
+                     acpi_handle handle = NULL;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_psy_ext_init(struct lwmi_om_priv *priv) · line 970

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+                     bool has_methods = true;
+
+                     ret = acpi_get_devices(hid, lwmi_acpi_match, &handle, NULL);
+                     if (ret || !handle)
+                             continue;
+
+                     for (unsigned int k = 0; entry->methods[k]; k++) {
+                             has_methods = acpi_has_method(handle, entry->methods[k]);
+                             if (!has_methods)
+                                     break;

So this goes to dev_dbg + return, why complicate the code flow with extra break + check that is never true?

Quoted 4 lines

If indentation is the worry here, the innermost bodies don't seem to require much from the outer ones so they could be moved to a function (j body + k loop).

When missing a method then try the next HID. In v4, I moved the lookup and method checks into a helper.

Thanks,
Aaron

Quoted 16 lines

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_psy_ext_init(struct lwmi_om_priv *priv) · line 981

Quoted 12 lines
drivers/platform/x86/lenovo/wmi-other.c +8 -0
+                     }
+                     if (!has_methods)
+                             continue;
+
+                     dev_dbg(&priv->wdev->dev, "ACPI device %s manages battery for device\n",
+                             hid);
+                     return;
+             }
      }
 
 load_psy_ext:

--
i.

#3 lore raw

Hi Aaron,

Quoted 104 lines · Aaron Ma

Hi,

Quoted 70 lines · Ilpo Järvinen
Quoted 67 lines · Aaron Ma

ThinkPads expose battery charge thresholds and charge_behaviour through thinkpad_acpi's ACPI battery hook using raw sysfs attribute groups. The Lenovo WMI "Other Mode" driver also registers a power_supply_ext for charge_behaviour and charge_types on systems advertising those WMI capabilities.

When lenovo-wmi-other registers first, thinkpad_acpi fails to create a duplicate charge_behaviour sysfs attribute and unregisters its battery extension. Conversely, registering or updating any power_supply_ext on the same battery runs sysfs_update_groups(), which removes standard power-supply attributes not owned by power_supply_core and wipes out thinkpad_acpi's charge_control_start_threshold, charge_control_end_threshold, and charge_behaviour files.

Deconflict both ideapad_laptop (VPC2004 with GBMD and SBMC) and thinkpad_acpi (LEN0268, LEN0068, or IBM0068 with BDSG and BICG) via a per-driver table guarded by IS_ENABLED() in Kconfig, and update the force_load_psy_ext description accordingly.

Fixes 9ca8fc065b88b ("platform/x86: lenovo-wmi-other: Add WMI battery charge limiting")

---
Changes in v3:
- Wrap deconfliction table entries in #if IS_ENABLED(CONFIG_*) and drop driver_enabled, as suggested by Rong Zhang.
- Group deconfliction entries per driver (HIDs list + methods list).

Changes in v2:
- Consolidate IdeaPad and ThinkPad deconfliction into a table of ACPI device IDs, method pairs, and driver configuration checks, as suggested by Derek J. Clark.
- Check GBMD and SBMC for IdeaPads, and BDSG and BICG for ThinkPads, instead of relying only on ThinkPad ACPI device presence.
- Check whether the corresponding driver is enabled before looking up its ACPI device, including for ideapad_laptop.
- Update the force_load_psy_ext description to cover both drivers.

v2: https://lore.kernel.org/platform-driver-x86/[email protected]/
v1: https://lore.kernel.org/platform-driver-x86/[email protected]/

 drivers/platform/x86/lenovo/wmi-other.c | 66 ++++++++++++++++++++-----
 1 file changed, 53 insertions(+), 13 deletions(-)
drivers/platform/x86/lenovo/wmi-other.c +2 -2
index bd64e7b44669d..340159df5f7cc 100644
@@ -189,10 +189,10 @@ MODULE_PARM_DESC(relax_fan_constraint,
 static bool force_load_psy_ext;
 module_param(force_load_psy_ext, bool, 0444);
 MODULE_PARM_DESC(force_load_psy_ext,
-     "This option will skip checking if the ideapad_laptop driver will conflict "
+     "This option will skip checking if the ideapad_laptop or thinkpad_acpi driver will conflict "
      "with adding an extension to set the battery charge behavior and battery charge "
      "control end threshold. It will also skip checking if the BIOS reports that "
-     "those features are fully supported. It is recommended to blacklist the ideapad "
+     "those features are fully supported. It is recommended to blacklist the conflicting "
      "driver before using this option.");
 
 /* ======== HWMON (component: lenovo-wmi-capdata 00 & fan) ======== */
@@ -905,17 +905,41 @@ static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl,
      return AE_CTRL_TERMINATE;
 }
 
Quoted 5 lines

drivers/platform/x86/lenovo/wmi-other.c static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl, · line 909

Quoted 2 lines
drivers/platform/x86/lenovo/wmi-other.c +2 -0
+struct lwmi_psy_deconflict {
+     char * const *hids;

Who is only the pointer const, not the char?

It only lacked const because it shared LWMI_PSY_DECONFLICT_STRS() with methods. I made hids const char * const * in v4.

Quoted 6 lines

drivers/platform/x86/lenovo/wmi-other.c static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl, · line 910

Quoted 1 line
drivers/platform/x86/lenovo/wmi-other.c +1 -0
+     char * const *methods;

If acpi_has_method would input const char * (to me it seems convertable as it only calls acpi_get_handle that takes const char *), this too could be const char.

Agreed. acpi_has_method() still takes char *, so I kept methods as char * const * in v4.

Quoted 10 lines

drivers/platform/x86/lenovo/wmi-other.c static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl, · line 916

Quoted 6 lines
drivers/platform/x86/lenovo/wmi-other.c +6 -0
+};
+
+#if IS_ENABLED(CONFIG_IDEAPAD_LAPTOP) || IS_ENABLED(CONFIG_THINKPAD_ACPI)
+#define LWMI_PSY_DECONFLICT_STRS(...) \
+     ((char * const[]) { __VA_ARGS__, NULL })
+#endif

What exactly does ifdef'ing this construct achieve? Does something hunt unused macros?

Quoted 2 lines

Yes, W=2 enables -Wunused-macros. The guard avoids the warning when both drivers are disabled.

Usually we don't particularly care about W=2, otherwise our lives would be too tough to continue.

W=2/3 only makes sense when you are looking for potential bugs or optimizations. If the code doesn't fall into either category, the warnings can be safely ignored.

Thanks,
Rong

Quoted 92 lines
Quoted 65 lines
Quoted 51 lines
drivers/platform/x86/lenovo/wmi-other.c +24 -11
+
+static const struct lwmi_psy_deconflict lwmi_psy_deconflict_list[] = {
+#if IS_ENABLED(CONFIG_IDEAPAD_LAPTOP)
+     {
+             .hids = LWMI_PSY_DECONFLICT_STRS("VPC2004"),
+             .methods = LWMI_PSY_DECONFLICT_STRS("GBMD", "SBMC"),
+     },
+#endif /* IS_ENABLED(CONFIG_IDEAPAD_LAPTOP) */
+#if IS_ENABLED(CONFIG_THINKPAD_ACPI)
+     {
+             .hids = LWMI_PSY_DECONFLICT_STRS("LEN0268", "LEN0068", "IBM0068"),
+             .methods = LWMI_PSY_DECONFLICT_STRS("BDSG", "BICG"),
+     },
+#endif /* IS_ENABLED(CONFIG_THINKPAD_ACPI) */
+};
+
 /**
  * lwmi_om_psy_ext_init() - Hooks power supply extension to device battery
  * @priv: Pointer to the lwmi_om_priv drvdata.
  *
- * Checks if the ideapad_laptop driver is going to manage charge attributes first,
- * then if not, hooks the battery to our WMI methods if they are supported.
+ * Checks if the ideapad_laptop or thinkpad_acpi driver is going to manage
+ * charge attributes first, then if not, hooks the battery to our WMI methods
+ * if they are supported.
  */
 static void lwmi_om_psy_ext_init(struct lwmi_om_priv *priv)
 {
-     static const char * const ideapad_hid = "VPC2004";
-     acpi_handle handle = NULL;
      unsigned int props = 0;
      int ret;
 
@@ -928,14 +952,30 @@ static void lwmi_om_psy_ext_init(struct lwmi_om_priv *priv)
      if (force_load_psy_ext)
              goto load_psy_ext;
 
-     /* Deconflict ideapad_laptop driver */
-     ret = acpi_get_devices(ideapad_hid, lwmi_acpi_match, &handle, NULL);
-     if (ret)
-             return;
+     for (unsigned int i = 0; i < ARRAY_SIZE(lwmi_psy_deconflict_list); i++) {
+             const struct lwmi_psy_deconflict *entry = &lwmi_psy_deconflict_list[i];
 
-     if (handle && acpi_has_method(handle, "GBMD") && acpi_has_method(handle, "SBMC")) {
-             dev_dbg(&priv->wdev->dev, "ideapad_laptop driver manages battery for device\n");
-             return;
+             for (unsigned int j = 0; entry->hids[j]; j++) {
+                     const char *hid = entry->hids[j];
+                     acpi_handle handle = NULL;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_psy_ext_init(struct lwmi_om_priv *priv) · line 970

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+                     bool has_methods = true;
+
+                     ret = acpi_get_devices(hid, lwmi_acpi_match, &handle, NULL);
+                     if (ret || !handle)
+                             continue;
+
+                     for (unsigned int k = 0; entry->methods[k]; k++) {
+                             has_methods = acpi_has_method(handle, entry->methods[k]);
+                             if (!has_methods)
+                                     break;

So this goes to dev_dbg + return, why complicate the code flow with extra break + check that is never true?

Quoted 4 lines

If indentation is the worry here, the innermost bodies don't seem to require much from the outer ones so they could be moved to a function (j body + k loop).

When missing a method then try the next HID. In v4, I moved the lookup and method checks into a helper.

Thanks,
Aaron

Quoted 16 lines

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_psy_ext_init(struct lwmi_om_priv *priv) · line 981

Quoted 12 lines
drivers/platform/x86/lenovo/wmi-other.c +8 -0
+                     }
+                     if (!has_methods)
+                             continue;
+
+                     dev_dbg(&priv->wdev->dev, "ACPI device %s manages battery for device\n",
+                             hid);
+                     return;
+             }
      }
 
 load_psy_ext:

--
i.

maintainer
#4 lore raw
In reply to Aaron Ma
Quoted 83 lines · Aaron Ma
Quoted 70 lines · Ilpo Järvinen
Quoted 67 lines · Aaron Ma

ThinkPads expose battery charge thresholds and charge_behaviour through thinkpad_acpi's ACPI battery hook using raw sysfs attribute groups. The Lenovo WMI "Other Mode" driver also registers a power_supply_ext for charge_behaviour and charge_types on systems advertising those WMI capabilities.

When lenovo-wmi-other registers first, thinkpad_acpi fails to create a duplicate charge_behaviour sysfs attribute and unregisters its battery extension. Conversely, registering or updating any power_supply_ext on the same battery runs sysfs_update_groups(), which removes standard power-supply attributes not owned by power_supply_core and wipes out thinkpad_acpi's charge_control_start_threshold, charge_control_end_threshold, and charge_behaviour files.

Deconflict both ideapad_laptop (VPC2004 with GBMD and SBMC) and thinkpad_acpi (LEN0268, LEN0068, or IBM0068 with BDSG and BICG) via a per-driver table guarded by IS_ENABLED() in Kconfig, and update the force_load_psy_ext description accordingly.

Fixes 9ca8fc065b88b ("platform/x86: lenovo-wmi-other: Add WMI battery charge limiting")

---
Changes in v3:
- Wrap deconfliction table entries in #if IS_ENABLED(CONFIG_*) and drop driver_enabled, as suggested by Rong Zhang.
- Group deconfliction entries per driver (HIDs list + methods list).

Changes in v2:
- Consolidate IdeaPad and ThinkPad deconfliction into a table of ACPI device IDs, method pairs, and driver configuration checks, as suggested by Derek J. Clark.
- Check GBMD and SBMC for IdeaPads, and BDSG and BICG for ThinkPads, instead of relying only on ThinkPad ACPI device presence.
- Check whether the corresponding driver is enabled before looking up its ACPI device, including for ideapad_laptop.
- Update the force_load_psy_ext description to cover both drivers.

v2: https://lore.kernel.org/platform-driver-x86/[email protected]/
v1: https://lore.kernel.org/platform-driver-x86/[email protected]/

 drivers/platform/x86/lenovo/wmi-other.c | 66 ++++++++++++++++++++-----
 1 file changed, 53 insertions(+), 13 deletions(-)
drivers/platform/x86/lenovo/wmi-other.c +2 -2
index bd64e7b44669d..340159df5f7cc 100644
@@ -189,10 +189,10 @@ MODULE_PARM_DESC(relax_fan_constraint,
 static bool force_load_psy_ext;
 module_param(force_load_psy_ext, bool, 0444);
 MODULE_PARM_DESC(force_load_psy_ext,
-     "This option will skip checking if the ideapad_laptop driver will conflict "
+     "This option will skip checking if the ideapad_laptop or thinkpad_acpi driver will conflict "
      "with adding an extension to set the battery charge behavior and battery charge "
      "control end threshold. It will also skip checking if the BIOS reports that "
-     "those features are fully supported. It is recommended to blacklist the ideapad "
+     "those features are fully supported. It is recommended to blacklist the conflicting "
      "driver before using this option.");
 
 /* ======== HWMON (component: lenovo-wmi-capdata 00 & fan) ======== */
@@ -905,17 +905,41 @@ static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl,
      return AE_CTRL_TERMINATE;
 }
 
Quoted 5 lines

drivers/platform/x86/lenovo/wmi-other.c static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl, · line 909

Quoted 2 lines
drivers/platform/x86/lenovo/wmi-other.c +2 -0
+struct lwmi_psy_deconflict {
+     char * const *hids;

Who is only the pointer const, not the char?

It only lacked const because it shared LWMI_PSY_DECONFLICT_STRS() with methods. I made hids const char * const * in v4.

Quoted 2 lines
Quoted 1 line
drivers/platform/x86/lenovo/wmi-other.c +1 -0
+     char * const *methods;
Quoted 7 lines
Quoted 4 lines

If acpi_has_method would input const char * (to me it seems convertable as it only calls acpi_get_handle that takes const char *), this too could be const char.

Agreed. acpi_has_method() still takes char *, so I kept methods as char * const * in v4.

Well, I was kind of hoping we'd get that conversion as well as a side-product of this.

Quoted 11 lines
Quoted 8 lines

drivers/platform/x86/lenovo/wmi-other.c static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl, · line 916

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+#if IS_ENABLED(CONFIG_IDEAPAD_LAPTOP) || IS_ENABLED(CONFIG_THINKPAD_ACPI)
+#define LWMI_PSY_DECONFLICT_STRS(...) \
+     ((char * const[]) { __VA_ARGS__, NULL })
+#endif

What exactly does ifdef'ing this construct achieve? Does something hunt unused macros?

Yes, W=2 enables -Wunused-macros. The guard avoids the warning when both drivers are disabled.

Okay, the cure (ifdeffery) seem worse than the disease... I guess it can't be helped then. :-/

Quoted 17 lines
Quoted 17 lines

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_psy_ext_init(struct lwmi_om_priv *priv) · line 970

Quoted 13 lines
drivers/platform/x86/lenovo/wmi-other.c +13 -0
+             for (unsigned int j = 0; entry->hids[j]; j++) {
+                     const char *hid = entry->hids[j];
+                     acpi_handle handle = NULL;
+                     bool has_methods = true;
+
+                     ret = acpi_get_devices(hid, lwmi_acpi_match, &handle, NULL);
+                     if (ret || !handle)
+                             continue;
+
+                     for (unsigned int k = 0; entry->methods[k]; k++) {
+                             has_methods = acpi_has_method(handle, entry->methods[k]);
+                             if (!has_methods)
+                                     break;

So this goes to dev_dbg + return, why complicate the code flow with extra break + check that is never true?

Quoted 7 lines
Quoted 4 lines

If indentation is the worry here, the innermost bodies don't seem to require much from the outer ones so they could be moved to a function (j body + k loop).

When missing a method then try the next HID. In v4, I moved the lookup and method checks into a helper.

It looked much better structured in v4, thanks.

Signature · 1 line
 i.