From: Mason Camara <ping@masoncamara.com>
To: Jiri Kosina <jikos@kernel.org>, Benjamin Tissoires <bentiss@kernel.org>
Cc: Shuah Khan <shuah@kernel.org>, John Chen <johnchen902@gmail.com>,
linux-input@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH 1/2] HID: input: honor report field offsets in battery queries
Date: Sat, 11 Jul 2026 22:47:01 -0600 [thread overview]
Message-ID: <20260712044702.893825-2-ping@masoncamara.com> (raw)
In-Reply-To: <20260712044702.893825-1-ping@masoncamara.com>
hidinput_query_battery_capacity() always reads buf[1] from a GET_REPORT
reply. This assumes capacity usage starts at bit 0 of the report payload
and that capacity occupies the first byte.
The Magic Mouse 2 puts a status byte before AbsoluteStateOfCharge in
input report 0x90. Its GET_REPORT response is:
90 04 5f
Here 0x04 is the status byte and 0x5f is the capacity. Reading buf[1]
reports 4% in sysfs.
This query runs when userspace reads capacity before an input report
updates the battery cache. The event path already extracts the usage
from the descriptor, so moving the mouse before the sysfs read can hide
the bug.
Record the report containing the capacity field, bit offset, and size when
setting up the battery. Allocate a buffer large enough for the complete
report, skip the report ID byte, and extract the value at the recorded bit
offset. Reject replies that do not contain the complete field.
Fixes: 9de07a4e8d4c ("HID: input: map battery capacity (00850065)")
Closes: https://bugzilla.kernel.org/show_bug.cgi?id=221263
Assisted-by: Codex:GPT-5
Signed-off-by: Mason Camara <ping@masoncamara.com>
---
drivers/hid/hid-input.c | 39 +++++++++++++++++++++++++++++----------
include/linux/hid.h | 6 ++++++
2 files changed, 35 insertions(+), 10 deletions(-)
diff --git a/drivers/hid/hid-input.c b/drivers/hid/hid-input.c
index 3487600cadb4..01ce9e176299 100644
--- a/drivers/hid/hid-input.c
+++ b/drivers/hid/hid-input.c
@@ -431,18 +431,28 @@ static int hidinput_scale_battery_capacity(struct hid_battery *bat,
static int hidinput_query_battery_capacity(struct hid_battery *bat)
{
+ u8 *buf __free(kfree) = NULL;
+ unsigned int field_end;
+ unsigned int report_len;
+ u32 value;
int ret;
- u8 *buf __free(kfree) = kmalloc(4, GFP_KERNEL);
+ report_len = hid_report_len(bat->report) + !bat->report->id;
+ buf = hid_alloc_report_buf(bat->report, GFP_KERNEL);
if (!buf)
return -ENOMEM;
- ret = hid_hw_raw_request(bat->dev, bat->report_id, buf, 4,
+ ret = hid_hw_raw_request(bat->dev, bat->report_id, buf, report_len,
bat->report_type, HID_REQ_GET_REPORT);
- if (ret < 2)
+ field_end = DIV_ROUND_UP(bat->report_offset + bat->report_size, 8) +
+ 1;
+ if (ret < 0 || (unsigned int)ret < field_end)
return -ENODATA;
- return hidinput_scale_battery_capacity(bat, buf[1]);
+ value = hid_field_extract(bat->dev, buf + 1, bat->report_offset,
+ bat->report_size);
+
+ return hidinput_scale_battery_capacity(bat, value);
}
static int hidinput_get_battery_property(struct power_supply *psy,
@@ -529,7 +539,8 @@ static void hidinput_cleanup_battery(void *res)
}
static int hidinput_setup_battery(struct hid_device *dev, unsigned report_type,
- struct hid_field *field, bool is_percentage)
+ struct hid_field *field, unsigned int usage_index,
+ bool is_percentage)
{
struct hid_battery *bat;
struct power_supply_desc *psy_desc;
@@ -593,6 +604,10 @@ static int hidinput_setup_battery(struct hid_device *dev, unsigned report_type,
bat->max = max;
bat->report_type = report_type;
bat->report_id = field->report->id;
+ bat->report = field->report;
+ bat->report_offset = field->report_offset +
+ usage_index * field->report_size;
+ bat->report_size = field->report_size;
bat->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
bat->status = HID_BATTERY_UNKNOWN;
@@ -687,7 +702,8 @@ static void hidinput_update_battery(struct hid_device *dev, int report_id,
}
#else /* !CONFIG_HID_BATTERY_STRENGTH */
static int hidinput_setup_battery(struct hid_device *dev, unsigned report_type,
- struct hid_field *field, bool is_percentage)
+ struct hid_field *field, unsigned int usage_index,
+ bool is_percentage)
{
return 0;
}
@@ -1035,7 +1051,8 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
break;
case 0x3b: /* Battery Strength */
- hidinput_setup_battery(device, HID_INPUT_REPORT, field, false);
+ hidinput_setup_battery(device, HID_INPUT_REPORT, field,
+ usage_index, false);
usage->type = EV_PWR;
return;
@@ -1313,7 +1330,8 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
case HID_UP_GENDEVCTRLS:
switch (usage->hid) {
case HID_DC_BATTERYSTRENGTH:
- hidinput_setup_battery(device, HID_INPUT_REPORT, field, false);
+ hidinput_setup_battery(device, HID_INPUT_REPORT, field,
+ usage_index, false);
usage->type = EV_PWR;
return;
}
@@ -1322,7 +1340,8 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
case HID_UP_BATTERY:
switch (usage->hid) {
case HID_BAT_ABSOLUTESTATEOFCHARGE:
- hidinput_setup_battery(device, HID_INPUT_REPORT, field, true);
+ hidinput_setup_battery(device, HID_INPUT_REPORT, field,
+ usage_index, true);
usage->type = EV_PWR;
return;
case HID_BAT_CHARGING:
@@ -2040,7 +2059,7 @@ static void report_features(struct hid_device *hid)
/* Verify if Battery Strength feature is available */
if (usage->hid == HID_DC_BATTERYSTRENGTH)
hidinput_setup_battery(hid, HID_FEATURE_REPORT,
- rep->field[i], false);
+ rep->field[i], j, false);
if (drv->feature_mapping)
drv->feature_mapping(hid, rep->field[i], usage);
diff --git a/include/linux/hid.h b/include/linux/hid.h
index b240baa95ab5..396de1080fdc 100644
--- a/include/linux/hid.h
+++ b/include/linux/hid.h
@@ -642,6 +642,9 @@ enum hid_battery_status {
* @max: maximum battery value from HID descriptor
* @report_type: HID report type (input/feature)
* @report_id: HID report ID for this battery
+ * @report: HID report containing the capacity field
+ * @report_offset: bit offset of the capacity field in the report
+ * @report_size: size of the capacity field in bits
* @charge_status: current charging status
* @status: battery reporting status
* @capacity: current battery capacity (0-100)
@@ -657,6 +660,9 @@ struct hid_battery {
__s32 max;
__s32 report_type;
__s32 report_id;
+ struct hid_report *report;
+ __u32 report_offset;
+ __u32 report_size;
__s32 charge_status;
enum hid_battery_status status;
__s32 capacity;
--
2.55.0
next prev parent reply other threads:[~2026-07-12 4:47 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-12 4:47 [PATCH 0/2] HID: fix battery queries with nonzero field offsets Mason Camara
2026-07-12 4:47 ` Mason Camara [this message]
2026-07-12 4:47 ` [PATCH 2/2] selftests/hid: test " Mason Camara
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=20260712044702.893825-2-ping@masoncamara.com \
--to=ping@masoncamara.com \
--cc=bentiss@kernel.org \
--cc=jikos@kernel.org \
--cc=johnchen902@gmail.com \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=shuah@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