From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.178 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796452002; b=e8cmuW6Ubr3lyfSMXNHSV/+M3sft0xj56JALRvPKT5dDx4lhDqMgO5zcZwQNaS1BrcHt qTCRxTI6owsMCWr2fwJ9Q0jTGzCIuHSt+vSnKnI9r5YfGq01QeOZAes5k6Y4Zsc34BDIo AlZpSFZiJoF9Xeji5ftZI3CB3sYOWOoKBIap97ktrHlk4AIIaL4qfJjDAKRwsgffB/TSz WfdORlWAIFgAcI5I7Cpy8iBtUgS8kI1V1A2KUPo5GdAoR68n14+dR30QKq43TuhjVMawz ufL5vwgPnihvrRZNcVsWl3bhCLqZBOC/uaSmAU0ANc4/fS9xJiTbUgkMBEcppbykWHQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796452002; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=QupY2lGoIlFnAYpRze7plpQyd6dkpy56gxnJKNW5FqA=; b=FQR4jSiysBZI8wgf8SptglvjVixg+CvbJ3qoa/Rxm5aUEaPg/CvIGu65a2n0mrJ6S4qo Jj82+jxOtj1SQFt9oxUS+J5uTPRJOzrfQgiYXKdecqBAn0aUntwRPVYngplwKwh251WIS fpAVs0IvV4CsgPP0V9BlmPBTSQQSwfE/8JBztQRsQQ+jm8yqbFdf2elSTSC02smDGju1O dlbs2gw9edVDrcn7cwHZSb8t8k0x/r1fdJpH4nNoxECo5FLb2s47OmLxQ56awWhxlmPJj wC7fRrWXgkNIc0d0QnBlmWOE2A3tXKodPW8CoC0kx7M73lf9YiqV+ibe8PGtvhBnjjw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=kenhou.net; spf=none smtp.mailfrom=kenhou.net; dkim=pass header.d=kenhou-net.20260707.gappssmtp.com header.i=@kenhou-net.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=tTFQD5DR; arc=none smtp.remote-ip=209.85.214.178 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6AADA606A32F for ; Sat, 5 Dec 2026 06:26:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6AADA606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kenhou.net Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=kenhou.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kenhou-net.20260707.gappssmtp.com header.i=@kenhou-net.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=tTFQD5DR Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-2f1713db53aso1846645ad.1 for ; Fri, 04 Dec 2026 22:26:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kenhou-net.20260707.gappssmtp.com; s=20260707; t=1796452001; x=1797056801; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=QupY2lGoIlFnAYpRze7plpQyd6dkpy56gxnJKNW5FqA=; b=tTFQD5DR11vfDhnUQoaIQoF3qls2jJmDINhMcXA3ekfqh9C5LYN5oSL8pbdbK9ajYl cZt/v99Dbsow8Kr5SoCXbx/QdoNoELiE3iReGZ7HI7VKd8G4zDQFBVhOKMup4Hf8+CPq zFdpwYcCd4ONAdY2ayYW0Y4w/Cno3MhqlOOhxF8+olLjnumnOBoSJgVJmHeTy5GtjIpk w3xGpWhkZwV8K/BRJf0suFiClUrMqd/pwRklzoq7+2nT+Wra0DkccovlrusrK1s3gnBB 32tQIecFjCznm3bSjwcV1SgCG3Em1SMtSY6s08ZVx5kRH9BcWxArG+emMfvhymsEtStd YVMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796452001; x=1797056801; h=content-transfer-encoding:mime-version: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=QupY2lGoIlFnAYpRze7plpQyd6dkpy56gxnJKNW5FqA=; b=h0VxYGqoFv7DbojwyQK+I+lxjqbbTb5b19mRXc1P0gAYjcIkw8ng+PZiSmvvMvn6Mn dbW8JnTot5GgzhR/q7HwDlQyfJ7nGThLeUwmspmHhsz/eJ9lR0zA9iOL+rD/tpFZaas4 AeTWdUlFBvarr5/bxtBrqoA+iPaaXk4H0jSrNI6d6ALYfUroUzFcSntbZs6P0EKl33q9 OiogK3wVM3rvu2F76nbTOrdHe6duvbZeO5NSyIppJn9qlCQT/sw3FIYZdGSOrMXgmVe3 4W5+WLJ1Xb9E2LmDGApoTm28AhoILJYu7PZ4soWBSk1YqMwl8ITIVwxzTwfR8+5TPUoF Pc/w== X-Forwarded-Encrypted: i=1; AL+hErG6UoEXcmdeWK2Gkg20uAo1SrxPxED+PLw8CDE+R+4gqE5n0QQo0FLMysqMoQP1CWSeCBTmTnTLs3tatw==@vger.kernel.org X-Gm-Message-State: AB4q+uF5sfngTKSmwwxlPINZbfdtWrzhetZR55s1uWYGiUxSW3wxrKl3 sds4iTuphRk/xZcdS3UMsAhA9wO16F5FTo1Qk2tpqzwpAWyk9v6kKYbhtKmSmhUpItwEOdELQPi N4Nq0AKZ33Q== X-Gm-Gg: AZFdTZdu2ZwsMzdxueeb0cHLiGfrfEo2n5AulEHihG66o4nGCkEUdQnUg8k4aHA6oUc lF34gN7QBiSZDBGylxBByvAtEdQPOYZUfDIKEdQcWgO9nuwwzRoL0O7kIgFiIXFeiM1YtZbA4wt Yz9HS16HGxaf6MGAAiTYIp+jUxV7Y4ZiBGsGNXqdl5c6UVO1nk6B1uYi5Hl/c47rr9XF8AB5Hw7 C0JsfdyfEwTQ+R7SnHMJV3zXAs/UM/cY2law5dageOnXuckWofraXCYuN070ZJOo7YmadeMXzFg DqVjFIW4YxZCQAVLElTnQSaXpHxb42MxRvy0hcGm73SWO1R3uIznOPzms/4LGtiTH4gGaY/yHW6 Q5kRhGwrePaoUPRPL5Vdi2Y7DfJEiPeBFIG8JQcPgDlCq2Laemz1JqNKD9k3hEfW9LI7lZEGVL1 DAPvkPpOSWOBin4AujVLBi6ewX5kVH17k99uJtD0xWhYLTPG4p7x8j0Ixy2U9YCCenaDwUqFwor WFKkJL8IxrsrImr1OtfN7vfaEdGPajiPqS3OUFME7tNSsqXpGrxg9ZayoAlH+Be3wESl+UyZr9W LmtSkvlcXEsc8958USUyO8tQekQzJmapS4bEGG2EyAFi87dYRomgGHCLQXqkljNS/CWywgzcd1W lms0bhfHgh8qr9oszF6sTHYAxF+Rf2s9g5InjsOk78YnTOkLeHPy/xGP4MqslWoxYUa8GR0Q3er VLPHe8 X-Received: by 2002:a17:90b:2711:b0:3b4:22d:e072 with SMTP id 98e67ed59e1d1-3b612489f37mr2588010a91.5.1796452000618; Fri, 04 Dec 2026 22:26:40 -0800 (PST) Received: from kenhou-claw.tailf09bb7.ts.net (p1571063-ipxg05601hiraide.tochigi.ocn.ne.jp. [153.185.50.63]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b5c7d10d11sm12957993a91.6.2026.12.04.22.26.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 22:26:40 -0800 (PST) From: Kenhou Iijima To: Jiri Kosina , Benjamin Tissoires , "Derek J . Clark" Cc: Denis Benato , Zhouwang Huang , Ana Veto , 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 Message-ID: <20261205062637.84074-1-kenhou@kenhou.net> X-Mailer: git-send-email 2.56.0 X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Link: https://lore.kernel.org/r/20261201012506.2313913-1-derekjohn.clark@gmail.com Signed-off-by: Kenhou Iijima --- 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 and 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 #include #include +#include #include #include #include +#include #include #include #include @@ -32,6 +34,7 @@ #include #include #include +#include #include #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