From patchwork Sat Feb 25 11:51:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Armin Wolf X-Patchwork-Id: 61513 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1456636wrd; Sat, 25 Feb 2023 04:18:22 -0800 (PST) X-Google-Smtp-Source: AK7set83A9pMZLzKTK27WkhJsLL+CkWVnryfTEkVOIF/kxxj1UU1D2ukhwMCkEPI+LOtsuawdRRK X-Received: by 2002:a17:902:9f8e:b0:19c:f1bd:e919 with SMTP id g14-20020a1709029f8e00b0019cf1bde919mr1229626plq.65.1677327502545; Sat, 25 Feb 2023 04:18:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677327502; cv=none; d=google.com; s=arc-20160816; b=Kcx+SLJ1BmBzEXI+8idBCuRsFqYXjKVoLiTKsCIyfg0eZ1xJmW92XacOfxOqmrLvf4 n0IUju4S19R6YO5f0P3GdFtlCHnWXPlTxLlJKNIjYAZdxPGPesIr5CCm2z7ASDMnEdRL Py7wozb2V/wfPEY4XKrU44UOqqDJWodbUWxhMSOIKD/FoCoTfvc+TodLkdFEoClg975E SeM3NQWBCj3G5RNLiPcPnoutbXAarELfLtrAi10RGEFfLeIdvQ2GCV6EJPo1fyRTnL/E KSBZOi2FBWAIXQ7GlneVq7lEmzfwk4eCdfggdxrVtz4bUeofraO9LkFDYO1Ny+ORh+Ra WrbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:ui-outboundreport:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=cmveYTH3wCNgDp6miVMTa0W8n6mSRkvQkyelBwj3nsw=; b=u0yzPRPBcTUuvm7ES5UPydU7JnoVjCAIRrRlyT3b4kJsNLCW+7WdU7vf8HGlche5vM 9zmv/TREJMxEun8IJucITjT1kIjmR7aOVSpBvYCxAdem7obbd5KalcS7R3FPspDPohcP xuMDSDjk1pFKPfylzRtjg+jzrWhyFYy4D6zcLFA9QJSJTYkOyijzTVRz5BR4B2UYiXNt 4vJ/j6UU0ktFMeLnvCmqrxxE17bvI5n3rYwbwdtG5Qpy3IXGLIKAtRZN6rCr7ybAV9y9 xuh+ZTpoYY/JiUSLYiFjijAS/fXzZ/nOIGEov5pczG+XrRN4rdiJwEz1KWfnIi6Bgv0s cZHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmx.de header.s=s31663417 header.b=c0xUIIR4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmx.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ji14-20020a170903324e00b0019ab107bd01si1539092plb.110.2023.02.25.04.18.05; Sat, 25 Feb 2023 04:18:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmx.de header.s=s31663417 header.b=c0xUIIR4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmx.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229637AbjBYLwN (ORCPT + 99 others); Sat, 25 Feb 2023 06:52:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229558AbjBYLwA (ORCPT ); Sat, 25 Feb 2023 06:52:00 -0500 Received: from mout.gmx.net (mout.gmx.net [212.227.17.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2B96419F31; Sat, 25 Feb 2023 03:51:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1677325914; i=w_armin@gmx.de; bh=Kf5tr2Sg9Jqyp+j3xyIw9/QWCzBL3T6tk+vssRythnI=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=c0xUIIR4n8Ta7Pq7e9hvf2X5cHaEbZm+PoZFbaPg7b6b2EFiv+u1G33ibkY4OZrql 6boChBK+jv1p4hPBagpvxw/b2Luc7pu2DKvhhz4Au2CpIRA9WfUO93GfRpE5QdCXkQ 8qztMahw+2Ti0FIwILlblrTUIFQDpHNGU57Lfq44Ofzxiw7Ri7coBD/9Bt2uY3NFYq UpvtfMIyVSE/VV9wtjfaFv+byC6cnd3ru8KNSVeIxOBdN4i6VyohLUrZMb15Gy+Phf E6Lmz4KgbZx1kWiMSSHhUdjEkzgrmJguL7UYxizCZTz0gsJiqFAllmphnSrCCziM0K SkrRXClOn0Omg== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from esprimo-mx.users.agdsn.de ([141.30.226.129]) by mail.gmx.net (mrgmx104 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MoO24-1oh6wL3TUO-00oncz; Sat, 25 Feb 2023 12:51:53 +0100 From: Armin Wolf To: rafael@kernel.org, lenb@kernel.org Cc: linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 4/4] ACPI: SBS: Fix handling of Smart Battery Selectors Date: Sat, 25 Feb 2023 12:51:44 +0100 Message-Id: <20230225115144.31212-5-W_Armin@gmx.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230225115144.31212-1-W_Armin@gmx.de> References: <20230225115144.31212-1-W_Armin@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:kj0EMGZQOKo1DIaNjvZpdw3cvFja6SPU6xwfeor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xB2Xo8muPkrUT5OldYPhbyibr/S6DMqnsjrFjaWsh2wdSAcEQBrCelsEHzViD 4roC4p8QYUijIyhcZvBflYhVO0A56iQAXBryWDZtRbuKV+NBRGLpI11WisE8bi+JY650rthPM pPHD92ESyUDgjLMtRkL1zb0Q4ZA= X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758805363397684950?= X-GMAIL-MSGID: =?utf-8?q?1758805363397684950?= The "Smart Battery Selector" standard says that when writing SelectorState (0x1), the nibbles which should not be modified need to be masked with 0xff. This is necessary since in contrast to a "Smart Battery Manager", the last three nibbles are writable. Failing to do so might trigger the following cycle: 1. Host accidentally changes power source of the system (3rd nibble) when selecting a battery. 2. Power source is invalid, Selector changes to another power source. 3. Selector notifies host that it changed the power source. 4. Host re-reads some batteries. 5. goto 1 for each re-read battery. This loop might also be entered when a battery which is not present is selected for SMBus access. In the end some workqueues fill up, which causes the system to lockup upon suspend/shutdown. Fix this by correctly masking the value to be written, and avoid selecting batteries which are absent. Tested on a Acer Travelmate 4002WLMi. Signed-off-by: Armin Wolf --- drivers/acpi/sbs.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) -- 2.30.2 diff --git a/drivers/acpi/sbs.c b/drivers/acpi/sbs.c index e90752d4f488..94e3c000df2e 100644 --- a/drivers/acpi/sbs.c +++ b/drivers/acpi/sbs.c @@ -473,23 +473,32 @@ static const struct device_attribute alarm_attr = { -------------------------------------------------------------------------- */ static int acpi_battery_read(struct acpi_battery *battery) { - int result = 0, saved_present = battery->present; + int result, saved_present = battery->present; u16 state; if (battery->sbs->manager_present) { result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SBS_MANAGER, 0x01, (u8 *)&state); - if (!result) - battery->present = state & (1 << battery->id); - state &= 0x0fff; + if (result) + return result; + + battery->present = state & (1 << battery->id); + if (!battery->present) + return 0; + + /* Masking necessary for Smart Battery Selectors */ + state = 0x0fff; state |= 1 << (battery->id + 12); acpi_smbus_write(battery->sbs->hc, SMBUS_WRITE_WORD, ACPI_SBS_MANAGER, 0x01, (u8 *)&state, 2); - } else if (battery->id == 0) - battery->present = 1; - - if (result || !battery->present) - return result; + } else { + if (battery->id == 0) { + battery->present = 1; + } else { + if (!battery->present) + return 0; + } + } if (saved_present != battery->present) { battery->update_time = 0;