Received: by 10.192.165.148 with SMTP id m20csp2524527imm; Sun, 22 Apr 2018 08:50:28 -0700 (PDT) X-Google-Smtp-Source: AIpwx49VQDPjghtG6WUosJHUypp3/5/nElUX75Hjp1ivfxeJbpjZ+6sf8ky5bQ9STuI6jTfAUsSw X-Received: by 10.99.165.3 with SMTP id n3mr14282345pgf.19.1524412228260; Sun, 22 Apr 2018 08:50:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524412228; cv=none; d=google.com; s=arc-20160816; b=VVzfZRRc66ZRQcrXTDEjAA7ZKHid5GuiDIl8rgsuw6tYiRovK8jjkMYBnIg1iH3vFM II4gZp+60YN11zSstCHLtOJ+4J84V9cD9Bqi/mcxU03Dk2w5ivQFbUeZCozPQv9Jag0G sx+mQGPwxDREZsmhlWjrIESKqqDr//vUBDBbPMjKcVCt5UwTMfjFI2vIDLmGaTTaTUvO noDxHBHjmITj0w7t367LkJp+wg/hAxl5FdB+apO+oxKUI8ITyP38zARVc/VGAKkcqMiO z10b/UP6eBIA8eqnyONlXqqKNM5DzCRhB6VsWQ12ik1ZKorC6hb6pbmY6rTQEHj8YIoP i7Ww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=8etDOnFHcjB3Elo7wVER7gy6lklEh3/rmLIiSVtA+e8=; b=zFrLooP8GOgnlvBmHh1kn3H6bxR1VacIKemfbGjh0Z/iqedTtv/QGg45b5LemxcJQ1 yCu2qoMAOHo9E3wk3mgTEyLp0rtEDgsrqbzng88lXtM4eeT1hbbPPb/a4y/48oOhmYCm OX7ktx7epimn0K/zKctQmFwAoMhaaJTn305AFB7xt1Dte2QHsnhjUeHBxNrbMQgIlppa 7a1GunFfn10mSUCry+1NdEdj+8r0nBMIqkLaaYW+RmUleNpC94nbosmlA4vCZpdKOSlY ED0TJIwbm6gbzVLw24QDoBfHHlB5VD/L5yEHGVu0hNTvH2s+SUcXuwL6bAZl8+TC46O6 GB1A== ARC-Authentication-Results: i=1; mx.google.com; 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 i12si8336587pgr.256.2018.04.22.08.50.13; Sun, 22 Apr 2018 08:50:28 -0700 (PDT) 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; 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 S1754779AbeDVOCB (ORCPT + 99 others); Sun, 22 Apr 2018 10:02:01 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:48186 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754089AbeDVOB4 (ORCPT ); Sun, 22 Apr 2018 10:01:56 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 6A115CAA; Sun, 22 Apr 2018 14:01:55 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dmitry Torokhov , Jiri Kosina Subject: [PATCH 4.16 169/196] HID: input: fix battery level reporting on BT mice Date: Sun, 22 Apr 2018 15:53:09 +0200 Message-Id: <20180422135113.006362287@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180422135104.278511750@linuxfoundation.org> References: <20180422135104.278511750@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.16-stable review patch. If anyone has any objections, please let me know. ------------------ From: Dmitry Torokhov commit 2e210bbb7429cdcf1a1a3ad00c1bf98bd9bf2452 upstream. The commit 581c4484769e ("HID: input: map digitizer battery usage") assumed that devices having input (qas opposed to feature) report for battery strength would report the data on their own, without the need to be polled by the kernel; unfortunately it is not so. Many wireless mice do not send unsolicited reports with battery strength data and have to be polled explicitly. As a complication, stylus devices on digitizers are not normally connected to the base and thus can not be polled - the base can only determine battery strength in the stylus when it is in proximity. To solve this issue, we add a special flag that tells the kernel to avoid polling the device (and expect unsolicited reports) and set it when report field with physical usage of digitizer stylus (HID_DG_STYLUS). Unless this flag is set, and we have not seen the unsolicited reports, the kernel will attempt to poll the device when userspace attempts to read "capacity" and "state" attributes of power_supply object corresponding to the devices battery. Fixes: 581c4484769e ("HID: input: map digitizer battery usage") Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=198095 Cc: stable@vger.kernel.org Reported-and-tested-by: Martin van Es Signed-off-by: Dmitry Torokhov Signed-off-by: Jiri Kosina Signed-off-by: Greg Kroah-Hartman --- drivers/hid/hid-input.c | 24 +++++++++++++++++------- include/linux/hid.h | 9 ++++++++- 2 files changed, 25 insertions(+), 8 deletions(-) --- a/drivers/hid/hid-input.c +++ b/drivers/hid/hid-input.c @@ -387,7 +387,8 @@ static int hidinput_get_battery_property break; case POWER_SUPPLY_PROP_CAPACITY: - if (dev->battery_report_type == HID_FEATURE_REPORT) { + if (dev->battery_status != HID_BATTERY_REPORTED && + !dev->battery_avoid_query) { value = hidinput_query_battery_capacity(dev); if (value < 0) return value; @@ -403,17 +404,17 @@ static int hidinput_get_battery_property break; case POWER_SUPPLY_PROP_STATUS: - if (!dev->battery_reported && - dev->battery_report_type == HID_FEATURE_REPORT) { + if (dev->battery_status != HID_BATTERY_REPORTED && + !dev->battery_avoid_query) { value = hidinput_query_battery_capacity(dev); if (value < 0) return value; dev->battery_capacity = value; - dev->battery_reported = true; + dev->battery_status = HID_BATTERY_QUERIED; } - if (!dev->battery_reported) + if (dev->battery_status == HID_BATTERY_UNKNOWN) val->intval = POWER_SUPPLY_STATUS_UNKNOWN; else if (dev->battery_capacity == 100) val->intval = POWER_SUPPLY_STATUS_FULL; @@ -486,6 +487,14 @@ static int hidinput_setup_battery(struct dev->battery_report_type = report_type; dev->battery_report_id = field->report->id; + /* + * Stylus is normally not connected to the device and thus we + * can't query the device and get meaningful battery strength. + * We have to wait for the device to report it on its own. + */ + dev->battery_avoid_query = report_type == HID_INPUT_REPORT && + field->physical == HID_DG_STYLUS; + dev->battery = power_supply_register(&dev->dev, psy_desc, &psy_cfg); if (IS_ERR(dev->battery)) { error = PTR_ERR(dev->battery); @@ -530,9 +539,10 @@ static void hidinput_update_battery(stru capacity = hidinput_scale_battery_capacity(dev, value); - if (!dev->battery_reported || capacity != dev->battery_capacity) { + if (dev->battery_status != HID_BATTERY_REPORTED || + capacity != dev->battery_capacity) { dev->battery_capacity = capacity; - dev->battery_reported = true; + dev->battery_status = HID_BATTERY_REPORTED; power_supply_changed(dev->battery); } } --- a/include/linux/hid.h +++ b/include/linux/hid.h @@ -515,6 +515,12 @@ enum hid_type { HID_TYPE_USBNONE }; +enum hid_battery_status { + HID_BATTERY_UNKNOWN = 0, + HID_BATTERY_QUERIED, /* Kernel explicitly queried battery strength */ + HID_BATTERY_REPORTED, /* Device sent unsolicited battery strength report */ +}; + struct hid_driver; struct hid_ll_driver; @@ -557,7 +563,8 @@ struct hid_device { /* device repo __s32 battery_max; __s32 battery_report_type; __s32 battery_report_id; - bool battery_reported; + enum hid_battery_status battery_status; + bool battery_avoid_query; #endif unsigned int status; /* see STAT flags above */