From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ot1-f41.google.com (mail-ot1-f41.google.com [209.85.210.41]) (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 C6EDF36E48E for ; Thu, 2 Jul 2026 19:22:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783020147; cv=none; b=OjnViZEHLGehx0jSjUBFnUwTdffChy56Ud03cF7QmXsjB+4JZiNLRXbBZfSaB600Z7//GAELMNVStZ2VsxcsXXpSZigKhXVU4bmry2rKh1vObdygS6npQIEf5jhCsc7vnMbbXo9SbzU/WLfUQKdkXQpMd38Xaf+1Da/IBuAW980= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783020147; c=relaxed/simple; bh=lDg7mPaMrJuyA4a5JT047YdkCP7xV4CskykPTzhUGoI=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=C1FkTx7ldYsbzURL94sXHUD5rR2PnAqIqHtoz1szbz1icwuivxHZ0hr1wzn8+UW9B04/0YeTgtJqsT54kuMpOV9L7YC/+6uyuHTRmt1ViTrrZwNrGReollplTjD0ZMinxrhEFVbwUCLSty993xqijXtY+c7yakegKzfLoKhP8cE= 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=lskvkdVA; arc=none smtp.client-ip=209.85.210.41 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="lskvkdVA" Received: by mail-ot1-f41.google.com with SMTP id 46e09a7af769-7e9ed64d490so923879a34.3 for ; Thu, 02 Jul 2026 12:22:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783020144; x=1783624944; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=3QmiCi8nlDSo8AinNYFGBg/s2BVFKV/5OCNteoXpkGk=; b=lskvkdVACGn4T35T2QvVww2DPq+ueEu04K3TpQrvWCf09X8/RQJFGUchj9vTAlZaKq xA3aMZxEwmMh5w/yWeESjyKXj0QubkRwv5mQgdht1qiBjT++OPPM+Fvi5mY2YBRydTxg FBxTa77GnRHTv3Ksig8cD1OTqGFil6UdGghlqnK0U19jSEdQbbOhXi4Jl3MRY/s7homO lmRsr2WwfRNJBqFS1Q94K+hNKbe+sHPi8IKnMmGgfSLH5OanqDz1cPJCLrXe1eZVd0IT 40GYLexsDWlTWbOilmA1pkY+I/sczpL/iVgi33UlancLMRyzrFfZifIIYj4YcBpTLTZH Z0jA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783020144; x=1783624944; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=3QmiCi8nlDSo8AinNYFGBg/s2BVFKV/5OCNteoXpkGk=; b=U932BVm4szuGuZX7w9fktoIYTM9j0pGmH4e8pVIDMHpUOtLdREdnFYqddPY58EWx7i 5dW2eVil3CX4bAIKHa6A+Gh6UBG985D6li+TPYaAAmC94RIk06heVu/vpg7Kci/uuBA8 heU9VJXG16UKpFIFBdyYn73wALkpwnoqV9fhsAKEYPPwUilH3yZsBBHcmzWVxcYHBY/6 OvNs8RqnIDMnw+Uu3AmFtcic/QqPTcxqrOVKSIDJb5eWQ/5TnVxhn9ihIeFVmIT84bCj ekdvwAtAEwNYCaGv3LCVyfKVBZ8Lt/pJ29uWz4vvoV5rZ6xDHc95mOUxkuHLUxQnCUop 6nKw== X-Forwarded-Encrypted: i=1; AFNElJ+kSHiOxVzA8qUzruz8qIZkdHDule1yvsv6OvOmEqW7yts6DrYY4njQwtwy1g7HysiTBg4e2ysEtyfGVaM=@vger.kernel.org X-Gm-Message-State: AOJu0YwuJuFQN8ELqlrZuSZMxl6LCYHZrn6yhx+i96He4yBTILOuqp/N z1ASWIgPfBy4A6EjZXe3PKn7rRJMmEAjsYpJmuPu/BYxgcBRYusXlmLz5HB50A3923o= X-Gm-Gg: AfdE7clAg9vSNPj4owarofAcA2PZf8zK5T8892ppUDDukvqbmhafrz5nmxNqajKQKTg XdaOdeR1VQPj276uFIezADtUkSiLcZSyW+oLXxXAqJGO6uLJ3YRtnnpb7MXby1v/ioJdVa6/F3h g9mW/67Ch0j623FBpjrWnMRyaWXA1uoklZO/tpF7iswmvJcq7grN7DIFIEeh1nL03Pu+oO09ue9 5kM1wRicZsaO2LXkGEmEoZynn97UK5ywiUKi4QnytKd7G6AYvh2n3lK02SIPVsnj2arHc4AIsb9 28ireARLJ1WDHNjmu9kjkstEJ5jyL6R/uxdFbQ0G/M6HGAyBC74S9lABEt0BVQlSSdGPPXXAEdv 0P6m25Fg85J4fveay83VEAhYpJoBBpg9XCvsB+5HS4zBum5JaXWffZXcBubul2lOz0lILDUQgV4 1dnlbbNUwLsW7N X-Received: by 2002:a05:6830:448a:b0:7e9:eb13:d042 with SMTP id 46e09a7af769-7eb48ae8a36mr4552908a34.8.1783020143675; Thu, 02 Jul 2026 12:22:23 -0700 (PDT) Received: from desktop ([2806:2f0:9260:f072:92d3:78b1:9863:130a]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-7eb544a26basm3238309a34.18.2026.07.02.12.22.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Jul 2026 12:22:23 -0700 (PDT) From: =?UTF-8?q?Jose=20Villase=C3=B1or=20Montfort?= To: Jiri Kosina , Benjamin Tissoires Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, =?UTF-8?q?Jose=20Villase=C3=B1or=20Montfort?= Subject: [PATCH] HID: input: read battery capacity from its actual report offset Date: Thu, 2 Jul 2026 13:21:39 -0600 Message-ID: <20260702192139.114809-1-pepemontfort@gmail.com> X-Mailer: git-send-email 2.54.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. The Apple Magic Trackpad 2 (USB-C, 004C:0324) precedes the AbsoluteStateOfCharge byte with a byte of status flags in its battery input report (report 0x90). Over Bluetooth the capacity is obtained through this generic query path, so it returns the flags byte instead of the charge level and reports a bogus, near-constant value (~4%) regardless of the real charge. A raw query returns: report 0x90 -> [0x90, 0x04, 0x31] ^flags ^SoC = 0x31 = 49% while the device is actually at ~49%. (Over USB the battery is fetched by the magicmouse driver's own path and is unaffected.) 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]). Signed-off-by: Jose VillaseƱor Montfort --- Tested on an Apple Magic Trackpad 2 (USB-C, 004C:0324) over Bluetooth on v7.1.0: /sys/class/power_supply/hid-*-battery/capacity went from a stuck 4% to the real ~49%, matching what the device reports over USB. drivers/hid/hid-input.c | 17 +++++++++++++---- include/linux/hid.h | 2 ++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/drivers/hid/hid-input.c b/drivers/hid/hid-input.c index 3487600ca..b55cbe7f6 100644 --- a/drivers/hid/hid-input.c +++ b/drivers/hid/hid-input.c @@ -432,17 +432,25 @@ static int hidinput_scale_battery_capacity(struct hid_battery *bat, 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, @@ -593,6 +601,7 @@ 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_offset = field->report_offset; bat->charge_status = POWER_SUPPLY_STATUS_DISCHARGING; bat->status = HID_BATTERY_UNKNOWN; diff --git a/include/linux/hid.h b/include/linux/hid.h index 47dc0bc89..51b21f980 100644 --- 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; base-commit: b7556c8e713c88596046a906c7c4385218d44736 -- 2.54.0