Re: [PATCH] ACPI: battery: negate current when discharging

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, May 7, 2025 at 6:32 AM Peter Marheine <pmarheine@xxxxxxxxxxxx> wrote:
>
> The ACPI specification requires that battery rate is always positive,
> but the kernel ABI for POWER_SUPPLY_PROP_CURRENT_NOW
> (Documentation/ABI/testing/sysfs-class-power) specifies that it should
> be negative when a battery is discharging. When reporting CURRENT_NOW,
> massage the value to match the documented ABI.
>
> This only changes the sign of `current_now` and not `power_now` because
> documentation doesn't describe any particular meaning for `power_now` so
> leaving `power_now` unchanged is less likely to confuse userspace
> unnecessarily, whereas becoming consistent with the documented ABI is
> worth potentially confusing clients that read `current_now`.
>
> Signed-off-by: Peter Marheine <pmarheine@xxxxxxxxxxxx>
> ---
>  drivers/acpi/battery.c | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
>
> diff --git a/drivers/acpi/battery.c b/drivers/acpi/battery.c
> index 6760330a8a..0ef03142fd 100644
> --- a/drivers/acpi/battery.c
> +++ b/drivers/acpi/battery.c
> @@ -242,6 +242,18 @@ static int acpi_battery_get_property(struct power_supply *psy,
>                         val->intval = battery->voltage_now * 1000;
>                 break;
>         case POWER_SUPPLY_PROP_CURRENT_NOW:
> +               if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
> +                       ret = -ENODEV;
> +               else if ((battery->state & ACPI_BATTERY_STATE_DISCHARGING) &&
> +                        acpi_battery_handle_discharging(battery)
> +                               == POWER_SUPPLY_STATUS_DISCHARGING)
> +                       /* ACPI specifies battery rate should always be
> +                        * positive, but this prop is negative when discharging.
> +                        */
> +                       val->intval = battery->rate_now * -1000;
> +               else
> +                       val->intval = battery->rate_now * 1000;
> +               break;
>         case POWER_SUPPLY_PROP_POWER_NOW:
>                 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
>                         ret = -ENODEV;
> --

What about this instead (modulo GMail-induced whitespace breakage):

---
 drivers/acpi/battery.c |   18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

--- a/drivers/acpi/battery.c
+++ b/drivers/acpi/battery.c
@@ -243,10 +243,22 @@
         break;
     case POWER_SUPPLY_PROP_CURRENT_NOW:
     case POWER_SUPPLY_PROP_POWER_NOW:
-        if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
+        if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN) {
             ret = -ENODEV;
-        else
-            val->intval = battery->rate_now * 1000;
+            break;
+        }
+
+        val->intval = battery->rate_now * 1000;
+        /*
+         * When discharging, the current should be reported as a
+         * negative number as per the power supply class interface
+         * definition.
+         */
+        if (psp == POWER_SUPPLY_PROP_CURRENT_NOW &&
+            (battery->state & ACPI_BATTERY_STATE_DISCHARGING) &&
+            acpi_battery_handle_discharging(battery) ==
POWER_SUPPLY_STATUS_DISCHARGING)
+            val->intval = -val->intval;
+
         break;
     case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
     case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:





[Index of Archives]     [Linux IBM ACPI]     [Linux Power Management]     [Linux Kernel]     [Linux Laptop]     [Kernel Newbies]     [Share Photos]     [Security]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Samba]     [Video 4 Linux]     [Device Mapper]     [Linux Resources]
  Powered by Linux