* [PATCH v4] HID: hid-msi: Guard against delayed 0x06 MCU ACKs
@ 2026-12-01 16:49 Kenhou Iijima
2026-12-01 16:57 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Kenhou Iijima @ 2026-12-01 16:49 UTC (permalink / raw)
To: Derek J. Clark
Cc: linux-input, Jiri Kosina, Benjamin Tissoires, Ana Veto,
Kenhou Iijima
On the MSI Claw handhelds (e.g. 0db0:1901), some timeout-0 commands
answer with more than one generic 0x06 ACK, and neither the count nor
the timing is fixed:
- SYNC_TO_ROM (0x22) emits an immediate ACK (~4 ms) plus a flash
commit ACK ~515-565 ms later on firmware 2.30, or sometimes only the
single ~515-565 ms commit ACK.
- SWITCH_MODE (0x24) can answer with a second ACK ~15-21 ms after the
command (~4-8 ms after the first), though issuing READ_GAMEPAD_MODE
immediately afterwards suppresses that second ACK.
- If a timeout-0 command such as SWITCH_MODE is issued while
SYNC_TO_ROM is committing to flash, the MCU queues its 0x06 ACKs
behind the flash commit and emits them in a ~16 ms burst right after
the commit ACK.
Two bugs occur in CLAW_COMMAND_TYPE_ACK when these delayed 0x06 packets
arrive after timeout-0 returns:
1. waiting_cmd only ignores 0x06 when it equals CLAW_COMMAND_TYPE_NONE.
If a delayed 0x06 arrives during a read command (e.g.
READ_GAMEPAD_MODE after an RGB update), it completes send_cmd_complete
early without parsing the read reply, returning stale cached data.
Expected delayed ACKs also emit "Got unexpected ACK from MCU" at
dev_warn level.
2. If a subsequent WRITE_PROFILE_DATA (0x21) executes inside the delayed
ACK window of an earlier SYNC_TO_ROM, SWITCH_MODE, or post-commit
queued ACK burst, the leftover 0x06 matches waiting_cmd ==
WRITE_PROFILE_DATA and completes the new write prematurely.
Fix both parts without relying on fragile ACK counting:
- Complete send_cmd_complete on a generic 0x06 ACK only while
waiting_cmd == CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA, and downgrade
leftover 0x06 messages to dev_dbg including waiting_cmd.
- Track a unified 35 ms 0x06 quiet window (ack_quiet_until) for
non-SYNC_TO_ROM timeout-0 commands inside cfg_mutex, and track the
SYNC_TO_ROM commit window (700 ms fallback, 100 ms minimum commit
threshold) outside cfg_mutex via rom_sync_complete, clearing both on
remove and suspend. Wait for active windows only before issuing
WRITE_PROFILE_DATA (serialized with SYNC_TO_ROM by rom_mutex) so reads
and SWITCH_MODE stay unblocked. When a commit-window 0x06 arrives
outside an active TX quiet window, complete rom_sync_complete early and
open a 35 ms quiet window to absorb any queued timeout-0 ACK burst.
Note that back-to-back WRITE_PROFILE_DATA stores after SYNC_TO_ROM now
wait up to ~515-565 ms (700 ms fallback) for the flash commit window to
settle instead of risking premature completion; reads and mode switches
are not delayed and no writes are dropped.
Fixes: a9517958e828 ("HID: hid-msi: Add MSI Claw configuration driver")
Reported-by: Derek J. Clark <derekjohn.clark@gmail.com>
Closes: https://lore.kernel.org/r/20261201012506.2313913-1-derekjohn.clark@gmail.com
Signed-off-by: Kenhou Iijima <kenhou@kenhou.net>
---
Changes in v4:
- Replace the waiting_cmd-only guard from v3 with delayed-0x06 ACK
window tracking (unified 35 ms ack_quiet_until inside cfg_mutex plus
SYNC_TO_ROM commit window via rom_sync_complete outside cfg_mutex),
draining active windows only before WRITE_PROFILE_DATA so reads and
SWITCH_MODE stay unblocked.
- Clear active windows on remove and suspend, and document the
rom_mutex serialization invariant with lockdep_assert_held().
- Tested Derek's late_acks counter proposal on a Claw 8 AI+ (fw 2.30)
with all userspace daemons stopped; it hit -EBUSY because firmware
emits a variable number of 0x06 ACKs and a pending counter consumes
a subsequent write's own immediate ACK before the delayed ACK arrives.
- Credit Derek J. Clark with Reported-by and Closes for identifying the
WRITE_PROFILE_DATA window race.
drivers/hid/hid-msi.c | 149 +++++++++++++++++++++++++++++++++++++++++-
1 file changed, 147 insertions(+), 2 deletions(-)
diff --git a/drivers/hid/hid-msi.c b/drivers/hid/hid-msi.c
index 23d731f700..9614f708b1 100644
--- a/drivers/hid/hid-msi.c
+++ b/drivers/hid/hid-msi.c
@@ -21,6 +21,7 @@
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/hid.h>
+#include <linux/jiffies.h>
#include <linux/kobject.h>
#include <linux/led-class-multicolor.h>
#include <linux/leds.h>
@@ -339,6 +340,12 @@ struct claw_drvdata {
spinlock_t profile_lock; /* Lock for profile_pending read/write */
struct hid_device *hdev;
bool orphan_ack_pending;
+ struct completion rom_sync_complete;
+ unsigned long rom_sync_min_ack; /* earliest jiffies for commit ACK */
+ unsigned long rom_sync_until; /* SYNC_TO_ROM delayed ACK deadline */
+ unsigned long ack_quiet_until; /* quiet deadline before profile write */
+ bool rom_sync_pending; /* guarded by cmd_lock */
+ bool ack_quiet_pending; /* guarded by cmd_lock */
struct mutex cfg_mutex; /* mutex for synchronous data */
struct mutex rom_mutex; /* mutex for SYNC_TO_ROM calls */
spinlock_t cmd_lock; /* Lock for cmd data read/write */
@@ -486,6 +493,30 @@ static int claw_profile_event(struct claw_drvdata *drvdata, struct claw_command_
return 0;
}
+static unsigned long claw_ack_window_remaining(struct claw_drvdata *drvdata,
+ bool *pending, unsigned long until)
+{
+ lockdep_assert_held(&drvdata->cmd_lock);
+
+ if (!*pending)
+ return 0;
+
+ if (time_before(jiffies, until))
+ return until - jiffies;
+
+ *pending = false;
+ return 0;
+}
+
+static void claw_clear_ack_windows(struct claw_drvdata *drvdata)
+{
+ scoped_guard(spinlock_irqsave, &drvdata->cmd_lock) {
+ drvdata->rom_sync_pending = false;
+ drvdata->ack_quiet_pending = false;
+ complete_all(&drvdata->rom_sync_complete);
+ }
+}
+
static int claw_raw_event(struct claw_drvdata *drvdata, struct hid_report *report,
u8 *data, int size)
{
@@ -525,14 +556,43 @@ static int claw_raw_event(struct claw_drvdata *drvdata, struct hid_report *repor
break;
case CLAW_COMMAND_TYPE_ACK:
+ /*
+ * SYNC_TO_ROM can emit an immediate ACK (~4 ms) and a flash
+ * commit ACK (~515-565 ms), or only the commit ACK. Once a
+ * commit-window 0x06 arrives outside an active TX quiet
+ * window, mark the ROM sync settled and open a 35 ms quiet
+ * window to absorb any timeout-0 ACKs (e.g. SWITCH_MODE)
+ * that the MCU queued behind the flash commit.
+ */
+ if (!claw_ack_window_remaining(drvdata,
+ &drvdata->ack_quiet_pending,
+ drvdata->ack_quiet_until) &&
+ drvdata->rom_sync_pending &&
+ time_after_eq(jiffies, drvdata->rom_sync_min_ack)) {
+ drvdata->rom_sync_pending = false;
+ drvdata->ack_quiet_until = jiffies + msecs_to_jiffies(35) + 1;
+ drvdata->ack_quiet_pending = true;
+ complete_all(&drvdata->rom_sync_complete);
+ }
+
if (drvdata->orphan_ack_pending) {
drvdata->orphan_ack_pending = false;
complete(&drvdata->orphan_ack_complete);
break;
}
- if (drvdata->waiting_cmd == CLAW_COMMAND_TYPE_NONE) {
- dev_warn(&drvdata->hdev->dev, "Got unexpected ACK from MCU, ignoring\n");
+ if (drvdata->waiting_cmd != CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA) {
+ /*
+ * Only WRITE_PROFILE_DATA waits for a generic 0x06 ACK;
+ * reads wait for their own ACK command type. Delayed
+ * 0x06 ACKs from SYNC_TO_ROM (~515-565 ms) or
+ * SWITCH_MODE (~15-21 ms) arrive after timeout-0
+ * returns and must not complete an in-flight read
+ * early.
+ */
+ dev_dbg(&drvdata->hdev->dev,
+ "Got unexpected ACK from MCU, ignoring (waiting CMD: %x)\n",
+ drvdata->waiting_cmd);
break;
}
@@ -585,6 +645,27 @@ static int __claw_hw_output_report(struct hid_device *hdev, u8 index, u8 *data,
drvdata->orphan_ack_pending = false;
}
+ /*
+ * Drain any 35 ms 0x06 quiet window (from a non-sync timeout-0 command
+ * such as SWITCH_MODE/RESET_DEVICE, or right after a SYNC_TO_ROM
+ * commit ACK when queued timeout-0 ACKs may burst) before sending
+ * WRITE_PROFILE_DATA so a leftover 0x06 cannot complete it early.
+ */
+ if (index == CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA) {
+ for (;;) {
+ unsigned long remaining;
+
+ scoped_guard(spinlock_irqsave, &drvdata->cmd_lock)
+ remaining = claw_ack_window_remaining(drvdata,
+ &drvdata->ack_quiet_pending,
+ drvdata->ack_quiet_until);
+ if (!remaining)
+ break;
+ if (msleep_interruptible(jiffies_to_msecs(remaining) + 1))
+ return -ERESTARTSYS;
+ }
+ }
+
if (header_size + len > CLAW_PACKET_SIZE)
return -EINVAL;
@@ -604,9 +685,20 @@ static int __claw_hw_output_report(struct hid_device *hdev, u8 index, u8 *data,
drvdata->waiting_cmd = index;
drvdata->cmd_status = -ETIMEDOUT;
} else {
+ unsigned long now = jiffies;
+
reinit_completion(&drvdata->orphan_ack_complete);
drvdata->waiting_cmd = CLAW_COMMAND_TYPE_NONE;
drvdata->orphan_ack_pending = true;
+ if (index == CLAW_COMMAND_TYPE_SYNC_TO_ROM) {
+ reinit_completion(&drvdata->rom_sync_complete);
+ drvdata->rom_sync_min_ack = now + msecs_to_jiffies(100);
+ drvdata->rom_sync_until = now + msecs_to_jiffies(700) + 1;
+ drvdata->rom_sync_pending = true;
+ } else {
+ drvdata->ack_quiet_until = now + msecs_to_jiffies(35) + 1;
+ drvdata->ack_quiet_pending = true;
+ }
}
}
@@ -640,6 +732,10 @@ static int __claw_hw_output_report(struct hid_device *hdev, u8 index, u8 *data,
scoped_guard(spinlock_irqsave, &drvdata->cmd_lock) {
drvdata->waiting_cmd = CLAW_COMMAND_TYPE_NONE;
drvdata->orphan_ack_pending = false;
+ if (!timeout && index == CLAW_COMMAND_TYPE_SYNC_TO_ROM) {
+ drvdata->rom_sync_pending = false;
+ complete_all(&drvdata->rom_sync_complete);
+ }
}
return ret;
}
@@ -649,6 +745,47 @@ static int claw_hw_output_report(struct hid_device *hdev, u8 index, u8 *data,
{
struct claw_drvdata *drvdata = hid_get_drvdata(hdev);
+ if (index == CLAW_COMMAND_TYPE_SYNC_TO_ROM)
+ lockdep_assert_held(&drvdata->rom_mutex);
+
+ /*
+ * Wait outside cfg_mutex for any pending SYNC_TO_ROM flash-commit ACK
+ * window to settle before issuing WRITE_PROFILE_DATA, so reads and
+ * mode switches stay unblocked and a delayed ~515-565 ms 0x06 cannot
+ * complete WRITE_PROFILE_DATA early. Callers of WRITE_PROFILE_DATA
+ * and SYNC_TO_ROM hold rom_mutex, so no new SYNC_TO_ROM can re-arm
+ * rom_sync_complete while waiting here.
+ */
+ if (index == CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA) {
+ unsigned long remaining;
+
+ lockdep_assert_held(&drvdata->rom_mutex);
+
+ scoped_guard(spinlock_irqsave, &drvdata->cmd_lock)
+ remaining = claw_ack_window_remaining(drvdata,
+ &drvdata->rom_sync_pending,
+ drvdata->rom_sync_until);
+
+ if (remaining) {
+ struct completion *sync = &drvdata->rom_sync_complete;
+ long wret;
+
+ wret = wait_for_completion_interruptible_timeout(sync, remaining);
+ if (wret < 0)
+ return wret;
+
+ scoped_guard(spinlock_irqsave, &drvdata->cmd_lock) {
+ if (wret == 0 ||
+ !claw_ack_window_remaining(drvdata,
+ &drvdata->rom_sync_pending,
+ drvdata->rom_sync_until)) {
+ drvdata->rom_sync_pending = false;
+ complete_all(sync);
+ }
+ }
+ }
+ }
+
guard(mutex)(&drvdata->cfg_mutex);
return __claw_hw_output_report(hdev, index, data, len, timeout);
}
@@ -1868,6 +2005,8 @@ static int claw_probe(struct hid_device *hdev, u8 ep)
spin_lock_init(&drvdata->rumble_lock);
init_completion(&drvdata->orphan_ack_complete);
init_completion(&drvdata->send_cmd_complete);
+ init_completion(&drvdata->rom_sync_complete);
+ complete_all(&drvdata->rom_sync_complete);
INIT_DELAYED_WORK(&drvdata->cfg_resume, &cfg_resume_fn);
INIT_DELAYED_WORK(&drvdata->cfg_setup, &cfg_setup_fn);
INIT_DELAYED_WORK(&drvdata->rgb_queue, &claw_rgb_queue_fn);
@@ -1946,6 +2085,8 @@ static void claw_remove(struct hid_device *hdev)
smp_store_release(&drvdata->rgb_registered, false);
}
+ claw_clear_ack_windows(drvdata);
+
if (gp_registered)
device_remove_group(&hdev->dev, &claw_gamepad_attr_group);
@@ -2016,6 +2157,10 @@ static int claw_suspend(struct hid_device *hdev)
cancel_delayed_work_sync(&drvdata->cfg_setup);
cancel_delayed_work_sync(&drvdata->rgb_queue);
+ /* Delayed ACKs across suspend will not arrive after resume. */
+ scoped_guard(mutex, &drvdata->cfg_mutex)
+ claw_clear_ack_windows(drvdata);
+
return 0;
}
--
2.54.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH v4] HID: hid-msi: Guard against delayed 0x06 MCU ACKs
2026-12-01 16:49 [PATCH v4] HID: hid-msi: Guard against delayed 0x06 MCU ACKs Kenhou Iijima
@ 2026-12-01 16:57 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-01 16:57 UTC (permalink / raw)
To: Kenhou Iijima; +Cc: linux-input, dmitry.torokhov
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] HID: hid-msi: double volatile jiffies read in
claw_ack_window_remaining()
--
commit 7768c8b350e8e88673d311b6a516c536e5724746
Author: Kenhou Iijima <kenhou@kenhou.net>
HID: hid-msi: Guard against delayed 0x06 MCU ACKs
This commit restricts generic 0x06 ACK completion to WRITE_PROFILE_DATA
and tracks quiet windows after timeout-0 commands and SYNC_TO_ROM so
delayed ACKs cannot complete subsequent commands early.
> diff --git a/drivers/hid/hid-msi.c b/drivers/hid/hid-msi.c
> index 23d731f700c90..9614f708b14b9 100644
> --- a/drivers/hid/hid-msi.c
> +++ b/drivers/hid/hid-msi.c
[ ... ]
> @@ -486,6 +493,30 @@ static int claw_profile_event(struct claw_drvdata *drvdata, struct claw_command_
> return 0;
> }
>
> +static unsigned long claw_ack_window_remaining(struct claw_drvdata *drvdata,
> + bool *pending, unsigned long until)
> +{
> + lockdep_assert_held(&drvdata->cmd_lock);
> +
> + if (!*pending)
> + return 0;
> +
> + if (time_before(jiffies, until))
> + return until - jiffies;
> +
> + *pending = false;
> + return 0;
> +}
[Severity: Medium]
Can jiffies advance on another CPU between time_before(jiffies, until) and
until - jiffies?
Because jiffies is volatile, the compiler emits two loads. If jiffies
reaches until on the second load, claw_ack_window_remaining() returns 0
without clearing *pending; if it advances past until (for example during a
multi-tick NO_HZ catchup or vCPU preemption), until - jiffies underflows
and causes __claw_hw_output_report() or claw_hw_output_report() to sleep
for a huge timeout while holding cfg_mutex or rom_mutex. Snapshotting
jiffies into a local variable once avoids both.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201164931.12935-1-kenhou@kenhou.net?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-01 16:57 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 16:49 [PATCH v4] HID: hid-msi: Guard against delayed 0x06 MCU ACKs Kenhou Iijima
2026-12-01 16:57 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox