Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: "Derek J. Clark" <derekjohn.clark@gmail.com>
To: jikos@kernel.org, bentiss@kernel.org
Cc: linux-input@vger.kernel.org,
	kernel@lists.opengamingcollective.org,
	gregkh@linuxfoundation.org, linux-usb@vger.kernel.org,
	pgriffais@valvesoftware.com, derekjohn.clark@gmail.com,
	stable@vger.kernel.org
Subject: [PATCH 2/8] HID: hid-msi: Move the ROM commit to an explicit apply attribute
Date: Thu,  3 Dec 2026 16:18:40 -0800	[thread overview]
Message-ID: <20261204001846.3801336-3-derekjohn.clark@gmail.com> (raw)
In-Reply-To: <20261204001846.3801336-1-derekjohn.clark@gmail.com>

Four write paths end with SYNC_TO_ROM, the command that copies the MCU
working copy to its internal flash. They are the M-key store behind
button_m1 and button_m2, rumble_intensity_left/right and the RGB state
write behind effect, speed and enabled.

Changes to settings without SYNC_TO_ROM survive until the MCU is power
cycled, so a write on every change adds unnecessary wear on the internal
flash. Add a write-only attribute, apply, that issues SYNC_TO_ROM on
demand and drop it from those four paths.

This changes how userspace interacts, though it isn't a critical
difference. Users who want the changes to persist will need to do an
additional step, but most users are using userspace tools that set the
last configured state on startup already. Every store currently reaches
the flash, causing excess wear, and the life of the chip outweighs
userspace ABI stability in this case.

Fixes: 93ff13ed1c4b ("HID: hid-msi: Add M-key mapping attributes")
Fixes: 95547c53f1c6 ("HID: hid-msi: Add Rumble Intensity Attributes")
Fixes: f27e2e3c80ed ("HID: hid-msi: Add RGB control interface")
Cc: stable@vger.kernel.org
Signed-off-by: Derek J. Clark <derekjohn.clark@gmail.com>
---
 drivers/hid/hid-msi.c | 72 +++++++++++++++++++++++--------------------
 1 file changed, 38 insertions(+), 34 deletions(-)

diff --git a/drivers/hid/hid-msi.c b/drivers/hid/hid-msi.c
index d4128955e463..0e1e5e6f664d 100644
--- a/drivers/hid/hid-msi.c
+++ b/drivers/hid/hid-msi.c
@@ -340,7 +340,7 @@ struct claw_drvdata {
 	struct hid_device *hdev;
 	bool orphan_ack_pending;
 	struct mutex cfg_mutex; /* mutex for synchronous data */
-	struct mutex rom_mutex; /* mutex for SYNC_TO_ROM calls */
+	struct mutex rom_mutex; /* Serialises profile writes and the ROM commit */
 	spinlock_t cmd_lock; /* Lock for cmd data read/write */
 	u8 waiting_cmd;
 	int cmd_status;
@@ -852,6 +852,39 @@ static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
 }
 static DEVICE_ATTR_WO(reset);
 
+static ssize_t apply_store(struct device *dev, struct device_attribute *attr,
+			   const char *buf, size_t count)
+{
+	struct hid_device *hdev = to_hid_device(dev);
+	struct claw_drvdata *drvdata = hid_get_drvdata(hdev);
+	bool val;
+	int ret;
+
+	scoped_guard(spinlock_irqsave, &drvdata->registration_lock) {
+		/* Pairs with smp_store_release from cfg_setup_fn in system_percpu_wq context */
+		if (!smp_load_acquire(&drvdata->gp_registered))
+			return -ENODEV;
+	}
+
+	ret = kstrtobool(buf, &val);
+	if (ret)
+		return ret;
+
+	if (!val)
+		return -EINVAL;
+
+	flush_delayed_work(&drvdata->rgb_queue);
+
+	guard(mutex)(&drvdata->rom_mutex);
+
+	ret = claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_SYNC_TO_ROM, NULL, 0, 0);
+	if (ret)
+		return ret;
+
+	return count;
+}
+static DEVICE_ATTR_WO(apply);
+
 static int mkey_mapping_name_to_code(const char *name)
 {
 	int i;
@@ -918,12 +951,6 @@ static int claw_mkey_store(struct device *dev, const char *buf, u8 mkey)
 					    (u8 *)&report, sizeof(report), 25);
 		if (ret)
 			return ret;
-		/* MCU will not send ACK until the USB transaction completes. ACK is sent
-		 * immediately after and will hit the stale state machine, before the next
-		 * command re-arms the state machine. Timeout 0 ensures no deadlock waiting
-		 * for ACK that ill never come.
-		 */
-		ret = claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_SYNC_TO_ROM, NULL, 0, 0);
 	}
 
 	return ret;
@@ -1054,15 +1081,6 @@ static ssize_t rumble_intensity_left_store(struct device *dev,
 	if (ret)
 		return ret;
 
-	/* MCU will not send ACK until the USB transaction completes. ACK is sent
-	 * immediately after and will hit the stale state machine, before the next
-	 * command re-arms the state machine. Timeout 0 ensures no deadlock waiting
-	 * for ACK that ill never come.
-	 */
-	ret = claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_SYNC_TO_ROM, NULL, 0, 0);
-	if (ret)
-		return ret;
-
 	return count;
 }
 
@@ -1128,15 +1146,6 @@ static ssize_t rumble_intensity_right_store(struct device *dev,
 	if (ret)
 		return ret;
 
-	/* MCU will not send ACK until the USB transaction completes. ACK is sent
-	 * immediately after and will hit the stale state machine, before the next
-	 * command re-arms the state machine. Timeout 0 ensures no deadlock waiting
-	 * for ACK that ill never come.
-	 */
-	ret = claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_SYNC_TO_ROM, NULL, 0, 0);
-	if (ret)
-		return ret;
-
 	return count;
 }
 
@@ -1196,7 +1205,8 @@ static umode_t claw_gamepad_attr_is_visible(struct kobject *kobj, struct attribu
 	    attr == &dev_attr_gamepad_mode_index.attr ||
 	    attr == &dev_attr_mkeys_function.attr ||
 	    attr == &dev_attr_mkeys_function_index.attr ||
-	    attr == &dev_attr_reset.attr)
+	    attr == &dev_attr_reset.attr ||
+	    attr == &dev_attr_apply.attr)
 		return attr->mode;
 
 	/* Hide rumble attrs if not supported */
@@ -1218,6 +1228,7 @@ static struct attribute *claw_gamepad_attrs[] = {
 	&dev_attr_mkeys_function.attr,
 	&dev_attr_mkeys_function_index.attr,
 	&dev_attr_reset.attr,
+	&dev_attr_apply.attr,
 	&dev_attr_rumble_intensity_left.attr,
 	&dev_attr_rumble_intensity_right.attr,
 	&dev_attr_rumble_intensity_range.attr,
@@ -1292,14 +1303,7 @@ static int claw_write_rgb_state(struct claw_drvdata *drvdata)
 		write_addr += CLAW_RGB_FRAME_OFFSET;
 	}
 
-	/* MCU will not send ACK until the USB transaction completes. ACK is sent
-	 * immediately after and will hit the stale state machine, before the next
-	 * command re-arms the state machine. Timeout 0 ensures no deadlock waiting
-	 * for ACK that ill never come.
-	 */
-	ret = claw_hw_output_report(drvdata->hdev, CLAW_COMMAND_TYPE_SYNC_TO_ROM, NULL, 0, 0);
-
-	return ret;
+	return 0;
 }
 
 /* Fill all zones with the same color */
-- 
2.56.0


       reply	other threads:[~2026-12-04  0:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20261204001846.3801336-1-derekjohn.clark@gmail.com>
2026-12-04  0:18 ` Derek J. Clark [this message]
2026-12-04  0:18 ` [PATCH 3/8] HID: hid-msi: Protect the RGB state with frame_lock Derek J. Clark
2026-12-04  0:18 ` [PATCH 4/8] HID: hid-msi: Re-init on reset during resume Derek J. Clark
2026-12-04  0:18 ` [PATCH 5/8] HID: hid-msi: Drain in-flight MCU traffic before suspend Derek J. Clark
2026-12-04  0:18 ` [PATCH 6/8] HID: hid-msi: Defer RGB updates while the MCU is suspended Derek J. Clark

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=20261204001846.3801336-3-derekjohn.clark@gmail.com \
    --to=derekjohn.clark@gmail.com \
    --cc=bentiss@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=jikos@kernel.org \
    --cc=kernel@lists.opengamingcollective.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=pgriffais@valvesoftware.com \
    --cc=stable@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