* [PATCH v5] HID: hid-msi: Send a uevent when the MCU changes the mode by itself
@ 2026-11-17 13:24 Ana Veto
2026-12-01 1:25 ` Derek J. Clark
0 siblings, 1 reply; 2+ messages in thread
From: Ana Veto @ 2026-11-17 13:24 UTC (permalink / raw)
To: derekjohn.clark, Jiri Kosina, Benjamin Tissoires
Cc: Ana Veto, linux-input, linux-kernel
The MCU of the MSI Claw changes the M-key function by itself once a
second while View+LB is held, and the controller can end up in desktop
mode, where the sticks drive the mouse. The MCU reports the current
mode and M-key function in a GAMEPAD_MODE_ACK report, unsolicited after
such a change and in reply to a query, and the driver stores both, but
userspace is not told. udev rules that keep the controller usable run
only on add, change and bind (for example InputPlumber's rules that
switch desktop mode back to xinput and set the M-keys to macro), so the
controller stays in desktop mode or with M1/M2 disabled until the
controller is probed again.
Emit KOBJ_CHANGE from a work item whenever a report changes the stored
mode or M-key function. Store the new values after a successful
SWITCH_MODE write as well, so that the driver's own writes raise no
event. A report that comes in during the write was sent either before
the MCU applied the write or after the MCU changed the state again, so
neither the report nor the written values can be trusted: read the state
back and emit the event, so that userspace checks the state. Read the
state from the MCU before each SWITCH_MODE write as well, as the show
functions do, so that the field that is not written is sent with the
MCU's value even when such a read-back failed. A failed read fails the
write.
Tested on an MSI Claw 8 EX AI+ (0db0:1901) with a v7.3.3-based kernel
and InputPlumber's rules. The controller was switched to desktop mode by
a SWITCH_MODE report written to hidraw, bypassing the driver. A read of
gamepad_mode then returned desktop and raised a change uevent, and the
rule wrote xinput 66 ms later. After another such switch, View+LB made
the MCU report mode 4 by itself, and the rule wrote xinput within 0.3 s.
While View+LB was held in xinput mode, the MCU reported the M-keys
disabled once a second, and the M-key rule set macro again each time. In
a build without the read before each write, a mode report with the state
from before a SWITCH_MODE write was also made to arrive during the
write, by a query written to hidraw while the driver waited to send the
write: the driver read the state back and raised an event, and the next
change of the M-key function by the MCU raised one as well. With such a
report and a failed read-back forced in a test build, an M-key function
write and then a gamepad mode write left the written M-key function in
place; without the read before each write, the second write sent the old
M-key function back.
Fixes: a9517958e828 ("HID: hid-msi: Add MSI Claw configuration driver")
Signed-off-by: Ana Veto <green.koala2979@fastmail.com>
---
Notes:
Based on hid.git for-next. This changes lines near "HID: hid-msi: Drain orphan MCU ACKs by issuing a query" (v2, posted 2026-11-14); the two patches apply in either order.
v5: read the state from the MCU before each SWITCH_MODE write. In v4, when the read-back after a raced write failed, the stored state stayed the one from before the write, and the next write of the other field sent that old value back (CodeRabbit review of the OGC copy of v4: https://github.com/OpenGamingCollective/linux/pull/137#pullrequestreview-5931619122). Tested on the Claw with the race and a failed read-back forced in a test build: with v4, an M-key function write followed by a gamepad mode write set the old M-key function again; with v5, the written one stayed. The two longer comments are one line each now. The tool trailer of v4 is gone, as the current checkpatch rejects that tag; tool use is in the last paragraph.
v4: if a GAMEPAD_MODE_ACK comes in during the SWITCH_MODE write, the driver now reads the state back and emits the uevent. In v3 such a report raised no event when it carried the state from before the write, and the stored state could stay wrong, so a later change by the MCU could go unreported as well (Sashiko review: https://lore.kernel.org/r/sashiko-outbox-215202@kernel.org). Tested on the Claw with the report forced into the write: with v3, no event in 2 of 2 runs; with v4, an event in 4 of 4.
v4: https://lore.kernel.org/r/20261115142110.65521-1-green.koala2979@fastmail.com
v3: https://lore.kernel.org/r/20261115032314.7094-1-green.koala2979@fastmail.com
v2: https://lore.kernel.org/r/20261114221440.12580-1-green.koala2979@fastmail.com
Claude Code (claude-opus-5-5, effort xhigh) wrote the code and the changelog and ran the tests on the Claw.
drivers/hid/hid-msi.c | 61 +++++++++++++++++++++++++++++++++++++++++--
1 file changed, 59 insertions(+), 2 deletions(-)
diff --git a/drivers/hid/hid-msi.c b/drivers/hid/hid-msi.c
index 3225d3b..b66d997 100644
--- a/drivers/hid/hid-msi.c
+++ b/drivers/hid/hid-msi.c
@@ -350,6 +350,8 @@ struct claw_drvdata {
/* Gamepad Variables */
enum claw_mkeys_function_index mkeys_function;
enum claw_gamepad_mode_index gamepad_mode;
+ struct work_struct mode_event; /* uevent when the MCU changes the mode */
+ unsigned int mode_reports; /* Mode reports stored, guarded by mode_lock */
u8 m1_codes[CLAW_KEYS_MAX];
u8 m2_codes[CLAW_KEYS_MAX];
u8 rumble_intensity_right;
@@ -387,19 +389,43 @@ static int get_endpoint_address(struct hid_device *hdev)
return -ENODEV;
}
+static void claw_mode_event_fn(struct work_struct *work)
+{
+ struct claw_drvdata *drvdata = container_of(work, struct claw_drvdata, mode_event);
+
+ kobject_uevent(&drvdata->hdev->dev.kobj, KOBJ_CHANGE);
+}
+
+static void claw_queue_mode_event(struct claw_drvdata *drvdata)
+{
+ guard(spinlock_irqsave)(&drvdata->registration_lock);
+ /* Pairs with smp_store_release from cfg_setup_fn and claw_remove */
+ if (smp_load_acquire(&drvdata->gp_registered))
+ schedule_work(&drvdata->mode_event);
+}
+
static int claw_gamepad_mode_event(struct claw_drvdata *drvdata,
struct claw_command_report *cmd_rep)
{
+ bool changed;
+
if (cmd_rep->data[0] >= ARRAY_SIZE(claw_gamepad_mode_text) ||
!claw_gamepad_mode_text[cmd_rep->data[0]] ||
cmd_rep->data[1] >= ARRAY_SIZE(claw_mkeys_function_text))
return -EINVAL;
scoped_guard(spinlock_irqsave, &drvdata->mode_lock) {
+ changed = drvdata->gamepad_mode != cmd_rep->data[0] ||
+ drvdata->mkeys_function != cmd_rep->data[1];
drvdata->gamepad_mode = cmd_rep->data[0];
drvdata->mkeys_function = cmd_rep->data[1];
+ drvdata->mode_reports++;
}
+ /* The MCU also changes the state by itself, e.g. on View+LB */
+ if (changed)
+ claw_queue_mode_event(drvdata);
+
return 0;
}
@@ -656,11 +682,19 @@ static int claw_hw_output_report(struct hid_device *hdev, u8 index, u8 *data,
static int claw_switch_mode(struct hid_device *hdev, enum claw_mode_field field, u8 val)
{
struct claw_drvdata *drvdata = hid_get_drvdata(hdev);
+ unsigned int reports;
+ bool raced;
u8 data[2];
+ int ret;
guard(mutex)(&drvdata->cfg_mutex);
+ ret = __claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_READ_GAMEPAD_MODE, NULL, 0, 25);
+ if (ret)
+ return ret;
+
scoped_guard(spinlock_irqsave, &drvdata->mode_lock) {
+ reports = drvdata->mode_reports;
switch (field) {
case CLAW_FIELD_GAMEPAD_MODE:
data[0] = val;
@@ -673,8 +707,27 @@ static int claw_switch_mode(struct hid_device *hdev, enum claw_mode_field field,
}
}
- return __claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_SWITCH_MODE, data,
- ARRAY_SIZE(data), 0);
+ ret = __claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_SWITCH_MODE, data,
+ ARRAY_SIZE(data), 0);
+ if (ret)
+ return ret;
+
+ /* The driver's own change is no change to report */
+ scoped_guard(spinlock_irqsave, &drvdata->mode_lock) {
+ raced = drvdata->mode_reports != reports;
+ if (!raced) {
+ drvdata->gamepad_mode = data[0];
+ drvdata->mkeys_function = data[1];
+ }
+ }
+
+ /* A report during the transfer can be older or newer than the write */
+ if (raced) {
+ __claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_READ_GAMEPAD_MODE, NULL, 0, 25);
+ claw_queue_mode_event(drvdata);
+ }
+
+ return 0;
}
static ssize_t gamepad_mode_store(struct device *dev, struct device_attribute *attr,
@@ -1871,6 +1924,7 @@ static int claw_probe(struct hid_device *hdev, u8 ep)
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);
+ INIT_WORK(&drvdata->mode_event, &claw_mode_event_fn);
/* For control interface: open the HID transport for sending commands. */
ret = hid_hw_open(hdev);
@@ -1946,6 +2000,9 @@ static void claw_remove(struct hid_device *hdev)
smp_store_release(&drvdata->rgb_registered, false);
}
+ /* gp_registered is false now, so no new mode event can be queued */
+ cancel_work_sync(&drvdata->mode_event);
+
if (gp_registered)
device_remove_group(&hdev->dev, &claw_gamepad_attr_group);
--
2.56.1
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH v5] HID: hid-msi: Send a uevent when the MCU changes the mode by itself
2026-11-17 13:24 [PATCH v5] HID: hid-msi: Send a uevent when the MCU changes the mode by itself Ana Veto
@ 2026-12-01 1:25 ` Derek J. Clark
0 siblings, 0 replies; 2+ messages in thread
From: Derek J. Clark @ 2026-12-01 1:25 UTC (permalink / raw)
To: Ana Veto; +Cc: linux-input, Jiri Kosina, Benjamin Tissoires, Kenhou Iijima
On Tue, 17 Nov 2026, Ana Veto wrote:
> Emit KOBJ_CHANGE from a work item whenever a report changes the stored
> mode or M-key function.
Ana,
v5 is close, I just have one issue. Holding View+LB is the user asking for
that mode, so we should be respecting user choice rather than spamming
userspace with uevents. The risk of the bug as it is now is that userspace
never returns to the previous state when the action is done being held.
I think it would be preferable to wait for the event storm to settle
before triggering the uevent to return to the pre-held state. Using a
mod_delayed_work ensures only the last event triggers.
I'm thinking something close to this:
<8>
---
drivers/hid/hid-msi.c | 39 +++++++++++++++++++++++++++++++--------
1 file changed, 31 insertions(+), 8 deletions(-)
--- a/drivers/hid/hid-msi.c
+++ b/drivers/hid/hid-msi.c
@@ -350,7 +350,8 @@
/* Gamepad Variables */
enum claw_mkeys_function_index mkeys_function;
enum claw_gamepad_mode_index gamepad_mode;
- struct work_struct mode_event; /* uevent when the MCU changes the mode */
+ struct delayed_work mode_event; /* uevent when the MCU changes the mode */
+ bool mode_event_owed; /* Change owed to userspace, guarded by mode_lock */
unsigned int mode_reports; /* Mode reports stored, guarded by mode_lock */
u8 m1_codes[CLAW_KEYS_MAX];
u8 m2_codes[CLAW_KEYS_MAX];
@@ -391,7 +392,15 @@
static void claw_mode_event_fn(struct work_struct *work)
{
- struct claw_drvdata *drvdata = container_of(work, struct claw_drvdata, mode_event);
+ struct delayed_work *dwork = container_of(work, struct delayed_work, work);
+ struct claw_drvdata *drvdata = container_of(dwork, struct claw_drvdata, mode_event);
+
+ /* The timer can also run out on reports that changed nothing. */
+ scoped_guard(spinlock_irqsave, &drvdata->mode_lock) {
+ if (!drvdata->mode_event_owed)
+ return;
+ drvdata->mode_event_owed = false;
+ }
kobject_uevent(&drvdata->hdev->dev.kobj, KOBJ_CHANGE);
}
@@ -401,7 +410,8 @@
guard(spinlock_irqsave)(&drvdata->registration_lock);
/* Pairs with smp_store_release from cfg_setup_fn and claw_remove */
if (smp_load_acquire(&drvdata->gp_registered))
- schedule_work(&drvdata->mode_event);
+ mod_delayed_work(system_dfl_wq, &drvdata->mode_event,
+ msecs_to_jiffies(2100));
}
static int claw_gamepad_mode_event(struct claw_drvdata *drvdata,
@@ -420,10 +430,15 @@
drvdata->gamepad_mode = cmd_rep->data[0];
drvdata->mkeys_function = cmd_rep->data[1];
drvdata->mode_reports++;
+ if (changed)
+ drvdata->mode_event_owed = true;
}
- /* The MCU also changes the state by itself, e.g. on View+LB */
- if (changed)
+ /* The MCU also changes the state by itself, e.g. on View+LB. Any report
+ * it sends on its own pushes the event back. The caller holds cmd_lock
+ * for the waiting_cmd read.
+ */
+ if (drvdata->waiting_cmd != CLAW_COMMAND_TYPE_READ_GAMEPAD_MODE)
claw_queue_mode_event(drvdata);
return 0;
@@ -723,7 +738,15 @@
/* A report during the transfer can be older or newer than the write */
if (raced) {
- __claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_READ_GAMEPAD_MODE, NULL, 0, 25);
+ ret = __claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_READ_GAMEPAD_MODE,
+ NULL, 0, 25);
+ if (ret)
+ dev_dbg(&hdev->dev,
+ "Failed to re-read the mode after a raced write: %d\n", ret);
+
+ scoped_guard(spinlock_irqsave, &drvdata->mode_lock)
+ drvdata->mode_event_owed = true;
+
claw_queue_mode_event(drvdata);
}
@@ -1924,7 +1947,7 @@
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);
- INIT_WORK(&drvdata->mode_event, &claw_mode_event_fn);
+ INIT_DELAYED_WORK(&drvdata->mode_event, &claw_mode_event_fn);
/* For control interface: open the HID transport for sending commands. */
ret = hid_hw_open(hdev);
@@ -2001,7 +2024,7 @@
}
/* gp_registered is false now, so no new mode event can be queued */
- cancel_work_sync(&drvdata->mode_event);
+ cancel_delayed_work_sync(&drvdata->mode_event);
if (gp_registered)
device_remove_group(&hdev->dev, &claw_gamepad_attr_group);
</8>
Holding View+LB here gives a report every 2000 ms with a few tens of
microseconds of jitter. The mode field does not move during it and the
M-key function alternates between macro and disabled, which is what makes
every report look like a change. 2100 ms produced no event at all while
the keys stayed down and one event 2.1 s after the last report, so
userspace sees the settled state once you let go.
One other thing, this needs Kenhou's patch ahead of it. The re-read you add
after a raced write is a READ_GAMEPAD_MODE, and SWITCH_MODE is sent with
timeout 0 and can leave a second generic 0x06 behind. Without his guard
that ACK completes the re-read early with cmd_status 0 and no payload, and
the value the uevent just told userspace to fetch is the cached one.
If you apply this on top for v6, 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] 2+ messages in thread
end of thread, other threads:[~2026-12-01 1:25 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 13:24 [PATCH v5] HID: hid-msi: Send a uevent when the MCU changes the mode by itself Ana Veto
2026-12-01 1:25 ` Derek J. Clark
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox