From nobody Sat Jul 25 19:28:28 2026 Received: from mail-qk1-f170.google.com (mail-qk1-f170.google.com [209.85.222.170]) (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 8B303363C6B for ; Tue, 14 Jul 2026 10:12:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784023967; cv=none; b=rlb7BYQHiAT8SF8z0jEDaBgCYi9Nr/8blAXOpTEnv7LsMmMJ4ZXjIpUz57Q2uZoUFL5nYSjHA1ZnJ3bNeqNT4jUHvSK0bWrtkTyZTPze8YQjXjjTk7xwlYn5t+nxrk7THyd/yslenpxHe0L5XTxP2QIC8wZVVPGFQ8Nywa2LuJs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784023967; c=relaxed/simple; bh=J0UlSkJ/Kgu/J9BJDifAWIGP6dDakAVWK8bjDCL9MFk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XOBCc7blnLF0oCewHiijEWRW1ZQM8jTYxzQDGdxUiCoZXoWBw1YEtvsDGMIE0/XqfcLJNp8U4JEGdOBFezL2G9G0/hLwICOcmCj68YTSaPmDwwj4J5M3kLgrozSGQvzaauLAmv1f8TQrFMZ2x4PXbyMta2vSEzht412zD/AK8sU= 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=IcOe5LWU; arc=none smtp.client-ip=209.85.222.170 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="IcOe5LWU" Received: by mail-qk1-f170.google.com with SMTP id af79cd13be357-9305a2121a1so85529585a.2 for ; Tue, 14 Jul 2026 03:12:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784023964; x=1784628764; 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:content-type; bh=fboD69W58/lw8bBMlFoHEiwTIiYQTbtsU798kq+SBeI=; b=IcOe5LWUapRxVKKpJfudAmM/2Df97Ie6W8nmqdILq54R2RoMOLLXUPoUQLkBv1Vw8Q BZJ+IeC33ZW/E5Uksnsm5qo0ME7iSvtoNe++PZ7NZgk3HHzfEgvuM377mFa08Bn76S6i 73jrdUTpU4pPcZPWrF8Hc6LaW+FbKkILOPVcumF+4VK51lyvK1NA0AeCdj2AI84ri7z9 Fg6734Xe8fG3dOdFXQYORN1ZpL6tyIn9UiWe2tekrOUZLVKOqes9aV/4lnndkSYjNHVa cXYm/sgKeF7qkPk4xl24PMk8rbbXtZfXW3LFbvEz1/kwBFNniSg25/l0m0+p2q5XyQ0s RG4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784023964; x=1784628764; 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:content-type; bh=fboD69W58/lw8bBMlFoHEiwTIiYQTbtsU798kq+SBeI=; b=Sc8740aPAR1ePBZ1zE8g6kbaqLO51ktmHwh/RAqTRTYPIprndQmyAoGrlPv99mgdBj 45OItxKgA6klF2AVbgzeJVul3pG7KKTDZw67dN4h7RdhkeduEuR55IkdkHk8RQCP5ghZ 8orOLfKepdhg5DSqq8o2NkI7yx8CaOkPJLn4kfMRy/35sqFcsAjmLw1hLsx7zqGLV+uF uYby0lpO0trv+rdiIwbB3wPL116oZXT5U1NvqZgjovTkJc0REBsTRt/yD/XUi36FX38+ UyscOY8Sq257r/IAGIsrSZTqRQHYlV4XSKP34D36fCou0EbUF0/Bjd/tvUdMiN+5N5J4 dr7g== X-Gm-Message-State: AOJu0Yyoal7HMh6eIBm2n6aqLGkcE/Q3s67vG3Y8fPkGI4MRlonGGABv Bg+tw9mGTYYjTUPn5F6CvnVXFXCO0+p6mETiDk7mBseEjyHDxRKYZjn1 X-Gm-Gg: AfdE7ckdPGp1Sc58bGgKZE8ZK0Bvn5UwTl7utekMJL+5AEU1Ogac40olh6hFC7Yo+Oy v1Jvzjs8aaG/yvSKYwql6WvLwhCb2evH6QXTp0EjgKBBkBAPA2eaYrP+RXrWGkk/YCqFuI3fjmA l6PTBH5QsteYWeoZzGyRAUhaV2v902aMAqPyuajCClytCuIXjne9kjXTGme5WFUZf/qMlJ0+Wy2 LdDCoBtBGG4HCdCjt4VjUxq8KuGXv33AsjP7Rggkv1KMKbpj5Qb6/MOOkUNAjADh2yX6MSM6TTn L3nvECtZczNk41SeXmbzSrQMIIsEzkm7HvaxuvPq67mzuVYK3vTsSNW+qfdc6fvRbjdEG/1DZPB 8UQ80gljC9lcG9NwqBDgt4kZUAPe8aY1lLQt5WCUX/QLpwRIlqZ8KeVNx8EyXWciANkcbzLNEi/ gUwlxgQnIxWcYFSKrmZti8V+y7gwYORLIVupzJbXIwj/xd X-Received: by 2002:a05:620a:4056:b0:92e:cc01:a67e with SMTP id af79cd13be357-92ef2ccac2emr1323685285a.76.1784023964155; Tue, 14 Jul 2026 03:12:44 -0700 (PDT) Received: from FairplayBox ([2601:5cf:837e:d920::f9a2]) by smtp.gmail.com with ESMTPSA id af79cd13be357-92ee5cfcb0dsm1463056185a.25.2026.07.14.03.12.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Jul 2026 03:12:43 -0700 (PDT) From: Alec Hall To: Jiri Kosina , Benjamin Tissoires , linux-input@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Alec Hall Subject: [PATCH 1/3] HID: apple: report Magic Keyboard battery over Bluetooth Date: Tue, 14 Jul 2026 06:12:33 -0400 Message-ID: <20260714101235.99447-2-signshop.alec@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260714101235.99447-1-signshop.alec@gmail.com> References: <20260714101235.99447-1-signshop.alec@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" The Magic Keyboard answers a GET_REPORT for battery input report 0x90 over Bluetooth exactly as it does over USB, but battery reporting was only ever enabled for USB: the Bluetooth device-table entry lacks the APPLE_RDESC_BATTERY quirk, so apple_fetch_battery() returns early and the kernel's power_supply is stuck at 0%. The USB-only restriction is not cosmetic. apple_fetch_battery() issues its GET_REPORT through hid_hw_request(), and over Bluetooth that goes through the uhid transport, whose raw_request sleeps waiting on user space. The fetch was driven from a timer callback (apple_battery_timer_tick), which runs in atomic softirq context where sleeping is forbidden, so requesting the battery there deadlocks the machine: run_timer_softirq __run_timer_base apple_battery_timer_tick __hid_request uhid_hid_raw_request /* sleeps in atomic context */ Move the periodic battery fetch to a delayed work item, which runs in process context and may sleep, and set APPLE_RDESC_BATTERY on the Bluetooth Magic Keyboard 2021 entry. The descriptor fixup guarded by the quirk only fires for the 83-byte USB report descriptor, so enabling the quirk over Bluetooth does not disturb the (different) Bluetooth descriptor. Also stop returning early once capacity reaches max: over Bluetooth the device sends no unsolicited updates, so the driver must keep polling to notice the level fall after a full charge. Only the plain Magic Keyboard 2021 is enabled and tested here; the fingerprint, numpad and 2024 Bluetooth variants likely need the same change but were not available to test. Assisted-by: Claude:claude-opus-4-8 Signed-off-by: Alec Hall --- drivers/hid/hid-apple.c | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c index bf7dd0fbf249..e46a59844a30 100644 --- a/drivers/hid/hid-apple.c +++ b/drivers/hid/hid-apple.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -116,7 +117,7 @@ struct apple_sc { unsigned int fn_on; unsigned int fn_found; DECLARE_BITMAP(pressed_numlock, KEY_CNT); - struct timer_list battery_timer; + struct delayed_work battery_work; struct apple_sc_backlight *backlight; }; =20 @@ -635,9 +636,6 @@ static int apple_fetch_battery(struct hid_device *hdev) if (!report || report->maxfield < 1) return -1; =20 - if (bat->capacity =3D=3D bat->max) - return -1; - hid_hw_request(hdev, report, HID_REQ_GET_REPORT); return 0; #else @@ -645,15 +643,20 @@ static int apple_fetch_battery(struct hid_device *hde= v) #endif } =20 -static void apple_battery_timer_tick(struct timer_list *t) +static void apple_battery_work(struct work_struct *work) { - struct apple_sc *asc =3D timer_container_of(asc, t, battery_timer); + struct apple_sc *asc =3D container_of(work, struct apple_sc, battery_work= .work); struct hid_device *hdev =3D asc->hdev; =20 - if (apple_fetch_battery(hdev) =3D=3D 0) { - mod_timer(&asc->battery_timer, - jiffies + secs_to_jiffies(APPLE_BATTERY_TIMEOUT_SEC)); - } + /* + * Runs in process context (workqueue), so the battery GET_REPORT is + * allowed to sleep. This is required for the uhid/Bluetooth transport, + * whose raw_request blocks waiting on userspace -- unlike a timer_list + * callback, which runs in atomic softirq context and would deadlock. + */ + if (apple_fetch_battery(hdev) =3D=3D 0) + schedule_delayed_work(&asc->battery_work, + secs_to_jiffies(APPLE_BATTERY_TIMEOUT_SEC)); } =20 /* @@ -968,10 +971,9 @@ static int apple_probe(struct hid_device *hdev, } =20 if (quirks & APPLE_RDESC_BATTERY) { - timer_setup(&asc->battery_timer, apple_battery_timer_tick, 0); - mod_timer(&asc->battery_timer, - jiffies + secs_to_jiffies(APPLE_BATTERY_TIMEOUT_SEC)); - apple_fetch_battery(hdev); + INIT_DELAYED_WORK(&asc->battery_work, apple_battery_work); + /* Kick an initial fetch; the work re-arms itself every timeout. */ + schedule_delayed_work(&asc->battery_work, 0); } =20 if (quirks & APPLE_BACKLIGHT_CTL) @@ -987,7 +989,7 @@ static int apple_probe(struct hid_device *hdev, =20 out_err: if (quirks & APPLE_RDESC_BATTERY) - timer_delete_sync(&asc->battery_timer); + cancel_delayed_work_sync(&asc->battery_work); =20 hid_hw_stop(hdev); return ret; @@ -998,7 +1000,7 @@ static void apple_remove(struct hid_device *hdev) struct apple_sc *asc =3D hid_get_drvdata(hdev); =20 if (asc->quirks & APPLE_RDESC_BATTERY) - timer_delete_sync(&asc->battery_timer); + cancel_delayed_work_sync(&asc->battery_work); =20 hid_hw_stop(hdev); } @@ -1201,7 +1203,7 @@ static const struct hid_device_id apple_devices[] =3D= { { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_= 2021), .driver_data =3D APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK | APPLE_RDESC_BATT= ERY }, { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYB= OARD_2021), - .driver_data =3D APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK }, + .driver_data =3D APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK | APPLE_RDESC_BATT= ERY }, { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_= FINGERPRINT_2021), .driver_data =3D APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK | APPLE_RDESC_BATT= ERY }, { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYB= OARD_FINGERPRINT_2021), --=20 2.55.0 From nobody Sat Jul 25 19:28:28 2026 Received: from mail-qk1-f169.google.com (mail-qk1-f169.google.com [209.85.222.169]) (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 467B036403C for ; Tue, 14 Jul 2026 10:12:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784023967; cv=none; b=MeLQ5pWXU64gaIgsL/I7aBdHrI8IXPeu9YwhiKOWglhnkyRBbEf7h9nSv6yeMFiSCIY4tMKT8OfG0PR5psyWGMo3BQnTOOsNBu1sABk3sNIdcQvCYmd7sZINAp/foLlOhtO7cm6bkNxkVUNH+WdMmg3u+K9ihKiUKMqufUQvCes= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784023967; c=relaxed/simple; bh=M3qI+r7hpBN01uwCIPljXnWSlB+ogpkoHJOtqNLrAHI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KygxwnfGuHFn5ER3yNnhbliGXFgxJFjfojbL77NCzRAbVX8P4/fzUtLrOmEvbrqnRtqoZ6uXHANQ/o5aE+k6QcHNR+ZT5zQS+oV5uSLdJSwMfzCdiM6Ty9bGlG+8VZOMnhlMmuqfJYHecvlZZiZ0yQl0AWwXFr1JH73modXAoKk= 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=Asd9JoAL; arc=none smtp.client-ip=209.85.222.169 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="Asd9JoAL" Received: by mail-qk1-f169.google.com with SMTP id af79cd13be357-92e6c4a867cso51613685a.0 for ; Tue, 14 Jul 2026 03:12:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784023965; x=1784628765; 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:content-type; bh=qqn3xnPudw35budYpsjbVJww8rbA3LMj5DFGAAy5AaQ=; b=Asd9JoALSlBVK6uKuRm7eLe7nkh+GDPhgn5E04lMIpDhWcQP7qhbzKdxs3A+US1Osr SspFeZm8RuefzigBvzVse1VDlUiKtn0aviNzFOS+N9DeenAgoA7YDZhnu3Qu9vn8ZGCV 3xcMseOUnnpugF0cYNOjTjR7TIv5Ty50MvcMcOIzkbsl+4TNQG1T3iq117zyhjDmPZ1P pxCMxAFA2O1EFG5l1vyxuFUeb0IOL0ZWEyUFP11LCy7iqV58g0I/JT5UuF5l0iJHonxz tb5CWmPtxqP+5m+3adR08rMmNRd5eA1ziuIZCS/iPqOxIpPJPMPE/HY23clG5zrLIKeS OcgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784023965; x=1784628765; 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:content-type; bh=qqn3xnPudw35budYpsjbVJww8rbA3LMj5DFGAAy5AaQ=; b=Rh3ClhBt7bHBrtW0haRVWIXc7lDIdcEp1FCHu8Z+chqjj+T9272pu3yEIl/NhFx3Gn dls+87h/GB6q0CrSM4t8yz8QR/1utinO4ErzkkvxKv+uu5EWhVqwm90J+MrOYH/OnnoT ZAvbczPQu+H+iVUD8eXjEkoUFaQfCjYOnG/teoEeG//Ec2BBqBzqUg/pps8y/9YE5WrV bmxWUZKjjJBL4y7F6HuWMFPZd7t4IGGY6eI4Nuoy5TyfvKM670SfBEAi9DW+C3hr8eVf oVGw3Om5hXdnOEK/0vk7/BAaqGeu3Pq527I+Eae1CxTr8thXPBBpZ3p5n4r0qZ41gRWe 0Lqg== X-Gm-Message-State: AOJu0Yw0dsRDppgqQ/7sncwn02XN/0aHSstv0KkD080p6Rfn8l18/kRv SkXgsGVvPXgy924sm7MJdBJHzPC5g6tS/PZuJzJj67O02jrgLHjrjxGqIcNpahJc+WCbnQ== X-Gm-Gg: AfdE7cnMPKTic6G4B/alNmOPPU5U9WBdsHYsmfYmKHX+vWOzYQPbLZtcev1Lp8X9yH8 JDWDP48zDlN/6swrh6uy2QBhrL7jyr8sokAH8ssipPSpS9/c7XtxlXbPsRHBwJVid1il93bATKi afx7qePTE6r9dgl9Ft5YeaSs7i/+I1CZ4mWPXdPXh+O6KXJhmLwAPQkQHwjc39gSa6wCt7t9sY4 uo1nUzbl5EL4gipSYn6QV1GFJGIMF3URhnz3bLqiOAc92WlrXal+vqke7DHpBwIq8cmrES5wzBl cN18llnL54jflX6hZGcdhaDZuGOGpMZqVT7pfvf9BJpMf6utWEymIIpDJ5JEltqO2fADeNKiy1j VSehWIHSopEAi6xag47znliJPNe/Zslz4oI33JB2G0O5LccXjGurJa2KyB/SIfOeS/iqk2wp8Mz OTTCmYPePE+2+bEnjMEtUzIVprcHJCd04r91z0s9yQIdi0 X-Received: by 2002:a05:620a:2951:b0:92e:c116:bef3 with SMTP id af79cd13be357-92ef2e69862mr1306029485a.80.1784023964879; Tue, 14 Jul 2026 03:12:44 -0700 (PDT) Received: from FairplayBox ([2601:5cf:837e:d920::f9a2]) by smtp.gmail.com with ESMTPSA id af79cd13be357-92ee5cfcb0dsm1463056185a.25.2026.07.14.03.12.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Jul 2026 03:12:44 -0700 (PDT) From: Alec Hall To: Jiri Kosina , Benjamin Tissoires , linux-input@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Alec Hall Subject: [PATCH 2/3] HID: magicmouse: report battery over Bluetooth Date: Tue, 14 Jul 2026 06:12:34 -0400 Message-ID: <20260714101235.99447-3-signshop.alec@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260714101235.99447-1-signshop.alec@gmail.com> References: <20260714101235.99447-1-signshop.alec@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" magicmouse_fetch_battery() only runs for USB Magic Mouse 2 / Magic Trackpad 2. Over Bluetooth the kernel never queries the battery and the power_supply reports 0%, even though the device answers a GET_REPORT for battery input report 0x90. (A Magic Trackpad 2 appears to work while charging only because it then pushes battery reports unsolicited; off the charger the reading goes stale.) As with hid-apple, the fetch cannot run from the battery timer over Bluetooth: hid_hw_request() sleeps on the uhid transport, but magicmouse_battery_timer_tick() runs in atomic softirq context. Move the periodic fetch to a delayed work item, add magicmouse_has_battery() and use it to arm the fetch for Magic Mouse 2 / Magic Trackpad 2 regardless of transport, and keep polling once full so the reading stays current over Bluetooth. Assisted-by: Claude:claude-opus-4-8 Signed-off-by: Alec Hall --- drivers/hid/hid-magicmouse.c | 62 +++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/drivers/hid/hid-magicmouse.c b/drivers/hid/hid-magicmouse.c index 802a3479e24b..6d0e76314b10 100644 --- a/drivers/hid/hid-magicmouse.c +++ b/drivers/hid/hid-magicmouse.c @@ -123,7 +123,7 @@ MODULE_PARM_DESC(report_undeciphered, "Report undeciphe= red multi-touch state fie * @tracking_ids: Mapping of current touch input data to @touches. * @hdev: Pointer to the underlying HID device. * @work: Workqueue to handle initialization retry for quirky devices. - * @battery_timer: Timer for obtaining battery level information. + * @battery_work: Delayed work for obtaining battery level information. */ struct magicmouse_sc { struct input_dev *input; @@ -148,7 +148,7 @@ struct magicmouse_sc { =20 struct hid_device *hdev; struct delayed_work work; - struct timer_list battery_timer; + struct delayed_work battery_work; }; =20 static int magicmouse_firm_touch(struct magicmouse_sc *msc) @@ -828,6 +828,19 @@ static bool is_usb_magictrackpad2(__u32 vendor, __u32 = product) product =3D=3D USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC; } =20 +/* + * Magic Mouse 2 and Magic Trackpad 2 expose a battery over both USB and + * Bluetooth (the pre-2 Magic Mouse/Trackpad do not). Transport-agnostic: + * over Bluetooth the vendor is BT_VENDOR_ID_APPLE but the product id matc= hes. + */ +static bool magicmouse_has_battery(struct hid_device *hdev) +{ + return hdev->product =3D=3D USB_DEVICE_ID_APPLE_MAGICMOUSE2 || + hdev->product =3D=3D USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC || + hdev->product =3D=3D USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 || + hdev->product =3D=3D USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC; +} + static int magicmouse_fetch_battery(struct hid_device *hdev) { #ifdef CONFIG_HID_BATTERY_STRENGTH @@ -836,9 +849,7 @@ static int magicmouse_fetch_battery(struct hid_device *= hdev) struct hid_battery *bat; =20 bat =3D hid_get_battery(hdev); - if (!bat || - (!is_usb_magicmouse2(hdev->vendor, hdev->product) && - !is_usb_magictrackpad2(hdev->vendor, hdev->product))) + if (!bat || !magicmouse_has_battery(hdev)) return -1; =20 report_enum =3D &hdev->report_enum[bat->report_type]; @@ -847,9 +858,6 @@ static int magicmouse_fetch_battery(struct hid_device *= hdev) if (!report || report->maxfield < 1) return -1; =20 - if (bat->capacity =3D=3D bat->max) - return -1; - hid_hw_request(hdev, report, HID_REQ_GET_REPORT); return 0; #else @@ -857,15 +865,21 @@ static int magicmouse_fetch_battery(struct hid_device= *hdev) #endif } =20 -static void magicmouse_battery_timer_tick(struct timer_list *t) +static void magicmouse_battery_work(struct work_struct *work) { - struct magicmouse_sc *msc =3D timer_container_of(msc, t, battery_timer); + struct magicmouse_sc *msc =3D container_of(work, struct magicmouse_sc, + battery_work.work); struct hid_device *hdev =3D msc->hdev; =20 - if (magicmouse_fetch_battery(hdev) =3D=3D 0) { - mod_timer(&msc->battery_timer, - jiffies + secs_to_jiffies(USB_BATTERY_TIMEOUT_SEC)); - } + /* + * Runs in process context (workqueue), so the battery GET_REPORT may + * sleep. This is required for the uhid/Bluetooth transport, whose + * raw_request blocks on userspace -- unlike a timer_list callback, + * which runs in atomic softirq context and would deadlock. + */ + if (magicmouse_fetch_battery(hdev) =3D=3D 0) + schedule_delayed_work(&msc->battery_work, + secs_to_jiffies(USB_BATTERY_TIMEOUT_SEC)); } =20 static int magicmouse_probe(struct hid_device *hdev, @@ -900,12 +914,10 @@ static int magicmouse_probe(struct hid_device *hdev, return ret; } =20 - if (is_usb_magicmouse2(id->vendor, id->product) || - is_usb_magictrackpad2(id->vendor, id->product)) { - timer_setup(&msc->battery_timer, magicmouse_battery_timer_tick, 0); - mod_timer(&msc->battery_timer, - jiffies + secs_to_jiffies(USB_BATTERY_TIMEOUT_SEC)); - magicmouse_fetch_battery(hdev); + if (magicmouse_has_battery(hdev)) { + INIT_DELAYED_WORK(&msc->battery_work, magicmouse_battery_work); + /* Kick an initial fetch; the work re-arms itself each timeout. */ + schedule_delayed_work(&msc->battery_work, 0); } =20 if (is_usb_magicmouse2(id->vendor, id->product) || @@ -973,9 +985,8 @@ static int magicmouse_probe(struct hid_device *hdev, =20 return 0; err_stop_hw: - if (is_usb_magicmouse2(id->vendor, id->product) || - is_usb_magictrackpad2(id->vendor, id->product)) - timer_delete_sync(&msc->battery_timer); + if (magicmouse_has_battery(hdev)) + cancel_delayed_work_sync(&msc->battery_work); =20 hid_hw_stop(hdev); return ret; @@ -987,9 +998,8 @@ static void magicmouse_remove(struct hid_device *hdev) =20 if (msc) { cancel_delayed_work_sync(&msc->work); - if (is_usb_magicmouse2(hdev->vendor, hdev->product) || - is_usb_magictrackpad2(hdev->vendor, hdev->product)) - timer_delete_sync(&msc->battery_timer); + if (magicmouse_has_battery(hdev)) + cancel_delayed_work_sync(&msc->battery_work); } =20 hid_hw_stop(hdev); --=20 2.55.0 From nobody Sat Jul 25 19:28:28 2026 Received: from mail-qk1-f175.google.com (mail-qk1-f175.google.com [209.85.222.175]) (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 BF1E23644C3 for ; Tue, 14 Jul 2026 10:12:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784023968; cv=none; b=JJ59XZurDyoME4SMlOW01YFiL/ihTY/GkZBP3pTvpReFcyYMhQM1yCE8OCqiv0nM0rmoFAbvPZ8qd/VQe5+fDCHMy+2QfywzXiqnsQKW9UIGbTkPs0Wpetac/qDfwQZqmXPLj9PVULH+1QNqVqaKD9N8ChTIMwdrINV4xFEhWrA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784023968; c=relaxed/simple; bh=2rfBDz6/aloAhda8Bjfwnb+C9SSh/0BYfqyneCTuzHE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ac3g+wvNaiRiz2+iSRB2l0H4yfp+hE1zZ/N94OxB2id4YTH08xd7vQ2yEjpd7up79ZQ4FD9JatI3z43qREtUhnpZIETTKNuE3H/9gTvhmBbgCT5JC0u0FOp68rI2cjpqLA5RO0fvYRFiYR8hrgrI9BuleZvzZ6WI1UXYheta5r0= 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=aHLPBcCx; arc=none smtp.client-ip=209.85.222.175 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="aHLPBcCx" Received: by mail-qk1-f175.google.com with SMTP id af79cd13be357-92e5cb052edso65087785a.2 for ; Tue, 14 Jul 2026 03:12:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784023965; x=1784628765; 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:content-type; bh=fYpfVNjMZg6VKmRPIHIJ/7Pqr/xSIkWL+N548aPvh7g=; b=aHLPBcCxuQVJjBfL7xnqp28wn4koh70LBcUjbdAS6ZLisC+LpHQme7JMV16jF9lLW9 lq3f7TlJTLFc5XSRNCAxlEUvTHwcW2211/BErjeqGjNYBP/e+F684jd6u4t4ROMQGCdP NQtkozEBArPyv7NBuxou2UFQ+/JbeY2dKVRsknjQCckqDBL/R9NruO7mzoXNIRCJil0l TN8BMnLNOYXNikOLIUqBiMXR/Va89e3eLi6KbRzoVy6fDGAaEBI9znojcD90Rgk4UyIL tIaj/B0iLUpsJzKeZT19vRVyLmCaaSM7Pgt32THT4zkS+cONc4grZjKIbknkED1+vf6p 5ccg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784023965; x=1784628765; 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:content-type; bh=fYpfVNjMZg6VKmRPIHIJ/7Pqr/xSIkWL+N548aPvh7g=; b=DvLe7GFV0me85YukvpoVqwwI+gQb9WGh+ME3Vekp2h31OHpgz3vJSdNyNMtLzxFDM0 VvEzFFGaKmD/l0WhSiNkOq8gVt83AI+V6KejJTsKv2lYUH1S1AWsbd/WXipPBtbVEIAA 0H9I1bRQQ/GtU91Gn9wOYfQlCz4cvwlo07zrK8MegwsiDcTAMrfmdVqGlN3WrIEEJsau Y7vLZP8S8GYpvvRmDdgur387cBvp4C4tx/R6hh9QhakGf+xu2oKD/7L/wsIChCkhv+PZ 8y9k0hGJQvuF/C51IX1n1canYKzWE3APW/vBAuPtUYJQnC+DmD7PU10OhwkgBHcgmDAJ Z9ng== X-Gm-Message-State: AOJu0YySxOSP6tixnpoTah15g5QAalhXZS+Q4v7OHgQJuLQY1VgXk+bt lR3yDL4Rj6vk0JndwnagjWTvjbLuixdCvZk1yBy1iI8cDOKs3FWaNNhq X-Gm-Gg: AfdE7cmA7KYTCfmrq4gxScwaITEQnGf29X1sPrC6EHsqzGUJwz1Btcyq5Ruo8T9AFfh jzq6+6+GAXIu+TDG3eRHqboBP+1vpKBSk7ucO8xyaz+H+BbXlVG0M/fhO4bWrvRi3JRG95r/A5I wwGZCTED+LcWv0frCp2ekEXXhGdlr6Fz9swlSlpOwHiAPoQIZCGOCokXQloOWjDQXH2Xl9REY8+ 563lGaMXaFNQtzXm0ATA2otFIw7icpKoClvIBNM3N9aJ18s72jitFXapvD99mWVX0uvolUDOQ1Z gxJyqYXFHPmuWjcT2TLEwskNVet9uWEOWyaCS40tCyrJKUHRqV8WkIuVNbZ7BL01nfbcgvdV1iI 9NTxrho2INnss1eVNpdYgWUPiuOv7Di6zuD30X1nH1TwEv4r118oXhSF9lt/Z1FKGcSqXIh8rfB 3QV6ji8weq9nE/67S5xpah0nmuDGx4uGJ697eVnS6Rep85 X-Received: by 2002:a05:620a:1991:b0:916:1806:302d with SMTP id af79cd13be357-92ef2bc9bc6mr1331703885a.58.1784023965571; Tue, 14 Jul 2026 03:12:45 -0700 (PDT) Received: from FairplayBox ([2601:5cf:837e:d920::f9a2]) by smtp.gmail.com with ESMTPSA id af79cd13be357-92ee5cfcb0dsm1463056185a.25.2026.07.14.03.12.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Jul 2026 03:12:45 -0700 (PDT) From: Alec Hall To: Jiri Kosina , Benjamin Tissoires , linux-input@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Alec Hall Subject: [PATCH 3/3] HID: magicmouse: report charge status over Bluetooth Date: Tue, 14 Jul 2026 06:12:35 -0400 Message-ID: <20260714101235.99447-4-signshop.alec@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260714101235.99447-1-signshop.alec@gmail.com> References: <20260714101235.99447-1-signshop.alec@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" Battery input report 0x90 carries a status byte that the generic HID battery code does not map to HID_BAT_CHARGING, so charge_status keeps its POWER_SUPPLY_STATUS_DISCHARGING default and user space never learns whether the device is on external power (e.g. a Magic Trackpad 2 shows "discharging" by default even while charging, and cannot report "discharging" the moment the cable is pulled at 100%). Parse the status byte in raw_event and set the battery charge status accordingly (bit 1 =3D external power, bit 0 =3D charge complete), notifying user space with power_supply_changed(). The bit meanings were determined by observation on a Magic Trackpad 2 rather than from documentation. Assisted-by: Claude:claude-opus-4-8 Signed-off-by: Alec Hall Reviewed-by/Tested-by on the respective threads. --- drivers/hid/hid-magicmouse.c | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/drivers/hid/hid-magicmouse.c b/drivers/hid/hid-magicmouse.c index 6d0e76314b10..8bb791efef3a 100644 --- a/drivers/hid/hid-magicmouse.c +++ b/drivers/hid/hid-magicmouse.c @@ -17,6 +17,7 @@ #include #include #include +#include =20 #include "hid-ids.h" =20 @@ -60,6 +61,10 @@ MODULE_PARM_DESC(report_undeciphered, "Report undecipher= ed multi-touch state fie #define MOUSE_REPORT_ID 0x29 #define MOUSE2_REPORT_ID 0x12 #define DOUBLE_REPORT_ID 0xf7 +/* Battery Input Report 0x90 =3D [report_id, status, capacity]. */ +#define MAGICMOUSE_BATTERY_REPORT_ID 0x90 +#define MAGICMOUSE_BATTERY_POWERED 0x02 /* external power connected */ +#define MAGICMOUSE_BATTERY_CHARGED 0x01 /* charge complete (on charger) */ #define USB_BATTERY_TIMEOUT_SEC 60 =20 /* These definitions are not precise, but they're close enough. (Bits @@ -383,6 +388,35 @@ static void magicmouse_emit_touch(struct magicmouse_sc= *msc, int raw_id, u8 *tda } } =20 +/* + * The battery report carries a charge-status byte the generic HID battery= code + * doesn't map, so translate the vendor status bits into a power_supply st= atus. + * This makes charging/discharging honest -- e.g. "discharging" as soon as= the + * cable is pulled, even at 100%. + */ +static void magicmouse_report_charge_status(struct hid_device *hdev, u8 st= atus) +{ +#ifdef CONFIG_HID_BATTERY_STRENGTH + struct hid_battery *bat =3D hid_get_battery(hdev); + int cs; + + if (!bat || !bat->ps) + return; + + if (!(status & MAGICMOUSE_BATTERY_POWERED)) + cs =3D POWER_SUPPLY_STATUS_DISCHARGING; + else if (status & MAGICMOUSE_BATTERY_CHARGED) + cs =3D POWER_SUPPLY_STATUS_FULL; + else + cs =3D POWER_SUPPLY_STATUS_CHARGING; + + if (bat->charge_status !=3D cs) { + bat->charge_status =3D cs; + power_supply_changed(bat->ps); + } +#endif +} + static int magicmouse_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size) { @@ -394,6 +428,9 @@ static int magicmouse_raw_event(struct hid_device *hdev, if (size < 1) return 0; =20 + if (data[0] =3D=3D MAGICMOUSE_BATTERY_REPORT_ID && size >=3D 3) + magicmouse_report_charge_status(hdev, data[1]); + switch (data[0]) { case TRACKPAD_REPORT_ID: case TRACKPAD2_BT_REPORT_ID: --=20 2.55.0