platform/x86: lenovo-wmi-other: Deconflict battery with thinkpad_acpi
Subsystems (2)
PATCH v4 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.
---
Changes in v4:
- Make hids const char * const * with separate HIDs and methods macros, as noted by Ilpo Järvinen.
- Move HID lookup and method checks into lwmi_acpi_device_has_methods(), as suggested by Ilpo Järvinen.
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.
v3: https://lore.kernel.org/platform-driver-x86/[email protected]/
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 | 84 ++++++++++++++++++++----- 1 file changed, 70 insertions(+), 14 deletions(-)
drivers/platform/x86/lenovo/wmi-other.c +70 -14
@@ -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,19 +905,69 @@ static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl,return AE_CTRL_TERMINATE ; } +struct lwmi_psy_deconflict { +const char *const *hids; +char *const *methods; +}; + +#if IS_ENABLED (CONFIG_IDEAPAD_LAPTOP ) ||IS_ENABLED (CONFIG_THINKPAD_ACPI ) +#define LWMI_PSY_DECONFLICT_HIDS (...) \ + ((const char * const[]) { __VA_ARGS__, NULL }) +#define LWMI_PSY_DECONFLICT_METHODS (...) \ + ((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_HIDS ("VPC2004" ), + .methods =LWMI_PSY_DECONFLICT_METHODS ("GBMD" ,"SBMC" ), + }, +#endif /* IS_ENABLED(CONFIG_IDEAPAD_LAPTOP) */ +#if IS_ENABLED(CONFIG_THINKPAD_ACPI) + { + .hids =LWMI_PSY_DECONFLICT_HIDS ("LEN0268" ,"LEN0068" ,"IBM0068" ), + .methods =LWMI_PSY_DECONFLICT_METHODS ("BDSG" ,"BICG" ), + }, +#endif /* IS_ENABLED(CONFIG_THINKPAD_ACPI) */ +}; + +/** +* lwmi_acpi_device_has_methods() - Check if an ACPI device has all methods +* @hid: ACPI hardware ID to match +* @methods: NULL-terminated array of required ACPI method names +* +* Return: true if a matching ACPI device is present and exposes all @methods, +* or false otherwise. +*/ +static bool lwmi_acpi_device_has_methods (const char *hid,char *const *methods) +{ +acpi_handle handle =NULL ; +int ret; + + ret =acpi_get_devices (hid, lwmi_acpi_match, &handle,NULL ); +if (ret || !handle) +return false; + +for (unsigned int i =0 ; methods[i]; i++) { +if (!acpi_has_method (handle, methods[i])) +return false; + } + +return true; +} +/** * 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;if (lwmi_psy_prop_is_supported (priv,POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR )) props |=LWMI_PSY_PROP_BEHAVIOUR ;
@@ -928,14 +978,20 @@ 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]; + +if (!lwmi_acpi_device_has_methods (hid, entry->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
Hi Aaron,
Quoted 71 lines · Aaron Ma
ThinkPads expose battery charge thresholds and
charge_behaviourthrough thinkpad_acpi's ACPI battery hook using raw sysfs attribute groups. The Lenovo WMI "Other Mode" driver also registers apower_supply_extforcharge_behaviourandcharge_typeson systems advertising those WMI capabilities.When lenovo-wmi-other registers first,
thinkpad_acpifails to create a duplicatecharge_behavioursysfs attribute and unregisters its battery extension. Conversely, registering or updating anypower_supply_exton the same battery runssysfs_update_groups(), which removes standard power-supply attributes not owned bypower_supply_coreand wipes out thinkpad_acpi'scharge_control_start_threshold,charge_control_end_threshold, andcharge_behaviourfiles.Deconflict both
ideapad_laptop(VPC2004 with GBMD and SBMC) andthinkpad_acpi(LEN0268, LEN0068, or IBM0068 with BDSG and BICG) via a per-driver table guarded byIS_ENABLED()inKconfig, and update theforce_load_psy_extdescription accordingly.Fixes 9ca8fc065b88b ("platform/x86: lenovo-wmi-other: Add WMI battery charge limiting")Reviewed-by Derek J. Clark <[email protected]>Co-developed-by Rong Zhang <[email protected]>Signed-off-by Rong Zhang <[email protected]>Signed-off-by Aaron Ma <[email protected]>---
Changes in v4:
- Make hids const char * const * with separate HIDs and methods macros, as noted by Ilpo Järvinen.
- Move HID lookup and method checks intolwmi_acpi_device_has_methods(), as suggested by Ilpo Järvinen.Changes in v3:
- Wrap deconfliction table entries in #ifIS_ENABLED(CONFIG_*)and dropdriver_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 forideapad_laptop.
- Update theforce_load_psy_extdescription to cover both drivers.v3: https://lore.kernel.org/platform-driver-x86/[email protected]/
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 | 84 ++++++++++++++++++++----- 1 file changed, 70 insertions(+), 14 deletions(-)drivers/platform/x86/lenovo/wmi-other.c +2
-2@@ -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 913
Quoted 9 lines
drivers/platform/x86/lenovo/wmi-other.c +6
-0@@ -905,19 +905,69 @@ static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl,return AE_CTRL_TERMINATE ; } +struct lwmi_psy_deconflict { +const char *const *hids; +char *const *methods; +}; + +#if IS_ENABLED (CONFIG_IDEAPAD_LAPTOP ) ||IS_ENABLED (CONFIG_THINKPAD_ACPI )
The #if did not exist in my original patch. Please don't increase the maintenance burden simply because of a W=2 warning.
See also my reply to v3.
drivers/platform/x86/lenovo/wmi-other.c static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl, · line 917
Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4
-0+#define LWMI_PSY_DECONFLICT_HIDS (...) \ + ((const char * const[]) { __VA_ARGS__, NULL }) +#define LWMI_PSY_DECONFLICT_METHODS (...) \ + ((char * const[]) { __VA_ARGS__, NULL })
My original patch safely dropped the const qualifier with cast when calling acpi_has_method(). If you really don't want to introduce new cast, please refactor acpi_has_method() instead of abusing the .data section.
Quoted 70 lines
drivers/platform/x86/lenovo/wmi-other.c +46
-9+#endif + +static const struct lwmi_psy_deconflict lwmi_psy_deconflict_list[] = { +#if IS_ENABLED(CONFIG_IDEAPAD_LAPTOP) + { + .hids =LWMI_PSY_DECONFLICT_HIDS ("VPC2004" ), + .methods =LWMI_PSY_DECONFLICT_METHODS ("GBMD" ,"SBMC" ), + }, +#endif /* IS_ENABLED(CONFIG_IDEAPAD_LAPTOP) */ +#if IS_ENABLED(CONFIG_THINKPAD_ACPI) + { + .hids =LWMI_PSY_DECONFLICT_HIDS ("LEN0268" ,"LEN0068" ,"IBM0068" ), + .methods =LWMI_PSY_DECONFLICT_METHODS ("BDSG" ,"BICG" ), + }, +#endif /* IS_ENABLED(CONFIG_THINKPAD_ACPI) */ +}; + +/** +* lwmi_acpi_device_has_methods() - Check if an ACPI device has all methods +* @hid: ACPI hardware ID to match +* @methods: NULL-terminated array of required ACPI method names +* +* Return: true if a matching ACPI device is present and exposes all @methods, +* or false otherwise. +*/ +static bool lwmi_acpi_device_has_methods (const char *hid,char *const *methods) +{ +acpi_handle handle =NULL ; +int ret; + + ret =acpi_get_devices (hid, lwmi_acpi_match, &handle,NULL ); +if (ret || !handle) +return false; + +for (unsigned int i =0 ; methods[i]; i++) { +if (!acpi_has_method (handle, methods[i])) +return false; + } + +return true; +} +/** * 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;if (lwmi_psy_prop_is_supported (priv,POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR )) props |=LWMI_PSY_PROP_BEHAVIOUR ;@@ -928,14 +978,20 @@ 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++) {
drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_psy_ext_init(struct lwmi_om_priv *priv) · line 988
Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +6
-3+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]; + +if (!lwmi_acpi_device_has_methods (hid, entry->methods)) +continue ;
Flip the branch to drop the `continue'.
Thanks,
Rong
Quoted 9 lines
drivers/platform/x86/lenovo/wmi-other.c +6
-0+ +dev_dbg (&priv->wdev->dev, +"ACPI device %s manages battery for device\n" , + hid); +return ; + } } load_psy_ext:
Ilpo Järvinen
maintainerQuoted 171 lines · Rong Zhang
Hi Aaron,
Quoted 71 lines · Aaron Ma
ThinkPads expose battery charge thresholds and
charge_behaviourthrough thinkpad_acpi's ACPI battery hook using raw sysfs attribute groups. The Lenovo WMI "Other Mode" driver also registers apower_supply_extforcharge_behaviourandcharge_typeson systems advertising those WMI capabilities.When lenovo-wmi-other registers first,
thinkpad_acpifails to create a duplicatecharge_behavioursysfs attribute and unregisters its battery extension. Conversely, registering or updating anypower_supply_exton the same battery runssysfs_update_groups(), which removes standard power-supply attributes not owned bypower_supply_coreand wipes out thinkpad_acpi'scharge_control_start_threshold,charge_control_end_threshold, andcharge_behaviourfiles.Deconflict both
ideapad_laptop(VPC2004 with GBMD and SBMC) andthinkpad_acpi(LEN0268, LEN0068, or IBM0068 with BDSG and BICG) via a per-driver table guarded byIS_ENABLED()inKconfig, and update theforce_load_psy_extdescription accordingly.Fixes 9ca8fc065b88b ("platform/x86: lenovo-wmi-other: Add WMI battery charge limiting")Reviewed-by Derek J. Clark <[email protected]>Co-developed-by Rong Zhang <[email protected]>Signed-off-by Rong Zhang <[email protected]>Signed-off-by Aaron Ma <[email protected]>---
Changes in v4:
- Make hids const char * const * with separate HIDs and methods macros, as noted by Ilpo Järvinen.
- Move HID lookup and method checks intolwmi_acpi_device_has_methods(), as suggested by Ilpo Järvinen.Changes in v3:
- Wrap deconfliction table entries in #ifIS_ENABLED(CONFIG_*)and dropdriver_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 forideapad_laptop.
- Update theforce_load_psy_extdescription to cover both drivers.v3: https://lore.kernel.org/platform-driver-x86/[email protected]/
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 | 84 ++++++++++++++++++++----- 1 file changed, 70 insertions(+), 14 deletions(-)drivers/platform/x86/lenovo/wmi-other.c +2
-2@@ -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 913
Quoted 9 lines
drivers/platform/x86/lenovo/wmi-other.c +6
-0@@ -905,19 +905,69 @@ static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl,return AE_CTRL_TERMINATE ; } +struct lwmi_psy_deconflict { +const char *const *hids; +char *const *methods; +}; + +#if IS_ENABLED (CONFIG_IDEAPAD_LAPTOP ) ||IS_ENABLED (CONFIG_THINKPAD_ACPI )The #if did not exist in my original patch. Please don't increase the maintenance burden simply because of a W=2 warning.
See also my reply to v3.
drivers/platform/x86/lenovo/wmi-other.c static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl, · line 917
Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4
-0+#define LWMI_PSY_DECONFLICT_HIDS (...) \ + ((const char * const[]) { __VA_ARGS__, NULL }) +#define LWMI_PSY_DECONFLICT_METHODS (...) \ + ((char * const[]) { __VA_ARGS__, NULL })My original patch safely dropped the const qualifier with cast when calling
acpi_has_method(). If you really don't want to introduce new cast, please refactoracpi_has_method()instead of abusing the .data section.Quoted 72 lines
drivers/platform/x86/lenovo/wmi-other.c +47
-9+#endif + +static const struct lwmi_psy_deconflict lwmi_psy_deconflict_list[] = { +#if IS_ENABLED(CONFIG_IDEAPAD_LAPTOP) + { + .hids =LWMI_PSY_DECONFLICT_HIDS ("VPC2004" ), + .methods =LWMI_PSY_DECONFLICT_METHODS ("GBMD" ,"SBMC" ), + }, +#endif /* IS_ENABLED(CONFIG_IDEAPAD_LAPTOP) */ +#if IS_ENABLED(CONFIG_THINKPAD_ACPI) + { + .hids =LWMI_PSY_DECONFLICT_HIDS ("LEN0268" ,"LEN0068" ,"IBM0068" ), + .methods =LWMI_PSY_DECONFLICT_METHODS ("BDSG" ,"BICG" ), + }, +#endif /* IS_ENABLED(CONFIG_THINKPAD_ACPI) */ +}; + +/** +* lwmi_acpi_device_has_methods() - Check if an ACPI device has all methods +* @hid: ACPI hardware ID to match +* @methods: NULL-terminated array of required ACPI method names +* +* Return: true if a matching ACPI device is present and exposes all @methods, +* or false otherwise. +*/ +static bool lwmi_acpi_device_has_methods (const char *hid,char *const *methods) +{ +acpi_handle handle =NULL ; +int ret; + + ret =acpi_get_devices (hid, lwmi_acpi_match, &handle,NULL ); +if (ret || !handle) +return false; + +for (unsigned int i =0 ; methods[i]; i++) { +if (!acpi_has_method (handle, methods[i])) +return false; + } + +return true; +} +/** * 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;if (lwmi_psy_prop_is_supported (priv,POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR )) props |=LWMI_PSY_PROP_BEHAVIOUR ;@@ -928,14 +978,20 @@ 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];
Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_psy_ext_init(struct lwmi_om_priv *priv) · line 988
Quoted 8 lines
drivers/platform/x86/lenovo/wmi-other.c +5
-3-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]; + +if (!lwmi_acpi_device_has_methods (hid, entry->methods)) +continue ;Flip the branch to drop the `continue'.
No, continue is preferred if there's no work to do with the current entry and we want to look the next entry. It's more obvious (analoguos to handle error early with return) than nesting if construct that would require looking what comes after it.
--
i.
Quoted 14 lines
Thanks,
RongQuoted 9 lines
drivers/platform/x86/lenovo/wmi-other.c +6
-0+ +dev_dbg (&priv->wdev->dev, +"ACPI device %s manages battery for device\n" , + hid); +return ; + } } load_psy_ext:
Hi Ilpo,
Quoted 184 lines · Ilpo Järvinen
Quoted 171 lines · Rong Zhang
Hi Aaron,
Quoted 71 lines · Aaron Ma
ThinkPads expose battery charge thresholds and
charge_behaviourthrough thinkpad_acpi's ACPI battery hook using raw sysfs attribute groups. The Lenovo WMI "Other Mode" driver also registers apower_supply_extforcharge_behaviourandcharge_typeson systems advertising those WMI capabilities.When lenovo-wmi-other registers first,
thinkpad_acpifails to create a duplicatecharge_behavioursysfs attribute and unregisters its battery extension. Conversely, registering or updating anypower_supply_exton the same battery runssysfs_update_groups(), which removes standard power-supply attributes not owned bypower_supply_coreand wipes out thinkpad_acpi'scharge_control_start_threshold,charge_control_end_threshold, andcharge_behaviourfiles.Deconflict both
ideapad_laptop(VPC2004 with GBMD and SBMC) andthinkpad_acpi(LEN0268, LEN0068, or IBM0068 with BDSG and BICG) via a per-driver table guarded byIS_ENABLED()inKconfig, and update theforce_load_psy_extdescription accordingly.Fixes 9ca8fc065b88b ("platform/x86: lenovo-wmi-other: Add WMI battery charge limiting")Reviewed-by Derek J. Clark <[email protected]>Co-developed-by Rong Zhang <[email protected]>Signed-off-by Rong Zhang <[email protected]>Signed-off-by Aaron Ma <[email protected]>---
Changes in v4:
- Make hids const char * const * with separate HIDs and methods macros, as noted by Ilpo Järvinen.
- Move HID lookup and method checks intolwmi_acpi_device_has_methods(), as suggested by Ilpo Järvinen.Changes in v3:
- Wrap deconfliction table entries in #ifIS_ENABLED(CONFIG_*)and dropdriver_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 forideapad_laptop.
- Update theforce_load_psy_extdescription to cover both drivers.v3: https://lore.kernel.org/platform-driver-x86/[email protected]/
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 | 84 ++++++++++++++++++++----- 1 file changed, 70 insertions(+), 14 deletions(-)drivers/platform/x86/lenovo/wmi-other.c +2
-2@@ -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 913
Quoted 9 lines
drivers/platform/x86/lenovo/wmi-other.c +6
-0@@ -905,19 +905,69 @@ static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl,return AE_CTRL_TERMINATE ; } +struct lwmi_psy_deconflict { +const char *const *hids; +char *const *methods; +}; + +#if IS_ENABLED (CONFIG_IDEAPAD_LAPTOP ) ||IS_ENABLED (CONFIG_THINKPAD_ACPI )The #if did not exist in my original patch. Please don't increase the maintenance burden simply because of a W=2 warning.
See also my reply to v3.
drivers/platform/x86/lenovo/wmi-other.c static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl, · line 917
Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4
-0+#define LWMI_PSY_DECONFLICT_HIDS (...) \ + ((const char * const[]) { __VA_ARGS__, NULL }) +#define LWMI_PSY_DECONFLICT_METHODS (...) \ + ((char * const[]) { __VA_ARGS__, NULL })My original patch safely dropped the const qualifier with cast when calling
acpi_has_method(). If you really don't want to introduce new cast, please refactoracpi_has_method()instead of abusing the .data section.Quoted 72 lines
drivers/platform/x86/lenovo/wmi-other.c +47
-9+#endif + +static const struct lwmi_psy_deconflict lwmi_psy_deconflict_list[] = { +#if IS_ENABLED(CONFIG_IDEAPAD_LAPTOP) + { + .hids =LWMI_PSY_DECONFLICT_HIDS ("VPC2004" ), + .methods =LWMI_PSY_DECONFLICT_METHODS ("GBMD" ,"SBMC" ), + }, +#endif /* IS_ENABLED(CONFIG_IDEAPAD_LAPTOP) */ +#if IS_ENABLED(CONFIG_THINKPAD_ACPI) + { + .hids =LWMI_PSY_DECONFLICT_HIDS ("LEN0268" ,"LEN0068" ,"IBM0068" ), + .methods =LWMI_PSY_DECONFLICT_METHODS ("BDSG" ,"BICG" ), + }, +#endif /* IS_ENABLED(CONFIG_THINKPAD_ACPI) */ +}; + +/** +* lwmi_acpi_device_has_methods() - Check if an ACPI device has all methods +* @hid: ACPI hardware ID to match +* @methods: NULL-terminated array of required ACPI method names +* +* Return: true if a matching ACPI device is present and exposes all @methods, +* or false otherwise. +*/ +static bool lwmi_acpi_device_has_methods (const char *hid,char *const *methods) +{ +acpi_handle handle =NULL ; +int ret; + + ret =acpi_get_devices (hid, lwmi_acpi_match, &handle,NULL ); +if (ret || !handle) +return false; + +for (unsigned int i =0 ; methods[i]; i++) { +if (!acpi_has_method (handle, methods[i])) +return false; + } + +return true; +} +/** * 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;if (lwmi_psy_prop_is_supported (priv,POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR )) props |=LWMI_PSY_PROP_BEHAVIOUR ;@@ -928,14 +978,20 @@ 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];Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_psy_ext_init(struct lwmi_om_priv *priv) · line 988
Quoted 8 lines
drivers/platform/x86/lenovo/wmi-other.c +5
-3-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]; + +if (!lwmi_acpi_device_has_methods (hid, entry->methods)) +continue ;Flip the branch to drop the `continue'.
Quoted 4 lines
No, continue is preferred if there's no work to do with the current entry and we want to look the next entry. It's more obvious (analoguos to handle error early with return) than nesting if construct that would require looking what comes after it.
Fine. This also makes sense.
Thanks,
Rong
Quoted 18 lines
--
i.Quoted 14 lines
Thanks,
RongQuoted 9 lines
drivers/platform/x86/lenovo/wmi-other.c +6
-0+ +dev_dbg (&priv->wdev->dev, +"ACPI device %s manages battery for device\n" , + hid); +return ; + } } load_psy_ext: