ACPI / battery: Deal with design or full capacity being reported as -1
PATCH 1/3 ACPI / battery: Deal with design or full capacity being reported as -1
Commit b41901a2cf06 ("ACPI / battery: Do not export energy_full[_design] on devices without full_charge_capacity") added support for some (broken) devices which always report 0 for both design- and full_charge-capacity.
This assumes that if the capacity is not being reported it is 0. The ThunderSoft TS178 tablet's _BIX implementation falsifies this assumption. It reports ACPI_BATTERY_VALUE_UNKNOWN (-1) as full_charge_capacity, which we treat as a valid value which causes several problems.
This commit fixes this by adding a new ACPI_BATTERY_CAPACITY_VALID() helper which checks that the value is not 0 and not -1; and using this whenever we need to test if either design_capacity or full_charge_capacity is valid.
--- drivers/acpi/battery.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)
drivers/acpi/battery.c +12 -7
@@ -38,6 +38,8 @@#define PREFIX "ACPI: "#define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF +#define ACPI_BATTERY_CAPACITY_VALID (capacity) \ + ((capacity) != 0 && (capacity) != ACPI_BATTERY_VALUE_UNKNOWN)#define ACPI_BATTERY_DEVICE_NAME "Battery"
@@ -192,7 +194,8 @@ static int acpi_battery_is_charged(struct acpi_battery *battery)static bool acpi_battery_is_degraded (struct acpi_battery *battery) { -return battery->full_charge_capacity && battery->design_capacity && +return ACPI_BATTERY_CAPACITY_VALID (battery->full_charge_capacity) && +ACPI_BATTERY_CAPACITY_VALID (battery->design_capacity) && battery->full_charge_capacity < battery->design_capacity; }
@@ -263,14 +266,14 @@ static int acpi_battery_get_property(struct power_supply *psy,break ;case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN :case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN : -if (battery->design_capacity ==ACPI_BATTERY_VALUE_UNKNOWN ) +if (!ACPI_BATTERY_CAPACITY_VALID (battery->design_capacity)) ret = -ENODEV ;else val->intval = battery->design_capacity *1000 ;break ;case POWER_SUPPLY_PROP_CHARGE_FULL :case POWER_SUPPLY_PROP_ENERGY_FULL : -if (battery->full_charge_capacity ==ACPI_BATTERY_VALUE_UNKNOWN ) +if (!ACPI_BATTERY_CAPACITY_VALID (battery->full_charge_capacity)) ret = -ENODEV ;else val->intval = battery->full_charge_capacity *1000 ;
@@ -283,11 +286,12 @@ static int acpi_battery_get_property(struct power_supply *psy, val->intval = battery->capacity_now *1000 ;break ;case POWER_SUPPLY_PROP_CAPACITY : -if (battery->capacity_now && battery->full_charge_capacity) +if (battery->capacity_now ==ACPI_BATTERY_VALUE_UNKNOWN || + !ACPI_BATTERY_CAPACITY_VALID (battery->full_charge_capacity)) + ret = -ENODEV ; +else val->intval = battery->capacity_now *100 / battery->full_charge_capacity; -else - val->intval =0 ;break ;case POWER_SUPPLY_PROP_CAPACITY_LEVEL :if (battery->state &ACPI_BATTERY_STATE_CRITICAL )
@@ -799,7 +803,8 @@ static int sysfs_add_battery(struct acpi_battery *battery) battery->bat_desc.properties = charge_battery_props; battery->bat_desc.num_properties =ARRAY_SIZE (charge_battery_props); - }else if (battery->full_charge_capacity ==0 ) { + }else if (!ACPI_BATTERY_CAPACITY_VALID ( + battery->full_charge_capacity)) { battery->bat_desc.properties = energy_battery_full_cap_broken_props; battery->bat_desc.num_properties =
Signature · 1 line
2.23.0
PATCH 2/3 ACPI / battery: Use design-cap for capacity calculations if full-cap is not available
The ThunderSoft TS178 tablet's _BIX implementation reports design_capacity but not full_charge_capacity.
Before this commit this would cause us to return -ENODEV for the capacity attribute, which userspace does not like. Specifically upower does this:
if (sysfs_file_exists (native_path, "capacity")) {
percentage = sysfs_get_double (native_path, "capacity");Where the sysfs_get_double() helper returns 0 when we return -ENODEV, so the battery always reads 0% if we return -ENODEV.
This commit fixes this by using the design-capacity instead of the full-charge-capacity when the full-charge-capacity is not available.
--- drivers/acpi/battery.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/acpi/battery.c +8 -3
@@ -217,7 +217,7 @@ static int acpi_battery_get_property(struct power_supply *psy,enum power_supply_property psp, union power_supply_propval *val) { - int ret = 0; + int full_capacity = ACPI_BATTERY_VALUE_UNKNOWN, ret = 0;struct acpi_battery *battery =to_acpi_battery (psy);if (acpi_battery_present (battery)) {
@@ -286,12 +286,17 @@ static int acpi_battery_get_property(struct power_supply *psy, val->intval = battery->capacity_now *1000 ;break ;case POWER_SUPPLY_PROP_CAPACITY : +if (ACPI_BATTERY_CAPACITY_VALID (battery->full_charge_capacity)) + full_capacity = battery->full_charge_capacity; +else if (ACPI_BATTERY_CAPACITY_VALID (battery->design_capacity)) + full_capacity = battery->design_capacity; +if (battery->capacity_now ==ACPI_BATTERY_VALUE_UNKNOWN || - !ACPI_BATTERY_CAPACITY_VALID (battery->full_charge_capacity)) + full_capacity ==ACPI_BATTERY_VALUE_UNKNOWN ) ret = -ENODEV ;else val->intval = battery->capacity_now *100 / - battery->full_charge_capacity; + full_capacity;break ;case POWER_SUPPLY_PROP_CAPACITY_LEVEL :if (battery->state &ACPI_BATTERY_STATE_CRITICAL )
Signature · 1 line
2.23.0
PATCH 3/3 ACPI / battery: Deal better with neither design nor full capacity not being reported
In reply to Hans de GoedeCommit b41901a2cf06 ("ACPI / battery: Do not export energy_full[_design] on devices without full_charge_capacity") added support for some (broken) devices which always report 0 for both design_capacity and full_charge_capacity.
Since the device that commit was written as a fix for is not reporting any form of "full" capacity we cannot calculate the value for the POWER_SUPPLY_PROP_CAPACITY, this is worked around by using an alternative array of available properties which does not contain this property.
This is necessary because userspace (upower) treats us returning -ENODEV as 0 and then typically will trigger an emergency shutdown because of that. Userspace does not do this if the capacity sysfs attribute is not present at all.
There are two potential problems with that commit:
1) It assumes that both full_charge- and design-capacity are broken at the same time and only checks if full_charge- is broken.
2) It assumes that this only ever happens for devices which report energy units rather then charge units.
This commit fixes both issues by only using the alternative array of available properties if both full_charge- and design-capacity are broken and by also adding an alternative array of available properties for devices using mA units.
--- drivers/acpi/battery.c | 51 ++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 12 deletions(-)
drivers/acpi/battery.c +39 -12
@@ -342,6 +342,20 @@ static enum power_supply_property charge_battery_props[] = { POWER_SUPPLY_PROP_SERIAL_NUMBER, }; +static enum power_supply_property charge_battery_full_cap_broken_props[] = { +POWER_SUPPLY_PROP_STATUS , +POWER_SUPPLY_PROP_PRESENT , +POWER_SUPPLY_PROP_TECHNOLOGY , +POWER_SUPPLY_PROP_CYCLE_COUNT , +POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN , +POWER_SUPPLY_PROP_VOLTAGE_NOW , +POWER_SUPPLY_PROP_CURRENT_NOW , +POWER_SUPPLY_PROP_CHARGE_NOW , +POWER_SUPPLY_PROP_MODEL_NAME , +POWER_SUPPLY_PROP_MANUFACTURER , +POWER_SUPPLY_PROP_SERIAL_NUMBER , +}; +static enum power_supply_property energy_battery_props[] = {POWER_SUPPLY_PROP_STATUS ,POWER_SUPPLY_PROP_PRESENT ,
@@ -803,21 +817,34 @@ static void __exit battery_hook_exit(void)static int sysfs_add_battery (struct acpi_battery *battery) {struct power_supply_config psy_cfg = { .drv_data = battery, }; +bool full_cap_broken = false; + +if (!ACPI_BATTERY_CAPACITY_VALID (battery->full_charge_capacity) && + !ACPI_BATTERY_CAPACITY_VALID (battery->design_capacity)) + full_cap_broken = true;if (battery->power_unit ==ACPI_BATTERY_POWER_UNIT_MA ) { - battery->bat_desc.properties = charge_battery_props; - battery->bat_desc.num_properties = -ARRAY_SIZE (charge_battery_props); - }else if (!ACPI_BATTERY_CAPACITY_VALID ( - battery->full_charge_capacity)) { - battery->bat_desc.properties = - energy_battery_full_cap_broken_props; - battery->bat_desc.num_properties = -ARRAY_SIZE (energy_battery_full_cap_broken_props); +if (full_cap_broken) { + battery->bat_desc.properties = + charge_battery_full_cap_broken_props; + battery->bat_desc.num_properties = +ARRAY_SIZE (charge_battery_full_cap_broken_props); + }else { + battery->bat_desc.properties = charge_battery_props; + battery->bat_desc.num_properties = +ARRAY_SIZE (charge_battery_props); + } }else { - battery->bat_desc.properties = energy_battery_props; - battery->bat_desc.num_properties = -ARRAY_SIZE (energy_battery_props); +if (full_cap_broken) { + battery->bat_desc.properties = + energy_battery_full_cap_broken_props; + battery->bat_desc.num_properties = +ARRAY_SIZE (energy_battery_full_cap_broken_props); + }else { + battery->bat_desc.properties = energy_battery_props; + battery->bat_desc.num_properties = +ARRAY_SIZE (energy_battery_props); + } } battery->bat_desc.name =acpi_device_bid (battery->device);
Signature · 1 line
2.23.0
Rafael J. Wysocki
maintainerQuoted 10 lines · Hans de Goede
Commit
b41901a2cf06("ACPI / battery: Do not export energy_full[_design] on devices withoutfull_charge_capacity") added support for some (broken) devices which always report 0 for both design- and full_charge-capacity.This assumes that if the capacity is not being reported it is 0. The ThunderSoft TS178 tablet's
_BIXimplementation falsifies this assumption. It reportsACPI_BATTERY_VALUE_UNKNOWN(-1) asfull_charge_capacity, which we treat as a valid value which causes several problems.
Quoted 6 lines
This commit fixes this by adding a new
ACPI_BATTERY_CAPACITY_VALID()helper which checks that the value is not 0 and not -1; and using this whenever we need to test if eitherdesign_capacityorfull_charge_capacityis valid.Fixes b41901a2cf06 ("ACPI / battery: Do not export energy_full[_design] on devices without full_charge_capacity")Signed-off-by Hans de Goede <[email protected]>
Applying along with the [2-3/3] as 5.6 material, thanks!
Quoted 73 lines
--- drivers/acpi/battery.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)drivers/acpi/battery.c +12
-8@@ -38,6 +38,8 @@#define PREFIX "ACPI: "#define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF +#define ACPI_BATTERY_CAPACITY_VALID (capacity) \ + ((capacity) != 0 && (capacity) != ACPI_BATTERY_VALUE_UNKNOWN)#define ACPI_BATTERY_DEVICE_NAME "Battery"@@ -192,7 +194,8 @@ static int acpi_battery_is_charged(struct acpi_battery *battery)static bool acpi_battery_is_degraded (struct acpi_battery *battery) { -return battery->full_charge_capacity && battery->design_capacity && +return ACPI_BATTERY_CAPACITY_VALID (battery->full_charge_capacity) && +ACPI_BATTERY_CAPACITY_VALID (battery->design_capacity) && battery->full_charge_capacity < battery->design_capacity; }@@ -263,14 +266,14 @@ static int acpi_battery_get_property(struct power_supply *psy,break ;case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN :case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN : -if (battery->design_capacity ==ACPI_BATTERY_VALUE_UNKNOWN ) +if (!ACPI_BATTERY_CAPACITY_VALID (battery->design_capacity)) ret = -ENODEV ;else val->intval = battery->design_capacity *1000 ;break ;case POWER_SUPPLY_PROP_CHARGE_FULL :case POWER_SUPPLY_PROP_ENERGY_FULL : -if (battery->full_charge_capacity ==ACPI_BATTERY_VALUE_UNKNOWN ) +if (!ACPI_BATTERY_CAPACITY_VALID (battery->full_charge_capacity)) ret = -ENODEV ;else val->intval = battery->full_charge_capacity *1000 ;@@ -283,11 +286,12 @@ static int acpi_battery_get_property(struct power_supply *psy, val->intval = battery->capacity_now *1000 ;break ;case POWER_SUPPLY_PROP_CAPACITY : -if (battery->capacity_now && battery->full_charge_capacity) +if (battery->capacity_now ==ACPI_BATTERY_VALUE_UNKNOWN || + !ACPI_BATTERY_CAPACITY_VALID (battery->full_charge_capacity)) + ret = -ENODEV ; +else val->intval = battery->capacity_now *100 / battery->full_charge_capacity; -else - val->intval =0 ;break ;case POWER_SUPPLY_PROP_CAPACITY_LEVEL :if (battery->state &ACPI_BATTERY_STATE_CRITICAL )@@ -799,7 +803,8 @@ static int sysfs_add_battery(struct acpi_battery *battery) battery->bat_desc.properties = charge_battery_props; battery->bat_desc.num_properties =ARRAY_SIZE (charge_battery_props); - }else if (battery->full_charge_capacity ==0 ) { + }else if (!ACPI_BATTERY_CAPACITY_VALID ( + battery->full_charge_capacity)) { battery->bat_desc.properties = energy_battery_full_cap_broken_props; battery->bat_desc.num_properties = --2.23.0