From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: stable@vger.kernel.org
Cc: "Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
patches@lists.linux.dev,
"Jose Villaseñor Montfort" <pepemontfort@gmail.com>,
"Alec Hall" <signshop.alec@gmail.com>,
"Jiri Kosina" <jkosina@suse.com>
Subject: [PATCH 7.2 74/82] HID: input: read battery capacity from its actual report offset
Date: Tue, 25 Aug 2026 15:26:01 +0200 [thread overview]
Message-ID: <20260825132544.390157109@linuxfoundation.org> (raw)
In-Reply-To: <20260825132541.560541185@linuxfoundation.org>
7.2-stable review patch. If anyone has any objections, please let me know.
------------------
From: Jose Villaseñor Montfort <pepemontfort@gmail.com>
commit d07644524b6511b622ee7b0e2e68c9ee43d522a4 upstream.
hidinput_query_battery_capacity() assumes the state-of-charge value is
the first byte following the report ID (buf[1]) and ignores where the
battery field actually sits within the report.
An Apple Magic Trackpad 2 precedes the AbsoluteStateOfCharge byte with a
byte of status flags in its battery reports, so this query returns the
flags byte instead of the charge level.
The device happens to make that easy to observe, because it exposes the
same cell twice: its report descriptor declares AbsoluteStateOfCharge in
two reports (0x90 and 0x9b), so hidinput_setup_battery() registers two
power supplies. Only the first one is refreshed by hid-magicmouse -- it
uses hid_get_battery(), which returns the first battery of the list --
and that refresh goes through the report event path, which parses the
field correctly. Nothing ever reports the second one, so every read of
its capacity takes the query path above. On a USB-C Magic Trackpad over
USB, on an unpatched 7.1.5:
hid-<serial>-battery-144 = 100% (Charging) <- report event path
hid-<serial>-battery-155 = 3% (Discharging) <- query path
Both are the same physical battery. A raw HIDIOCGINPUT of the two
reports at that same moment:
report 0x90 -> [90 03 64]
report 0x9b -> [9b 03 64 64 00 00 10 00 00 00 00 00 00 00]
^flags ^SoC = 0x64 = 100%
The device answers correctly in both cases; only the offset the kernel
reads the capacity from is wrong. 0x03 is the flags byte (present,
charging), reported as "3%".
Bluetooth takes the same query path for its capacity, where the trackpad
reported a bogus near-constant ~4% -- 0b100, the FullyCharged flag --
regardless of the real charge.
Store the battery field's offset within the report at setup time and use
it when querying, so the capacity is read from its real position. The
report event path already parses the field correctly through the HID
core; only the explicit GET_REPORT query was wrong.
Devices whose capacity field is the first field in the report have a
report_offset of 0 and are unaffected (buf[1 + 0] == buf[1]).
Fixes: 581c4484769e ("HID: input: map digitizer battery usage")
Cc: stable@vger.kernel.org
Signed-off-by: Jose Villaseñor Montfort <pepemontfort@gmail.com>
Reviewed-by: Alec Hall <signshop.alec@gmail.com>
Signed-off-by: Jiri Kosina <jkosina@suse.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
drivers/hid/hid-input.c | 17 +++++++++++++----
include/linux/hid.h | 2 ++
2 files changed, 15 insertions(+), 4 deletions(-)
--- a/drivers/hid/hid-input.c
+++ b/drivers/hid/hid-input.c
@@ -435,17 +435,25 @@ static int hidinput_scale_battery_capaci
static int hidinput_query_battery_capacity(struct hid_battery *bat)
{
int ret;
+ /*
+ * The capacity field may not be the first field in the report: some
+ * devices (e.g. the Apple Magic Trackpad 2 over Bluetooth) precede it
+ * with status flags. Read it from its actual byte offset in the report
+ * (report_offset is in bits; the leading byte is the report id).
+ */
+ int offset = 1 + bat->report_offset / 8;
+ int len = offset + 1;
- u8 *buf __free(kfree) = kmalloc(4, GFP_KERNEL);
+ u8 *buf __free(kfree) = kmalloc(max(len, 4), 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, max(len, 4),
bat->report_type, HID_REQ_GET_REPORT);
- if (ret < 2)
+ if (ret < len)
return -ENODATA;
- return hidinput_scale_battery_capacity(bat, buf[1]);
+ return hidinput_scale_battery_capacity(bat, buf[offset]);
}
static int hidinput_get_battery_property(struct power_supply *psy,
@@ -596,6 +604,7 @@ static int hidinput_setup_battery(struct
bat->max = max;
bat->report_type = report_type;
bat->report_id = field->report->id;
+ bat->report_offset = field->report_offset;
bat->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
bat->status = HID_BATTERY_UNKNOWN;
--- a/include/linux/hid.h
+++ b/include/linux/hid.h
@@ -642,6 +642,7 @@ 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_offset: bit offset of the capacity field within its report
* @charge_status: current charging status
* @status: battery reporting status
* @capacity: current battery capacity (0-100)
@@ -657,6 +658,7 @@ struct hid_battery {
__s32 max;
__s32 report_type;
__s32 report_id;
+ __s32 report_offset;
__s32 charge_status;
enum hid_battery_status status;
__s32 capacity;
next prev parent reply other threads:[~2026-08-25 13:32 UTC|newest]
Thread overview: 95+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-25 13:24 [PATCH 7.2 00/82] 7.2.1-rc1 review Greg Kroah-Hartman
2026-08-25 13:24 ` [PATCH 7.2 01/82] PCI: host-generic: Fix NULL pointer dereference on 32-bit CAM systems Greg Kroah-Hartman
2026-08-25 13:24 ` [PATCH 7.2 02/82] Bluetooth: RFCOMM: take rfcomm_mutex for the deferred setup accept Greg Kroah-Hartman
2026-08-25 13:24 ` [PATCH 7.2 03/82] iommu/tegra241-cmdqv: Fix CMD_SYNC use-after-free on teardown Greg Kroah-Hartman
2026-08-25 13:24 ` [PATCH 7.2 04/82] iommu/iommufd: Fix NULL pointer deref in iommufd_ioas_change_process when racing with iopt_map_file_pages Greg Kroah-Hartman
2026-08-25 13:24 ` [PATCH 7.2 05/82] ALSA: FCP: Use a private URB for the notification endpoint Greg Kroah-Hartman
2026-08-25 13:24 ` [PATCH 7.2 06/82] ALSA: scarlett2: " Greg Kroah-Hartman
2026-08-25 13:24 ` [PATCH 7.2 07/82] rndis_host: add overflow check in rndis_rx_fixup() Greg Kroah-Hartman
2026-08-25 13:24 ` [PATCH 7.2 08/82] nvmet: fix NULL pointer dereference in nvmet_execute_identify_nslist() Greg Kroah-Hartman
2026-08-25 13:24 ` [PATCH 7.2 09/82] futex/pi: Reject cross-mm private futex owners Greg Kroah-Hartman
2026-08-25 13:24 ` [PATCH 7.2 10/82] futex: Sanitize and document task_struct::futex::state transitions Greg Kroah-Hartman
2026-08-25 13:24 ` [PATCH 7.2 11/82] futex/pi: Plug private futex exec() race Greg Kroah-Hartman
2026-08-25 13:24 ` [PATCH 7.2 12/82] futex: Avoid private hash use-after-free on final put Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 13/82] futex: Fix race on the initial mm->futex.phash.ref allocation Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 14/82] io_uring/futex: dont mark futex wake requests as inflight Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 15/82] io_uring/futex: only mark private futex waits " Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 16/82] ALSA: dummy: Check card index validity at probe Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 17/82] io_uring/cmd: fix iovec leak when the async cmd is not recycled Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 18/82] io_uring/io-wq: fix worker accounting when canceling creation callbacks Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 19/82] io_uring/rsrc: fix folio size overflow in io_vec_fill_bvec() Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 20/82] io_uring/uring_cmd: dont skip completion for a synchronous multishot cmd Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 21/82] io_uring: defer eventfd signaling when queued from a wakeup handler Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 22/82] ocfs2: fix missing metadata reservation for large xattrs Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 23/82] null_blk: fix UBSAN shift-out-of-bounds when zone_size is 0 or overflows Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 24/82] kcov: fix data corruption and race conditions on PREEMPT_RT Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 25/82] ext4: stop retrying saturated xattr cache entries Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 26/82] nilfs2: reject invalid block index in GC ioctl Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 27/82] ext4: clear error before retrying inode xattr space fallback Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 28/82] ext4: avoid tail write_begin walk for uptodate folios Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 29/82] ext4: propagate errors from fast commit range replay Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 30/82] ext4: dont enable DAX on new encrypted files Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 31/82] ext4: fix incorrect function call when initializing s_resgid Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 32/82] xfs: validate attr entry pointer before field access Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 33/82] xfs: restore nofs context unconditionally in xfs_trans_roll Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 34/82] nfc: digital: clamp SENSF_RES length to the destination buffer Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 35/82] nfc: fdp: bound the device-reported read length and fix an skb leak Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 36/82] nfc: microread: validate target discovery payload lengths Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 37/82] nfc: llcp: bound the connect_sn TLV walk to the skb Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 38/82] nfc: llcp: fix OOB read and u8 offset wrap in TLV parsers Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 39/82] nfc: llcp: reject PDUs shorter than the LLCP header Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 40/82] nfc: pn533: purge fragmented skbs during cleanup Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 41/82] nfc: st21nfca: validate ATR_REQ length against the received frame Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 42/82] nfc: nci: add data_len bound checks to activation parameter extractors Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 43/82] nfc: nci: fix out-of-bounds write in nci_target_auto_activated() Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 44/82] nfc: nci: fix uninit-value in the RF discover/activated NTF handlers Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 45/82] nfc: nci: free destination parameters when closing a connection Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 46/82] ipv4: reject undersized MTUs in ip_do_fragment() Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 47/82] ipv6: fix use-after-free in ip6_finish_output2() Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 48/82] mailbox: mchp-ipc-sbi: Add null check for devm_kasprintf() Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 49/82] dmaengine: fsl-edma: Add error handling for devm_kasprintf Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 50/82] nvmet-auth: zero the AUTH_RECEIVE response buffer Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 51/82] nvmet-fc: fix invalid free in LS IOD error path Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 52/82] nvmet-tcp: bound SGL data length before allocating command buffers Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 53/82] nvmet-tcp: Do not WARN on remotely-controlled oversized SGL allocations Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 54/82] nvmet: pci-epf: fix use-after-free in nvmet_pci_epf_exec_iod_work() Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 55/82] nvmet: pci-epf: put CQ ref on create_cq mapping failure Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 56/82] HID: magicmouse: fix battery reporting for Bluetooth Magic Trackpad USB-C Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 57/82] HID: magicmouse: prevent unbounded recursion in magicmouse_raw_event() Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 58/82] HID: magicmouse: re-enable multitouch after reset-resume Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 59/82] HID: magicmouse: do not keep a stale msc->input if no input is claimed Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 60/82] HID: core: fix OOB read of field->usage in hid_set_field() Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 61/82] HID: pidff: fix OOB write when hid->inputs is empty Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 62/82] net/ionic: avoid OOB TX partner lookup for hwstamp RXQ Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 63/82] HID: asus: fix missing hid_is_usb() check Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 64/82] HID: huawei: " Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 65/82] HID: nintendo: fix out-of-bounds read in joycon_ctlr_read_handler() Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 66/82] HID: nintendo: register input device after capabilities are set Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 67/82] HID: nintendo: stop device IO before hid_hw_stop on probe failure Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 68/82] HID: rapoo: fix missing hid_is_usb() check Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 69/82] HID: core: fix number/pointer type confusion on long items Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 70/82] HID: ft260: fix stack-use-after-return write in I2C read race Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 71/82] HID: sensor: custom: Fix use-after-free in enable_sensor Greg Kroah-Hartman
2026-08-25 13:25 ` [PATCH 7.2 72/82] HID: uclogic: fix use-after-free of inrange_timer on remove Greg Kroah-Hartman
2026-08-25 13:26 ` [PATCH 7.2 73/82] HID: hyperv: validate initial device info bounds Greg Kroah-Hartman
2026-08-25 13:26 ` Greg Kroah-Hartman [this message]
2026-08-25 13:26 ` [PATCH 7.2 75/82] drm/xe: Dont hand out the flat CCS storage as usable VRAM Greg Kroah-Hartman
2026-08-25 13:26 ` [PATCH 7.2 76/82] Bluetooth: hci_event: fix LE list UAF on reset Greg Kroah-Hartman
2026-08-25 13:26 ` [PATCH 7.2 77/82] Bluetooth: hci_event: validate LE Set CIG Parameters response Greg Kroah-Hartman
2026-08-25 13:26 ` [PATCH 7.2 78/82] Bluetooth: hci_sync: Fix accept list UAF during suspend Greg Kroah-Hartman
2026-08-25 13:26 ` [PATCH 7.2 79/82] Bluetooth: ISO: do not force BT_LISTEN after a failed BIG sync Greg Kroah-Hartman
2026-08-25 13:26 ` [PATCH 7.2 80/82] Bluetooth: ISO: zero the sockaddr before returning it in getname Greg Kroah-Hartman
2026-08-25 13:26 ` [PATCH 7.2 81/82] Bluetooth: MGMT: reject HCI_CMD_SYNC params_len above 255 Greg Kroah-Hartman
2026-08-25 13:26 ` [PATCH 7.2 82/82] Bluetooth: hci_aml: validate firmware segment lengths Greg Kroah-Hartman
2026-08-25 15:47 ` [PATCH 7.2 00/82] 7.2.1-rc1 review Ronald Warsow
2026-08-25 21:40 ` Justin Forbes
2026-08-26 0:02 ` Florian Fainelli
2026-08-26 0:02 ` Shuah Khan
2026-08-26 5:57 ` Ron Economos
2026-08-26 7:57 ` Barry K. Nathan
2026-08-26 10:32 ` Brett A C Sheffield
2026-08-26 12:32 ` Miguel Ojeda
2026-08-26 18:03 ` Krzysztof Wilczyński
2026-08-26 19:33 ` Peter Schneider
2026-08-26 19:38 ` Benjamin Boortz
2026-08-27 12:18 ` Mark Brown
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=20260825132544.390157109@linuxfoundation.org \
--to=gregkh@linuxfoundation.org \
--cc=jkosina@suse.com \
--cc=patches@lists.linux.dev \
--cc=pepemontfort@gmail.com \
--cc=signshop.alec@gmail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.