Linux Input/HID development
 help / color / mirror / Atom feed
From: Kenhou Iijima <kenhou@kenhou.net>
To: "Derek J. Clark" <derekjohn.clark@gmail.com>
Cc: linux-input@vger.kernel.org, Jiri Kosina <jikos@kernel.org>,
	Benjamin Tissoires <bentiss@kernel.org>,
	Ana Veto <green.koala2979@fastmail.com>,
	Kenhou Iijima <kenhou@kenhou.net>
Subject: [PATCH v4] HID: hid-msi: Guard against delayed 0x06 MCU ACKs
Date: Wed,  2 Dec 2026 01:49:31 +0900	[thread overview]
Message-ID: <20261201164931.12935-1-kenhou@kenhou.net> (raw)

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


             reply	other threads:[~2026-12-01 16:49 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 16:49 Kenhou Iijima [this message]
2026-12-01 16:57 ` [PATCH v4] HID: hid-msi: Guard against delayed 0x06 MCU ACKs sashiko-bot

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=20261201164931.12935-1-kenhou@kenhou.net \
    --to=kenhou@kenhou.net \
    --cc=bentiss@kernel.org \
    --cc=derekjohn.clark@gmail.com \
    --cc=green.koala2979@fastmail.com \
    --cc=jikos@kernel.org \
    --cc=linux-input@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