Linux Input/HID development
 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 6/8] HID: hid-msi: Defer RGB updates while the MCU is suspended
Date: Thu,  3 Dec 2026 16:18:44 -0800	[thread overview]
Message-ID: <20261204001846.3801336-7-derekjohn.clark@gmail.com> (raw)
In-Reply-To: <20261204001846.3801336-1-derekjohn.clark@gmail.com>

An RGB update while the MCU is suspended, or still coming back after
resume, is rejected with -EBUSY and lost. The sysfs store has already
committed the new value, so it reports a setting the MCU never got.

Mark the RGB state dirty when an update is scheduled and clear it once
claw_write_rgb_state has written every frame. Clearing it while the
report is composed loses the update. The suspend gate sits after
composition, so the state is marked clean and then never sent. A dirty
state is replayed on resume. claw_rgb_schedule takes no mutex, so the
LED path stays non-blocking. cfg_setup_fn skips the RGB read-back while
state is pending, since the read would overwrite a newer change.

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 | 49 ++++++++++++++++++++++++++++++++++++-------
 1 file changed, 42 insertions(+), 7 deletions(-)

diff --git a/drivers/hid/hid-msi.c b/drivers/hid/hid-msi.c
index d6134ff310f3..d2cd0d7397aa 100644
--- a/drivers/hid/hid-msi.c
+++ b/drivers/hid/hid-msi.c
@@ -372,6 +372,7 @@ struct claw_drvdata {
 	bool rgb_registered;
 	u8 rgb_frame_count;
 	bool rgb_enabled;
+	bool rgb_dirty; /* State held by the driver that the MCU has not taken */
 	u8 rgb_speed;
 	u16 rgb_addr;
 };
@@ -1319,13 +1320,19 @@ static int claw_write_rgb_state(struct claw_drvdata *drvdata)
 		/* Serialize the rgb_report and write it to MCU */
 		ret = claw_hw_output_report(drvdata->hdev, CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA,
 					    (u8 *)&report, sizeof(report), 25);
-		if (ret)
+		if (ret) {
+			/* Pairs with smp_load_acquire from cfg_resume_fn. */
+			smp_store_release(&drvdata->rgb_dirty, true);
 			return ret;
+		}
 
 		/* Increment the write addr by the offset for the next frame */
 		write_addr += CLAW_RGB_FRAME_OFFSET;
 	}
 
+	/* Pairs with smp_load_acquire from cfg_resume_fn */
+	smp_store_release(&drvdata->rgb_dirty, false);
+
 	return 0;
 }
 
@@ -1513,10 +1520,18 @@ static void claw_rgb_queue_fn(struct work_struct *work)
 	}
 
 	ret = claw_apply_rgb_state(drvdata);
-	if (ret)
+	if (ret && ret != -EBUSY)
 		dev_err(&drvdata->hdev->dev, "Failed to apply RGB state: %d\n", ret);
 }
 
+static void claw_rgb_schedule(struct claw_drvdata *drvdata)
+{
+	/* Pairs with the loads in cfg_resume_fn and cfg_setup_fn */
+	smp_store_release(&drvdata->rgb_dirty, true);
+
+	mod_delayed_work(system_dfl_wq, &drvdata->rgb_queue, msecs_to_jiffies(50));
+}
+
 static ssize_t effect_store(struct device *dev,
 			    struct device_attribute *attr,
 			    const char *buf, size_t count)
@@ -1539,7 +1554,7 @@ static ssize_t effect_store(struct device *dev,
 	scoped_guard(spinlock_irqsave, &drvdata->frame_lock)
 		drvdata->rgb_effect = ret;
 
-	mod_delayed_work(system_dfl_wq, &drvdata->rgb_queue, msecs_to_jiffies(50));
+	claw_rgb_schedule(drvdata);
 
 	return count;
 }
@@ -1607,7 +1622,7 @@ static ssize_t enabled_store(struct device *dev,
 	scoped_guard(spinlock_irqsave, &drvdata->frame_lock)
 		drvdata->rgb_enabled = val;
 
-	mod_delayed_work(system_dfl_wq, &drvdata->rgb_queue, msecs_to_jiffies(50));
+	claw_rgb_schedule(drvdata);
 
 	return count;
 }
@@ -1668,7 +1683,7 @@ static ssize_t speed_store(struct device *dev, struct device_attribute *attr,
 	scoped_guard(spinlock_irqsave, &drvdata->frame_lock)
 		drvdata->rgb_speed = speed;
 
-	mod_delayed_work(system_dfl_wq, &drvdata->rgb_queue, msecs_to_jiffies(50));
+	claw_rgb_schedule(drvdata);
 
 	return count;
 }
@@ -1713,7 +1728,7 @@ static void claw_led_brightness_set(struct led_classdev *led_cdev,
 			return;
 	}
 
-	mod_delayed_work(system_dfl_wq, &drvdata->rgb_queue, msecs_to_jiffies(50));
+	claw_rgb_schedule(drvdata);
 }
 
 static struct attribute *claw_rgb_attrs[] = {
@@ -1750,6 +1765,7 @@ static void cfg_setup_fn(struct work_struct *work)
 	struct delayed_work *dwork = container_of(work, struct delayed_work, work);
 	struct claw_drvdata *drvdata = container_of(dwork, struct claw_drvdata, cfg_setup);
 	bool gamepad_ready = false, rgb_ready = false, gp_registered, rgb_registered;
+	bool dirty;
 	int ret;
 
 	ret = claw_hw_output_report(drvdata->hdev, CLAW_COMMAND_TYPE_READ_GAMEPAD_MODE,
@@ -1762,6 +1778,13 @@ static void cfg_setup_fn(struct work_struct *work)
 	gamepad_ready = true;
 
 prep_rgb:
+	/* Pairs with smp_store_release from claw_rgb_schedule */
+	dirty = smp_load_acquire(&drvdata->rgb_dirty);
+	if (dirty) {
+		rgb_ready = true;
+		goto try_gamepad;
+	}
+
 	ret = claw_read_rgb_config(drvdata->hdev);
 	if (ret) {
 		dev_err(&drvdata->hdev->dev,
@@ -1828,13 +1851,20 @@ static void cfg_setup_fn(struct work_struct *work)
 		kobject_uevent(&drvdata->hdev->dev.kobj, KOBJ_CHANGE);
 	if (rgb_registered)
 		kobject_uevent(&drvdata->led_mc.led_cdev.dev->kobj, KOBJ_CHANGE);
+
+	if (rgb_ready) {
+		/* Pairs with smp_store_release from claw_rgb_schedule */
+		dirty = smp_load_acquire(&drvdata->rgb_dirty);
+		if (dirty)
+			claw_rgb_schedule(drvdata);
+	}
 }
 
 static void cfg_resume_fn(struct work_struct *work)
 {
 	struct delayed_work *dwork = container_of(work, struct delayed_work, work);
 	struct claw_drvdata *drvdata = container_of(dwork, struct claw_drvdata, cfg_resume);
-	bool gp_registered, rgb_registered, resync;
+	bool gp_registered, rgb_registered, resync, rgb_dirty;
 
 	scoped_guard(mutex, &drvdata->rom_mutex)
 		scoped_guard(mutex, &drvdata->cfg_mutex)
@@ -1843,6 +1873,9 @@ static void cfg_resume_fn(struct work_struct *work)
 	/* Pairs with smp_store_release from claw_resume in process context */
 	resync = smp_load_acquire(&drvdata->resync);
 
+	/* Pairs with smp_store_release from claw_rgb_schedule. */
+	rgb_dirty = smp_load_acquire(&drvdata->rgb_dirty);
+
 	scoped_guard(spinlock_irqsave, &drvdata->registration_lock) {
 		/* Pairs with smp_store_release from cfg_setup_fn in system_percpu_wq context */
 		gp_registered = smp_load_acquire(&drvdata->gp_registered);
@@ -1852,6 +1885,8 @@ static void cfg_resume_fn(struct work_struct *work)
 
 	if (resync || !gp_registered || !rgb_registered)
 		schedule_delayed_work(&drvdata->cfg_setup, msecs_to_jiffies(500));
+	else if (rgb_dirty)
+		claw_rgb_schedule(drvdata);
 }
 
 static void claw_features_supported(struct claw_drvdata *drvdata)
-- 
2.56.0


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

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  0:18 [PATCH 0/8] MSI Claw suspend, resume and ROM commit fixes Derek J. Clark
2026-12-04  0:18 ` [PATCH 1/8] HID: hid-msi: Queue the RGB work item on system_dfl_wq Derek J. Clark
2026-12-04  0:18 ` [PATCH 2/8] HID: hid-msi: Move the ROM commit to an explicit apply attribute Derek J. Clark
2026-12-04  0:35   ` sashiko-bot
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:37   ` sashiko-bot
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:38   ` sashiko-bot
2026-12-04  0:18 ` Derek J. Clark [this message]
2026-12-04  0:32   ` [PATCH 6/8] HID: hid-msi: Defer RGB updates while the MCU is suspended sashiko-bot
2026-12-04  0:18 ` [PATCH 7/8] HID: hid-msi: Switch the RGB LED to brightness_set_blocking Derek J. Clark
2026-12-04  0:39   ` sashiko-bot
2026-12-04  0:18 ` [PATCH 8/8] USB: core: quirks: Add RESET_RESUME quirk for MSI Claw 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-7-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