From: Kenhou Iijima <kenhou@kenhou.net>
To: Jiri Kosina <jikos@kernel.org>,
Benjamin Tissoires <bentiss@kernel.org>,
"Derek J . Clark" <derekjohn.clark@gmail.com>
Cc: Denis Benato <denis.benato@linux.dev>,
Zhouwang Huang <honjow311@gmail.com>,
Ana Veto <green.koala2979@fastmail.com>,
linux-input@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v5] HID: hid-msi: Guard against delayed 0x06 MCU ACKs
Date: Sat, 5 Dec 2026 15:26:37 +0900 [thread overview]
Message-ID: <20261205062637.84074-1-kenhou@kenhou.net> (raw)
On the MSI Claw handhelds (e.g. 0db0:1901, Claw 8 AI+ fw 2.30 and Claw
A8 fw 3.13), some timeout-0 commands answer with generic 0x06 ACKs whose
count and timing vary across commands and firmware versions:
- SYNC_TO_ROM (0x22) emits an immediate ACK (~4 ms) plus a flash
commit ACK ~515-566 ms later on firmware 2.30, or only a single
commit ACK at the same offset (observed intermittently on 2.30 and
consistently on 3.13); back-to-back SYNC_TO_ROMs within a single
commit window can also coalesce into one reply.
- SWITCH_MODE (0x24) can answer with a second ACK ~13.5-21.5 ms after
the command (~4-8 ms after the first), though a follow-up command
such as READ_GAMEPAD_MODE or another SWITCH_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 35 ms 0x06 quiet window (ack_quiet_until) for non-SYNC_TO_ROM
timeout-0 commands and track the SYNC_TO_ROM commit window (700 ms
fallback, 100 ms minimum commit threshold) via rom_sync_until,
waking waiters through a shared waitqueue (ack_waitq). 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.
- Run both waits outside cfg_mutex first so uncontended profile writes
never delay concurrent readers; bound each wait to the remainder
observed on entry and re-check ack_quiet_until once after acquiring
cfg_mutex so a concurrent SWITCH_MODE or RESET_DEVICE can neither
race past the quiet window nor starve WRITE_PROFILE_DATA. When a
commit-window 0x06 arrives outside an active TX quiet window, mark
the ROM sync settled, open a 35 ms quiet window to absorb any queued
timeout-0 ACK burst, and wake ack_waitq.
- Clear and wake both windows via claw_clear_ack_windows() (taking only
cmd_lock): in claw_remove(), clear before removing sysfs/LED
attributes and before and after cancelling rgb_queue; in
claw_suspend(), clear after cancelling delayed work, both before and
under cfg_mutex.
Since every 0x06 packet is identical on the wire, a delayed reply that
lands after the 100 ms commit threshold is indistinguishable from the
real commit ACK. The 35 ms quiet window opened on such a reply covers
the observed queued-ACK burst, though this remains a heuristic without
firmware support.
Note that back-to-back WRITE_PROFILE_DATA stores after SYNC_TO_ROM now
wait typically ~515-566 ms (up to the 700 ms fallback if no commit ACK
is seen) for the flash commit window to settle instead of risking
premature completion; reads and mode switches do not queue behind
uncontended writes and no writes are dropped.
Fixes: a9517958e828 ("HID: hid-msi: Add MSI Claw configuration driver")
Reported-by: Derek J. Clark <derekjohn.clark@gmail.com>
Link: https://lore.kernel.org/r/20261201012506.2313913-1-derekjohn.clark@gmail.com
Signed-off-by: Kenhou Iijima <kenhou@kenhou.net>
---
Changes in v5 (since v4):
- Snapshot jiffies into a single `now` value and pass it into
claw_ack_window_remaining() so the expiry check and remainder
subtraction cannot observe two different volatile reads (Derek).
- Replace rom_sync_complete and the cfg_mutex-held msleep_interruptible()
quiet-window loop with a shared waitqueue (ack_waitq) and run both
waits in claw_hw_output_report() before acquiring cfg_mutex so
uncontended profile writes never delay concurrent readers; bound each
claw_wait_ack_window() call to the remainder observed on entry and
re-check ack_quiet_until once under cfg_mutex so concurrent
SWITCH_MODE or RESET_DEVICE commands can neither race past the quiet
window, race with completion reinitialization, nor starve
WRITE_PROFILE_DATA (Derek).
- Call claw_clear_ack_windows() (taking only cmd_lock and waking
ack_waitq) before removing sysfs/LED attributes and before and after
cancelling rgb_queue in claw_remove(), and both before and under
cfg_mutex after cancelling delayed work in claw_suspend(), so active
window waiters wake without holding rom_mutex or cfg_mutex (Derek).
- On a failed timeout-0 transmission, restore any still-live inherited
ROM-sync or quiet window without clobbering a concurrent clear or
newer IRQ-armed window, comment the "+1 jiffy" deadline rounding, and
include <linux/lockdep.h> and <linux/wait.h> explicitly.
- Update the commit message with Derek's firmware 3.13 measurements on
the Claw A8 and rapid SWITCH_MODE behavior, switch Closes: to Link:,
and note the post-100 ms stray-reply heuristic limitation.
- Link to v4: https://lore.kernel.org/r/20261201164931.12935-1-kenhou@kenhou.net
drivers/hid/hid-msi.c | 227 +++++++++++++++++++++++++++++++++++++++++-
1 file changed, 225 insertions(+), 2 deletions(-)
diff --git a/drivers/hid/hid-msi.c b/drivers/hid/hid-msi.c
index 23d731f700..611cd224f3 100644
--- a/drivers/hid/hid-msi.c
+++ b/drivers/hid/hid-msi.c
@@ -21,9 +21,11 @@
#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>
+#include <linux/lockdep.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/pm.h>
@@ -32,6 +34,7 @@
#include <linux/types.h>
#include <linux/unaligned.h>
#include <linux/usb.h>
+#include <linux/wait.h>
#include <linux/workqueue.h>
#include "hid-ids.h"
@@ -332,6 +335,12 @@ struct claw_drvdata {
enum claw_profile_ack_pending profile_pending;
struct completion orphan_ack_complete;
struct completion send_cmd_complete;
+ wait_queue_head_t ack_waitq;
+ 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 delayed_work cfg_resume;
struct delayed_work cfg_setup;
spinlock_t registration_lock; /* Lock for registration read/write */
@@ -486,10 +495,36 @@ 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,
+ unsigned long now)
+{
+ lockdep_assert_held(&drvdata->cmd_lock);
+
+ if (!*pending)
+ return 0;
+
+ if (time_before(now, until))
+ return until - now;
+
+ *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;
+ wake_up_all(&drvdata->ack_waitq);
+ }
+}
+
static int claw_raw_event(struct claw_drvdata *drvdata, struct hid_report *report,
u8 *data, int size)
{
struct claw_command_report *cmd_rep;
+ unsigned long now;
int ret = 0;
if (size != CLAW_PACKET_SIZE)
@@ -525,14 +560,45 @@ 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-566 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.
+ */
+ now = jiffies;
+ if (!claw_ack_window_remaining(drvdata,
+ &drvdata->ack_quiet_pending,
+ drvdata->ack_quiet_until, now) &&
+ drvdata->rom_sync_pending &&
+ time_after_eq(now, drvdata->rom_sync_min_ack)) {
+ drvdata->rom_sync_pending = false;
+ /* +1 jiffy so the window never expires early */
+ drvdata->ack_quiet_until = now + msecs_to_jiffies(35) + 1;
+ drvdata->ack_quiet_pending = true;
+ wake_up_all(&drvdata->ack_waitq);
+ }
+
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-566 ms) or
+ * SWITCH_MODE (~13.5-21.5 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;
}
@@ -562,6 +628,44 @@ static int msi_raw_event(struct hid_device *hdev, struct hid_report *report,
return claw_raw_event(drvdata, report, data, size);
}
+static bool claw_ack_window_done(struct claw_drvdata *drvdata,
+ bool *pending, unsigned long *until)
+{
+ scoped_guard(spinlock_irqsave, &drvdata->cmd_lock)
+ return !claw_ack_window_remaining(drvdata, pending,
+ *until, jiffies);
+}
+
+/*
+ * Wait at most once for the window remainder observed on entry. Callers
+ * holding the serializing mutex (rom_mutex for rom_sync, cfg_mutex +
+ * rom_mutex for the final ack_quiet check) cannot see the window re-armed
+ * while sleeping; the opportunistic pre-cfg_mutex ack_quiet check must not
+ * chase re-arms or continuous SWITCH_MODE stores could starve the writer.
+ */
+static int claw_wait_ack_window(struct claw_drvdata *drvdata,
+ bool *pending, unsigned long *until)
+{
+ unsigned long remaining;
+ long wret;
+
+ scoped_guard(spinlock_irqsave, &drvdata->cmd_lock)
+ remaining = claw_ack_window_remaining(drvdata, pending,
+ *until, jiffies);
+ if (!remaining)
+ return 0;
+
+ wret = wait_event_interruptible_timeout(drvdata->ack_waitq,
+ claw_ack_window_done(drvdata,
+ pending,
+ until),
+ remaining);
+ if (wret < 0)
+ return wret;
+
+ return 0;
+}
+
/* Caller must hold drvdata->cfg_mutex. */
static int __claw_hw_output_report(struct hid_device *hdev, u8 index, u8 *data,
size_t len, unsigned int timeout)
@@ -569,7 +673,12 @@ static int __claw_hw_output_report(struct hid_device *hdev, u8 index, u8 *data,
unsigned char *dmabuf __free(kfree) = NULL;
u8 header[] = { CLAW_OUTPUT_REPORT_ID, 0, 0, 0x3c, index };
struct claw_drvdata *drvdata = hid_get_drvdata(hdev);
+ unsigned long prev_rom_min_ack = 0, prev_rom_until = 0;
+ unsigned long prev_quiet_until = 0, quiet_deadline = 0;
size_t header_size = ARRAY_SIZE(header);
+ unsigned long rom_deadline = 0;
+ bool prev_quiet_pending = false;
+ bool prev_rom_pending = false;
bool orphaned;
int ret;
@@ -604,9 +713,40 @@ 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) {
+ bool *rp = &drvdata->rom_sync_pending;
+
+ prev_rom_pending =
+ claw_ack_window_remaining(drvdata, rp,
+ drvdata->rom_sync_until,
+ now) != 0;
+ prev_rom_min_ack = drvdata->rom_sync_min_ack;
+ prev_rom_until = drvdata->rom_sync_until;
+ /* +1 jiffy so the window never expires early */
+ rom_deadline = now + msecs_to_jiffies(700) + 1;
+ drvdata->rom_sync_min_ack = now + msecs_to_jiffies(100);
+ drvdata->rom_sync_until = rom_deadline;
+ drvdata->rom_sync_pending = true;
+ } else {
+ /* +1 jiffy so the window never expires early */
+ unsigned long deadline = now + msecs_to_jiffies(35) + 1;
+ bool *qp = &drvdata->ack_quiet_pending;
+
+ prev_quiet_pending =
+ claw_ack_window_remaining(drvdata, qp,
+ drvdata->ack_quiet_until,
+ now) != 0;
+ prev_quiet_until = drvdata->ack_quiet_until;
+ if (!prev_quiet_pending || time_after(deadline, prev_quiet_until))
+ drvdata->ack_quiet_until = deadline;
+ quiet_deadline = drvdata->ack_quiet_until;
+ drvdata->ack_quiet_pending = true;
+ }
}
}
@@ -640,6 +780,33 @@ 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) {
+ if (index == CLAW_COMMAND_TYPE_SYNC_TO_ROM) {
+ if (drvdata->rom_sync_pending &&
+ drvdata->rom_sync_until == rom_deadline) {
+ drvdata->rom_sync_min_ack = prev_rom_min_ack;
+ drvdata->rom_sync_until = prev_rom_until;
+ if (!claw_ack_window_remaining(drvdata,
+ &prev_rom_pending,
+ prev_rom_until,
+ jiffies)) {
+ drvdata->rom_sync_pending = false;
+ wake_up_all(&drvdata->ack_waitq);
+ }
+ }
+ } else if (drvdata->ack_quiet_pending &&
+ drvdata->ack_quiet_until == quiet_deadline) {
+ /* Restore the quiet window this command inherited. */
+ drvdata->ack_quiet_until = prev_quiet_until;
+ if (!claw_ack_window_remaining(drvdata,
+ &prev_quiet_pending,
+ prev_quiet_until,
+ jiffies)) {
+ drvdata->ack_quiet_pending = false;
+ wake_up_all(&drvdata->ack_waitq);
+ }
+ }
+ }
}
return ret;
}
@@ -648,6 +815,46 @@ static int claw_hw_output_report(struct hid_device *hdev, u8 index, u8 *data,
size_t len, unsigned int timeout)
{
struct claw_drvdata *drvdata = hid_get_drvdata(hdev);
+ int ret;
+
+ if (index == CLAW_COMMAND_TYPE_SYNC_TO_ROM)
+ lockdep_assert_held(&drvdata->rom_mutex);
+
+ /*
+ * Drain any pending SYNC_TO_ROM flash-commit ACK window (~515-566 ms)
+ * outside cfg_mutex before issuing WRITE_PROFILE_DATA, so reads and
+ * mode switches never queue behind the flash commit. Callers of
+ * WRITE_PROFILE_DATA and SYNC_TO_ROM hold rom_mutex, so no new
+ * SYNC_TO_ROM can re-arm rom_sync_until while waiting here.
+ *
+ * Then wait for any 35 ms 0x06 quiet window (from SWITCH_MODE,
+ * RESET_DEVICE, or a post-commit queued ACK burst) outside cfg_mutex
+ * first so uncontended writes never delay concurrent readers; if
+ * another command armed a fresh quiet window before we acquired
+ * cfg_mutex, finish that bounded <= 35 ms wait under cfg_mutex so
+ * continuous mode switches cannot starve WRITE_PROFILE_DATA.
+ */
+ if (index == CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA) {
+ lockdep_assert_held(&drvdata->rom_mutex);
+
+ ret = claw_wait_ack_window(drvdata, &drvdata->rom_sync_pending,
+ &drvdata->rom_sync_until);
+ if (ret)
+ return ret;
+
+ ret = claw_wait_ack_window(drvdata, &drvdata->ack_quiet_pending,
+ &drvdata->ack_quiet_until);
+ if (ret)
+ return ret;
+
+ guard(mutex)(&drvdata->cfg_mutex);
+ ret = claw_wait_ack_window(drvdata, &drvdata->ack_quiet_pending,
+ &drvdata->ack_quiet_until);
+ if (ret)
+ return ret;
+
+ return __claw_hw_output_report(hdev, index, data, len, timeout);
+ }
guard(mutex)(&drvdata->cfg_mutex);
return __claw_hw_output_report(hdev, index, data, len, timeout);
@@ -1868,6 +2075,7 @@ 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_waitqueue_head(&drvdata->ack_waitq);
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 +2154,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);
@@ -1953,7 +2163,11 @@ static void claw_remove(struct hid_device *hdev)
device_remove_group(drvdata->led_mc.led_cdev.dev, &claw_rgb_attr_group);
led_classdev_multicolor_unregister(&drvdata->led_mc);
}
+
+ /* Wake rgb_queue as well if a draining sysfs store re-armed a window. */
+ claw_clear_ack_windows(drvdata);
cancel_delayed_work_sync(&drvdata->rgb_queue);
+ claw_clear_ack_windows(drvdata);
hid_hw_close(hdev);
}
@@ -2016,6 +2230,15 @@ 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. Clear
+ * windows before taking cfg_mutex so any active window waiter wakes
+ * first, then clear again under cfg_mutex.
+ */
+ claw_clear_ack_windows(drvdata);
+ scoped_guard(mutex, &drvdata->cfg_mutex)
+ claw_clear_ack_windows(drvdata);
+
return 0;
}
--
2.56.0
reply other threads:[~2026-12-05 6:26 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20261205062637.84074-1-kenhou@kenhou.net \
--to=kenhou@kenhou.net \
--cc=bentiss@kernel.org \
--cc=denis.benato@linux.dev \
--cc=derekjohn.clark@gmail.com \
--cc=green.koala2979@fastmail.com \
--cc=honjow311@gmail.com \
--cc=jikos@kernel.org \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox