Linux Input/HID development
 help / color / mirror / Atom feed
* [PATCH] HID: hid-msi: Drain orphan MCU ACKs by issuing a query
@ 2026-11-14 10:10 Kenhou Iijima
  2026-11-14 10:29 ` sashiko-bot
  2026-11-14 12:50 ` [PATCH v2] " Kenhou Iijima
  0 siblings, 2 replies; 6+ messages in thread
From: Kenhou Iijima @ 2026-11-14 10:10 UTC (permalink / raw)
  To: Derek J . Clark
  Cc: Jiri Kosina, Benjamin Tissoires, Denis Benato, Zhouwang Huang,
	linux-input, linux-kernel, Kenhou Iijima

The Claw MCU re-sends an unhandled ACK roughly every 2 seconds until it
receives another command. When an ACK arrives with waiting_cmd set to
CLAW_COMMAND_TYPE_NONE, such as after a command is written through
hidraw or once orphan_ack_pending has been cleared without a subsequent
command, the driver currently logs "Got unexpected ACK from MCU,
ignoring" on every re-transmission and never clears the MCU's pending
ACK state.

Schedule a work item that issues a harmless READ_GAMEPAD_MODE query when
an unexpected ACK arrives. Receiving any command causes the MCU to stop
re-sending the pending ACK, eliminating the repeated events and log
spam.

Fixes: a9517958e828 ("HID: hid-msi: Add MSI Claw configuration driver")
Signed-off-by: Kenhou Iijima <kenhou@kenhou.net>
---
Tested on an MSI Claw 8 AI+ A2VM (0db0:1901, bcdDevice 2.30).

 drivers/hid/hid-msi.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/drivers/hid/hid-msi.c b/drivers/hid/hid-msi.c
index 23d731f700..e52923fcc8 100644
--- a/drivers/hid/hid-msi.c
+++ b/drivers/hid/hid-msi.c
@@ -332,6 +332,7 @@ struct claw_drvdata {
 	enum claw_profile_ack_pending profile_pending;
 	struct completion orphan_ack_complete;
 	struct completion send_cmd_complete;
+	struct work_struct orphan_ack_work;
 	struct delayed_work cfg_resume;
 	struct delayed_work cfg_setup;
 	spinlock_t registration_lock; /* Lock for registration read/write */
@@ -532,7 +533,12 @@ static int claw_raw_event(struct claw_drvdata *drvdata, struct hid_report *repor
 		}
 
 		if (drvdata->waiting_cmd == CLAW_COMMAND_TYPE_NONE) {
-			dev_warn(&drvdata->hdev->dev, "Got unexpected ACK from MCU, ignoring\n");
+			/*
+			 * The MCU re-sends an unhandled ACK roughly every 2 s
+			 * until it receives another command. Drain it by
+			 * scheduling a harmless query so the ACK storm stops.
+			 */
+			schedule_work(&drvdata->orphan_ack_work);
 			break;
 		}
 
@@ -653,6 +659,20 @@ static int claw_hw_output_report(struct hid_device *hdev, u8 index, u8 *data,
 	return __claw_hw_output_report(hdev, index, data, len, timeout);
 }
 
+static void claw_orphan_ack_drain(struct work_struct *work)
+{
+	struct claw_drvdata *drvdata = container_of(work, struct claw_drvdata,
+						    orphan_ack_work);
+
+	/*
+	 * A query is enough to make the MCU stop re-sending the pending ACK.
+	 * claw_hw_output_report() also waits for any orphan ACK before
+	 * sending, so this cannot interleave with a real command sequence.
+	 */
+	claw_hw_output_report(drvdata->hdev, CLAW_COMMAND_TYPE_READ_GAMEPAD_MODE,
+			      NULL, 0, 25);
+}
+
 static int claw_switch_mode(struct hid_device *hdev, enum claw_mode_field field, u8 val)
 {
 	struct claw_drvdata *drvdata = hid_get_drvdata(hdev);
@@ -1868,6 +1888,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_WORK(&drvdata->orphan_ack_work, &claw_orphan_ack_drain);
 	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);
@@ -1934,6 +1955,7 @@ static void claw_remove(struct hid_device *hdev)
 
 	cancel_delayed_work_sync(&drvdata->cfg_resume);
 	cancel_delayed_work_sync(&drvdata->cfg_setup);
+	cancel_work_sync(&drvdata->orphan_ack_work);
 
 	scoped_guard(spinlock_irqsave, &drvdata->registration_lock) {
 		/* Pairs with smp_store_release from cfg_setup_fn in system_wq context */
@@ -2014,6 +2036,7 @@ static int claw_suspend(struct hid_device *hdev)
 
 	cancel_delayed_work_sync(&drvdata->cfg_resume);
 	cancel_delayed_work_sync(&drvdata->cfg_setup);
+	cancel_work_sync(&drvdata->orphan_ack_work);
 	cancel_delayed_work_sync(&drvdata->rgb_queue);
 
 	return 0;
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] HID: hid-msi: Drain orphan MCU ACKs by issuing a query
  2026-11-14 10:10 [PATCH] HID: hid-msi: Drain orphan MCU ACKs by issuing a query Kenhou Iijima
@ 2026-11-14 10:29 ` sashiko-bot
  2026-11-14 12:50 ` [PATCH v2] " Kenhou Iijima
  1 sibling, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-14 10:29 UTC (permalink / raw)
  To: Kenhou Iijima; +Cc: linux-input, dmitry.torokhov

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] HID: hid-msi: race in claw_suspend() lets orphan_ack_work run
  after suspend
- [Medium] HID: hid-msi: orphan ACK in claw_raw_event() completes read
  commands early
--

commit d1c0d0f5c28e7bbec3449fd155157b8c3178c746
Author: Kenhou Iijima <kenhou@kenhou.net>

HID: hid-msi: Drain orphan MCU ACKs by issuing a query

This commit schedules a work item to issue a READ_GAMEPAD_MODE query when an
unexpected ACK arrives from the MSI Claw MCU so the MCU stops re-sending the
pending ACK.

> diff --git a/drivers/hid/hid-msi.c b/drivers/hid/hid-msi.c
> index 23d731f700c90..e52923fcc8260 100644
> --- a/drivers/hid/hid-msi.c
> +++ b/drivers/hid/hid-msi.c

[ ... ]

> @@ -532,7 +533,12 @@ static int claw_raw_event(struct claw_drvdata *drvdata, struct hid_report *repor
>  		}
>  
>  		if (drvdata->waiting_cmd == CLAW_COMMAND_TYPE_NONE) {
> -			dev_warn(&drvdata->hdev->dev, "Got unexpected ACK from MCU, ignoring\n");
> +			/*
> +			 * The MCU re-sends an unhandled ACK roughly every 2 s
> +			 * until it receives another command. Drain it by
> +			 * scheduling a harmless query so the ACK storm stops.
> +			 */
> +			schedule_work(&drvdata->orphan_ack_work);
>  			break;
>  		}

[Severity: Medium]
Can an orphan CLAW_COMMAND_TYPE_ACK arriving while a read command is in flight
complete drvdata->send_cmd_complete early?

__claw_hw_output_report() sets drvdata->waiting_cmd to
CLAW_COMMAND_TYPE_READ_GAMEPAD_MODE or CLAW_COMMAND_TYPE_READ_PROFILE before
transmitting the request.  If a re-transmitted CLAW_COMMAND_TYPE_ACK arrives
before the read response (CLAW_COMMAND_TYPE_GAMEPAD_MODE_ACK or
CLAW_COMMAND_TYPE_READ_PROFILE_ACK), waiting_cmd is not
CLAW_COMMAND_TYPE_NONE, so claw_raw_event() falls through, sets
drvdata->cmd_status = 0, and completes drvdata->send_cmd_complete.  Callers
such as gamepad_mode_show() and claw_mkey_show() then return stale or
uninitialized data while leaving drvdata->profile_pending set.

In addition, when the initial ACK for a timeout-0 command arrives with
drvdata->orphan_ack_pending set, the earlier branch clears orphan_ack_pending
and breaks without scheduling orphan_ack_work, leaving the MCU's pending ACK
state undrained so it still re-transmits 2 seconds later.

> @@ -653,6 +659,20 @@ static int claw_hw_output_report(struct hid_device *hdev, u8 index, u8 *data,
>  	return __claw_hw_output_report(hdev, index, data, len, timeout);
>  }
>  
> +static void claw_orphan_ack_drain(struct work_struct *work)
> +{
> +	struct claw_drvdata *drvdata = container_of(work, struct claw_drvdata,
> +						    orphan_ack_work);
> +
> +	/*
> +	 * A query is enough to make the MCU stop re-sending the pending ACK.
> +	 * claw_hw_output_report() also waits for any orphan ACK before
> +	 * sending, so this cannot interleave with a real command sequence.
> +	 */
> +	claw_hw_output_report(drvdata->hdev, CLAW_COMMAND_TYPE_READ_GAMEPAD_MODE,
> +			      NULL, 0, 25);
> +}

[ ... ]

> @@ -2014,6 +2036,7 @@ static int claw_suspend(struct hid_device *hdev)
>  
>  	cancel_delayed_work_sync(&drvdata->cfg_resume);
>  	cancel_delayed_work_sync(&drvdata->cfg_setup);
> +	cancel_work_sync(&drvdata->orphan_ack_work);
>  	cancel_delayed_work_sync(&drvdata->rgb_queue);
>  
>  	return 0;

[Severity: Medium]
Can claw_raw_event() re-schedule drvdata->orphan_ack_work after claw_suspend()
cancels it?

In drivers/hid/usbhid/hid-core.c, hid_suspend() runs hid_driver_suspend()
before hid_cease_io() stops usbhid->urbin, and claw_suspend() also cancels
orphan_ack_work before cancel_delayed_work_sync(&drvdata->rgb_queue) (where
claw_rgb_queue_fn() sends CLAW_COMMAND_TYPE_SYNC_TO_ROM with timeout 0):

CPU0                              CPU1
hid_suspend()
  ...
  claw_suspend()
    cancel_work_sync(&drvdata->orphan_ack_work)
                                  claw_raw_event()
                                    schedule_work(&drvdata->orphan_ack_work)
    cancel_delayed_work_sync(&drvdata->rgb_queue)
  ...
  hid_cease_io(usbhid)
                                  claw_orphan_ack_drain()
                                    claw_hw_output_report()
                                      ...
                                      usb_interrupt_msg()  <-- I/O while suspended

Because nothing prevents claw_raw_event() from queueing orphan_ack_work during
suspend, claw_orphan_ack_drain() can run and issue synchronous USB output
transfers against a suspending or already-suspended device after usbhid->urbin
has been stopped.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261114101045.21986-1-kenhou@kenhou.net?part=1

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2] HID: hid-msi: Drain orphan MCU ACKs by issuing a query
  2026-11-14 10:10 [PATCH] HID: hid-msi: Drain orphan MCU ACKs by issuing a query Kenhou Iijima
  2026-11-14 10:29 ` sashiko-bot
@ 2026-11-14 12:50 ` Kenhou Iijima
  2026-11-16 13:46   ` [PATCH v3] HID: hid-msi: Ignore unexpected 0x06 ACKs outside WRITE_PROFILE_DATA Kenhou Iijima
  1 sibling, 1 reply; 6+ messages in thread
From: Kenhou Iijima @ 2026-11-14 12:50 UTC (permalink / raw)
  To: Derek J . Clark, Jiri Kosina, Benjamin Tissoires
  Cc: Denis Benato, Zhouwang Huang, linux-input, linux-kernel,
	Kenhou Iijima

On the MSI Claw 8 AI+ (0db0:1901, firmware 2.30), when a timeout-0
command such as SYNC_TO_ROM (0x22) or SWITCH_MODE (0x24) is issued
(either by the driver or from userspace via hidraw), the controller MCU
sends an asynchronous 0x06 ACK. Unlike regular command responses, the MCU
firmware keeps re-sending this 0x06 ACK approximately every 2 seconds
indefinitely until it receives another feature command (such as
READ_GAMEPAD_MODE).

When issued from userspace via hidraw while hid-msi is bound, the
driver never arms orphan_ack_pending, so every 0x06 ACK arrives with
waiting_cmd == CLAW_COMMAND_TYPE_NONE. When issued in-kernel with
timeout == 0, the initial ACK clears orphan_ack_pending if it arrives
within the 25 ms orphan_ack_complete window (whereas a SYNC_TO_ROM flash
commit ACK arrives ~560 ms later, after a subsequent command or its
timeout clears orphan_ack_pending), and all subsequent 2-second retries
likewise arrive with waiting_cmd != CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA.
Because nothing sends a follow-up command when the device is otherwise
idle, the MCU stays stuck in its ACK-retry state:

1. An endless stream of 0x06 packets is delivered over the USB interrupt
   IN endpoint every 2 seconds until the next command is sent.
2. Only WRITE_PROFILE_DATA (0x21) expects a generic 0x06 ACK; read
   commands wait for their own ACK command types. Without guarding
   CLAW_COMMAND_TYPE_ACK against other waiting commands, a stray or
   delayed 0x06 ACK arriving while READ_PROFILE or READ_GAMEPAD_MODE is
   in flight completes send_cmd_complete early with an empty response
   buffer, corrupting the read result.

Fix this by:
- Scheduling a work item (orphan_ack_work) that issues a harmless
  CLAW_COMMAND_TYPE_READ_GAMEPAD_MODE query whenever an unhandled 0x06
  ACK arrives (whether caught by orphan_ack_pending or when waiting_cmd
  is not CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA), which clears the MCU's
  pending ACK state so it stops re-transmitting 0x06 every 2 seconds.
- Completing send_cmd_complete on CLAW_COMMAND_TYPE_ACK only when
  waiting_cmd is CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA, and draining any
  other 0x06 ACK via orphan_ack_work instead.
- Cancelling orphan_ack_work via disable_work_sync() in claw_remove()
  and msi_suspend(), and re-enabling it via enable_work() in
  msi_resume(). Set .reset_resume = pm_ptr(msi_resume) as well, because
  usbhid defines .reset_resume without falling back to
  hdev->driver->resume in hid_driver_reset_resume(), so reset-resume
  paths also need msi_resume() to re-enable orphan_ack_work.

Fixes: a9517958e828 ("HID: hid-msi: Add MSI Claw configuration driver")
Signed-off-by: Kenhou Iijima <kenhou@kenhou.net>
---
Changes in v2:
- Guard CLAW_COMMAND_TYPE_ACK so send_cmd_complete is only completed
  when waiting_cmd == CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA, avoiding
  early completion of in-flight reads; schedule orphan_ack_work (with a
  dev_dbg log) for any other 0x06 ACK (whether caught by
  orphan_ack_pending or arriving later) instead of waiting for a 2 s
  retry.
- Use disable_work_sync() in claw_remove() and claw_suspend() paired
  with enable_work() in claw_resume(), and wire .reset_resume to
  msi_resume so urbin interrupts cannot re-queue work across suspend,
  reset-resume, or removal.
- Document why only WRITE_PROFILE_DATA expects 0x06 and why drain
  errors (e.g. during RESET_DEVICE or disconnect) are ignored.
- Link to v1: https://lore.kernel.org/linux-input/20261114101045.21986-1-kenhou@kenhou.net/

 drivers/hid/hid-msi.c | 38 +++++++++++++++++++++++++++++++++++---
 1 file changed, 35 insertions(+), 3 deletions(-)

diff --git a/drivers/hid/hid-msi.c b/drivers/hid/hid-msi.c
index 23d731f700..054babadda 100644
--- a/drivers/hid/hid-msi.c
+++ b/drivers/hid/hid-msi.c
@@ -332,6 +332,7 @@ struct claw_drvdata {
 	enum claw_profile_ack_pending profile_pending;
 	struct completion orphan_ack_complete;
 	struct completion send_cmd_complete;
+	struct work_struct orphan_ack_work;
 	struct delayed_work cfg_resume;
 	struct delayed_work cfg_setup;
 	spinlock_t registration_lock; /* Lock for registration read/write */
@@ -528,11 +529,20 @@ static int claw_raw_event(struct claw_drvdata *drvdata, struct hid_report *repor
 		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. Any other
+			 * 0x06 ACK is an unhandled orphan ACK that the MCU will
+			 * re-send roughly every 2 s until it receives another
+			 * command, so schedule a harmless query to drain it.
+			 */
+			dev_dbg(&drvdata->hdev->dev,
+				"Draining orphan ACK (waiting CMD: %x)\n",
+				drvdata->waiting_cmd);
+			schedule_work(&drvdata->orphan_ack_work);
 			break;
 		}
 
@@ -653,6 +663,22 @@ static int claw_hw_output_report(struct hid_device *hdev, u8 index, u8 *data,
 	return __claw_hw_output_report(hdev, index, data, len, timeout);
 }
 
+static void claw_orphan_ack_drain(struct work_struct *work)
+{
+	struct claw_drvdata *drvdata = container_of(work, struct claw_drvdata,
+						    orphan_ack_work);
+
+	/*
+	 * A query is enough to make the MCU stop re-sending the pending ACK.
+	 * claw_hw_output_report() also waits for any orphan ACK before
+	 * sending, so this cannot interleave with a real command sequence.
+	 * Best-effort drain: errors (e.g. during RESET_DEVICE or disconnect)
+	 * are harmless and intentionally ignored.
+	 */
+	claw_hw_output_report(drvdata->hdev, CLAW_COMMAND_TYPE_READ_GAMEPAD_MODE,
+			      NULL, 0, 25);
+}
+
 static int claw_switch_mode(struct hid_device *hdev, enum claw_mode_field field, u8 val)
 {
 	struct claw_drvdata *drvdata = hid_get_drvdata(hdev);
@@ -1868,6 +1894,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_WORK(&drvdata->orphan_ack_work, &claw_orphan_ack_drain);
 	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);
@@ -1954,6 +1981,7 @@ static void claw_remove(struct hid_device *hdev)
 		led_classdev_multicolor_unregister(&drvdata->led_mc);
 	}
 	cancel_delayed_work_sync(&drvdata->rgb_queue);
+	disable_work_sync(&drvdata->orphan_ack_work);
 
 	hid_hw_close(hdev);
 }
@@ -1983,6 +2011,8 @@ static int claw_resume(struct hid_device *hdev)
 	if (!drvdata)
 		return -ENODEV;
 
+	enable_work(&drvdata->orphan_ack_work);
+
 	/* MCU can take up to 500ms to be ready after resume */
 	schedule_delayed_work(&drvdata->cfg_resume, msecs_to_jiffies(500));
 	return 0;
@@ -2015,6 +2045,7 @@ static int claw_suspend(struct hid_device *hdev)
 	cancel_delayed_work_sync(&drvdata->cfg_resume);
 	cancel_delayed_work_sync(&drvdata->cfg_setup);
 	cancel_delayed_work_sync(&drvdata->rgb_queue);
+	disable_work_sync(&drvdata->orphan_ack_work);
 
 	return 0;
 }
@@ -2052,6 +2083,7 @@ static struct hid_driver msi_driver = {
 	.probe		= msi_probe,
 	.remove		= msi_remove,
 	.resume		= pm_ptr(msi_resume),
+	.reset_resume	= pm_ptr(msi_resume),
 	.suspend	= pm_ptr(msi_suspend),
 };
 module_hid_driver(msi_driver);
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v3] HID: hid-msi: Ignore unexpected 0x06 ACKs outside WRITE_PROFILE_DATA
  2026-11-14 12:50 ` [PATCH v2] " Kenhou Iijima
@ 2026-11-16 13:46   ` Kenhou Iijima
  2026-12-01  1:25     ` Derek J. Clark
  0 siblings, 1 reply; 6+ messages in thread
From: Kenhou Iijima @ 2026-11-16 13:46 UTC (permalink / raw)
  To: Derek J . Clark, Jiri Kosina, Benjamin Tissoires
  Cc: Denis Benato, Zhouwang Huang, linux-input, linux-kernel,
	Kenhou Iijima

On the MSI Claw handhelds (e.g. 0db0:1901), some timeout-0 commands
answer with more than one generic 0x06 ACK: SYNC_TO_ROM (0x22) sends an
immediate ACK plus a flash commit ACK about 560 ms later, and
SWITCH_MODE (0x24) can answer with a second ACK about 15 ms after the
command (~8 ms after the first ACK). The first ACK is consumed by the
orphan_ack_pending path; the delayed ones fall through to the rest of
the CLAW_COMMAND_TYPE_ACK handler.

That handler only ignores an ACK when waiting_cmd is
CLAW_COMMAND_TYPE_NONE. If a delayed 0x06 arrives while a read command
is in flight, e.g. READ_GAMEPAD_MODE after an RGB update sequence, the
ACK completes send_cmd_complete early without invoking the read response
handler: the caller returns stale cached data and the real 0x27 reply
arriving moments later is discarded. Reproduced on firmware 2.30: a
READ_GAMEPAD_MODE that normally takes about 6 ms completed in 1.56 ms
and returned the cached gamepad mode. The same fall-through also makes
every expected second ACK print a "Got unexpected ACK from MCU"
dev_warn during normal operation.

Only WRITE_PROFILE_DATA (0x21) waits for a generic 0x06 ACK; reads wait
for their own ACK command types. Complete send_cmd_complete on an ACK
only while WRITE_PROFILE_DATA is pending, and downgrade the rest to a
dev_dbg including the command being waited on so they can still be
traced when needed.

Fixes: a9517958e828 ("HID: hid-msi: Add MSI Claw configuration driver")
Signed-off-by: Kenhou Iijima <kenhou@kenhou.net>
---
Changes in v3:
- Apologies for the noise in v1/v2: the "MCU re-sends the 0x06 ACK
  every ~2 s" claim was my own misdiagnosis. A local userspace LED
  daemon on my test machine had a bug that kept re-writing the LED
  sysfs every ~2 s, and each write's trailing SYNC_TO_ROM produced its
  normal ~560 ms flash-commit ACK. Timestamped captures of the config
  interface on firmware 2.30 with local userspace helpers stopped
  confirm the firmware only ever sends the two ACKs described above.
  Drop orphan_ack_work, claw_orphan_ack_drain(), the
  disable_work_sync()/enable_work() pairing and the .reset_resume hook
  accordingly: there is nothing left to drain.
- Keep the core fix: complete send_cmd_complete on a generic 0x06 ACK
  only while WRITE_PROFILE_DATA is pending, and demote the
  ignored-ACK log to dev_dbg with the command being waited on.

- Link to v2: https://lore.kernel.org/linux-input/20261114125059.17772-1-kenhou@kenhou.net/
- Link to v1: https://lore.kernel.org/linux-input/20261114101045.21986-1-kenhou@kenhou.net/
 drivers/hid/hid-msi.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/drivers/hid/hid-msi.c b/drivers/hid/hid-msi.c
index 23d731f700..0c30846ac8 100644
--- a/drivers/hid/hid-msi.c
+++ b/drivers/hid/hid-msi.c
@@ -531,8 +531,20 @@ static int claw_raw_event(struct claw_drvdata *drvdata, struct hid_report *repor
 			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. SYNC_TO_ROM
+			 * answers twice (an immediate ACK and a flash commit ACK
+			 * ~560 ms later) and SWITCH_MODE can answer with a second
+			 * delayed ACK as well; the first is consumed above, so any
+			 * 0x06 arriving here is a leftover of an already finished
+			 * command. Ignore it instead of completing the pending
+			 * read early.
+			 */
+			dev_dbg(&drvdata->hdev->dev,
+				"Got unexpected ACK from MCU, ignoring (waiting CMD: %x)\n",
+				drvdata->waiting_cmd);
 			break;
 		}
 
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v3] HID: hid-msi: Ignore unexpected 0x06 ACKs outside WRITE_PROFILE_DATA
  2026-11-16 13:46   ` [PATCH v3] HID: hid-msi: Ignore unexpected 0x06 ACKs outside WRITE_PROFILE_DATA Kenhou Iijima
@ 2026-12-01  1:25     ` Derek J. Clark
  2026-12-01 16:49       ` Kenhou Iijima
  0 siblings, 1 reply; 6+ messages in thread
From: Derek J. Clark @ 2026-12-01  1:25 UTC (permalink / raw)
  To: Kenhou Iijima; +Cc: linux-input, Jiri Kosina, Benjamin Tissoires, Ana Veto

On Mon, 16 Nov 2026, Kenhou Iijima wrote:
> Only WRITE_PROFILE_DATA (0x21) waits for a generic 0x06 ACK; reads wait
> for their own ACK command types. Complete send_cmd_complete on an ACK
> only while WRITE_PROFILE_DATA is pending, ...

Kenhou,

Dropping the drain work and the .reset_resume hook deconflicts my pending
series that uses it to re-read the gamepad mode and the RGB profile after
a bus reset. Thank you for that.

The guard can still be completed by a subsequent WRITE_PROFILE_DATA within
the ACK window.

  t=0      store: WRITE_PROFILE_DATA, then SYNC_TO_ROM with timeout 0
  t=4ms    ACK 1 arrives, consumed by the orphan_ack_pending path
  t=10ms   next store: WRITE_PROFILE_DATA is the outstanding command
  t=560ms  ACK 2, the commit ACK for the command sent at t=0, arrives.
           With your guard it matches waiting_cmd == WRITE_PROFILE_DATA,
           so it completes that write. The store returns success for a
           write the MCU has not acknowledged, and the write's own ACK is
           then discarded because waiting_cmd is NONE by then.

If we add a counter of the ACK's incremented when the command that
produces it is sent, and drop it wherever it lands, then we can satisfy an
arbitrary number of sequential calls. Something like:

<8>
---
 drivers/hid/hid-msi.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

--- a/drivers/hid/hid-msi.c
+++ b/drivers/hid/hid-msi.c
@@ -339,6 +339,7 @@
 	spinlock_t profile_lock; /* Lock for profile_pending read/write */
 	struct hid_device *hdev;
 	bool orphan_ack_pending;
+	u8 late_acks; /* Second generic ACK the MCU still owes, 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 */
@@ -531,6 +532,12 @@
 			break;
 		}
 
+		if (drvdata->late_acks) {
+			drvdata->late_acks--;
+			dev_dbg(&drvdata->hdev->dev, "Dropped a counted MCU ACK\n");
+			break;
+		}
+
 		if (drvdata->waiting_cmd != CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA) {
 			/*
 			 * Only WRITE_PROFILE_DATA waits for a generic 0x06 ACK;
@@ -619,6 +626,15 @@
 			reinit_completion(&drvdata->orphan_ack_complete);
 			drvdata->waiting_cmd = CLAW_COMMAND_TYPE_NONE;
 			drvdata->orphan_ack_pending = true;
+			/*
+			 * SYNC_TO_ROM and SWITCH_MODE answer a second time, long after
+			 * the transfer that produced it. Count the extra ACK so it is
+			 * dropped wherever it lands rather than taken for the reply of
+			 * whatever command is pending when it arrives.
+			 */
+			if (index == CLAW_COMMAND_TYPE_SYNC_TO_ROM ||
+			    index == CLAW_COMMAND_TYPE_SWITCH_MODE)
+				drvdata->late_acks++;
 		}
 	}
 
@@ -652,6 +668,7 @@
 	scoped_guard(spinlock_irqsave, &drvdata->cmd_lock) {
 		drvdata->waiting_cmd = CLAW_COMMAND_TYPE_NONE;
 		drvdata->orphan_ack_pending = false;
+		drvdata->late_acks = 0;
 	}
 	return ret;
 }
@@ -2028,6 +2045,10 @@
 	cancel_delayed_work_sync(&drvdata->cfg_setup);
 	cancel_delayed_work_sync(&drvdata->rgb_queue);
 
+	/* A counted ACK can be overtaken by suspend and never arrive. */
+	scoped_guard(spinlock_irqsave, &drvdata->cmd_lock)
+		drvdata->late_acks = 0;
+
 	return 0;
 }
 

</8>

If you apply this on top for v4, please add these before your SOB:

  Co-developed-by: "Derek J. Clark" <derekjohn.clark@gmail.com>
  Signed-off-by: "Derek J. Clark" <derekjohn.clark@gmail.com>

Thanks,
Derek

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3] HID: hid-msi: Ignore unexpected 0x06 ACKs outside WRITE_PROFILE_DATA
  2026-12-01  1:25     ` Derek J. Clark
@ 2026-12-01 16:49       ` Kenhou Iijima
  0 siblings, 0 replies; 6+ messages in thread
From: Kenhou Iijima @ 2026-12-01 16:49 UTC (permalink / raw)
  To: Derek J. Clark
  Cc: Kenhou Iijima, linux-input, Jiri Kosina, Benjamin Tissoires,
	Ana Veto

Hi Derek,

Thanks for spotting the WRITE_PROFILE_DATA window race — that scenario is real
on firmware 2.30, and testing it on my Claw 8 AI+ (fw 2.30, with all userspace
daemons stopped) also showed why modeling it with a `late_acks` packet counter
breaks on this MCU.

The core issue is that the hazard is time-bounded rather than a fixed packet
debt. Testing `late_acks` on hardware hit -EBUSY for two distinct reasons:

1. Normal back-to-back writes get their own immediate 0x06 eaten:
   With `rumble_intensity_left` followed 10 ms later by
   `rumble_intensity_right`:

     left store:   0x21 -> 0x06 (3 ms) -> SYNC_TO_ROM (0x22), late_acks = 1
                   0x06 (3 ms, consumed by orphan_ack_pending)
     +10 ms right: 0x21 sent (waiting_cmd = 0x21)
     +3 ms later:  0x06 arrives (the new write's own ACK, well before +560 ms)
                   -> "Dropped a counted MCU ACK" (late_acks -> 0)
                   -> "Remaining timeout: 0" (-EBUSY)

   The same -EBUSY reproduces on `gamepad_mode` -> immediate write (47.97 ms,
   -EBUSY) and `SYNC_TO_ROM` + 560 ms -> write (34.79 ms, -EBUSY).

2. The MCU emits a variable number of 0x06 ACKs (1 or 2), stranding `late_acks`:
   - Issuing READ_GAMEPAD_MODE (0x26) right after SWITCH_MODE (0x24) suppresses
     SWITCH_MODE's second 0x06 ACK. `late_acks` stays 1 and breaks the next
     WRITE_PROFILE_DATA even 1.0 s later (-EBUSY).
   - SYNC_TO_ROM (0x22) also frequently emits only the single ~515-565 ms
     commit 0x06 with no immediate +4 ms ACK; once the 25 ms orphan wait
     expires (or orphan_ack_pending eats that single commit ACK), `late_acks`
     again drifts out of sync. Conversely, zeroing `late_acks` on `err:` drops
     pending state on an unrelated failure or signal interruption, re-opening
     the premature-completion race.

Separately, testing concurrent commands revealed another MCU behavior that also
calls for a time/completion window rather than a counter: while SYNC_TO_ROM is
committing to flash, the MCU answers reads immediately (~4-9 ms) but queues
SWITCH_MODE (0x24) behind the commit and emits a ~16 ms 3-packet 0x06 burst
(+0/+8/+16 ms) right when the flash commit completes.

For v4, I switched to tracking time/completion windows instead of counting ACKs:
- Keep the waiting_cmd == WRITE_PROFILE_DATA guard and dev_dbg downgrade for
  leftover 0x06 packets.
- Track a unified 35 ms 0x06 quiet window (`ack_quiet_until`, inside cfg_mutex)
  for non-SYNC_TO_ROM timeout-0 commands and track the SYNC_TO_ROM commit
  window (700 ms fallback / 100 ms min threshold, outside cfg_mutex via
  rom_sync_complete), clearing both on remove and suspend.
- Wait on active windows only before WRITE_PROFILE_DATA 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 post-commit 0x06 burst.

With v4 on the same daemon-free suite (including readback verification,
concurrent reads + SWITCH_MODE during SYNC_TO_ROM, and SIGALRM interruption),
every case passes with zero timeouts or lost commits.

I'll send v4 shortly with Reported-by/Closes crediting your report.

Regards,
Kenhou

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-12-01 16:49 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-14 10:10 [PATCH] HID: hid-msi: Drain orphan MCU ACKs by issuing a query Kenhou Iijima
2026-11-14 10:29 ` sashiko-bot
2026-11-14 12:50 ` [PATCH v2] " Kenhou Iijima
2026-11-16 13:46   ` [PATCH v3] HID: hid-msi: Ignore unexpected 0x06 ACKs outside WRITE_PROFILE_DATA Kenhou Iijima
2026-12-01  1:25     ` Derek J. Clark
2026-12-01 16:49       ` Kenhou Iijima

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox