platform/x86: Add Battery Threshold support
PATCH 1/1 platform/x86: Add Battery Threshold support
Hello,
I aim in this patch to add support to
Battery threshold via SBCT and GBCT
acpi methods from the acpi table
--- drivers/platform/x86/hp/hp-wmi.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
drivers/platform/x86/hp/hp-wmi.c +25 -0
@@ -59,6 +59,8 @@ enum hp_ec_offsets {#define HP_POWER_LIMIT_DEFAULT 0x00#define HP_POWER_LIMIT_NO_CHANGE 0xFF +#define HP_BATTERY_THRESHOLD_CAP 0x37 +#define zero_if_sup (tmp) (zero_insize_support?0:sizeof(tmp)) // use when zero insize is requiredenum hp_thermal_profile_omen_v0 {
@@ -448,6 +450,7 @@ static struct notifier_block platform_power_source_nb;static enum platform_profile_option active_platform_profile;static bool platform_profile_support;static bool zero_insize_support; +static bool battery_threshold_support;static struct rfkill *wifi_rfkill;static struct rfkill *bluetooth_rfkill;
@@ -1130,6 +1133,22 @@ static struct attribute *hp_wmi_attrs[] = { };ATTRIBUTE_GROUPS (hp_wmi); +static int hp_battery_threshold_check (void ) +{ +u8 buffer[128 ] = {0 }; +int ret; + ret =hp_wmi_perform_query (HP_BATTERY_THRESHOLD_CAP ,HPWMI_READ , buffer,128 ,128 ); +if (ret !=0 ) { + battery_threshold_support = false; +return -1 ; + } +if (buffer[28 ] ==0xAA ) { + battery_threshold_support = true; +return 0 ; + } +return false; +} +static void hp_wmi_notify (union acpi_object *obj,void *context) {u32 event_id, event_data;
@@ -2287,6 +2306,12 @@ static int __init hp_wmi_bios_setup(struct platform_device *device)hp_wmi_rfkill2_setup (device); } + err =hp_battery_threshold_check (); + +if (err ==0 ) { +pr_info ("Battery Threshold is supported" ); + } + err =hp_wmi_hwmon_init ();if (err <0 )
Signature · 1 line
2.54.0
Ilpo Järvinen
maintainerOn Sat, 6 Jun 2026, yahia wrote:
Thanks for the patch.
One PATCH is enough on the subject lines. :-)
Quoted 4 lines
Hello,
I aim in this patch to add support to
Battery threshold via SBCT and GBCT
acpi methods from the acpi table
acpi -> ACPI
Please write a proper changelog using imperative tone and real English sentences.
Don't use "I" (or "we") nor "this patch" but start with the verb:
Add support ...
Quoted 2 lines
Signed-off-by yahia <[email protected]>
Please add also your surname before the email address.
For more information, please seeDocumentation/process/submitting-patches.rst
Quoted 26 lines
--- drivers/platform/x86/hp/hp-wmi.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)drivers/platform/x86/hp/hp-wmi.c +3
-0@@ -59,6 +59,8 @@ enum hp_ec_offsets {#define HP_POWER_LIMIT_DEFAULT 0x00#define HP_POWER_LIMIT_NO_CHANGE 0xFF +#define HP_BATTERY_THRESHOLD_CAP 0x37 +#define zero_if_sup (tmp) (zero_insize_support?0:sizeof(tmp)) // use when zero insize is requiredenum hp_thermal_profile_omen_v0 {@@ -448,6 +450,7 @@ static struct notifier_block platform_power_source_nb;static enum platform_profile_option active_platform_profile;static bool platform_profile_support;static bool zero_insize_support; +static bool battery_threshold_support;static struct rfkill *wifi_rfkill;static struct rfkill *bluetooth_rfkill;
drivers/platform/x86/hp/hp-wmi.c static struct attribute *hp_wmi_attrs[] = { · line 1138
Quoted 6 lines
drivers/platform/x86/hp/hp-wmi.c +3
-0@@ -1130,6 +1133,22 @@ static struct attribute *hp_wmi_attrs[] = { };ATTRIBUTE_GROUPS (hp_wmi); +static int hp_battery_threshold_check (void ) +{ +u8 buffer[128 ] = {0 };
= {}; is enough to initialize it.
drivers/platform/x86/hp/hp-wmi.c static struct attribute *hp_wmi_attrs[] = { · line 1139
Quoted 1 line
drivers/platform/x86/hp/hp-wmi.c +1
-0+int ret;
Add an empty line after local vars.
drivers/platform/x86/hp/hp-wmi.c static struct attribute *hp_wmi_attrs[] = { · line 1140
Quoted 1 line
drivers/platform/x86/hp/hp-wmi.c +1
-0+ ret =hp_wmi_perform_query (HP_BATTERY_THRESHOLD_CAP ,HPWMI_READ , buffer,128 ,128 );
sizeof(buffer)
drivers/platform/x86/hp/hp-wmi.c static struct attribute *hp_wmi_attrs[] = { · line 1142
Quoted 2 lines
drivers/platform/x86/hp/hp-wmi.c +2
-0+if (ret !=0 ) { + battery_threshold_support = false;
battery_threshold_support is initialized to false so do you need this?
drivers/platform/x86/hp/hp-wmi.c static struct attribute *hp_wmi_attrs[] = { · line 1143
Quoted 1 line
drivers/platform/x86/hp/hp-wmi.c +1
-0+return -1 ;
You should generally pass the error code onwards. Though the positive returns you should handle too so it would be better to split it into two if()s.
drivers/platform/x86/hp/hp-wmi.c static struct attribute *hp_wmi_attrs[] = { · line 1145
Quoted 2 lines
drivers/platform/x86/hp/hp-wmi.c +2
-0+ } +if (buffer[28 ] ==0xAA ) {
Please name 0xaa with a define.
Maybe 28 as well should be named with a define.
drivers/platform/x86/hp/hp-wmi.c static struct attribute *hp_wmi_attrs[] = { · line 1149
Quoted 4 lines
drivers/platform/x86/hp/hp-wmi.c +4
-0+ battery_threshold_support = true; +return 0 ; + } +return false;
Returning false from int function???
drivers/platform/x86/hp/hp-wmi.c static int __init hp_wmi_bios_setup(struct platform_device *device) · line 2311
Quoted 12 lines
drivers/platform/x86/hp/hp-wmi.c +5
-0+} +static void hp_wmi_notify (union acpi_object *obj,void *context) {u32 event_id, event_data;@@ -2287,6 +2306,12 @@ static int __init hp_wmi_bios_setup(struct platform_device *device)hp_wmi_rfkill2_setup (device); } + err =hp_battery_threshold_check (); + +if (err ==0 ) {
Please don't leave empty lines in between call and it's error handling (but see the next comment below).
drivers/platform/x86/hp/hp-wmi.c static int __init hp_wmi_bios_setup(struct platform_device *device) · line 2312
Quoted 1 line
drivers/platform/x86/hp/hp-wmi.c +1
-0+pr_info ("Battery Threshold is supported" );
Success path should be silent.
After correcting these issues, please send v2.
Quoted 6 lines
drivers/platform/x86/hp/hp-wmi.c +2
-0+ } + err =hp_wmi_hwmon_init ();if (err <0 )
Signature · 1 line
i.
PATCH v2 1/1 hp-wmi: add battery threshold support
Hello, Ilpo
Thanks for your review,
This V2 patch implements Battery threshold support via ACPI methods SBCO and GBCO,This Implementation adds battery_get function to retrieve the current battery threshold mode and use it to identify capabilities to simplify code instead of another function specifically for detection
--- drivers/platform/x86/hp/hp-wmi.c | 61 ++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+)
drivers/platform/x86/hp/hp-wmi.c +60 -0
@@ -11,6 +11,7 @@* Copyright (C) 2005 Dmitry Torokhov <[email protected]> */ #define pr_fmt (fmt) KBUILD_MODNAME ": " fmt#include <linux/acpi.h>
@@ -292,6 +293,18 @@ enum hp_wmi_event_ids { HPWMI_SMART_EXPERIENCE_APP = 0x21, }; +/* +* Profiles for hp battery threshold profile 0x05 and 0x06 map to 4 and 5 respectively +*/ +enum hp_battery_modes { +HP_BATTERY_MODE_NORMAL =0x00 , +HP_BATTERY_MODE_ADAPTIVE =0x02 , +HP_BATTERY_THRESHOLD_4 =0x05 , +HP_BATTERY_THRESHOLD_5 =0x06 , +}; + +#define HPWMI_BATTERY_THRESHOLD_SBCO 0x2B // SBCO Command id +#define HPWMI_AC_REQUIRED 0x35 // obscure edge case where the bios requires the user to be charged while setting a profile/* * struct bios_args buffer is dynamically allocated. New WMI command types * were introduced that exceeds 128-byte data size. Changes to handle
@@ -448,6 +461,7 @@ static struct notifier_block platform_power_source_nb;static enum platform_profile_option active_platform_profile;static bool platform_profile_support;static bool zero_insize_support; +static bool battery_threshold_support;static struct rfkill *wifi_rfkill;static struct rfkill *bluetooth_rfkill;
@@ -1085,6 +1099,50 @@ static ssize_t postcode_store(struct device *dev, struct device_attribute *attr,return count; } +static int battery_get (void ) { +struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER ,NULL }; +int mode =-1 ; +if (ACPI_SUCCESS (acpi_evaluate_object (NULL ,"\\_SB.WMID.GBCO" ,NULL , &output))) { +union acpi_object *obj = (union acpi_object *)output.pointer; +if (obj && obj->type ==ACPI_TYPE_PACKAGE && obj->package.count >=3 ) {// standard package sent by bios + mode = (int )obj->package.elements[2 ].buffer.pointer[0 ];// bios returns the result in the third buffer + } +kfree (output.pointer); + }else + mode = -EIO ; +return mode; +} + +static int battery_set (int profile) +{ +if (!battery_threshold_support) { +pr_err ("Battery threshold is not supported" ); +return -EOPNOTSUPP ; + } +union acpi_object args[4 ]; +acpi_status status; +struct acpi_object_list arg_list = { .count =4 , .pointer = args }; +memset (args,0 ,sizeof (args)); + args[0 ].type =ACPI_TYPE_INTEGER ; + args[0 ].integer.value =0 ; + args[1 ].type =ACPI_TYPE_INTEGER ; + args[1 ].integer.value = profile; + args[2 ].type =ACPI_TYPE_INTEGER ;// padding required by bios + args[2 ].integer.value =0 ; + args[3 ].type =ACPI_TYPE_INTEGER ; + args[3 ].integer.value =0 ; + status =acpi_evaluate_object (NULL ,"\\_SB.WMID.SBCO" , &arg_list,NULL ); +if (unlikely (status ==HPWMI_AC_REQUIRED )) { +pr_err ("You have to be plugged in while setting a battery profile" );// random edge case in the bios +return -EIO ; + } +if (ACPI_FAILURE (status)) { +pr_info ("Write failed with Error code 0x%08x\n" , status); +return -EIO ; + } +return status; +} +static int camera_shutter_input_setup (void ) {int err;
@@ -2292,6 +2350,9 @@ static int __init hp_wmi_bios_setup(struct platform_device *device)if (err <0 )return err; +int ret =battery_get (); +if (ret >=0 ) + battery_threshold_support = true;thermal_profile_setup (device);return 0 ;
Signature · 1 line
2.54.0
Ilpo Järvinen
maintainerRe: [PATCH v2] hp-wmi: add battery threshold support
Quoted 6 lines · yahia
Hello, Ilpo
Thanks for your review,
This V2 patch implements Battery threshold support via ACPI methods SBCO and GBCO,This Implementation addsbattery_getfunction to retrieve the current battery threshold mode and use it to identify capabilities to simplify code instead of another function specifically for detection
As already stated, please write proper English sentences.
Anything you want to say that isn't part of the changelog should appear under --- line.
Quoted 2 lines
Signed-off-by yahia ahmed <[email protected]>---
Missing version history.
Quoted 8 lines
drivers/platform/x86/hp/hp-wmi.c | 61 ++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+)drivers/platform/x86/hp/hp-wmi.c +0
-0
drivers/platform/x86/hp/hp-wmi.c line 16
Quoted 6 lines
drivers/platform/x86/hp/hp-wmi.c +0
-0@@ -11,6 +11,7 @@* Copyright (C) 2005 Dmitry Torokhov <[email protected]> */ #define pr_fmt (fmt) KBUILD_MODNAME ": " fmt#include <linux/acpi.h>
Eh?
drivers/platform/x86/hp/hp-wmi.c enum hp_wmi_event_ids { · line 303
Quoted 12 lines
drivers/platform/x86/hp/hp-wmi.c +8
-0@@ -292,6 +293,18 @@ enum hp_wmi_event_ids { HPWMI_SMART_EXPERIENCE_APP = 0x21, }; +/* +* Profiles for hp battery threshold profile 0x05 and 0x06 map to 4 and 5 respectively +*/ +enum hp_battery_modes { +HP_BATTERY_MODE_NORMAL =0x00 , +HP_BATTERY_MODE_ADAPTIVE =0x02 , +HP_BATTERY_THRESHOLD_4 =0x05 , +HP_BATTERY_THRESHOLD_5 =0x06 ,
The values should be aligned.
drivers/platform/x86/hp/hp-wmi.c enum hp_wmi_event_ids { · line 307
Quoted 4 lines
drivers/platform/x86/hp/hp-wmi.c +4
-0+}; + +#define HPWMI_BATTERY_THRESHOLD_SBCO 0x2B // SBCO Command id +#define HPWMI_AC_REQUIRED 0x35 // obscure edge case where the bios requires the user to be charged while setting a profile
???
Quoted 64 lines
drivers/platform/x86/hp/hp-wmi.c +45
-0/* * struct bios_args buffer is dynamically allocated. New WMI command types * were introduced that exceeds 128-byte data size. Changes to handle @@ -448,6 +461,7 @@ static struct notifier_block platform_power_source_nb;static enum platform_profile_option active_platform_profile;static bool platform_profile_support;static bool zero_insize_support; +static bool battery_threshold_support;static struct rfkill *wifi_rfkill;static struct rfkill *bluetooth_rfkill;@@ -1085,6 +1099,50 @@ static ssize_t postcode_store(struct device *dev, struct device_attribute *attr,return count; } +static int battery_get (void ) { +struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER ,NULL }; +int mode =-1 ; +if (ACPI_SUCCESS (acpi_evaluate_object (NULL ,"\\_SB.WMID.GBCO" ,NULL , &output))) { +union acpi_object *obj = (union acpi_object *)output.pointer; +if (obj && obj->type ==ACPI_TYPE_PACKAGE && obj->package.count >=3 ) {// standard package sent by bios + mode = (int )obj->package.elements[2 ].buffer.pointer[0 ];// bios returns the result in the third buffer + } +kfree (output.pointer); + }else + mode = -EIO ; +return mode; +} + +static int battery_set (int profile) +{ +if (!battery_threshold_support) { +pr_err ("Battery threshold is not supported" ); +return -EOPNOTSUPP ; + } +union acpi_object args[4 ]; +acpi_status status; +struct acpi_object_list arg_list = { .count =4 , .pointer = args }; +memset (args,0 ,sizeof (args)); + args[0 ].type =ACPI_TYPE_INTEGER ; + args[0 ].integer.value =0 ; + args[1 ].type =ACPI_TYPE_INTEGER ; + args[1 ].integer.value = profile; + args[2 ].type =ACPI_TYPE_INTEGER ;// padding required by bios + args[2 ].integer.value =0 ; + args[3 ].type =ACPI_TYPE_INTEGER ; + args[3 ].integer.value =0 ; + status =acpi_evaluate_object (NULL ,"\\_SB.WMID.SBCO" , &arg_list,NULL ); +if (unlikely (status ==HPWMI_AC_REQUIRED )) { +pr_err ("You have to be plugged in while setting a battery profile" );// random edge case in the bios +return -EIO ; + } +if (ACPI_FAILURE (status)) { +pr_info ("Write failed with Error code 0x%08x\n" , status); +return -EIO ; + } +return status; +} +static int camera_shutter_input_setup (void ) {int err;
drivers/platform/x86/hp/hp-wmi.c static int __init hp_wmi_bios_setup(struct platform_device *device) · line 2358
Quoted 9 lines
drivers/platform/x86/hp/hp-wmi.c +3
-0@@ -2292,6 +2350,9 @@ static int __init hp_wmi_bios_setup(struct platform_device *device)if (err <0 )return err; +int ret =battery_get (); +if (ret >=0 ) + battery_threshold_support = true;thermal_profile_setup (device);return 0 ;
Unfortunately, this submission doesn't match kernel coding style guidelines you can find under Documentation/.
Please review the patch yourself before submitting the next version and correct all error you find before the submission.
Signature · 1 line
i.
PATCH v3 1/1 hp-wmi: add battery threshold support
From: yahia ahmed <[email protected]>
Implement battery threshold support using the SBCO and GBCO ACPI methods, Add battery_get to retrieve the current battery mode and provide a function to set the battery threshold.
---
v3:
- Fix coding style errors and warning
- Refine the changelogs for clarity
v2:
- Simplify detection logic via battery_get
- Add Battery Profiles enum table
- Add functions to set the battery mode
--- drivers/platform/x86/hp/hp-wmi.c | 37 ++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 14 deletions(-)
drivers/platform/x86/hp/hp-wmi.c +23 -14
@@ -11,7 +11,6 @@* Copyright (C) 2005 Dmitry Torokhov <[email protected]> */ -#include <asm-generic/errno.h> #define pr_fmt (fmt) KBUILD_MODNAME ": " fmt#include <linux/acpi.h>
@@ -297,15 +296,16 @@ enum hp_wmi_event_ids {* Profiles for hp battery threshold profile 0x05 and 0x06 map to 4 and 5 respectively */ enum hp_battery_modes { -HP_BATTERY_MODE_NORMAL =0x00 , +HP_BATTERY_MODE_NORMAL =0x00 ,HP_BATTERY_MODE_ADAPTIVE =0x02 , -HP_BATTERY_THRESHOLD_4 =0x05 , -HP_BATTERY_THRESHOLD_5 =0x06 , +HP_BATTERY_THRESHOLD_4 =0x05 , +HP_BATTERY_THRESHOLD_5 =0x06 , }; -#define HPWMI_BATTERY_THRESHOLD_SBCO 0x2B // SBCO Command id -#define HPWMI_AC_REQUIRED 0x35 // obscure edge case where the bios requires the user to be charged while setting a profile -/* +#define HPWMI_BATTERY_THRESHOLD_SBCO 0x2B/* SBCO Command id */ +#define HPWMI_AC_REQUIRED 0x35 +/* obscure edge case where the bios requires the user to be charging while setting a profile +* * struct bios_args buffer is dynamically allocated. New WMI command types * were introduced that exceeds 128-byte data size. Changes to handle * the data size allocation scheme were kept in hp_wmi_perform_qurey function.
@@ -1099,21 +1099,26 @@ static ssize_t postcode_store(struct device *dev, struct device_attribute *attr,return count; } -static int battery_get (void ) { +static int battery_get (void ) +{struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER ,NULL };int mode =-1 ; +if (ACPI_SUCCESS (acpi_evaluate_object (NULL ,"\\_SB.WMID.GBCO" ,NULL , &output))) {union acpi_object *obj = (union acpi_object *)output.pointer; -if (obj && obj->type ==ACPI_TYPE_PACKAGE && obj->package.count >=3 ) {// standard package sent by bios - mode = (int )obj->package.elements[2 ].buffer.pointer[0 ];// bios returns the result in the third buffer + +if (obj && obj->type ==ACPI_TYPE_PACKAGE /* standard package sent by bios */ + && obj->package.count >=3 ) { +/* bios returns the result in the third buffer */ + mode = (int )obj->package.elements[2 ].buffer.pointer[0 ]; }kfree (output.pointer); - }else + }else mode = -EIO ;return mode; } -static int battery_set (int profile) +static int battery_set (int profile) {if (!battery_threshold_support) {pr_err ("Battery threshold is not supported" );
@@ -1122,18 +1127,21 @@ static int battery_set(int profile)union acpi_object args[4 ];acpi_status status;struct acpi_object_list arg_list = { .count =4 , .pointer = args }; +memset (args,0 ,sizeof (args)); args[0 ].type =ACPI_TYPE_INTEGER ; args[0 ].integer.value =0 ; args[1 ].type =ACPI_TYPE_INTEGER ; args[1 ].integer.value = profile; - args[2 ].type =ACPI_TYPE_INTEGER ;// padding required by bios + args[2 ].type =ACPI_TYPE_INTEGER ;/* padding required by bios */ args[2 ].integer.value =0 ; args[3 ].type =ACPI_TYPE_INTEGER ; args[3 ].integer.value =0 ; status =acpi_evaluate_object (NULL ,"\\_SB.WMID.SBCO" , &arg_list,NULL ); +if (unlikely (status ==HPWMI_AC_REQUIRED )) { -pr_err ("You have to be plugged in while setting a battery profile" );// random edge case in the bios +/* random edge case in the bios */ +pr_err ("You have to be plugged in while setting a battery profile" );return -EIO ; }if (ACPI_FAILURE (status)) {
@@ -2351,6 +2359,7 @@ static int __init hp_wmi_bios_setup(struct platform_device *device)return err;int ret =battery_get (); +if (ret >=0 ) battery_threshold_support = true;thermal_profile_setup (device);
Signature · 1 line
2.54.0
Ilpo Järvinen
maintainerRe: [PATCH v3] hp-wmi: add battery threshold support
Quoted 112 lines · yahia
From: yahia ahmed <[email protected]>
Implement battery threshold support using the SBCO and GBCO ACPI methods, Add
battery_getto retrieve the current battery mode and provide a function to set the battery threshold.Signed-off-by yahia ahmed <[email protected]>---
v3:
- Fix coding style errors and warning
- Refine the changelogs for clarityv2:
- Simplify detection logic viabattery_get
- Add Battery Profiles enum table
- Add functions to set the battery mode--- drivers/platform/x86/hp/hp-wmi.c | 37 ++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 14 deletions(-)drivers/platform/x86/hp/hp-wmi.c +22
-14@@ -11,7 +11,6 @@* Copyright (C) 2005 Dmitry Torokhov <[email protected]> */ -#include <asm-generic/errno.h> #define pr_fmt (fmt) KBUILD_MODNAME ": " fmt#include <linux/acpi.h> @@ -297,15 +296,16 @@ enum hp_wmi_event_ids {* Profiles for hp battery threshold profile 0x05 and 0x06 map to 4 and 5 respectively */ enum hp_battery_modes { -HP_BATTERY_MODE_NORMAL =0x00 , +HP_BATTERY_MODE_NORMAL =0x00 ,HP_BATTERY_MODE_ADAPTIVE =0x02 , -HP_BATTERY_THRESHOLD_4 =0x05 , -HP_BATTERY_THRESHOLD_5 =0x06 , +HP_BATTERY_THRESHOLD_4 =0x05 , +HP_BATTERY_THRESHOLD_5 =0x06 , }; -#define HPWMI_BATTERY_THRESHOLD_SBCO 0x2B // SBCO Command id -#define HPWMI_AC_REQUIRED 0x35 // obscure edge case where the bios requires the user to be charged while setting a profile -/* +#define HPWMI_BATTERY_THRESHOLD_SBCO 0x2B/* SBCO Command id */ +#define HPWMI_AC_REQUIRED 0x35 +/* obscure edge case where the bios requires the user to be charging while setting a profile +* * struct bios_args buffer is dynamically allocated. New WMI command types * were introduced that exceeds 128-byte data size. Changes to handle * the data size allocation scheme were kept in hp_wmi_perform_qurey function. @@ -1099,21 +1099,26 @@ static ssize_t postcode_store(struct device *dev, struct device_attribute *attr,return count; } -static int battery_get (void ) { +static int battery_get (void ) +{struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER ,NULL };int mode =-1 ; +if (ACPI_SUCCESS (acpi_evaluate_object (NULL ,"\\_SB.WMID.GBCO" ,NULL , &output))) {union acpi_object *obj = (union acpi_object *)output.pointer; -if (obj && obj->type ==ACPI_TYPE_PACKAGE && obj->package.count >=3 ) {// standard package sent by bios - mode = (int )obj->package.elements[2 ].buffer.pointer[0 ];// bios returns the result in the third buffer + +if (obj && obj->type ==ACPI_TYPE_PACKAGE /* standard package sent by bios */ + && obj->package.count >=3 ) { +/* bios returns the result in the third buffer */ + mode = (int )obj->package.elements[2 ].buffer.pointer[0 ]; }kfree (output.pointer); - }else + }else mode = -EIO ;return mode; } -static int battery_set (int profile) +static int battery_set (int profile) {if (!battery_threshold_support) {pr_err ("Battery threshold is not supported" );@@ -1122,18 +1127,21 @@ static int battery_set(int profile)union acpi_object args[4 ];acpi_status status;struct acpi_object_list arg_list = { .count =4 , .pointer = args }; +memset (args,0 ,sizeof (args)); args[0 ].type =ACPI_TYPE_INTEGER ; args[0 ].integer.value =0 ; args[1 ].type =ACPI_TYPE_INTEGER ; args[1 ].integer.value = profile; - args[2 ].type =ACPI_TYPE_INTEGER ;// padding required by bios + args[2 ].type =ACPI_TYPE_INTEGER ;/* padding required by bios */ args[2 ].integer.value =0 ; args[3 ].type =ACPI_TYPE_INTEGER ; args[3 ].integer.value =0 ; status =acpi_evaluate_object (NULL ,"\\_SB.WMID.SBCO" , &arg_list,NULL ); +if (unlikely (status ==HPWMI_AC_REQUIRED )) { -pr_err ("You have to be plugged in while setting a battery profile" );// random edge case in the bios +/* random edge case in the bios */ +pr_err ("You have to be plugged in while setting a battery profile" );return -EIO ; }if (ACPI_FAILURE (status)) {
drivers/platform/x86/hp/hp-wmi.c static int __init hp_wmi_bios_setup(struct platform_device *device) · line 2365
Quoted 7 lines
drivers/platform/x86/hp/hp-wmi.c +1
-0@@ -2351,6 +2359,7 @@ static int __init hp_wmi_bios_setup(struct platform_device *device)return err;int ret =battery_get (); +if (ret >=0 ) battery_threshold_support = true;thermal_profile_setup (device);
This patch wouldn't even apply.
Also, I'm very skeptical you reviewed it yourself before sending it as some of the changes are direct opposite of my earlier review comments (irrespective of whether the patch would apply or not).
Signature · 1 line
i.
PATCH 1/1 hp-wmi: add battery threshold support
From: yahia ahmed <[email protected]>
Hello, Ilpo
Rebased the code to the for-next branch and Removed unused definitions and changed the style to match your earlier comments, Also wanted to query whether this feature should be accessible via a sysfs interface or by a parameter
---
v4:
- Remove redundant definitions
- Rebase the code
v3:
- Fix coding style errors and warning
- Refine the changelogs for clarity
v2:
- Simplify detection logic via battery_get
- Add Battery Profiles enum table
- Add functions to set the battery mode
--- drivers/platform/x86/hp/hp-wmi.c | 70 ++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+)
drivers/platform/x86/hp/hp-wmi.c +70 -0
@@ -292,6 +292,17 @@ enum hp_wmi_event_ids { HPWMI_SMART_EXPERIENCE_APP = 0x21, }; +/* +* Profiles for hp battery threshold profile 0x05 and 0x06 map to 4 and 5 respectively +*/ +enum hp_battery_modes { +HP_BATTERY_MODE_NORMAL =0x00 , +HP_BATTERY_MODE_ADAPTIVE =0x02 , +HP_BATTERY_THRESHOLD_80 =0x05 , +HP_BATTERY_THRESHOLD_50 =0x06 , +}; + +#define HPWMI_AC_REQUIRED 0x35/* * struct bios_args buffer is dynamically allocated. New WMI command types * were introduced that exceeds 128-byte data size. Changes to handle
@@ -448,6 +459,7 @@ static struct notifier_block platform_power_source_nb;static enum platform_profile_option active_platform_profile;static bool platform_profile_support;static bool zero_insize_support; +static bool battery_threshold_support;static struct rfkill *wifi_rfkill;static struct rfkill *bluetooth_rfkill;
@@ -1085,6 +1097,60 @@ static ssize_t postcode_store(struct device *dev, struct device_attribute *attr,return count; } +static int battery_get (void ) +{ +struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER ,NULL }; +int mode =-1 ; + +if (ACPI_SUCCESS (acpi_evaluate_object (NULL ,"\\_SB.WMID.GBCO" ,NULL , &output))) { +union acpi_object *obj = (union acpi_object *)output.pointer; + +if (obj && obj->type ==ACPI_TYPE_PACKAGE &&/* standard package sent by bios */ + obj->package.count >=3 ) { +/* bios returns the result in the third buffer */ + mode = (int )obj->package.elements[2 ].buffer.pointer[0 ]; + } +kfree (output.pointer); + }else { + mode = -EIO ; + } +return mode; +} + +static int battery_set (int profile) +{ +union acpi_object args[4 ]; +acpi_status status; +struct acpi_object_list arg_list = { .count =4 , .pointer = args }; + +if (!battery_threshold_support) { +pr_err ("Battery threshold is not supported" ); +return -EOPNOTSUPP ; + } + +memset (args,0 ,sizeof (args)); + args[0 ].type =ACPI_TYPE_INTEGER ; + args[0 ].integer.value =0 ; + args[1 ].type =ACPI_TYPE_INTEGER ; + args[1 ].integer.value = profile; + args[2 ].type =ACPI_TYPE_INTEGER ;/* padding required by bios */ + args[2 ].integer.value =0 ; + args[3 ].type =ACPI_TYPE_INTEGER ; + args[3 ].integer.value =0 ; + status =acpi_evaluate_object (NULL ,"\\_SB.WMID.SBCO" , &arg_list,NULL ); + +if (unlikely (status ==HPWMI_AC_REQUIRED )) { +/* edge case in the bios */ +pr_err ("You have to be plugged in while setting a battery profile" ); +return -EIO ; + } +if (ACPI_FAILURE (status)) { +pr_info ("Write failed with Error code 0x%08x\n" , status); +return -EIO ; + } +return status; +} +static int camera_shutter_input_setup (void ) {int err;
@@ -2292,6 +2358,10 @@ static int __init hp_wmi_bios_setup(struct platform_device *device)if (err <0 )return err; +int ret =battery_get (); + +if (ret >=0 ) + battery_threshold_support = true;thermal_profile_setup (device);return 0 ;
Signature · 1 line
2.54.0
Ilpo Järvinen
maintainerRe: [PATCH] hp-wmi: add battery threshold support
Quoted 58 lines · yahia
From: yahia ahmed <[email protected]>
Hello, Ilpo
Rebased the code to the for-next branch and Removed unused definitions and changed the style to match your earlier comments, Also wanted to query whether this feature should be accessible via a sysfs interface or by a parameter
Signed-off-by yahia ahmed <[email protected]>---
v4:
- Remove redundant definitions
- Rebase the code
v3:
- Fix coding style errors and warning
- Refine the changelogs for clarityv2:
- Simplify detection logic viabattery_get
- Add Battery Profiles enum table
- Add functions to set the battery mode--- drivers/platform/x86/hp/hp-wmi.c | 70 ++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+)drivers/platform/x86/hp/hp-wmi.c +12
-0@@ -292,6 +292,17 @@ enum hp_wmi_event_ids { HPWMI_SMART_EXPERIENCE_APP = 0x21, }; +/* +* Profiles for hp battery threshold profile 0x05 and 0x06 map to 4 and 5 respectively +*/ +enum hp_battery_modes { +HP_BATTERY_MODE_NORMAL =0x00 , +HP_BATTERY_MODE_ADAPTIVE =0x02 , +HP_BATTERY_THRESHOLD_80 =0x05 , +HP_BATTERY_THRESHOLD_50 =0x06 , +}; + +#define HPWMI_AC_REQUIRED 0x35/* * struct bios_args buffer is dynamically allocated. New WMI command types * were introduced that exceeds 128-byte data size. Changes to handle @@ -448,6 +459,7 @@ static struct notifier_block platform_power_source_nb;static enum platform_profile_option active_platform_profile;static bool platform_profile_support;static bool zero_insize_support; +static bool battery_threshold_support;static struct rfkill *wifi_rfkill;static struct rfkill *bluetooth_rfkill;
drivers/platform/x86/hp/hp-wmi.c static ssize_t postcode_store(struct device *dev, struct device_attribute *attr, · line 1103
Quoted 7 lines
drivers/platform/x86/hp/hp-wmi.c +4
-0@@ -1085,6 +1097,60 @@ static ssize_t postcode_store(struct device *dev, struct device_attribute *attr,return count; } +static int battery_get (void ) +{ +struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER ,NULL }; +int mode =-1 ;
No need to initialize this after changes I suggested below.
drivers/platform/x86/hp/hp-wmi.c static ssize_t postcode_store(struct device *dev, struct device_attribute *attr, · line 1105
Quoted 2 lines
drivers/platform/x86/hp/hp-wmi.c +2
-0+ +if (ACPI_SUCCESS (acpi_evaluate_object (NULL ,"\\_SB.WMID.GBCO" ,NULL , &output))) {
Reverse the logic and return error immediately on failure.
drivers/platform/x86/hp/hp-wmi.c static ssize_t postcode_store(struct device *dev, struct device_attribute *attr, · line 1106
Quoted 1 line
drivers/platform/x86/hp/hp-wmi.c +1
-0+union acpi_object *obj = (union acpi_object *)output.pointer;
Please use __free().
drivers/platform/x86/hp/hp-wmi.c static ssize_t postcode_store(struct device *dev, struct device_attribute *attr, · line 1108
Quoted 2 lines
drivers/platform/x86/hp/hp-wmi.c +2
-0+ + if (obj && obj->type == ACPI_TYPE_PACKAGE && /* standard package sent by bios */
And how does that comment help the code reader to understand the code better? Please drop it.
drivers/platform/x86/hp/hp-wmi.c static ssize_t postcode_store(struct device *dev, struct device_attribute *attr, · line 1109
Quoted 1 line
drivers/platform/x86/hp/hp-wmi.c +1
-0+ obj->package.count >= 3) {
This is misaligned.
Also here, reverse logic so you can return error immediately.
drivers/platform/x86/hp/hp-wmi.c static ssize_t postcode_store(struct device *dev, struct device_attribute *attr, · line 1110
Quoted 1 line
drivers/platform/x86/hp/hp-wmi.c +1
-0+/* bios returns the result in the third buffer */
That's what the code tells us as well, no need to such comments that add no value over what can be easily read from the code itself.
Usually, only comment tricky and unexpected things and rely on developers being aware of trivialities like this.
(Another thing that often needs comments is locking.)
Quoted 28 lines
drivers/platform/x86/hp/hp-wmi.c +28
-0+ mode = (int )obj->package.elements[2 ].buffer.pointer[0 ]; + } +kfree (output.pointer); + }else { + mode = -EIO ; + } +return mode; +} + +static int battery_set (int profile) +{ +union acpi_object args[4 ]; +acpi_status status; +struct acpi_object_list arg_list = { .count =4 , .pointer = args }; + +if (!battery_threshold_support) { +pr_err ("Battery threshold is not supported" ); +return -EOPNOTSUPP ; + } + +memset (args,0 ,sizeof (args)); + args[0 ].type =ACPI_TYPE_INTEGER ; + args[0 ].integer.value =0 ; + args[1 ].type =ACPI_TYPE_INTEGER ; + args[1 ].integer.value = profile; + args[2 ].type =ACPI_TYPE_INTEGER ;/* padding required by bios */ + args[2 ].integer.value =0 ; + args[3 ].type =ACPI_TYPE_INTEGER ;
drivers/platform/x86/hp/hp-wmi.c static ssize_t postcode_store(struct device *dev, struct device_attribute *attr, · line 1148
Quoted 10 lines
drivers/platform/x86/hp/hp-wmi.c +10
-0+ args[3 ].integer.value =0 ; + status =acpi_evaluate_object (NULL ,"\\_SB.WMID.SBCO" , &arg_list,NULL ); + +if (unlikely (status ==HPWMI_AC_REQUIRED )) { +/* edge case in the bios */ +pr_err ("You have to be plugged in while setting a battery profile" ); +return -EIO ; + } +if (ACPI_FAILURE (status)) { +pr_info ("Write failed with Error code 0x%08x\n" , status);
This won't be helpful for a normnal user, either lower to debug level or drop.
drivers/platform/x86/hp/hp-wmi.c static ssize_t postcode_store(struct device *dev, struct device_attribute *attr, · line 1150
Quoted 2 lines
drivers/platform/x86/hp/hp-wmi.c +2
-0+return -EIO ; + }
Put this error handling first and don't leave an empty line between call and its error handling.
drivers/platform/x86/hp/hp-wmi.c static int __init hp_wmi_bios_setup(struct platform_device *device) · line 2361
Quoted 11 lines
drivers/platform/x86/hp/hp-wmi.c +4
-0+return status; +} +static int camera_shutter_input_setup (void ) {int err;@@ -2292,6 +2358,10 @@ static int __init hp_wmi_bios_setup(struct platform_device *device)if (err <0 )return err; +int ret =battery_get ();
You're not allowed to declare variables mid-function/block unless it is for using __free() (for which the reasonale is explained in cleanup.h).
There seems already be err variable so I'll not accept adding "ret" alongside it.
I personally refer using "ret" because it doesn't carry connotation about "error" "err" does but if you want to use "ret" instead, a separate rename variable patch is required and the naming should be relatively uniform within a driver.
drivers/platform/x86/hp/hp-wmi.c static int __init hp_wmi_bios_setup(struct platform_device *device) · line 2362
Quoted 1 line
drivers/platform/x86/hp/hp-wmi.c +1
-0+
Remove this blank line.
Quoted 6 lines
drivers/platform/x86/hp/hp-wmi.c +2
-0+if (ret >=0 ) + battery_threshold_support = true;thermal_profile_setup (device);return 0 ;
Signature · 1 line
i.
PATCH v5 1/1 hp-wmi: add battery threshold support
From: yahia ahmed <[email protected]>
Introduce battery threshold support via SBCO and GBCO WMI methods, add battery_get function to retrieve the current battery mode and to test compatibility, add battery_set function to set a battery mode with any value between 0 and 5, anything larger creates a crash or creates a softbrick of the laptop that requires AC power to boot.
---
v5:
- Use hp_wmi_perform_query for battery_set
- Remove mode 6 as it requires AC power to boot
- Fix styling errors and remove redundant comments v4:
- Remove redundant definitions
- Rebase the code
v3:
- Fix coding style errors and warning
- Refine the changelogs for clarity
v2:
- Simplify detection logic via battery_get
- Add Battery Profiles enum table
- Add functions to set the battery mode
--- drivers/platform/x86/hp/hp-wmi.c | 50 +++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-)
drivers/platform/x86/hp/hp-wmi.c +49 -1
@@ -302,8 +302,16 @@ enum hp_wmi_event_ids {HPWMI_FN_P_HOTKEY =0x1B ,HPWMI_OMEN_KEY =0x1D ,HPWMI_SMART_EXPERIENCE_APP =0x21 , + HPWMI_BATTERY_CONTROL = 0x2b, }; +enum hp_battery_modes { +HP_BATTERY_MODE_NORMAL =0x00 , +HP_BATTERY_MODE_ADAPTIVE =0x02 , +HP_BATTERY_THRESHOLD_80 =0x05 , +}; + +#define HPWMI_AC_REQUIRED 0x35/* * struct bios_args buffer is dynamically allocated. New WMI command types * were introduced that exceeds 128-byte data size. Changes to handle
@@ -460,6 +468,7 @@ static struct notifier_block platform_power_source_nb;static enum platform_profile_option active_platform_profile;static bool platform_profile_support;static bool zero_insize_support; +static bool battery_threshold_support;static struct rfkill *wifi_rfkill;static struct rfkill *bluetooth_rfkill;
@@ -1097,6 +1106,43 @@ static ssize_t postcode_store(struct device *dev, struct device_attribute *attr,return count; } +static int battery_get (void ) +{ +struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER ,NULL }; +int mode; + +if (ACPI_FAILURE (acpi_evaluate_object (NULL ,"\\_SB.WMID.GBCO" ,NULL , &output))) +return -EIO ; + union acpi_object *obj __free(kfree) = (union acpi_object *)output.pointer; + +if (!obj || obj->type !=ACPI_TYPE_PACKAGE || obj->package.count <3 ) +return -EIO ; + mode = (int )obj->package.elements[2 ].buffer.pointer[0 ]; +return mode; +} + +static int battery_set (u8 profile) +{ +u8 data[4 ]; +int status; + +if (!battery_threshold_support) { +pr_err ("Battery threshold is not supported" ); +return -EOPNOTSUPP ; + } +memset (&data,0 ,sizeof (data)); + data[1 ] = profile; + status =hp_wmi_perform_query (HPWMI_BATTERY_CONTROL ,HPWMI_WRITE , &data,sizeof (data),0 ); + +if (status <0 ) +return -EIO ; +if (status ==HPWMI_AC_REQUIRED ) { +pr_err ("You have to be plugged in to set a profile" ); +return -EIO ; + } +return status; +} +static int camera_shutter_input_setup (void ) {int err;
@@ -2303,7 +2349,9 @@ static int __init hp_wmi_bios_setup(struct platform_device *device)if (err <0 )return err; - + err =battery_get (); +if (err >=0 ) + battery_threshold_support = true;thermal_profile_setup (device);return 0 ;
Signature · 1 line
2.55.0
Ilpo Järvinen
maintainerRe: [PATCH v5] hp-wmi: add battery threshold support
Quoted 4 lines · yahia
From: yahia ahmed <[email protected]>
Introduce battery threshold support via SBCO and GBCO WMI methods, add
battery_getfunction to retrieve
All function names in changelog text should include ().
Quoted 5 lines
the current battery mode and to test compatibility, add
battery_setfunction to set a battery mode with any value between 0 and 5, anything larger creates a crash or creates a softbrick of the laptop that requires AC power to boot.
Please reflow to 72 chars.
Quoted 56 lines
Signed-off-by yahia ahmed <[email protected]>---
v5:
- Usehp_wmi_perform_queryforbattery_set
- Remove mode 6 as it requires AC power to boot
- Fix styling errors and remove redundant comments v4:
- Remove redundant definitions
- Rebase the code
v3:
- Fix coding style errors and warning
- Refine the changelogs for clarityv2:
- Simplify detection logic viabattery_get
- Add Battery Profiles enum table
- Add functions to set the battery mode--- drivers/platform/x86/hp/hp-wmi.c | 50 +++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-)drivers/platform/x86/hp/hp-wmi.c +10
-0@@ -302,8 +302,16 @@ enum hp_wmi_event_ids {HPWMI_FN_P_HOTKEY =0x1B ,HPWMI_OMEN_KEY =0x1D ,HPWMI_SMART_EXPERIENCE_APP =0x21 , + HPWMI_BATTERY_CONTROL = 0x2b, }; +enum hp_battery_modes { +HP_BATTERY_MODE_NORMAL =0x00 , +HP_BATTERY_MODE_ADAPTIVE =0x02 , +HP_BATTERY_THRESHOLD_80 =0x05 , +}; + +#define HPWMI_AC_REQUIRED 0x35/* * struct bios_args buffer is dynamically allocated. New WMI command types * were introduced that exceeds 128-byte data size. Changes to handle @@ -460,6 +468,7 @@ static struct notifier_block platform_power_source_nb;static enum platform_profile_option active_platform_profile;static bool platform_profile_support;static bool zero_insize_support; +static bool battery_threshold_support;static struct rfkill *wifi_rfkill;static struct rfkill *bluetooth_rfkill;@@ -1097,6 +1106,43 @@ static ssize_t postcode_store(struct device *dev, struct device_attribute *attr,return count; } +static int battery_get (void )
drivers/platform/x86/hp/hp-wmi.c static ssize_t postcode_store(struct device *dev, struct device_attribute *attr, · line 1119
Quoted 10 lines
drivers/platform/x86/hp/hp-wmi.c +10
-0+{ +struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER ,NULL }; +int mode; + +if (ACPI_FAILURE (acpi_evaluate_object (NULL ,"\\_SB.WMID.GBCO" ,NULL , &output))) +return -EIO ; + union acpi_object *obj __free(kfree) = (union acpi_object *)output.pointer; + +if (!obj || obj->type !=ACPI_TYPE_PACKAGE || obj->package.count <3 ) +return -EIO ;
The logic flow is now better but the blank line is in wrong place. The !obj etc check should immediately follow the assignment as it's kind of "error check" for it.
I suggest placing blank line instead before obj declaration.
drivers/platform/x86/hp/hp-wmi.c static ssize_t postcode_store(struct device *dev, struct device_attribute *attr, · line 1124
Quoted 5 lines
drivers/platform/x86/hp/hp-wmi.c +5
-0+ mode = (int )obj->package.elements[2 ].buffer.pointer[0 ]; +return mode; +} + +static int battery_set (u8 profile)
There are no callers for this function???
This patch also in general has the feel it is missing something.
drivers/platform/x86/hp/hp-wmi.c static ssize_t postcode_store(struct device *dev, struct device_attribute *attr, · line 1130
Quoted 6 lines
drivers/platform/x86/hp/hp-wmi.c +6
-0+{ +u8 data[4 ]; +int status; + +if (!battery_threshold_support) { +pr_err ("Battery threshold is not supported" );
This lacks \n.
BUT, what info does this offer beyond the function already returning -EOPNOTSUPP which alone tells very much the same thing? I suggest dropping it.
drivers/platform/x86/hp/hp-wmi.c static ssize_t postcode_store(struct device *dev, struct device_attribute *attr, · line 1134
Quoted 4 lines
drivers/platform/x86/hp/hp-wmi.c +4
-0+return -EOPNOTSUPP ; + } +memset (&data,0 ,sizeof (data)); + data[1 ] = profile;
Nothing checked the value so using this function can result in crash/softbrick (as per your changelog text)??
drivers/platform/x86/hp/hp-wmi.c static ssize_t postcode_store(struct device *dev, struct device_attribute *attr, · line 1137
Quoted 3 lines
drivers/platform/x86/hp/hp-wmi.c +3
-0+ status =hp_wmi_perform_query (HPWMI_BATTERY_CONTROL ,HPWMI_WRITE , &data,sizeof (data),0 ); + +if (status <0 )
There shouldn't be blank line between call and its error handling.
drivers/platform/x86/hp/hp-wmi.c static ssize_t postcode_store(struct device *dev, struct device_attribute *attr, · line 1140
Quoted 3 lines
drivers/platform/x86/hp/hp-wmi.c +3
-0+return -EIO ; +if (status ==HPWMI_AC_REQUIRED ) { +pr_err ("You have to be plugged in to set a profile" );
\n missing here as well.
Quoted 20 lines
drivers/platform/x86/hp/hp-wmi.c +8
-1+return -EIO ; + } +return status; +} +static int camera_shutter_input_setup (void ) {int err;@@ -2303,7 +2349,9 @@ static int __init hp_wmi_bios_setup(struct platform_device *device)if (err <0 )return err; - + err =battery_get (); +if (err >=0 ) + battery_threshold_support = true;thermal_profile_setup (device);return 0 ;
Signature · 1 line
i.
PATCH v6 1/1 hp-wmi: add battery threshold support
From: yahia ahmed <[email protected]>
Introduce battery threshold support via SBCO and GBCO WMI methods, add battery_get() function to retrieve the current battery mode and to test compatibility, add battery_set() function to set a battery mode with any value between 0 and 5, Expose this feature to userspace via sysfs.
---
Note on behaviour:
The mode wont persist past shutdown if not plugged because the EC clears its registers to save power, mode 2 is adaptive, meaning that the EC will set its desired percentage between 80 and 95% mode 5 is roughly around 78 to 80% and if a mode was set and the current battery percentage is higher the EC will drain the battery.
v6:
- Add sysfs interface
- Remove redundant enums
- Add safety checks
- Fix style errors
v5:
- Use hp_wmi_perform_query for battery_set
- Remove mode 6 as it requires AC power to boot
- Fix styling errors and remove redundant comments v4:
- Remove redundant definitions
- Rebase the code
v3:
- Fix coding style errors and warning
- Refine the changelogs for clarity
v2:
- Simplify detection logic via battery_get
- Add Battery Profiles enum table
- Add functions to set the battery mode
--- drivers/platform/x86/hp/hp-wmi.c | 75 +++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-)
drivers/platform/x86/hp/hp-wmi.c +74 -1
@@ -302,8 +302,10 @@ enum hp_wmi_event_ids {HPWMI_FN_P_HOTKEY =0x1B ,HPWMI_OMEN_KEY =0x1D ,HPWMI_SMART_EXPERIENCE_APP =0x21 , + HPWMI_BATTERY_CONTROL = 0x2b, }; +#define HPWMI_AC_REQUIRED 0x35/* * struct bios_args buffer is dynamically allocated. New WMI command types * were introduced that exceeds 128-byte data size. Changes to handle
@@ -460,6 +462,7 @@ static struct notifier_block platform_power_source_nb;static enum platform_profile_option active_platform_profile;static bool platform_profile_support;static bool zero_insize_support; +static bool battery_threshold_support;static struct rfkill *wifi_rfkill;static struct rfkill *bluetooth_rfkill;
@@ -1097,6 +1100,67 @@ static ssize_t postcode_store(struct device *dev, struct device_attribute *attr,return count; } +static int battery_get (void ) +{ +struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER ,NULL }; +int mode; + +if (ACPI_FAILURE (acpi_evaluate_object (NULL ,"\\_SB.WMID.GBCO" ,NULL , &output))) +return -EIO ; + + union acpi_object *obj __free(kfree) = (union acpi_object *)output.pointer; + +if (!obj || obj->type !=ACPI_TYPE_PACKAGE || obj->package.count <3 + || obj->package.elements[2 ].type !=ACPI_TYPE_BUFFER ) +return -EIO ; + mode = (int )obj->package.elements[2 ].buffer.pointer[0 ]; +return mode; +} + +static int battery_set (u8 profile) +{ +u8 data[4 ]; +int status; +if (!battery_threshold_support) +return -EOPNOTSUPP ; +if (profile >5 ) +return -EINVAL ; +memset (&data,0 ,sizeof (data)); + data[1 ] = profile; + status =hp_wmi_perform_query (HPWMI_BATTERY_CONTROL ,HPWMI_WRITE , &data,sizeof (data),0 ); +if (status <0 ) +return -EIO ; +if (status ==HPWMI_AC_REQUIRED ) { +pr_err ("You have to be plugged in to set a profile\n" ); +return -EIO ; + } +return 0 ; +} + +static ssize_t battery_threshold_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +int mode =battery_get (); +if (mode <0 ) +return -EINVAL ; +return sysfs_emit (buf,"%d\n" , mode); +} + +static ssize_t battery_threshold_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +int ret; +u8 profile; + ret =kstrtou8 (buf,10 , &profile); +if (ret) +return ret; + ret =battery_set (profile); +if (ret <0 ) +return ret; +return count; +} +static DEVICE_ATTR_RW (battery_threshold); +static int camera_shutter_input_setup (void ) {int err;
@@ -2303,7 +2367,14 @@ static int __init hp_wmi_bios_setup(struct platform_device *device)if (err <0 )return err; - + err =battery_get (); +if (err >=0 ) { + err =device_create_file (&device->dev, &dev_attr_battery_threshold); +if (err) +device_remove_file (&device->dev, &dev_attr_battery_threshold); +else + battery_threshold_support = true; + }thermal_profile_setup (device);return 0 ;
@@ -2335,6 +2406,8 @@ static void __exit hp_wmi_bios_remove(struct platform_device *device) priv =platform_get_drvdata (device);if (priv)cancel_delayed_work_sync (&priv->keep_alive_dwork); +if (battery_threshold_support) +device_remove_file (&device->dev, &dev_attr_battery_threshold); }static int hp_wmi_resume_handler (struct device *device)
Signature · 1 line
2.55.0