From: Aldo Ariel Panzardo <qwe.aldo@gmail.com>
To: Jason Gerecke <killertofu@gmail.com>
Cc: Jiri Kosina <jikos@kernel.org>,
Benjamin Tissoires <bentiss@kernel.org>,
Ping Cheng <ping.cheng@wacom.com>,
Jason Gerecke <jason.gerecke@wacom.com>,
linux-input@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: [PATCH v3] HID: wacom: validate battery range before scaling
Date: Fri, 6 Nov 2026 18:35:26 -0300 [thread overview]
Message-ID: <20261106213526.3393779-1-qwe.aldo@gmail.com> (raw)
In-Reply-To: <CANRwn3Tt0VguzG=_azEZAP8vfxr0BbyWj=Nu-R3a0TA1nNGqog@mail.gmail.com>
Battery events divide by the difference between the logical maximum
and minimum without validating the range. A malformed HID descriptor
with equal limits can therefore trigger a divide-by-zero exception.
Move percentage conversion to a helper that rejects non-positive
ranges and accounts for a nonzero logical minimum. Clamp the result
to [0, 100] so that hardware whose ADC occasionally reports slightly
beyond its declared limits still registers its battery and updates
capacity normally.
Fixes: 37d160193834 ("HID: wacom: generic: Scale battery capacity measurements to percentages")
Cc: stable@vger.kernel.org
Signed-off-by: Aldo Ariel Panzardo <qwe.aldo@gmail.com>
---
Thanks for the feedback, Jason -- you are right, the s64 stuff was
overkill here. Much simpler this way.
Changes in v3:
- Drop s64 widening and div64_s64 as requested by Jason Gerecke.
Use __s32 range with a <= 0 guard instead. Keep the clamping.
Changes in v2:
- Clamp the computed percentage to [0, 100] instead of returning
false, so that slightly out-of-range ADC values do not prevent
battery registration or silently drop updates.
drivers/hid/wacom_wac.c | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/drivers/hid/wacom_wac.c b/drivers/hid/wacom_wac.c
index 361738b..36bab07 100644
--- a/drivers/hid/wacom_wac.c
+++ b/drivers/hid/wacom_wac.c
@@ -1960,6 +1960,16 @@ static void wacom_wac_battery_usage_mapping(struct hid_device *hdev,
return;
}
+static __s32 wacom_wac_battery_percentage(struct hid_field *field, __s32 value)
+{
+ __s32 range = field->logical_maximum - field->logical_minimum;
+
+ if (range <= 0)
+ return 0;
+
+ return clamp_val((value - field->logical_minimum) * 100 / range, 0, 100);
+}
+
static void wacom_wac_battery_event(struct hid_device *hdev, struct hid_field *field,
struct hid_usage *usage, __s32 value)
{
@@ -1973,7 +1983,7 @@ static void wacom_wac_battery_event(struct hid_device *hdev, struct hid_field *f
wacom_wac->hid_data.bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
}
else {
- value = value * 100 / (field->logical_maximum - field->logical_minimum);
+ value = wacom_wac_battery_percentage(field, value);
wacom_wac->hid_data.battery_capacity = value;
wacom_wac->hid_data.bat_connected = 1;
wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
@@ -1981,7 +1991,7 @@ static void wacom_wac_battery_event(struct hid_device *hdev, struct hid_field *f
wacom_wac->features.quirks |= WACOM_QUIRK_BATTERY;
break;
case WACOM_HID_WD_BATTERY_LEVEL:
- value = value * 100 / (field->logical_maximum - field->logical_minimum);
+ value = wacom_wac_battery_percentage(field, value);
wacom_wac->hid_data.battery_capacity = value;
wacom_wac->hid_data.bat_connected = 1;
wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
--
2.53.0
prev parent reply other threads:[~2026-11-06 21:35 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-15 4:12 [PATCH] " Aldo Ariel Panzardo
2026-09-15 4:25 ` [PATCH v2] " Aldo Ariel Panzardo
2026-11-06 19:48 ` Jason Gerecke
2026-11-06 21:35 ` Aldo Ariel Panzardo [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261106213526.3393779-1-qwe.aldo@gmail.com \
--to=qwe.aldo@gmail.com \
--cc=bentiss@kernel.org \
--cc=jason.gerecke@wacom.com \
--cc=jikos@kernel.org \
--cc=killertofu@gmail.com \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ping.cheng@wacom.com \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®