Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3708665ybv; Mon, 10 Feb 2020 05:06:09 -0800 (PST) X-Google-Smtp-Source: APXvYqzalFrS2IVZOSTvz8pdXeogcUSkMVxW5XQg6ytfzFWP2bPe8JWDFAPnOyyKc9MD/0NHa7WV X-Received: by 2002:aca:d5d3:: with SMTP id m202mr704684oig.161.1581339969266; Mon, 10 Feb 2020 05:06:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581339969; cv=none; d=google.com; s=arc-20160816; b=V5rbIqwIqO2KphhfDE8tj/zsBcuUcGj3U0CxKbW1zNNWvg9CS40ubz3a7KteQH8oyu YnAdZr8qYpsaS/C9sJ7ujdkH+sHdFc1kvc3zwRG4KsoDIrv8zUsS1cJ3agmQ7UpC3K8s 5RBuiW1tFzeQnIr5A8hXGTM9GO50pWVyEIyZUFJcdUPDwLj64HhYYbLh+d/omFDiOx8g BTQyCyEK1W+TWNI/f1wNSXXp1URhgZ04VumrbjL8FgflB8QbuPFIk47wXeJT6amj+oZL 29tLaOnxEgQA+jlElDsFQ2iyc4kt6M55zuB7OLHZDwOVTBvr910wwcViZ0hk579nlHCG CxHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=7l4WMdZnTcMECGdr1pP8nxkv1tefDag0dNGhN7UEz34=; b=a/QMADZZqYtHprF735FNvgvfEj/P6enUu5p6OGS2ndGPfK9BaSpgPpQrxwL4gmfAzY 4MZwiAEwXgqppmgfzMAn2HMjuCGTygAAwXHa0d3qcb/BCvxD+Tf2PMHxwojpalAC69r2 fOoXXpfM+D2BxqH+blQsIAXNQKfwCpdlDN/OU3BNxkFhayVc7m0mi192sD/j1e6m0I+x +3qyQV4pHJNbXKe1EWUX2pc0aSsBDp2PsHTsaYBpk6OcrlOhxpqOCay+7mahbiReY0Tl 7RB5w7M1axdBjPGsFoEhDAkIOKPcZSlxyIIQ+4jOMP59WhV1L7QYOohQG81hsbCDFMG/ FMMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=eIPCmxk3; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k184si149004oib.239.2020.02.10.05.05.57; Mon, 10 Feb 2020 05:06:09 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=eIPCmxk3; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730359AbgBJNFp (ORCPT + 99 others); Mon, 10 Feb 2020 08:05:45 -0500 Received: from mail.kernel.org ([198.145.29.99]:38940 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729587AbgBJMkA (ORCPT ); Mon, 10 Feb 2020 07:40:00 -0500 Received: from localhost (unknown [209.37.97.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D315D24672; Mon, 10 Feb 2020 12:39:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1581338398; bh=viCN2OLrdVaSy6OC4bX/zxx4udJHBfesir8T4kojJ2c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eIPCmxk39kfc31TRniHd0tP/RiB+xWiA40DD6AE/pmxHz3OdBbVZB7LP0FeGn3q4l TnYHETj7FUWtFmp9hT6TIvK7JqLO+Tnx4zsNOcPJyZh+4HHKD4/mLY64cWUsIhyluQ X0qFQgU1371BA4gWYSpR4Wpv/HDPrR+j/RPtwgDA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Hans de Goede , "Rafael J. Wysocki" Subject: [PATCH 5.5 096/367] ACPI / battery: Deal with design or full capacity being reported as -1 Date: Mon, 10 Feb 2020 04:30:09 -0800 Message-Id: <20200210122433.233401391@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200210122423.695146547@linuxfoundation.org> References: <20200210122423.695146547@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Hans de Goede commit cc99f0ad52467028cb1251160f23ad4bb65baf20 upstream. 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. Fixes: b41901a2cf06 ("ACPI / battery: Do not export energy_full[_design] on devices without full_charge_capacity") Cc: 4.19+ # 4.19+ Signed-off-by: Hans de Goede Signed-off-by: Rafael J. Wysocki Signed-off-by: Greg Kroah-Hartman --- drivers/acpi/battery.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) --- a/drivers/acpi/battery.c +++ b/drivers/acpi/battery.c @@ -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(struc 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(str 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(str 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->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 =