From nobody Tue Apr 7 10:37:53 2026 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7942C27280A for ; Sat, 14 Mar 2026 01:05:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773450343; cv=none; b=EYxtrzq2QmhOAartsu8n4IgwyKZSdsUZUWTQTCeh13W3C8WCIJjprEGWzyk2PoC99lYVo/EXebn83s12OlANyYMSXb0cVzzJfJkLGOisUM9eH8G2Wb4XRNyWKoQZYFuamqiuMX07o+o9oKYyQZ0Ek9SUD5Vk7GMYr2aZH03sEfU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773450343; c=relaxed/simple; bh=SPg2hA7y1CcYnIkWT+SZNHxhcM9Bjt4HXuIIXfopeG8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bSqgyX/q8t/z7lYOEwTcl5Ep+0hexFdNUROrBOhcBm9ET45f+NmikniiCQVLwc0A/VsWdYWsH7s3jfWjofZBRjuxAOCmAD2o2BYAVYqU597ktSLnh7susWpTIk+wzapAAVj5+bGUT/URD9CsJE+quOua1nqxnrCVDMgjqTjhjaM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=aRj9yChb; arc=none smtp.client-ip=209.85.128.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="aRj9yChb" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-483487335c2so26470035e9.2 for ; Fri, 13 Mar 2026 18:05:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773450340; x=1774055140; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wP0R4g6EguFpERuWw6kFrrPwl8MQ9NSP5DgPehkcTOc=; b=aRj9yChbVOveC5SplYMPdXRBjFmsFXy0MoxPX3B5zsq8ZY0JciaJHOf7YCNFFDlrov yDpg0FCEULA19n1/IaCmQ/yehFjDp2eumOP32HnxqgCVEVych78SLZsaeW7MJ5HavfHd 7dwYzxKMYEaLszSUuAkecRe7co0uLr6lQifFxpqMgsMDld0afVvy84HwaWxeoNUg0Jwz 5/xafz2BNul9dj6qmw/ApIgB0h5HwXwtDEFd9PWrTl5FjS3alUIo9a02M6k1+GedY8M6 iNhnEOvLw4W20bc3iZ/EHXhMhNa+2fQwNyXrds25K2NA5FBEIunxtV34zEg0lnlRvMjV O4Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773450340; x=1774055140; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=wP0R4g6EguFpERuWw6kFrrPwl8MQ9NSP5DgPehkcTOc=; b=YLa200zldEmTmb2dDLq7CHUIAmCduk6Amlp+lHYH+uqkhY7dXUfyNDFm3UnJeMaTSF GZWdn99XmZnONm/WJAaGb755K8ZzOjq1WtHgMIuOguxzMSEr+WOaV6SDpAK0gDfj2vwp CQl/2/k3Ucu3BU597aJRGT6OxrNVmz9XXB0QQ5aIzMmBdQJ6ertvn/l7odXiNuUIsUsw re9Z5/xSxMIzcshjIeECc5nU1idumbd6eUAv9yIF1LAovIKvjtXwKTvcI4i7nCCLluSH dxM7EW+CrAq5adXDbJEv8SR0Z2U0WdO+/gaTKSXHgojzDqLFyzishrSbML7/NlOqOcvY bcUQ== X-Forwarded-Encrypted: i=1; AJvYcCVSDfdDs1Nzyldvp6MZMiaJbXwesq5Vqvuvxvv/elkoPkHToztdS0OunKMkjftpEi0Xqpw3LKvpVnpBUdU=@vger.kernel.org X-Gm-Message-State: AOJu0Yy69xLRd01LT0FH5lGXQQGXK7n0/9322uidjTTy8NbStI1oW7+k zv4EPj9KkCQ8dL5C6tVYx+8Hgjxjgo4PQWGDOYvk/MGAC6o113VezF4= X-Gm-Gg: ATEYQzzWoTRdpXimVhu2CfSjWxOH7OxpzQ384pvEFR2jFfkKt7fKtVjfcp7sELb5iT7 gbKG8Aahp3moJYUCOexvP2g9MGPoQ9b/RSvg7cbV76OqQ4UyBgQEMwr01jSti0O9oApiLVhv+OM WTRxxQWDWbwmb5gtDIh3nNXaF4G3Plh8p7TrZQIZFrdJOETAHrG/kUdn1IsTcLF7wbtja3Q4cZt 094042RJez6BZhDZqeuLFhHWLCyl2Lh1dxifRoVMM3f52CSBtsl2rDtM1J/TTPKe+ZglgG6EGcM keFelnVSJCS+PndgyRISmambDOVXMKS2HTAIHdb6cVAr7f3kRLOD5Rxt6IhQryKPRXC9mL5SG4a uPG9aKj5za+O1e10ZUNpggr1oo35WPsz6sQ3wjU5//xGKnSyWzv27s/HrjGepwR2i/KBfQ2+nR8 9zWgmrw/gliYyGcB5lVFbxc0iPyCKI0Qww0eM1M+iuCgBl7Ru5gWMsaBqVYJyTdY5E9l9OV5n4o hqLjNKgPT7PtM9qxfzSLEN8D+4GXOZ5dWQ8zLTWI0RzHbsOHW2nOr2+i3uVapSFUMRXAoMV/C+V s7bWes91Tj4WiSY= X-Received: by 2002:a05:600c:6287:b0:485:3f72:323f with SMTP id 5b1f17b1804b1-485566d2ff2mr89507195e9.11.1773450339517; Fri, 13 Mar 2026 18:05:39 -0700 (PDT) Received: from sissix.lzampier.com (92.40.172.137.threembb.co.uk. [92.40.172.137]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43a03cfd18fsm13059963f8f.36.2026.03.13.18.05.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Mar 2026 18:05:39 -0700 (PDT) From: Lucas Zampieri To: linux-input@vger.kernel.org Cc: Lucas Zampieri , linux-kernel@vger.kernel.org, Jiri Kosina , Benjamin Tissoires , Sebastian Reichel , Bastien Nocera , linux-pm@vger.kernel.org Subject: [PATCH v7 2/3] HID: input: Introduce struct hid_battery and refactor battery code Date: Sat, 14 Mar 2026 01:05:29 +0000 Message-ID: <20260314010533.110278-3-lcasmz54@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260314010533.110278-1-lcasmz54@gmail.com> References: <20260314010533.110278-1-lcasmz54@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce struct hid_battery to encapsulate individual battery state, preparing for future multi-battery support. The new structure contains all battery-related fields previously stored directly in hid_device (capacity, min, max, report_type, report_id, charge_status, etc.). The hid_device->battery pointer type changes from struct power_supply* to struct hid_battery*, and all battery functions are refactored accordingly. A hid_get_battery() helper is added for external drivers, with hid-apple.c and hid-magicmouse.c updated to use the new API. The hid-input-test.c KUnit tests are also updated for the new structure. No functional changes for single-battery devices. Signed-off-by: Lucas Zampieri --- drivers/hid/hid-apple.c | 10 +-- drivers/hid/hid-input-test.c | 39 ++++++----- drivers/hid/hid-input.c | 131 +++++++++++++++++++---------------- drivers/hid/hid-magicmouse.c | 10 +-- include/linux/hid.h | 52 ++++++++++---- 5 files changed, 146 insertions(+), 96 deletions(-) diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c index fc5897a6b..8b8b05c8a 100644 --- a/drivers/hid/hid-apple.c +++ b/drivers/hid/hid-apple.c @@ -623,17 +623,19 @@ static int apple_fetch_battery(struct hid_device *hde= v) struct apple_sc *asc =3D hid_get_drvdata(hdev); struct hid_report_enum *report_enum; struct hid_report *report; + struct hid_battery *bat; =20 - if (!(asc->quirks & APPLE_RDESC_BATTERY) || !hdev->battery) + bat =3D hid_get_battery(hdev); + if (!(asc->quirks & APPLE_RDESC_BATTERY) || !bat) return -1; =20 - report_enum =3D &hdev->report_enum[hdev->battery_report_type]; - report =3D report_enum->report_id_hash[hdev->battery_report_id]; + report_enum =3D &hdev->report_enum[bat->report_type]; + report =3D report_enum->report_id_hash[bat->report_id]; =20 if (!report || report->maxfield < 1) return -1; =20 - if (hdev->battery_capacity =3D=3D hdev->battery_max) + if (bat->capacity =3D=3D bat->max) return -1; =20 hid_hw_request(hdev, report, HID_REQ_GET_REPORT); diff --git a/drivers/hid/hid-input-test.c b/drivers/hid/hid-input-test.c index 6f5c71660..c92008daf 100644 --- a/drivers/hid/hid-input-test.c +++ b/drivers/hid/hid-input-test.c @@ -9,54 +9,59 @@ =20 static void hid_test_input_update_battery_charge_status(struct kunit *test) { - struct hid_device *dev; + struct hid_battery *bat; bool handled; =20 - dev =3D kunit_kzalloc(test, sizeof(*dev), GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev); + bat =3D kunit_kzalloc(test, sizeof(*bat), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bat); =20 - handled =3D hidinput_update_battery_charge_status(dev, HID_DG_HEIGHT, 0); + handled =3D hidinput_update_battery_charge_status(bat, HID_DG_HEIGHT, 0); KUNIT_EXPECT_FALSE(test, handled); - KUNIT_EXPECT_EQ(test, dev->battery_charge_status, POWER_SUPPLY_STATUS_UNK= NOWN); + KUNIT_EXPECT_EQ(test, bat->charge_status, POWER_SUPPLY_STATUS_UNKNOWN); =20 - handled =3D hidinput_update_battery_charge_status(dev, HID_BAT_CHARGING, = 0); + handled =3D hidinput_update_battery_charge_status(bat, HID_BAT_CHARGING, = 0); KUNIT_EXPECT_TRUE(test, handled); - KUNIT_EXPECT_EQ(test, dev->battery_charge_status, POWER_SUPPLY_STATUS_DIS= CHARGING); + KUNIT_EXPECT_EQ(test, bat->charge_status, POWER_SUPPLY_STATUS_DISCHARGING= ); =20 - handled =3D hidinput_update_battery_charge_status(dev, HID_BAT_CHARGING, = 1); + handled =3D hidinput_update_battery_charge_status(bat, HID_BAT_CHARGING, = 1); KUNIT_EXPECT_TRUE(test, handled); - KUNIT_EXPECT_EQ(test, dev->battery_charge_status, POWER_SUPPLY_STATUS_CHA= RGING); + KUNIT_EXPECT_EQ(test, bat->charge_status, POWER_SUPPLY_STATUS_CHARGING); } =20 static void hid_test_input_get_battery_property(struct kunit *test) { struct power_supply *psy; + struct hid_battery *bat; struct hid_device *dev; union power_supply_propval val; int ret; =20 dev =3D kunit_kzalloc(test, sizeof(*dev), GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev); - dev->battery_avoid_query =3D true; + + bat =3D kunit_kzalloc(test, sizeof(*bat), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bat); + bat->dev =3D dev; + bat->avoid_query =3D true; =20 psy =3D kunit_kzalloc(test, sizeof(*psy), GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, psy); - psy->drv_data =3D dev; + psy->drv_data =3D bat; =20 - dev->battery_status =3D HID_BATTERY_UNKNOWN; - dev->battery_charge_status =3D POWER_SUPPLY_STATUS_CHARGING; + bat->status =3D HID_BATTERY_UNKNOWN; + bat->charge_status =3D POWER_SUPPLY_STATUS_CHARGING; ret =3D hidinput_get_battery_property(psy, POWER_SUPPLY_PROP_STATUS, &val= ); KUNIT_EXPECT_EQ(test, ret, 0); KUNIT_EXPECT_EQ(test, val.intval, POWER_SUPPLY_STATUS_UNKNOWN); =20 - dev->battery_status =3D HID_BATTERY_REPORTED; - dev->battery_charge_status =3D POWER_SUPPLY_STATUS_CHARGING; + bat->status =3D HID_BATTERY_REPORTED; + bat->charge_status =3D POWER_SUPPLY_STATUS_CHARGING; ret =3D hidinput_get_battery_property(psy, POWER_SUPPLY_PROP_STATUS, &val= ); KUNIT_EXPECT_EQ(test, ret, 0); KUNIT_EXPECT_EQ(test, val.intval, POWER_SUPPLY_STATUS_CHARGING); =20 - dev->battery_status =3D HID_BATTERY_REPORTED; - dev->battery_charge_status =3D POWER_SUPPLY_STATUS_DISCHARGING; + bat->status =3D HID_BATTERY_REPORTED; + bat->charge_status =3D POWER_SUPPLY_STATUS_DISCHARGING; ret =3D hidinput_get_battery_property(psy, POWER_SUPPLY_PROP_STATUS, &val= ); KUNIT_EXPECT_EQ(test, ret, 0); KUNIT_EXPECT_EQ(test, val.intval, POWER_SUPPLY_STATUS_DISCHARGING); diff --git a/drivers/hid/hid-input.c b/drivers/hid/hid-input.c index 7e0f971ef..b5d34658b 100644 --- a/drivers/hid/hid-input.c +++ b/drivers/hid/hid-input.c @@ -416,18 +416,18 @@ static unsigned find_battery_quirk(struct hid_device = *hdev) return quirks; } =20 -static int hidinput_scale_battery_capacity(struct hid_device *dev, +static int hidinput_scale_battery_capacity(struct hid_battery *bat, int value) { - if (dev->battery_min < dev->battery_max && - value >=3D dev->battery_min && value <=3D dev->battery_max) - value =3D ((value - dev->battery_min) * 100) / - (dev->battery_max - dev->battery_min); + if (bat->min < bat->max && + value >=3D bat->min && value <=3D bat->max) + value =3D ((value - bat->min) * 100) / + (bat->max - bat->min); =20 return value; } =20 -static int hidinput_query_battery_capacity(struct hid_device *dev) +static int hidinput_query_battery_capacity(struct hid_battery *bat) { int ret; =20 @@ -435,19 +435,20 @@ static int hidinput_query_battery_capacity(struct hid= _device *dev) if (!buf) return -ENOMEM; =20 - ret =3D hid_hw_raw_request(dev, dev->battery_report_id, buf, 4, - dev->battery_report_type, HID_REQ_GET_REPORT); + ret =3D hid_hw_raw_request(bat->dev, bat->report_id, buf, 4, + bat->report_type, HID_REQ_GET_REPORT); if (ret < 2) return -ENODATA; =20 - return hidinput_scale_battery_capacity(dev, buf[1]); + return hidinput_scale_battery_capacity(bat, buf[1]); } =20 static int hidinput_get_battery_property(struct power_supply *psy, enum power_supply_property prop, union power_supply_propval *val) { - struct hid_device *dev =3D power_supply_get_drvdata(psy); + struct hid_battery *bat =3D power_supply_get_drvdata(psy); + struct hid_device *dev =3D bat->dev; int value; int ret =3D 0; =20 @@ -457,17 +458,17 @@ static int hidinput_get_battery_property(struct power= _supply *psy, break; =20 case POWER_SUPPLY_PROP_PRESENT: - val->intval =3D dev->battery_present; + val->intval =3D bat->present; break; =20 case POWER_SUPPLY_PROP_CAPACITY: - if (dev->battery_status !=3D HID_BATTERY_REPORTED && - !dev->battery_avoid_query) { - value =3D hidinput_query_battery_capacity(dev); + if (bat->status !=3D HID_BATTERY_REPORTED && + !bat->avoid_query) { + value =3D hidinput_query_battery_capacity(bat); if (value < 0) return value; } else { - value =3D dev->battery_capacity; + value =3D bat->capacity; } =20 val->intval =3D value; @@ -478,20 +479,20 @@ static int hidinput_get_battery_property(struct power= _supply *psy, break; =20 case POWER_SUPPLY_PROP_STATUS: - if (dev->battery_status !=3D HID_BATTERY_REPORTED && - !dev->battery_avoid_query) { - value =3D hidinput_query_battery_capacity(dev); + if (bat->status !=3D HID_BATTERY_REPORTED && + !bat->avoid_query) { + value =3D hidinput_query_battery_capacity(bat); if (value < 0) return value; =20 - dev->battery_capacity =3D value; - dev->battery_status =3D HID_BATTERY_QUERIED; + bat->capacity =3D value; + bat->status =3D HID_BATTERY_QUERIED; } =20 - if (dev->battery_status =3D=3D HID_BATTERY_UNKNOWN) + if (bat->status =3D=3D HID_BATTERY_UNKNOWN) val->intval =3D POWER_SUPPLY_STATUS_UNKNOWN; else - val->intval =3D dev->battery_charge_status; + val->intval =3D bat->charge_status; break; =20 case POWER_SUPPLY_PROP_SCOPE: @@ -509,8 +510,9 @@ static int hidinput_get_battery_property(struct power_s= upply *psy, static int hidinput_setup_battery(struct hid_device *dev, unsigned report_= type, struct hid_field *field, bool is_percentage) { + struct hid_battery *bat; struct power_supply_desc *psy_desc; - struct power_supply_config psy_cfg =3D { .drv_data =3D dev, }; + struct power_supply_config psy_cfg =3D { 0 }; unsigned quirks; s32 min, max; int error; @@ -526,16 +528,22 @@ static int hidinput_setup_battery(struct hid_device *= dev, unsigned report_type, if (quirks & HID_BATTERY_QUIRK_IGNORE) return 0; =20 - psy_desc =3D devm_kzalloc(&dev->dev, sizeof(*psy_desc), GFP_KERNEL); - if (!psy_desc) + bat =3D devm_kzalloc(&dev->dev, sizeof(*bat), GFP_KERNEL); + if (!bat) return -ENOMEM; =20 + psy_desc =3D devm_kzalloc(&dev->dev, sizeof(*psy_desc), GFP_KERNEL); + if (!psy_desc) { + error =3D -ENOMEM; + goto err_free_bat; + } + psy_desc->name =3D devm_kasprintf(&dev->dev, GFP_KERNEL, "hid-%s-battery", strlen(dev->uniq) ? dev->uniq : dev_name(&dev->dev)); if (!psy_desc->name) { error =3D -ENOMEM; - goto err_free_mem; + goto err_free_desc; } =20 psy_desc->type =3D POWER_SUPPLY_TYPE_BATTERY; @@ -555,51 +563,57 @@ static int hidinput_setup_battery(struct hid_device *= dev, unsigned report_type, if (quirks & HID_BATTERY_QUIRK_FEATURE) report_type =3D HID_FEATURE_REPORT; =20 - dev->battery_min =3D min; - dev->battery_max =3D max; - dev->battery_report_type =3D report_type; - dev->battery_report_id =3D field->report->id; - dev->battery_charge_status =3D POWER_SUPPLY_STATUS_DISCHARGING; + bat->dev =3D dev; + bat->min =3D min; + bat->max =3D max; + bat->report_type =3D report_type; + bat->report_id =3D field->report->id; + bat->charge_status =3D POWER_SUPPLY_STATUS_DISCHARGING; + bat->status =3D HID_BATTERY_UNKNOWN; =20 /* * 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 =3D report_type =3D=3D HID_INPUT_REPORT && - field->physical =3D=3D HID_DG_STYLUS; + bat->avoid_query =3D report_type =3D=3D HID_INPUT_REPORT && + field->physical =3D=3D HID_DG_STYLUS; =20 if (quirks & HID_BATTERY_QUIRK_AVOID_QUERY) - dev->battery_avoid_query =3D true; + bat->avoid_query =3D true; =20 - dev->battery_present =3D (quirks & HID_BATTERY_QUIRK_DYNAMIC) ? false : t= rue; + bat->present =3D (quirks & HID_BATTERY_QUIRK_DYNAMIC) ? false : true; =20 - dev->battery =3D devm_power_supply_register(&dev->dev, psy_desc, &psy_cfg= ); - if (IS_ERR(dev->battery)) { - error =3D PTR_ERR(dev->battery); + psy_cfg.drv_data =3D bat; + bat->ps =3D devm_power_supply_register(&dev->dev, psy_desc, &psy_cfg); + if (IS_ERR(bat->ps)) { + error =3D PTR_ERR(bat->ps); hid_warn(dev, "can't register power supply: %d\n", error); goto err_free_name; } =20 - power_supply_powers(dev->battery, &dev->dev); + power_supply_powers(bat->ps, &dev->dev); + dev->battery =3D bat; return 0; =20 err_free_name: devm_kfree(&dev->dev, psy_desc->name); -err_free_mem: +err_free_desc: devm_kfree(&dev->dev, psy_desc); +err_free_bat: + devm_kfree(&dev->dev, bat); dev->battery =3D NULL; return error; } =20 -static bool hidinput_update_battery_charge_status(struct hid_device *dev, +static bool hidinput_update_battery_charge_status(struct hid_battery *bat, unsigned int usage, int value) { switch (usage) { case HID_BAT_CHARGING: - dev->battery_charge_status =3D value ? - POWER_SUPPLY_STATUS_CHARGING : - POWER_SUPPLY_STATUS_DISCHARGING; + bat->charge_status =3D value ? + POWER_SUPPLY_STATUS_CHARGING : + POWER_SUPPLY_STATUS_DISCHARGING; return true; } =20 @@ -609,34 +623,35 @@ static bool hidinput_update_battery_charge_status(str= uct hid_device *dev, static void hidinput_update_battery(struct hid_device *dev, unsigned int u= sage, int value) { + struct hid_battery *bat =3D dev->battery; int capacity; =20 - if (!dev->battery) + if (!bat) return; =20 - if (hidinput_update_battery_charge_status(dev, usage, value)) { - dev->battery_present =3D true; - power_supply_changed(dev->battery); + if (hidinput_update_battery_charge_status(bat, usage, value)) { + bat->present =3D true; + power_supply_changed(bat->ps); return; } =20 if ((usage & HID_USAGE_PAGE) =3D=3D HID_UP_DIGITIZER && value =3D=3D 0) return; =20 - if (value < dev->battery_min || value > dev->battery_max) + if (value < bat->min || value > bat->max) return; =20 - capacity =3D hidinput_scale_battery_capacity(dev, value); + capacity =3D hidinput_scale_battery_capacity(bat, value); =20 - if (dev->battery_status !=3D HID_BATTERY_REPORTED || - capacity !=3D dev->battery_capacity || - ktime_after(ktime_get_coarse(), dev->battery_ratelimit_time)) { - dev->battery_present =3D true; - dev->battery_capacity =3D capacity; - dev->battery_status =3D HID_BATTERY_REPORTED; - dev->battery_ratelimit_time =3D + if (bat->status !=3D HID_BATTERY_REPORTED || + capacity !=3D bat->capacity || + ktime_after(ktime_get_coarse(), bat->ratelimit_time)) { + bat->present =3D true; + bat->capacity =3D capacity; + bat->status =3D HID_BATTERY_REPORTED; + bat->ratelimit_time =3D ktime_add_ms(ktime_get_coarse(), 30 * 1000); - power_supply_changed(dev->battery); + power_supply_changed(bat->ps); } } #else /* !CONFIG_HID_BATTERY_STRENGTH */ diff --git a/drivers/hid/hid-magicmouse.c b/drivers/hid/hid-magicmouse.c index 9eadf3252..e70bd3dc0 100644 --- a/drivers/hid/hid-magicmouse.c +++ b/drivers/hid/hid-magicmouse.c @@ -817,19 +817,21 @@ static int magicmouse_fetch_battery(struct hid_device= *hdev) #ifdef CONFIG_HID_BATTERY_STRENGTH struct hid_report_enum *report_enum; struct hid_report *report; + struct hid_battery *bat; =20 - if (!hdev->battery || + bat =3D hid_get_battery(hdev); + if (!bat || (!is_usb_magicmouse2(hdev->vendor, hdev->product) && !is_usb_magictrackpad2(hdev->vendor, hdev->product))) return -1; =20 - report_enum =3D &hdev->report_enum[hdev->battery_report_type]; - report =3D report_enum->report_id_hash[hdev->battery_report_id]; + report_enum =3D &hdev->report_enum[bat->report_type]; + report =3D report_enum->report_id_hash[bat->report_id]; =20 if (!report || report->maxfield < 1) return -1; =20 - if (hdev->battery_capacity =3D=3D hdev->battery_max) + if (bat->capacity =3D=3D bat->max) return -1; =20 hid_hw_request(hdev, report, HID_REQ_GET_REPORT); diff --git a/include/linux/hid.h b/include/linux/hid.h index 44357295b..71beff003 100644 --- a/include/linux/hid.h +++ b/include/linux/hid.h @@ -634,6 +634,36 @@ enum hid_battery_status { HID_BATTERY_REPORTED, /* Device sent unsolicited battery strength report= */ }; =20 +/** + * struct hid_battery - represents a single battery power supply + * @dev: pointer to the parent hid_device + * @ps: the power supply instance + * @min: minimum battery value from HID descriptor + * @max: maximum battery value from HID descriptor + * @report_type: HID report type (input/feature) + * @report_id: HID report ID for this battery + * @charge_status: current charging status + * @status: battery reporting status + * @capacity: current battery capacity (0-100) + * @avoid_query: if true, avoid querying battery (e.g., for stylus) + * @present: if true, battery is present (may be dynamic) + * @ratelimit_time: rate limiting for battery reports + */ +struct hid_battery { + struct hid_device *dev; + struct power_supply *ps; + __s32 min; + __s32 max; + __s32 report_type; + __s32 report_id; + __s32 charge_status; + enum hid_battery_status status; + __s32 capacity; + bool avoid_query; + bool present; + ktime_t ratelimit_time; +}; + struct hid_driver; struct hid_ll_driver; =20 @@ -670,20 +700,9 @@ struct hid_device { #ifdef CONFIG_HID_BATTERY_STRENGTH /* * Power supply information for HID devices which report - * battery strength. power_supply was successfully registered if - * battery is non-NULL. + * battery strength. battery is non-NULL if successfully registered. */ - struct power_supply *battery; - __s32 battery_capacity; - __s32 battery_min; - __s32 battery_max; - __s32 battery_report_type; - __s32 battery_report_id; - __s32 battery_charge_status; - enum hid_battery_status battery_status; - bool battery_avoid_query; - bool battery_present; - ktime_t battery_ratelimit_time; + struct hid_battery *battery; #endif =20 unsigned long status; /* see STAT flags above */ @@ -745,6 +764,13 @@ static inline void hid_set_drvdata(struct hid_device *= hdev, void *data) dev_set_drvdata(&hdev->dev, data); } =20 +#ifdef CONFIG_HID_BATTERY_STRENGTH +static inline struct hid_battery *hid_get_battery(struct hid_device *hdev) +{ + return hdev->battery; +} +#endif + #define HID_GLOBAL_STACK_SIZE 4 #define HID_COLLECTION_STACK_SIZE 4 =20 --=20 2.53.0