Skip to contentSkip to conversation

Add MSI Claw HID Configuration Driver

PATCH Superseded Derek J. Clark 3 participants 12 messages May 10 – May 12

Subsystems (2)

v1 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

cover Add MSI Claw HID Configuration Driver

This series adds and HID Configuration driver for the MSI Claw line of Handheld Gaming PC's. The MSI Claw HID interface provides multiple features, such as the ability to switch between xinput, dinput, and a desktop mode, RGB control, rumble intensity, and mapping of the rear "M" keys. There are additional gamepad modes that are not included in this driver as they appear to be used in assembly line testing or are incomplete in the firmware. During my testing I found them to be unstable.

The initial version of this driver was written by Denis Benato, which contained the initial reverse-engineering and implementation for the gamepad mode switching. This work was later expanded by Zhouwang Huang to include more gamepad modes and additional features. Finally, I refactored the entire driver, fixed multiple bugs, and refined the overall format to conform to kernel driver best practices and style guide.

Claude was used initially by Zhouwang Huang to quickly parse HID captures during the reverse-engineering of some of the features. Since Claude had already been used, as a test of its capabilities I had it implement the rumble intensity attribute after I had already rewritten most of the driver, which I then manually edited to fix some mistakes. I also used Claude to review the driver and these patches for any mistakes and bugs.

Derek J. Clark (4):
HID: hid-msi-claw: Add MSI Claw configuration driver
HID: hid-msi-claw: Add M-key mapping attributes
HID: hid-msi-claw: Add RGB control interface
HID: hid-msi-claw: Add Rumble Intensity Attributes

 MAINTAINERS                |    6 +
 drivers/hid/Kconfig        |   12 +
 drivers/hid/Makefile       |    1 +
 drivers/hid/hid-ids.h      |    6 +
 drivers/hid/hid-msi-claw.c | 1577 ++++++++++++++++++++++++++++++++++++
 5 files changed, 1602 insertions(+)
 create mode 100644 drivers/hid/hid-msi-claw.c
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/4 HID: hid-msi-claw: Add MSI Claw configuration driver

Adds configuration HID driver for the MSI Claw series of handheld PC's. In this initial patch add the initial driver outline and attributes for changing the gamepad mode, M-key behavior, and add a WO reset function.

Sending the SWITCH_MODE and RESET commands causes a USB disconnect in the device. The completion will therefore never get hit and would trigger an -EIO. To avoid showing the user an error for every write to these attrs a bypass for the completion handling is introduced when timeout == 0.

The initial version of this patch was written by Denis Benato, which contained the initial reverse-engineering and implementation for the gamepad mode switching. This work was later expanded by Zhouwang Huang to include more gamepad modes. Finally, I refactored the drivers data in/out flow and overall format to conform to kernel driver best practices and style guides. Claude was used as an initial reviewer of this patch.

---
 MAINTAINERS                |   6 +
 drivers/hid/Kconfig        |  12 +
 drivers/hid/Makefile       |   1 +
 drivers/hid/hid-ids.h      |   6 +
 drivers/hid/hid-msi-claw.c | 538 +++++++++++++++++++++++++++++++++++++
 5 files changed, 563 insertions(+)
 create mode 100644 drivers/hid/hid-msi-claw.c
MAINTAINERS +6 -0
index 6f6517bf4f97..5de5e62d9c92 100644
@@ -17965,6 +17965,12 @@ S:	Odd Fixes
 F:	Documentation/devicetree/bindings/net/ieee802154/mrf24j40.txt
 F:	drivers/net/ieee802154/mrf24j40.c
 
+MSI CLAW HID DRIVER
+M:	Derek J. Clark <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/hid/hid-msi-claw.c
+
 MSI EC DRIVER
 M:	Nikita Kravets <[email protected]>
 L:	[email protected]
drivers/hid/Kconfig +12 -0
index 10c12d8e6557..0cbe10ad6367 100644
@@ -492,6 +492,18 @@ config HID_GT683R
 	Currently the following devices are know to be supported:
 	  - MSI GT683R
 
+config HID_MSI_CLAW
+	tristate "MSI Claw Gamepad Support"
+	depends on USB_HID
+	select LEDS_CLASS
+	select LEDS_CLASS_MULTICOLOR
+	help
+	Support for the MSI Claw RGB and controller configuration
+
+	Say Y here to include configuration interface support for the MSI Claw Line
+	of Handheld Console Controllers. Say M here to compile this driver as a
+	module. The module will be called hid-msi-claw.
+
 config HID_KEYTOUCH
 	tristate "Keytouch HID devices"
 	help
drivers/hid/Makefile +1 -0
index 07dfdb6a49c5..c1dea89f1e87 100644
@@ -62,6 +62,7 @@ obj-$(CONFIG_HID_GOOGLE_HAMMER)	+= hid-google-hammer.o
 obj-$(CONFIG_HID_GOOGLE_STADIA_FF)	+= hid-google-stadiaff.o
 obj-$(CONFIG_HID_VIVALDI)	+= hid-vivaldi.o
 obj-$(CONFIG_HID_GT683R)	+= hid-gt683r.o
+obj-$(CONFIG_HID_MSI_CLAW)	+= hid-msi-claw.o
 obj-$(CONFIG_HID_GYRATION)	+= hid-gyration.o
 obj-$(CONFIG_HID_HOLTEK)	+= hid-holtek-kbd.o
 obj-$(CONFIG_HID_HOLTEK)	+= hid-holtek-mouse.o
drivers/hid/hid-ids.h +6 -0
index 933b7943bdb5..6d0d34806931 100644
@@ -1047,7 +1047,13 @@
 #define USB_DEVICE_ID_MOZA_R16_R21_2	0x0010
 
 #define USB_VENDOR_ID_MSI		0x1770
+#define USB_VENDOR_ID_MSI_2		0x0db0
 #define USB_DEVICE_ID_MSI_GT683R_LED_PANEL 0xff00
+#define USB_DEVICE_ID_MSI_CLAW_XINPUT	0x1901
+#define USB_DEVICE_ID_MSI_CLAW_DINPUT	0x1902
+#define USB_DEVICE_ID_MSI_CLAW_DESKTOP	0x1903
+#define USB_DEVICE_ID_MSI_CLAW_BIOS	0x1904
+
 
 #define USB_VENDOR_ID_NATIONAL_SEMICONDUCTOR 0x0400
 #define USB_DEVICE_ID_N_S_HARMONY	0xc359
drivers/hid/hid-msi-claw.c new file +538 -0

538 lines not on this page · raw message

Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/4 HID: hid-msi-claw: Add M-key mapping attributes

In reply to Derek J. Clark

Adds attributes that allow for remapping the M-keys with up to 5 values when in macro mode. There are 2 mappable buttons on the rear of the device, M1 on the right and M2 on the left. When mapped, the events will fire from one of three event devices: gamepad buttons will fire from the device handled by xpad, while keyboard and mouse events will fire from respectively typed evdevs provided by the input core. Names of each mapping have been kept as close to the event that will fire from the evdev as possible, with context added to the ABS_ events on the direction of the movement.

Initial reverse-engineering and implementation of this feature was done by Zhouwang Huang. I refactored the overall format to conform to kernel driver best practices and style guides. Claude was used as an initial reviewer of this patch.

Assisted-by Claude:claude-sonnet-4-6
---
 drivers/hid/hid-msi-claw.c | 390 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 389 insertions(+), 1 deletion(-)
drivers/hid/hid-msi-claw.c +389 -1
index 7a3cd940ec49..60694d075d56 100644
@@ -40,6 +40,8 @@
 #define CLAW_DINPUT_CFG_INTF_IN	0x82
 #define CLAW_XINPUT_CFG_INTF_IN	0x83
 
+#define CLAW_KEYS_MAX		5
+
 enum claw_command_index {
 	CLAW_COMMAND_TYPE_READ_PROFILE =		0x04,
 	CLAW_COMMAND_TYPE_READ_PROFILE_ACK =		0x05,
@@ -64,6 +66,17 @@ static const char * const claw_gamepad_mode_text[] = {
 	[CLAW_GAMEPAD_MODE_DESKTOP] =	"desktop",
 };
 
+enum claw_profile_ack_pending {
+	CLAW_NO_PENDING,
+	CLAW_M1_PENDING,
+	CLAW_M2_PENDING,
+};
+
+enum claw_key_index {
+	CLAW_KEY_M1,
+	CLAW_KEY_M2,
+};
+
 enum claw_mkeys_function_index {
 	CLAW_MKEY_FUNCTION_MACRO,
 	CLAW_MKEY_FUNCTION_COMBO,
@@ -76,6 +89,154 @@ static const char * const claw_mkeys_function_text[] = {
 	[CLAW_MKEY_FUNCTION_DISABLED] =	"disabled",
 };
 
+static const struct {
+	u8 code;
+	const char *name;
+} claw_button_mapping_key_map[] = {
+	/* Gamepad buttons */
+	{ 0x01, "ABS_HAT0Y_UP" },
+	{ 0x02, "ABS_HAT0Y_DOWN" },
+	{ 0x03, "ABS_HAT0X_LEFT" },
+	{ 0x04, "ABS_HAT0X_RIGHT" },
+	{ 0x05, "BTN_TL" },
+	{ 0x06, "BTN_TR" },
+	{ 0x07, "BTN_THUMBL" },
+	{ 0x08, "BTN_THUMBR" },
+	{ 0x09, "BTN_SOUTH" },
+	{ 0x0a, "BTN_EAST" },
+	{ 0x0b, "BTN_NORTH" },
+	{ 0x0c, "BTN_WEST" },
+	{ 0x0d, "BTN_MODE" },
+	{ 0x0e, "BTN_SELECT" },
+	{ 0x0f, "BTN_START" },
+	{ 0x13, "BTN_TL2"},
+	{ 0x14, "BTN_TR2"},
+	{ 0x15, "ABS_Y_UP"},
+	{ 0x16, "ABS_Y_DOWN"},
+	{ 0x17, "ABS_X_LEFT"},
+	{ 0x18, "ABS_X_LEFT_RIGHT"},
+	{ 0x19, "ABS_RY_UP"},
+	{ 0x1a, "ABS_RY_DOWN"},
+	{ 0x1b, "ABS_RX_LEFT"},
+	{ 0x1c, "ABS_RX_RIGHT"},
+	/* Keyboard keys */
+	{ 0x32, "KEY_ESC" },
+	{ 0x33, "KEY_F1" },
+	{ 0x34, "KEY_F2" },
+	{ 0x35, "KEY_F3" },
+	{ 0x36, "KEY_F4" },
+	{ 0x37, "KEY_F5" },
+	{ 0x38, "KEY_F6" },
+	{ 0x39, "KEY_F7" },
+	{ 0x3a, "KEY_F8" },
+	{ 0x3b, "KEY_F9" },
+	{ 0x3c, "KEY_F10" },
+	{ 0x3d, "KEY_F11" },
+	{ 0x3e, "KEY_F12" },
+	{ 0x3f, "KEY_GRAVE" },
+	{ 0x40, "KEY_1" },
+	{ 0x41, "KEY_2" },
+	{ 0x42, "KEY_3" },
+	{ 0x43, "KEY_4" },
+	{ 0x44, "KEY_5" },
+	{ 0x45, "KEY_6" },
+	{ 0x46, "KEY_7" },
+	{ 0x47, "KEY_8" },
+	{ 0x48, "KEY_9" },
+	{ 0x49, "KEY_0" },
+	{ 0x4a, "KEY_MINUS" },
+	{ 0x4b, "KEY_EQUAL" },
+	{ 0x4c, "KEY_BACKSPACE" },
+	{ 0x4d, "KEY_TAB" },
+	{ 0x4e, "KEY_Q" },
+	{ 0x4f, "KEY_W" },
+	{ 0x50, "KEY_E" },
+	{ 0x51, "KEY_R" },
+	{ 0x52, "KEY_T" },
+	{ 0x53, "KEY_Y" },
+	{ 0x54, "KEY_U" },
+	{ 0x55, "KEY_I" },
+	{ 0x56, "KEY_O" },
+	{ 0x57, "KEY_P" },
+	{ 0x58, "KEY_LEFTBRACE" },
+	{ 0x59, "KEY_RIGHTBRACE" },
+	{ 0x5a, "KEY_BACKSLASH" },
+	{ 0x5b, "KEY_CAPSLOCK" },
+	{ 0x5c, "KEY_A" },
+	{ 0x5d, "KEY_S" },
+	{ 0x5e, "KEY_D" },
+	{ 0x5f, "KEY_F" },
+	{ 0x60, "KEY_G" },
+	{ 0x61, "KEY_H" },
+	{ 0x62, "KEY_J" },
+	{ 0x63, "KEY_K" },
+	{ 0x64, "KEY_L" },
+	{ 0x65, "KEY_SEMICOLON" },
+	{ 0x66, "KEY_APOSTROPHE" },
+	{ 0x67, "KEY_ENTER" },
+	{ 0x68, "KEY_LEFTSHIFT" },
+	{ 0x69, "KEY_Z" },
+	{ 0x6a, "KEY_X" },
+	{ 0x6b, "KEY_C" },
+	{ 0x6c, "KEY_V" },
+	{ 0x6d, "KEY_B" },
+	{ 0x6e, "KEY_N" },
+	{ 0x6f, "KEY_M" },
+	{ 0x70, "KEY_COMMA" },
+	{ 0x71, "KEY_DOT" },
+	{ 0x72, "KEY_SLASH" },
+	{ 0x73, "KEY_RIGHTSHIFT" },
+	{ 0x74, "KEY_LEFTCTRL" },
+	{ 0x75, "KEY_LEFTMETA" },
+	{ 0x76, "KEY_LEFTALT" },
+	{ 0x77, "KEY_SPACE" },
+	{ 0x78, "KEY_RIGHTALT" },
+	{ 0x79, "KEY_RIGHTCTRL" },
+	{ 0x7a, "KEY_INSERT" },
+	{ 0x7b, "KEY_HOME" },
+	{ 0x7c, "KEY_PAGEUP" },
+	{ 0x7d, "KEY_DELETE" },
+	{ 0x7e, "KEY_END" },
+	{ 0x7f, "KEY_PAGEDOWN" },
+	{ 0x8a, "KEY_KPENTER" },
+	{ 0x8b, "KEY_KP0" },
+	{ 0x8c, "KEY_KP1" },
+	{ 0x8d, "KEY_KP2" },
+	{ 0x8e, "KEY_KP3" },
+	{ 0x8f, "KEY_KP4" },
+	{ 0x90, "KEY_KP5" },
+	{ 0x91, "KEY_KP6" },
+	{ 0x92, "KEY_KP7" },
+	{ 0x93, "KEY_KP8" },
+	{ 0x94, "KEY_KP9" },
+	{ 0x95, "MD_PLAY" },
+	{ 0x96, "MD_STOP" },
+	{ 0x97, "MD_NEXT" },
+	{ 0x98, "MD_PREV" },
+	{ 0x99, "MD_VOL_UP" },
+	{ 0x9a, "MD_VOL_DOWN" },
+	{ 0x9b, "MD_VOL_MUTE" },
+	{ 0x9c, "KEY_F23" },
+	/* Mouse events */
+	{ 0xc8, "BTN_LEFT" },
+	{ 0xc9, "BTN_MIDDLE" },
+	{ 0xca, "BTN_RIGHT" },
+	{ 0xcb, "BTN_SIDE" },
+	{ 0xcc, "BTN_EXTRA" },
+	{ 0xcd, "REL_WHEEL_UP" },
+	{ 0xce, "REL_WHEEL_DOWN" },
+};
+
+static const u16 button_mapping_addr_old[] = {
+	0x007a,  /* M1 */
+	0x011f,  /* M2 */
+};
+
+static const u16 button_mapping_addr_new[] = {
+	0x00bb,  /* M1 */
+	0x0164,  /* M2 */
+};
+
 struct claw_command_report {
 	u8 report_id;
 	u8 padding[2];
@@ -86,16 +247,22 @@ struct claw_command_report {
 
 struct claw_drvdata {
 	/* MCU General Variables */
+	enum claw_profile_ack_pending profile_pending;
 	struct completion send_cmd_complete;
 	struct delayed_work cfg_resume;
 	struct delayed_work cfg_setup;
 	struct hid_device *hdev;
 	struct mutex cfg_mutex; /* mutex for synchronous data */
+	u16 bcd_device;
 	int endpoint;
 
 	/* Gamepad Variables */
 	enum claw_mkeys_function_index mkeys_function;
 	enum claw_gamepad_mode_index gamepad_mode;
+	u8 m1_codes[CLAW_KEYS_MAX];
+	u8 m2_codes[CLAW_KEYS_MAX];
+	const u16 *bmap_addr;
+	bool bmap_support;
 };
 
 static int get_endpoint_address(struct hid_device *hdev)
@@ -123,6 +290,31 @@ static int claw_gamepad_mode_event(struct claw_drvdata *drvdata,
 	return 0;
 }
 
+static int claw_profile_event(struct claw_drvdata *drvdata, struct claw_command_report *cmd_rep)
+{
+	u8 *codes;
+	int i;
+
+	switch (drvdata->profile_pending) {
+	case CLAW_M1_PENDING:
+	case CLAW_M2_PENDING:
+		codes = (drvdata->profile_pending == CLAW_M1_PENDING) ?
+			drvdata->m1_codes : drvdata->m2_codes;
+		/* Extract key codes; replace disabled (0xff) with 0x00, which is (null) in _show */
+		for (i = 0; i < CLAW_KEYS_MAX; i++)
+			codes[i] = (cmd_rep->data[6 + i] != 0xff) ? cmd_rep->data[6 + i] : 0x00;
+		break;
+	default:
+		dev_warn(&drvdata->hdev->dev,
+			 "Got profile event without changes pending from command:%x\n",
+			 cmd_rep->cmd);
+		return -EINVAL;
+	}
+	drvdata->profile_pending = CLAW_NO_PENDING;
+
+	return 0;
+}
+
 static int claw_raw_event(struct hid_device *hdev, struct hid_report *report,
 			  u8 *data, int size)
 {
@@ -149,6 +341,9 @@ static int claw_raw_event(struct hid_device *hdev, struct hid_report *report,
 	case CLAW_COMMAND_TYPE_GAMEPAD_MODE_ACK:
 		ret = claw_gamepad_mode_event(drvdata, cmd_rep);
 		break;
+	case CLAW_COMMAND_TYPE_READ_PROFILE_ACK:
+		ret = claw_profile_event(drvdata, cmd_rep);
+		break;
 	case CLAW_COMMAND_TYPE_ACK:
 		break;
 	default:
@@ -356,6 +551,157 @@ static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
 }
 static DEVICE_ATTR_WO(reset);
 
+static int button_mapping_name_to_code(const char *name)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(claw_button_mapping_key_map); i++) {
+		if (!strcmp(name, claw_button_mapping_key_map[i].name))
+			return claw_button_mapping_key_map[i].code;
+	}
+
+	return -EINVAL;
+}
+
+static const char *button_mapping_code_to_name(u8 code)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(claw_button_mapping_key_map); i++) {
+		if (claw_button_mapping_key_map[i].code == code)
+			return claw_button_mapping_key_map[i].name;
+	}
+
+	return NULL;
+}
+
+static int claw_buttons_store(struct device *dev, const char *buf, u8 mkey_idx)
+{
+	struct hid_device *hdev = to_hid_device(dev);
+	struct claw_drvdata *drvdata = hid_get_drvdata(hdev);
+	u8 data[] = { 0x01, (drvdata->bmap_addr[mkey_idx] >> 8) & 0xff,
+		      drvdata->bmap_addr[mkey_idx] & 0xff, 0x07,
+		      0x04, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff };
+	size_t len = ARRAY_SIZE(data);
+	int ret, key_count, i;
+	char **raw_keys;
+
+	raw_keys = argv_split(GFP_KERNEL, buf, &key_count);
+	if (!raw_keys)
+		return -ENOMEM;
+
+	if (key_count > CLAW_KEYS_MAX) {
+		ret = -EINVAL;
+		goto err_free;
+	}
+
+	if (key_count == 0)
+		goto set_buttons;
+
+	for (i = 0; i < key_count; i++) {
+		ret = button_mapping_name_to_code(raw_keys[i]);
+		if (ret < 0)
+			goto err_free;
+
+		data[6 + i] = ret;
+	}
+
+set_buttons:
+	ret = mcu_property_out(hdev, CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA, data, len, 8);
+	if (ret < 0)
+		goto err_free;
+
+	ret = mcu_property_out(hdev, CLAW_COMMAND_TYPE_SYNC_TO_ROM, NULL, 0, 0);
+
+err_free:
+	argv_free(raw_keys);
+	return ret;
+}
+
+static int claw_buttons_show(struct device *dev, char *buf, enum claw_key_index m_key)
+{
+	struct hid_device *hdev = to_hid_device(dev);
+	struct claw_drvdata *drvdata = hid_get_drvdata(hdev);
+	u8 data[] = { 0x01, (drvdata->bmap_addr[m_key] >> 8) & 0xff,
+		      drvdata->bmap_addr[m_key] & 0xff, 0x07 };
+	size_t len = ARRAY_SIZE(data);
+	int i, ret, count = 0;
+	const char *name;
+	u8 *codes;
+
+	codes = (m_key == CLAW_KEY_M1) ? drvdata->m1_codes : drvdata->m2_codes;
+	drvdata->profile_pending = (m_key == CLAW_KEY_M1) ? CLAW_M1_PENDING : CLAW_M2_PENDING;
+
+	ret = mcu_property_out(hdev, CLAW_COMMAND_TYPE_READ_PROFILE, data, len, 8);
+	if (ret)
+		return ret;
+
+	for (i = 0; i < CLAW_KEYS_MAX; i++) {
+		name = button_mapping_code_to_name(codes[i]);
+		if (name)
+			count += sysfs_emit_at(buf, count, "%s ", name);
+	}
+
+	if (!count)
+		return sysfs_emit(buf, "(not set)\n");
+
+	buf[count - 1] = '\n';
+
+	return count;
+}
+
+static ssize_t button_m1_store(struct device *dev, struct device_attribute *attr,
+			       const char *buf, size_t count)
+{
+	int ret;
+
+	ret = claw_buttons_store(dev, buf, CLAW_KEY_M1);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+static ssize_t button_m1_show(struct device *dev, struct device_attribute *attr,
+			      char *buf)
+{
+	return claw_buttons_show(dev, buf, CLAW_KEY_M1);
+}
+static DEVICE_ATTR_RW(button_m1);
+
+static ssize_t button_m2_store(struct device *dev, struct device_attribute *attr,
+			       const char *buf, size_t count)
+{
+	int ret;
+
+	ret = claw_buttons_store(dev, buf, CLAW_KEY_M2);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+static ssize_t button_m2_show(struct device *dev, struct device_attribute *attr,
+			      char *buf)
+{
+	return claw_buttons_show(dev, buf, CLAW_KEY_M2);
+}
+static DEVICE_ATTR_RW(button_m2);
+
+static ssize_t button_mapping_options_show(struct device *dev,
+					   struct device_attribute *attr, char *buf)
+{
+	int i, count = 0;
+
+	for (i = 0; i < ARRAY_SIZE(claw_button_mapping_key_map); i++)
+		count += sysfs_emit_at(buf, count, "%s ", claw_button_mapping_key_map[i].name);
+
+	buf[count - 1] = '\n';
+
+	return count;
+}
+static DEVICE_ATTR_RO(button_mapping_options);
+
 static umode_t claw_gamepad_attr_is_visible(struct kobject *kobj, struct attribute *attr,
 					    int n)
 {
@@ -368,10 +714,22 @@ static umode_t claw_gamepad_attr_is_visible(struct kobject *kobj, struct attribu
 		return 0;
 	}
 
-	return attr->mode;
+	/* Always show attrs available on all firmware */
+	if (attr == &dev_attr_gamepad_mode.attr ||
+	    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)
+		return attr->mode;
+
+	/* Hide button mapping attrs if it isn't supported */
+	return drvdata->bmap_support ? attr->mode : 0;
 }
 
 static struct attribute *claw_gamepad_attrs[] = {
+	&dev_attr_button_m1.attr,
+	&dev_attr_button_m2.attr,
+	&dev_attr_button_mapping_options.attr,
 	&dev_attr_gamepad_mode.attr,
 	&dev_attr_gamepad_mode_index.attr,
 	&dev_attr_mkeys_function.attr,
@@ -424,6 +782,29 @@ static void cfg_resume_fn(struct work_struct *work)
 		dev_err(&drvdata->hdev->dev, "Failed to set gamepad mode settings: %d\n", ret);
 }
 
+static void claw_features_supported(struct claw_drvdata *drvdata)
+{
+	u8 major = (drvdata->bcd_device >> 8) & 0xff;
+	u8 minor = drvdata->bcd_device & 0xff;
+
+	if (major == 0x01) {
+		drvdata->bmap_support = true;
+		if (minor >= 0x66)
+			drvdata->bmap_addr = button_mapping_addr_new;
+		else
+			drvdata->bmap_addr = button_mapping_addr_old;
+		return;
+	}
+
+	if ((major == 0x02 && minor >= 0x17) || major >= 0x03) {
+		drvdata->bmap_support = true;
+		drvdata->bmap_addr = button_mapping_addr_new;
+		return;
+	}
+
+	drvdata->bmap_support = false;
+}
+
 static int claw_probe(struct hid_device *hdev, const struct hid_device_id *id)
 {
 	struct claw_drvdata *drvdata;
@@ -470,6 +851,13 @@ static int claw_probe(struct hid_device *hdev, const struct hid_device_id *id)
 	if (ret)
 		goto err_stop_hw;
 
+	/* Determine feature level from firmware version */
+	drvdata->bcd_device = le16_to_cpu(udev->descriptor.bcdDevice);
+	claw_features_supported(drvdata);
+
+	if (!drvdata->bmap_support)
+		dev_warn(&hdev->dev, "M-Key mapping is not supported. Update firmware to enable.\n");
+
 	init_completion(&drvdata->send_cmd_complete);
 
 	INIT_DELAYED_WORK(&drvdata->cfg_resume, &cfg_resume_fn);
Signature · 1 line
2.53.0
#3 lore raw

Re: [PATCH 2/4] HID: hid-msi-claw: Add M-key mapping attributes

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Data race on `drvdata->profile_pending` and lack of transaction atomicity during concurrent sysfs…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Data race on drvdata->profile_pending and lack of transaction atomicity during concurrent sysfs accesses. --

commit b9bc6d29326e3dd3ed065aca7059668a1b674bc8
Author: Derek J. Clark <[email protected]>

HID: hid-msi-claw: Add M-key mapping attributes

Adds sysfs attributes to remap the M1 and M2 rear buttons to up to 5 events (gamepad, keyboard, or mouse) when in macro mode.

Quoted 4 lines
drivers/hid/hid-msi-claw.c +0 -0
index 7a3cd940ec49e..60694d075d56f 100644

[ ... ]

Quoted 2 lines
drivers/hid/hid-msi-claw.c +2 -0
+static int claw_buttons_store(struct device *dev, const char *buf, u8 mkey_idx)
+{

[ ... ]

drivers/hid/hid-msi-claw.c static ssize_t reset_store(struct device *dev, struct device_attribute *attr, · line 614

Quoted 6 lines
drivers/hid/hid-msi-claw.c +6 -0
+set_buttons:
+	ret = mcu_property_out(hdev, CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA, data, len, 8);
+	if (ret < 0)
+		goto err_free;
+
+	ret = mcu_property_out(hdev, CLAW_COMMAND_TYPE_SYNC_TO_ROM, NULL, 0, 0);

Is it possible for concurrent writers to interleave these commands?

Looking at mcu_property_out(), it takes the cfg_mutex internally, but there is no lock held across both calls here. If a second writer executes its WRITE_PROFILE_DATA before the first writer executes SYNC_TO_ROM, could the MCU be left in an inconsistent state?

[ ... ]

Quoted 2 lines
drivers/hid/hid-msi-claw.c +2 -0
+static int claw_buttons_show(struct device *dev, char *buf, enum claw_key_index m_key)
+{

[ ... ]

drivers/hid/hid-msi-claw.c static ssize_t reset_store(struct device *dev, struct device_attribute *attr, · line 635

Quoted 4 lines
drivers/hid/hid-msi-claw.c +4 -0
+	codes = (m_key == CLAW_KEY_M1) ? drvdata->m1_codes : drvdata->m2_codes;
+	drvdata->profile_pending = (m_key == CLAW_KEY_M1) ? CLAW_M1_PENDING : CLAW_M2_PENDING;
+
+	ret = mcu_property_out(hdev, CLAW_COMMAND_TYPE_READ_PROFILE, data, len, 8);

Can concurrent reads of button_m1 and button_m2 race and corrupt the profile_pending state?

If Thread A and Thread B access the sysfs attributes simultaneously, profile_pending is updated locklessly here before mcu_property_out() takes the cfg_mutex.

Could Thread B overwrite profile_pending to CLAW_M2_PENDING before Thread A's command executes? If so, when the hardware replies to Thread A's M1 command, claw_profile_event() might route Thread A's data into Thread B's m2_codes buffer and clear the state to CLAW_NO_PENDING.

This would cause Thread B's subsequent reply to be dropped and trigger the "Got profile event without changes pending" warning.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#4 lore raw

Re: [PATCH 2/4] HID: hid-msi-claw: Add M-key mapping attributes

Quoted 29 lines · On May 11, 2026 1:51:07 PM PDT, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Data race on drvdata->profile_pending and lack of transaction atomicity during concurrent sysfs accesses. --

commit b9bc6d29326e3dd3ed065aca7059668a1b674bc8
Author: Derek J. Clark <[email protected]>

HID: hid-msi-claw: Add M-key mapping attributes

Adds sysfs attributes to remap the M1 and M2 rear buttons to up to 5 events (gamepad, keyboard, or mouse) when in macro mode.

Quoted 4 lines
drivers/hid/hid-msi-claw.c +0 -0
index 7a3cd940ec49e..60694d075d56f 100644

[ ... ]

Quoted 2 lines
drivers/hid/hid-msi-claw.c +2 -0
+static int claw_buttons_store(struct device *dev, const char *buf, u8 mkey_idx)
+{

[ ... ]

drivers/hid/hid-msi-claw.c static ssize_t reset_store(struct device *dev, struct device_attribute *attr, · line 614

Quoted 6 lines
drivers/hid/hid-msi-claw.c +6 -0
+set_buttons:
+	ret = mcu_property_out(hdev, CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA, data, len, 8);
+	if (ret < 0)
+		goto err_free;
+
+	ret = mcu_property_out(hdev, CLAW_COMMAND_TYPE_SYNC_TO_ROM, NULL, 0, 0);

Is it possible for concurrent writers to interleave these commands?

Quoted 5 lines

Looking at mcu_property_out(), it takes the cfg_mutex internally, but there is no lock held across both calls here. If a second writer executes its WRITE_PROFILE_DATA before the first writer executes SYNC_TO_ROM, could the MCU be left in an inconsistent state?

No, the sync to rom simply writes the state to permanent memory so it remains in the same configuration after a power cycle. The first call writes it to temporary storage.

Quoted 16 lines

[ ... ]

Quoted 2 lines
drivers/hid/hid-msi-claw.c +2 -0
+static int claw_buttons_show(struct device *dev, char *buf, enum claw_key_index m_key)
+{

[ ... ]

drivers/hid/hid-msi-claw.c static ssize_t reset_store(struct device *dev, struct device_attribute *attr, · line 635

Quoted 4 lines
drivers/hid/hid-msi-claw.c +4 -0
+	codes = (m_key == CLAW_KEY_M1) ? drvdata->m1_codes : drvdata->m2_codes;
+	drvdata->profile_pending = (m_key == CLAW_KEY_M1) ? CLAW_M1_PENDING : CLAW_M2_PENDING;
+
+	ret = mcu_property_out(hdev, CLAW_COMMAND_TYPE_READ_PROFILE, data, len, 8);

Can concurrent reads of button_m1 and button_m2 race and corrupt the profile_pending state?

If Thread A and Thread B access the sysfs attributes simultaneously, profile_pending is updated locklessly here before mcu_property_out() takes the cfg_mutex.

Quoted 8 lines

Could Thread B overwrite profile_pending to CLAW_M2_PENDING before Thread A's command executes? If so, when the hardware replies to Thread A's M1 command, claw_profile_event() might route Thread A's data into Thread B's m2_codes buffer and clear the state to CLAW_NO_PENDING.

This would cause Thread B's subsequent reply to be dropped and trigger the "Got profile event without changes pending" warning.

Possible. I'll look at improving this pattern by managing the pending flag with a mutex.
- Derek

#5 lore raw

PATCH 3/4 HID: hid-msi-claw: Add RGB control interface

In reply to Derek J. Clark

Adds RGB control interface for MSI Claw devices. The MSI Claw uses a fairly unique RGB interface. It has 9 total zones (4 per joystick ring and 1 for the ABXY buttons), and supports up to 8 sequential frames of RGB zone data. Each frame is written to a specific area of MCU memory by the profile command, the value of which changes based on the firmware of the device. Unlike other devices (such as the Legion Go or the OneXPlayer devices), there are no hard coded effects built into the MCU. Instead, the basic effects are provided as a series of frame data. I have mirrored the effects available in Windows in this driver, while keeping the effect names consistent with the Lenovo drivers for the effects that are similar.

Initial reverse-engineering and implementation of this feature was done by Zhouwang Huang. I refactored the overall format to conform to kernel driver best practices and style guides. Claude was used as an initial reviewer of this patch.

Assisted-by Claude:claude-sonnet-4-6
---
 drivers/hid/hid-msi-claw.c | 533 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 530 insertions(+), 3 deletions(-)
drivers/hid/hid-msi-claw.c +530 -3
index 60694d075d56..f4fe74a784c2 100644
@@ -21,6 +21,7 @@
 #include <linux/device.h>
 #include <linux/hid.h>
 #include <linux/kobject.h>
+#include <linux/led-class-multicolor.h>
 #include <linux/leds.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
@@ -42,6 +43,10 @@
 
 #define CLAW_KEYS_MAX		5
 
+#define CLAW_RGB_ZONES		9
+#define CLAW_RGB_MAX_FRAMES	8
+#define CLAW_RGB_FRAME_OFFSET	0x24
+
 enum claw_command_index {
 	CLAW_COMMAND_TYPE_READ_PROFILE =		0x04,
 	CLAW_COMMAND_TYPE_READ_PROFILE_ACK =		0x05,
@@ -70,6 +75,7 @@ enum claw_profile_ack_pending {
 	CLAW_NO_PENDING,
 	CLAW_M1_PENDING,
 	CLAW_M2_PENDING,
+	CLAW_RGB_PENDING,
 };
 
 enum claw_key_index {
@@ -227,6 +233,22 @@ static const struct {
 	{ 0xce, "REL_WHEEL_DOWN" },
 };
 
+enum claw_rgb_effect_index {
+	CLAW_RGB_EFFECT_MONOCOLOR,
+	CLAW_RGB_EFFECT_BREATHE,
+	CLAW_RGB_EFFECT_CHROMA,
+	CLAW_RGB_EFFECT_RAINBOW,
+	CLAW_RGB_EFFECT_FROSTFIRE,
+};
+
+static const char * const claw_rgb_effect_text[] = {
+	[CLAW_RGB_EFFECT_MONOCOLOR] =	"monocolor",
+	[CLAW_RGB_EFFECT_BREATHE] =	"breathe",
+	[CLAW_RGB_EFFECT_CHROMA] =	"chroma",
+	[CLAW_RGB_EFFECT_RAINBOW] =	"rainbow",
+	[CLAW_RGB_EFFECT_FROSTFIRE] =	"frostfire",
+};
+
 static const u16 button_mapping_addr_old[] = {
 	0x007a,  /* M1 */
 	0x011f,  /* M2 */
@@ -237,6 +259,9 @@ static const u16 button_mapping_addr_new[] = {
 	0x0164,  /* M2 */
 };
 
+static const u16 rgb_addr_old = 0x01fa;
+static const u16 rgb_addr_new = 0x024a;
+
 struct claw_command_report {
 	u8 report_id;
 	u8 padding[2];
@@ -245,6 +270,28 @@ struct claw_command_report {
 	u8 data[59];
 } __packed;
 
+struct rgb_zone {
+	u8 red;
+	u8 green;
+	u8 blue;
+};
+
+struct rgb_frame {
+	struct rgb_zone zone[9];
+};
+
+struct rgb_report {
+	u8 profile;
+	__be16 read_addr;
+	u8 frame_bytes;
+	u8 padding;
+	u8 frame_count;
+	u8 state;
+	u8 speed;
+	u8 brightness;
+	struct rgb_frame zone_data;
+} __packed;
+
 struct claw_drvdata {
 	/* MCU General Variables */
 	enum claw_profile_ack_pending profile_pending;
@@ -263,6 +310,16 @@ struct claw_drvdata {
 	u8 m2_codes[CLAW_KEYS_MAX];
 	const u16 *bmap_addr;
 	bool bmap_support;
+
+	/* RGB Variables */
+	struct rgb_frame rgb_frames[CLAW_RGB_MAX_FRAMES];
+	enum claw_rgb_effect_index rgb_effect;
+	struct led_classdev_mc led_mc;
+	struct delayed_work rgb_queue;
+	u8 rgb_frame_count;
+	bool rgb_enabled;
+	u8 rgb_speed;
+	u16 rgb_addr;
 };
 
 static int get_endpoint_address(struct hid_device *hdev)
@@ -292,7 +349,10 @@ static int claw_gamepad_mode_event(struct claw_drvdata *drvdata,
 
 static int claw_profile_event(struct claw_drvdata *drvdata, struct claw_command_report *cmd_rep)
 {
-	u8 *codes;
+	struct rgb_report *frame;
+	u16 rgb_addr, read_addr;
+	u8 *codes, f_idx;
+	u16 frame_calc;
 	int i;
 
 	switch (drvdata->profile_pending) {
@@ -304,6 +364,39 @@ static int claw_profile_event(struct claw_drvdata *drvdata, struct claw_command_
 		for (i = 0; i < CLAW_KEYS_MAX; i++)
 			codes[i] = (cmd_rep->data[6 + i] != 0xff) ? cmd_rep->data[6 + i] : 0x00;
 		break;
+	case CLAW_RGB_PENDING:
+		frame = (struct rgb_report *)cmd_rep->data;
+		rgb_addr = drvdata->rgb_addr;
+		read_addr = be16_to_cpu(frame->read_addr);
+		frame_calc = (read_addr - rgb_addr) / CLAW_RGB_FRAME_OFFSET;
+		if (frame_calc > U8_MAX) {
+			dev_err(drvdata->led_mc.led_cdev.dev, "Got unsupported frame index: %x\n",
+				frame_calc);
+			return -EINVAL;
+		}
+		f_idx = frame_calc;
+
+		if (f_idx >= CLAW_RGB_MAX_FRAMES) {
+			dev_err(drvdata->led_mc.led_cdev.dev, "Got illegal frame index: %x\n",
+				f_idx);
+			return -EINVAL;
+		}
+
+		/* Always treat the first frame as the truth for these constants */
+		if (f_idx == 0) {
+			drvdata->rgb_frame_count = frame->frame_count;
+			/* Invert device speed (20-0) to sysfs speed (0-20) */
+			drvdata->rgb_speed = frame->speed;
+			drvdata->led_mc.led_cdev.brightness = frame->brightness;
+			drvdata->led_mc.subled_info[0].intensity = frame->zone_data.zone[0].red;
+			drvdata->led_mc.subled_info[1].intensity = frame->zone_data.zone[0].green;
+			drvdata->led_mc.subled_info[2].intensity = frame->zone_data.zone[0].blue;
+		}
+
+		memcpy(&drvdata->rgb_frames[f_idx], &frame->zone_data,
+		       sizeof(struct rgb_frame));
+
+		break;
 	default:
 		dev_warn(&drvdata->hdev->dev,
 			 "Got profile event without changes pending from command:%x\n",
@@ -743,6 +836,389 @@ static const struct attribute_group claw_gamepad_attr_group = {
 	.is_visible = claw_gamepad_attr_is_visible,
 };
 
+/* Read RGB config from device */
+static int claw_read_rgb_config(struct hid_device *hdev)
+{
+	u8 data[4] = { 0x01, 0x00, 0x00, CLAW_RGB_FRAME_OFFSET };
+	struct claw_drvdata *drvdata = hid_get_drvdata(hdev);
+	u16 read_addr = drvdata->rgb_addr;
+	size_t len = ARRAY_SIZE(data);
+	int ret, i;
+
+	if (!drvdata->rgb_addr)
+		return -ENODEV;
+
+	/* Loop through all 8 pages of RGB data */
+	for (i = 0; i < 8; i++) {
+		drvdata->profile_pending = CLAW_RGB_PENDING;
+		data[1] = (read_addr >> 8) & 0xff;
+		data[2] = read_addr & 0x00ff;
+		ret = mcu_property_out(hdev, CLAW_COMMAND_TYPE_READ_PROFILE, data, len, 8);
+		if (ret)
+			return ret;
+
+		read_addr += CLAW_RGB_FRAME_OFFSET;
+	}
+
+	return 0;
+}
+
+/* Send RGB configuration to device */
+static int claw_write_rgb_state(struct claw_drvdata *drvdata)
+{
+	struct rgb_report report = { 0x01, 0x0000, CLAW_RGB_FRAME_OFFSET, 0x00,
+			drvdata->rgb_frame_count, 0x09, drvdata->rgb_speed,
+			drvdata->led_mc.led_cdev.brightness };
+	u16 write_addr = drvdata->rgb_addr;
+	size_t len = sizeof(report);
+	int f, ret;
+
+	if (!drvdata->rgb_addr)
+		return -ENODEV;
+
+	/* Loop through (up to) 8 pages of RGB data */
+	for (f = 0; f < drvdata->rgb_frame_count; f++) {
+		report.zone_data = drvdata->rgb_frames[f];
+
+		/* Set the MCU address to write the frame data to */
+		report.read_addr = cpu_to_be16(write_addr);
+
+		/* Serialize the rgb_report and write it to MCU */
+		ret = mcu_property_out(drvdata->hdev, CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA,
+				       (u8 *)&report, len, 8);
+		if (ret)
+			return ret;
+
+		/* Increment the write addr by the offset for the next frame */
+		write_addr += CLAW_RGB_FRAME_OFFSET;
+	}
+
+	return 0;
+}
+
+/* Fill all zones with the same color */
+static void claw_frame_fill_solid(struct rgb_frame *frame, struct rgb_zone zone)
+{
+	int z;
+
+	for (z = 0; z < CLAW_RGB_ZONES; z++)
+		frame->zone[z] = zone;
+}
+
+/* Apply solid effect (1 frame, all zones same color) */
+static int claw_apply_monocolor(struct claw_drvdata *drvdata)
+{
+	struct mc_subled *subleds = drvdata->led_mc.subled_info;
+	struct rgb_zone zone = { subleds[0].intensity, subleds[1].intensity,
+				 subleds[2].intensity };
+
+	drvdata->rgb_frame_count = 1;
+	claw_frame_fill_solid(&drvdata->rgb_frames[0], zone);
+
+	return claw_write_rgb_state(drvdata);
+}
+
+/* Apply breathe effect (2 frames: color -> off) */
+static int claw_apply_breathe(struct claw_drvdata *drvdata)
+{
+	struct mc_subled *subleds = drvdata->led_mc.subled_info;
+	struct rgb_zone zone = { subleds[0].intensity, subleds[1].intensity,
+				 subleds[2].intensity };
+	static const struct rgb_zone off = { 0, 0, 0 };
+
+	drvdata->rgb_frame_count = 2;
+	claw_frame_fill_solid(&drvdata->rgb_frames[0], zone);
+	claw_frame_fill_solid(&drvdata->rgb_frames[1], off);
+
+	return claw_write_rgb_state(drvdata);
+}
+
+/* Apply chroma effect (6 frames: rainbow cycle, all zones sync) */
+static int claw_apply_chroma(struct claw_drvdata *drvdata)
+{
+	static const struct rgb_zone colors[] = {
+		{255,   0,   0},  /* red     */
+		{255, 255,   0},  /* yellow  */
+		{  0, 255,   0},  /* green   */
+		{  0, 255, 255},  /* cyan    */
+		{  0,   0, 255},  /* blue    */
+		{255,   0, 255},  /* magenta */
+	};
+	u8 frame_count = ARRAY_SIZE(colors);
+	int frame;
+
+	drvdata->rgb_frame_count = frame_count;
+
+	for (frame = 0; frame < frame_count; frame++)
+		claw_frame_fill_solid(&drvdata->rgb_frames[frame], colors[frame]);
+
+	return claw_write_rgb_state(drvdata);
+}
+
+/* Apply rainbow effect (4 frames: rotating colors around joysticks) */
+static int claw_apply_rainbow(struct claw_drvdata *drvdata)
+{
+	static const struct rgb_zone colors[] = {
+		{255,   0,   0},  /* red   */
+		{  0, 255,   0},  /* green */
+		{  0, 255, 255},  /* cyan  */
+		{  0,   0, 255},  /* blue  */
+	};
+	u8 frame_count = ARRAY_SIZE(colors);
+	int frame, zone;
+
+	drvdata->rgb_frame_count = frame_count;
+
+	for (frame = 0; frame < frame_count; frame++) {
+		for (zone = 0; zone < 4; zone++) {
+			drvdata->rgb_frames[frame].zone[zone]     = colors[(zone + frame) % 4];
+			drvdata->rgb_frames[frame].zone[zone + 4] = colors[(zone + frame) % 4];
+		}
+		drvdata->rgb_frames[frame].zone[8] = colors[frame];
+	}
+
+	return claw_write_rgb_state(drvdata);
+}
+
+/*
+ * Apply frostfire effect (4 frames: fire vs ice rotating)
+ * Right joystick: fire red -> dark -> ice blue -> dark (clockwise)
+ * Left joystick: ice blue -> dark -> fire red -> dark (counter-clockwise)
+ * ABXY: fire red -> dark -> ice blue -> dark
+ */
+static int claw_apply_frostfire(struct claw_drvdata *drvdata)
+{
+	static const struct rgb_zone colors[] = {
+		{255,   0,   0},  /* fire red */
+		{  0,   0,   0},  /* dark     */
+		{  0,   0, 255},  /* ice blue */
+		{  0,   0,   0},  /* dark     */
+	};
+	u8 frame_count = ARRAY_SIZE(colors);
+	int frame, zone;
+
+	drvdata->rgb_frame_count = frame_count;
+
+	for (frame = 0; frame < frame_count; frame++) {
+		for (zone = 0; zone < 4; zone++) {
+			drvdata->rgb_frames[frame].zone[zone]     = colors[(zone + frame) % 4];
+			drvdata->rgb_frames[frame].zone[zone + 4] = colors[(zone - frame + 6) % 4];
+		}
+		drvdata->rgb_frames[frame].zone[8] = colors[frame];
+	}
+
+	return claw_write_rgb_state(drvdata);
+}
+
+/* Apply current state to device */
+static int claw_apply_rgb_state(struct claw_drvdata *drvdata)
+{
+	static const struct rgb_zone off = { 0, 0, 0 };
+
+	if (!drvdata->rgb_enabled) {
+		drvdata->rgb_frame_count = 1;
+		claw_frame_fill_solid(&drvdata->rgb_frames[0], off);
+		return claw_write_rgb_state(drvdata);
+	}
+
+	switch (drvdata->rgb_effect) {
+	case CLAW_RGB_EFFECT_MONOCOLOR:
+		return claw_apply_monocolor(drvdata);
+	case CLAW_RGB_EFFECT_BREATHE:
+		return claw_apply_breathe(drvdata);
+	case CLAW_RGB_EFFECT_CHROMA:
+		return claw_apply_chroma(drvdata);
+	case CLAW_RGB_EFFECT_RAINBOW:
+		return claw_apply_rainbow(drvdata);
+	case CLAW_RGB_EFFECT_FROSTFIRE:
+		return claw_apply_frostfire(drvdata);
+	default:
+		dev_err(drvdata->led_mc.led_cdev.dev,
+			"No supported rgb_effect selected\n");
+		return -EINVAL;
+	}
+}
+
+static void claw_rgb_queue_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, rgb_queue);
+	int ret;
+
+	ret = claw_apply_rgb_state(drvdata);
+	if (ret)
+		dev_err(drvdata->led_mc.led_cdev.dev,
+			"Failed to apply RGB state: %d\n", ret);
+}
+
+static ssize_t effect_store(struct device *dev,
+			    struct device_attribute *attr,
+			    const char *buf, size_t count)
+{
+	struct led_classdev *led_cdev = dev_get_drvdata(dev);
+	struct led_classdev_mc *led_mc = container_of(led_cdev, struct led_classdev_mc, led_cdev);
+	struct claw_drvdata *drvdata = container_of(led_mc, struct claw_drvdata, led_mc);
+	int ret;
+
+	ret = sysfs_match_string(claw_rgb_effect_text, buf);
+	if (ret < 0)
+		return ret;
+
+	drvdata->rgb_effect = ret;
+	mod_delayed_work(system_wq, &drvdata->rgb_queue, msecs_to_jiffies(50));
+
+	return count;
+}
+
+static ssize_t effect_show(struct device *dev,
+			   struct device_attribute *attr, char *buf)
+{
+	struct led_classdev *led_cdev = dev_get_drvdata(dev);
+	struct led_classdev_mc *led_mc = container_of(led_cdev, struct led_classdev_mc, led_cdev);
+	struct claw_drvdata *drvdata = container_of(led_mc, struct claw_drvdata, led_mc);
+
+	if (drvdata->rgb_effect >= ARRAY_SIZE(claw_rgb_effect_text))
+		return -EINVAL;
+
+	return sysfs_emit(buf, "%s\n", claw_rgb_effect_text[drvdata->rgb_effect]);
+}
+
+static DEVICE_ATTR_RW(effect);
+
+static ssize_t effect_index_show(struct device *dev,
+				 struct device_attribute *attr, char *buf)
+{
+	int i, count = 0;
+
+	for (i = 0; i < ARRAY_SIZE(claw_rgb_effect_text); i++)
+		count += sysfs_emit_at(buf, count, "%s ", claw_rgb_effect_text[i]);
+
+	if (count)
+		buf[count - 1] = '\n';
+
+	return count;
+}
+static DEVICE_ATTR_RO(effect_index);
+
+static ssize_t enabled_store(struct device *dev,
+			     struct device_attribute *attr,
+			     const char *buf, size_t count)
+{
+	struct led_classdev *led_cdev = dev_get_drvdata(dev);
+	struct led_classdev_mc *led_mc = container_of(led_cdev, struct led_classdev_mc, led_cdev);
+	struct claw_drvdata *drvdata = container_of(led_mc, struct claw_drvdata, led_mc);
+	bool val;
+	int ret;
+
+	ret = kstrtobool(buf, &val);
+	if (ret)
+		return ret;
+
+	drvdata->rgb_enabled = val;
+	mod_delayed_work(system_wq, &drvdata->rgb_queue, msecs_to_jiffies(50));
+
+	return count;
+}
+
+static ssize_t enabled_show(struct device *dev,
+			    struct device_attribute *attr, char *buf)
+{
+	struct led_classdev *led_cdev = dev_get_drvdata(dev);
+	struct led_classdev_mc *led_mc = container_of(led_cdev, struct led_classdev_mc, led_cdev);
+	struct claw_drvdata *drvdata = container_of(led_mc, struct claw_drvdata, led_mc);
+
+	return sysfs_emit(buf, "%s\n", drvdata->rgb_enabled ? "true" : "false");
+}
+static DEVICE_ATTR_RW(enabled);
+
+static ssize_t enabled_index_show(struct device *dev,
+				  struct device_attribute *attr, char *buf)
+{
+	return sysfs_emit(buf, "true false\n");
+}
+static DEVICE_ATTR_RO(enabled_index);
+
+static ssize_t speed_store(struct device *dev, struct device_attribute *attr,
+			   const char *buf, size_t count)
+{
+	struct led_classdev *led_cdev = dev_get_drvdata(dev);
+	struct led_classdev_mc *led_mc = container_of(led_cdev, struct led_classdev_mc, led_cdev);
+	struct claw_drvdata *drvdata = container_of(led_mc, struct claw_drvdata, led_mc);
+	unsigned int val, speed;
+	int ret;
+
+	ret = kstrtouint(buf, 10, &val);
+	if (ret)
+		return ret;
+
+	if (val > 20)
+		return -EINVAL;
+
+	/* 0 is fastest, invert value for intuitive userspace speed */
+	speed = 20 - val;
+
+	drvdata->rgb_speed = speed;
+	mod_delayed_work(system_wq, &drvdata->rgb_queue, msecs_to_jiffies(50));
+
+	return count;
+}
+
+static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
+			  char *buf)
+{
+	struct hid_device *hdev = to_hid_device(dev);
+	struct claw_drvdata *drvdata = hid_get_drvdata(hdev);
+	u8 speed = 20 - drvdata->rgb_speed;
+
+	return sysfs_emit(buf, "%u\n", speed);
+}
+static DEVICE_ATTR_RW(speed);
+
+static ssize_t speed_range_show(struct device *dev,
+				struct device_attribute *attr, char *buf)
+{
+	return sysfs_emit(buf, "0-20\n");
+}
+static DEVICE_ATTR_RO(speed_range);
+
+static void claw_led_brightness_set(struct led_classdev *led_cdev,
+				    enum led_brightness _brightness)
+{
+	struct led_classdev_mc *led_mc = container_of(led_cdev, struct led_classdev_mc, led_cdev);
+	struct claw_drvdata *drvdata = container_of(led_mc, struct claw_drvdata, led_mc);
+
+	mod_delayed_work(system_wq, &drvdata->rgb_queue, msecs_to_jiffies(50));
+}
+
+static struct attribute *claw_rgb_attrs[] = {
+	&dev_attr_effect.attr,
+	&dev_attr_effect_index.attr,
+	&dev_attr_enabled.attr,
+	&dev_attr_enabled_index.attr,
+	&dev_attr_speed.attr,
+	&dev_attr_speed_range.attr,
+	NULL,
+};
+
+static const struct attribute_group rgb_attr_group = {
+	.attrs = claw_rgb_attrs,
+};
+
+static struct mc_subled claw_rgb_subled_info[] = {
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.channel = 0x1,
+	},
+	{
+		.color_index = LED_COLOR_ID_GREEN,
+		.channel = 0x2,
+	},
+	{
+		.color_index = LED_COLOR_ID_BLUE,
+		.channel = 0x3,
+	},
+};
+
 static void claw_remove(struct hid_device *hdev);
 
 static void cfg_setup_fn(struct work_struct *work)
@@ -758,6 +1234,13 @@ static void cfg_setup_fn(struct work_struct *work)
 		claw_remove(drvdata->hdev);
 	}
 
+	ret = claw_read_rgb_config(drvdata->hdev);
+	if (ret) {
+		dev_err(drvdata->led_mc.led_cdev.dev,
+			"Failed to setup device, can't read RGB config: %d\n", ret);
+		claw_remove(drvdata->hdev);
+	}
+
 	/* Add sysfs attributes after we get the device state */
 	ret = sysfs_create_group(&drvdata->hdev->dev.kobj, &claw_gamepad_attr_group);
 	if (ret) {
@@ -766,7 +1249,15 @@ static void cfg_setup_fn(struct work_struct *work)
 		claw_remove(drvdata->hdev);
 	}
 
+	ret = device_add_group(drvdata->led_mc.led_cdev.dev, &rgb_attr_group);
+	if (ret) {
+		dev_err(&drvdata->hdev->dev,
+			"Failed to setup device, can't create led attributes: %d\n", ret);
+		claw_remove(drvdata->hdev);
+	}
+
 	kobject_uevent(&drvdata->hdev->dev.kobj, KOBJ_CHANGE);
+	kobject_uevent(&drvdata->led_mc.led_cdev.dev->kobj, KOBJ_CHANGE);
 }
 
 static void cfg_resume_fn(struct work_struct *work)
@@ -776,6 +1267,10 @@ static void cfg_resume_fn(struct work_struct *work)
 	u8 data[2] = { drvdata->gamepad_mode, drvdata->mkeys_function };
 	int ret;
 
+	ret = claw_read_rgb_config(drvdata->hdev);
+	if (ret)
+		dev_err(drvdata->led_mc.led_cdev.dev, "Failed to read RGB config: %d\n", ret);
+
 	ret = mcu_property_out(drvdata->hdev, CLAW_COMMAND_TYPE_SWITCH_MODE, data,
 			       ARRAY_SIZE(data), 0);
 	if (ret)
@@ -789,20 +1284,25 @@ static void claw_features_supported(struct claw_drvdata *drvdata)
 
 	if (major == 0x01) {
 		drvdata->bmap_support = true;
-		if (minor >= 0x66)
+		if (minor >= 0x66) {
 			drvdata->bmap_addr = button_mapping_addr_new;
-		else
+			drvdata->rgb_addr = rgb_addr_new;
+		} else {
 			drvdata->bmap_addr = button_mapping_addr_old;
+			drvdata->rgb_addr = rgb_addr_old;
+		}
 		return;
 	}
 
 	if ((major == 0x02 && minor >= 0x17) || major >= 0x03) {
 		drvdata->bmap_support = true;
 		drvdata->bmap_addr = button_mapping_addr_new;
+		drvdata->rgb_addr = rgb_addr_new;
 		return;
 	}
 
 	drvdata->bmap_support = false;
+	drvdata->rgb_addr = rgb_addr_old;
 }
 
 static int claw_probe(struct hid_device *hdev, const struct hid_device_id *id)
@@ -860,12 +1360,36 @@ static int claw_probe(struct hid_device *hdev, const struct hid_device_id *id)
 
 	init_completion(&drvdata->send_cmd_complete);
 
+	/* Initialize RGB LED */
+	INIT_DELAYED_WORK(&drvdata->rgb_queue, &claw_rgb_queue_fn);
+
+	drvdata->led_mc.led_cdev.name = "msi_claw:rgb:joystick_rings";
+	drvdata->led_mc.led_cdev.brightness = 0x50;
+	drvdata->led_mc.led_cdev.max_brightness = 0x64;
+	drvdata->led_mc.led_cdev.color = LED_COLOR_ID_RGB;
+	drvdata->led_mc.led_cdev.brightness_set = claw_led_brightness_set;
+	drvdata->led_mc.num_colors = 3;
+	drvdata->led_mc.subled_info = devm_kmemdup(&hdev->dev, claw_rgb_subled_info,
+						   sizeof(claw_rgb_subled_info), GFP_KERNEL);
+	if (!drvdata->led_mc.subled_info) {
+		ret = -ENOMEM;
+		goto err_close;
+	}
+
+	drvdata->rgb_enabled = true;
+
+	ret = devm_led_classdev_multicolor_register(&hdev->dev, &drvdata->led_mc);
+	if (ret)
+		goto err_close;
+
 	INIT_DELAYED_WORK(&drvdata->cfg_resume, &cfg_resume_fn);
 	INIT_DELAYED_WORK(&drvdata->cfg_setup, &cfg_setup_fn);
 	schedule_delayed_work(&drvdata->cfg_setup, msecs_to_jiffies(500));
 
 	return 0;
 
+err_close:
+	hid_hw_close(hdev);
 err_stop_hw:
 	hid_hw_stop(hdev);
 err_probe:
@@ -881,6 +1405,9 @@ static void claw_remove(struct hid_device *hdev)
 
 	if (drvdata->endpoint == CLAW_XINPUT_CFG_INTF_IN ||
 	    drvdata->endpoint == CLAW_DINPUT_CFG_INTF_IN) {
+		/* Block writes to brightness/multi_intensity during teardown */
+		drvdata->led_mc.led_cdev.brightness_set = NULL;
+		cancel_delayed_work_sync(&drvdata->rgb_queue);
 		sysfs_remove_group(&hdev->dev.kobj, &claw_gamepad_attr_group);
 		cancel_delayed_work_sync(&drvdata->cfg_setup);
 		cancel_delayed_work_sync(&drvdata->cfg_resume);
Signature · 1 line
2.53.0
#6 lore raw

Re: [PATCH 3/4] HID: hid-msi-claw: Add RGB control interface

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] `speed_show()` incorrectly casts the LED class device pointer to a `hid_device`, leading to an…

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] speed_show() incorrectly casts the LED class device pointer to a hid_device, leading to an out-of-bounds memory read and kernel crash.
- [High] Deadlock and missing return in cfg_setup_fn when handling initialization errors.
- [High] Sysfs attributes re-arm the cancelled delayed work after device teardown, leading to a Use-After-Free.
- [High] Racy nullification of brightness_set causes a TOCTOU NULL pointer dereference.
- [Medium] Communication failure in claw_read_rgb_config leaves the profile parser in a corrupted state.
--

commit 9663adbabcba6be0b443e7073133d650a790c871
Author: Derek J. Clark <[email protected]>

HID: hid-msi-claw: Add RGB control interface

Adds RGB control interface for MSI Claw devices with 9 total zones and supports up to 8 sequential frames of RGB zone data.

Quoted 4 lines
drivers/hid/hid-msi-claw.c +0 -0
index 60694d075d56f..f4fe74a784c29 100644

[ ... ]

Quoted 14 lines
drivers/hid/hid-msi-claw.c +10 -0
@@ -743,6 +836,389 @@ static const struct attribute_group claw_gamepad_attr_group = {
 	.is_visible = claw_gamepad_attr_is_visible,
 };
 
+/* Read RGB config from device */
+static int claw_read_rgb_config(struct hid_device *hdev)
+{
+	u8 data[4] = { 0x01, 0x00, 0x00, CLAW_RGB_FRAME_OFFSET };
+	struct claw_drvdata *drvdata = hid_get_drvdata(hdev);
+	u16 read_addr = drvdata->rgb_addr;
+	size_t len = ARRAY_SIZE(data);
+	int ret, i;
+
+	if (!drvdata->rgb_addr)

drivers/hid/hid-msi-claw.c static const struct attribute_group claw_gamepad_attr_group = { · line 858

Quoted 10 lines
drivers/hid/hid-msi-claw.c +10 -0
+		return -ENODEV;
+
+	/* Loop through all 8 pages of RGB data */
+	for (i = 0; i < 8; i++) {
+		drvdata->profile_pending = CLAW_RGB_PENDING;
+		data[1] = (read_addr >> 8) & 0xff;
+		data[2] = read_addr & 0x00ff;
+		ret = mcu_property_out(hdev, CLAW_COMMAND_TYPE_READ_PROFILE, data, len, 8);
+		if (ret)
+			return ret;

If mcu_property_out() fails here, does drvdata->profile_pending remain set to CLAW_RGB_PENDING? If a subsequent unrelated profile event or stray ACK arrives, could claw_profile_event() incorrectly parse its payload as an RGB frame?

[ ... ]

drivers/hid/hid-msi-claw.c static const struct attribute_group claw_gamepad_attr_group = { · line 1170

Quoted 5 lines
drivers/hid/hid-msi-claw.c +5 -0
+static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
+			  char *buf)
+{
+	struct hid_device *hdev = to_hid_device(dev);
+	struct claw_drvdata *drvdata = hid_get_drvdata(hdev);

Since the rgb_attr_group attributes are attached to the LED class device rather than the HID device, does to_hid_device(dev) result in an invalid pointer dereference here? Other sysfs functions in this patch seem to correctly use dev_get_drvdata(dev) to retrieve the led_classdev.

[ ... ]

drivers/hid/hid-msi-claw.c static void cfg_setup_fn(struct work_struct *work) · line 1242

Quoted 10 lines
drivers/hid/hid-msi-claw.c +6 -0
@@ -758,6 +1234,13 @@ static void cfg_setup_fn(struct work_struct *work)
 		claw_remove(drvdata->hdev);
 	}
 
+	ret = claw_read_rgb_config(drvdata->hdev);
+	if (ret) {
+		dev_err(drvdata->led_mc.led_cdev.dev,
+			"Failed to setup device, can't read RGB config: %d\n", ret);
+		claw_remove(drvdata->hdev);
+	}

If claw_remove() is called from within the cfg_setup worker thread, and claw_remove() calls cancel_delayed_work_sync(&drvdata->cfg_setup), won't this deadlock waiting for itself to complete?

Also, since there is no return statement after claw_remove() is called in these error blocks, will this proceed to execute the rest of the function on a stopped device?

[ ... ]

drivers/hid/hid-msi-claw.c static void claw_remove(struct hid_device *hdev) · line 1409

Quoted 6 lines
drivers/hid/hid-msi-claw.c +2 -0
@@ -881,6 +1405,9 @@ static void claw_remove(struct hid_device *hdev)
 
 	if (drvdata->endpoint == CLAW_XINPUT_CFG_INTF_IN ||
 	    drvdata->endpoint == CLAW_DINPUT_CFG_INTF_IN) {
+		/* Block writes to brightness/multi_intensity during teardown */
+		drvdata->led_mc.led_cdev.brightness_set = NULL;

Is it safe to dynamically nullify the brightness_set callback here? If a concurrent brightness update in the LED core checks the pointer, sees it as valid, and then this nullification happens before the actual function call, could it result in a NULL pointer dereference?

drivers/hid/hid-msi-claw.c static void claw_remove(struct hid_device *hdev) · line 1410

Quoted 1 line
drivers/hid/hid-msi-claw.c +1 -0
+		cancel_delayed_work_sync(&drvdata->rgb_queue);

Since the LED class device is registered with devres (devm_led_classdev_multicolor_register), the sysfs attributes like effect, speed, and enabled remain accessible until after claw_remove() completes.

If userspace writes to any of those attributes in this window, could mod_delayed_work() re-arm rgb_queue, leading to a use-after-free when drvdata is subsequently freed?

Quoted 3 lines
drivers/hid/hid-msi-claw.c +0 -0
 		sysfs_remove_group(&hdev->dev.kobj, &claw_gamepad_attr_group);
 		cancel_delayed_work_sync(&drvdata->cfg_setup);
 		cancel_delayed_work_sync(&drvdata->cfg_resume);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#7 lore raw

Re: [PATCH 3/4] HID: hid-msi-claw: Add RGB control interface

Quoted 44 lines · On May 11, 2026 2:22:29 PM PDT, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] speed_show() incorrectly casts the LED class device pointer to a hid_device, leading to an out-of-bounds memory read and kernel crash.
- [High] Deadlock and missing return in cfg_setup_fn when handling initialization errors.
- [High] Sysfs attributes re-arm the cancelled delayed work after device teardown, leading to a Use-After-Free.
- [High] Racy nullification of brightness_set causes a TOCTOU NULL pointer dereference.
- [Medium] Communication failure in claw_read_rgb_config leaves the profile parser in a corrupted state.
--

commit 9663adbabcba6be0b443e7073133d650a790c871
Author: Derek J. Clark <[email protected]>

HID: hid-msi-claw: Add RGB control interface

Adds RGB control interface for MSI Claw devices with 9 total zones and supports up to 8 sequential frames of RGB zone data.

Quoted 4 lines
drivers/hid/hid-msi-claw.c +0 -0
index 60694d075d56f..f4fe74a784c29 100644

[ ... ]

Quoted 21 lines
drivers/hid/hid-msi-claw.c +17 -0
@@ -743,6 +836,389 @@ static const struct attribute_group claw_gamepad_attr_group = {
 	.is_visible = claw_gamepad_attr_is_visible,
 };
 
+/* Read RGB config from device */
+static int claw_read_rgb_config(struct hid_device *hdev)
+{
+	u8 data[4] = { 0x01, 0x00, 0x00, CLAW_RGB_FRAME_OFFSET };
+	struct claw_drvdata *drvdata = hid_get_drvdata(hdev);
+	u16 read_addr = drvdata->rgb_addr;
+	size_t len = ARRAY_SIZE(data);
+	int ret, i;
+
+	if (!drvdata->rgb_addr)
+		return -ENODEV;
+
+	/* Loop through all 8 pages of RGB data */
+	for (i = 0; i < 8; i++) {
+		drvdata->profile_pending = CLAW_RGB_PENDING;
+		data[1] = (read_addr >> 8) & 0xff;
+		data[2] = read_addr & 0x00ff;
Quoted 9 lines

drivers/hid/hid-msi-claw.c static const struct attribute_group claw_gamepad_attr_group = { · line 858

Quoted 3 lines
drivers/hid/hid-msi-claw.c +3 -0
+		ret = mcu_property_out(hdev, CLAW_COMMAND_TYPE_READ_PROFILE, data, len, 8);
+		if (ret)
+			return ret;

If mcu_property_out() fails here, does drvdata->profile_pending remain set to CLAW_RGB_PENDING? If a subsequent unrelated profile event or stray ACK arrives, could claw_profile_event() incorrectly parse its payload as an RGB frame?

Hi AI bot,

I am responding for the humans that will be reading this.

Valid. Pending should be reset after errors. TBS, I need to find a way to push pending assignment into the mutex, or there will be issues as found in the other message.

Quoted 13 lines

[ ... ]

drivers/hid/hid-msi-claw.c static const struct attribute_group claw_gamepad_attr_group = { · line 1170

Quoted 5 lines
drivers/hid/hid-msi-claw.c +5 -0
+static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
+			  char *buf)
+{
+	struct hid_device *hdev = to_hid_device(dev);
+	struct claw_drvdata *drvdata = hid_get_drvdata(hdev);

Since the rgb_attr_group attributes are attached to the LED class device rather than the HID device, does to_hid_device(dev) result in an invalid pointer dereference here? Other sysfs functions in this patch seem to correctly use dev_get_drvdata(dev) to retrieve the led_classdev.

Also valid, though it doesn't crash when run it would be more proper to treat the led cdev as the parent.

Quoted 5 lines
Quoted 5 lines
drivers/hid/hid-msi-claw.c +0 -0
@@ -758,6 +1234,13 @@ static void cfg_setup_fn(struct work_struct *work)
 		claw_remove(drvdata->hdev);
 	}
 
Quoted 10 lines

drivers/hid/hid-msi-claw.c static void cfg_setup_fn(struct work_struct *work) · line 1242

Quoted 6 lines
drivers/hid/hid-msi-claw.c +6 -0
+	ret = claw_read_rgb_config(drvdata->hdev);
+	if (ret) {
+		dev_err(drvdata->led_mc.led_cdev.dev,
+			"Failed to setup device, can't read RGB config: %d\n", ret);
+		claw_remove(drvdata->hdev);
+	}

If claw_remove() is called from within the cfg_setup worker thread, and claw_remove() calls cancel_delayed_work_sync(&drvdata->cfg_setup), won't this deadlock waiting for itself to complete?

Since it's a void function I can't return an error. I suppose it can just dev_err and return without initializing any sysfs attributes.

Quoted 8 lines

Also, since there is no return statement after claw_remove() is called in these error blocks, will this proceed to execute the rest of the function on a stopped device?

[ ... ]

Quoted 2 lines
drivers/hid/hid-msi-claw.c +0 -0
@@ -881,6 +1405,9 @@ static void claw_remove(struct hid_device *hdev)
 
Quoted 9 lines

drivers/hid/hid-msi-claw.c static void claw_remove(struct hid_device *hdev) · line 1409

Quoted 4 lines
drivers/hid/hid-msi-claw.c +2 -0
 	if (drvdata->endpoint == CLAW_XINPUT_CFG_INTF_IN ||
 	    drvdata->endpoint == CLAW_DINPUT_CFG_INTF_IN) {
+		/* Block writes to brightness/multi_intensity during teardown */
+		drvdata->led_mc.led_cdev.brightness_set = NULL;

Is it safe to dynamically nullify the brightness_set callback here? If a concurrent brightness update in the LED core checks the pointer, sees it as valid, and then this nullification happens before the actual function call, could it result in a NULL pointer dereference?

This was done specifically to address a timing issue with the LED core and userspace writing to brightness during teardown. Without this we will get a null pointer dereference when the brightness function calls mcu_property_out.

Quoted 10 lines

drivers/hid/hid-msi-claw.c static void claw_remove(struct hid_device *hdev) · line 1410

Quoted 1 line
drivers/hid/hid-msi-claw.c +1 -0
+		cancel_delayed_work_sync(&drvdata->rgb_queue);

Since the LED class device is registered with devres (devm_led_classdev_multicolor_register), the sysfs attributes like effect, speed, and enabled remain accessible until after claw_remove() completes.

If userspace writes to any of those attributes in this window, could mod_delayed_work() re-arm rgb_queue, leading to a use-after-free when drvdata is subsequently freed?

Swapping this and the sysfs remove after should solve it.

-

Quoted 4 lines
Quoted 3 lines
drivers/hid/hid-msi-claw.c +0 -0
 		sysfs_remove_group(&hdev->dev.kobj, &claw_gamepad_attr_group);
 		cancel_delayed_work_sync(&drvdata->cfg_setup);
 		cancel_delayed_work_sync(&drvdata->cfg_resume);
#8 lore raw

PATCH 4/4 HID: hid-msi-claw: Add Rumble Intensity Attributes

In reply to Derek J. Clark

Adds intensity adjustment for the left and right rumble motors.

Claude was used during the reverse-engineering data gathering for this feature done by Zhouwang Huang. As the code had already been affected, I used Claude to create the initial framing for the feature, then did manual cleanup of the _show and _store functions afterwards to fix bugs and keep the coding style consistent. Claude was also used as an initial reviewer of this patch.

Assisted-by Claude:claude-sonnet-4-6
---
 drivers/hid/hid-msi-claw.c | 139 +++++++++++++++++++++++++++++++++++++
 1 file changed, 139 insertions(+)
drivers/hid/hid-msi-claw.c +139 -0
index f4fe74a784c2..6d089f49abdb 100644
@@ -76,6 +76,8 @@ enum claw_profile_ack_pending {
 	CLAW_M1_PENDING,
 	CLAW_M2_PENDING,
 	CLAW_RGB_PENDING,
+	CLAW_RUMBLE_LEFT_PENDING,
+	CLAW_RUMBLE_RIGHT_PENDING,
 };
 
 enum claw_key_index {
@@ -262,6 +264,11 @@ static const u16 button_mapping_addr_new[] = {
 static const u16 rgb_addr_old = 0x01fa;
 static const u16 rgb_addr_new = 0x024a;
 
+static const u16 rumble_addr[] = {
+	0x0022,  /* left  */
+	0x0023,  /* right */
+};
+
 struct claw_command_report {
 	u8 report_id;
 	u8 padding[2];
@@ -308,7 +315,10 @@ struct claw_drvdata {
 	enum claw_gamepad_mode_index gamepad_mode;
 	u8 m1_codes[CLAW_KEYS_MAX];
 	u8 m2_codes[CLAW_KEYS_MAX];
+	u8 rumble_intensity_right;
+	u8 rumble_intensity_left;
 	const u16 *bmap_addr;
+	bool rumble_support;
 	bool bmap_support;
 
 	/* RGB Variables */
@@ -396,6 +406,12 @@ static int claw_profile_event(struct claw_drvdata *drvdata, struct claw_command_
 		memcpy(&drvdata->rgb_frames[f_idx], &frame->zone_data,
 		       sizeof(struct rgb_frame));
 
+		break;
+	case CLAW_RUMBLE_LEFT_PENDING:
+		drvdata->rumble_intensity_left = cmd_rep->data[4];
+		break;
+	case CLAW_RUMBLE_RIGHT_PENDING:
+		drvdata->rumble_intensity_right = cmd_rep->data[4];
 		break;
 	default:
 		dev_warn(&drvdata->hdev->dev,
@@ -795,6 +811,116 @@ static ssize_t button_mapping_options_show(struct device *dev,
 }
 static DEVICE_ATTR_RO(button_mapping_options);
 
+static ssize_t rumble_intensity_left_store(struct device *dev,
+					   struct device_attribute *attr,
+					   const char *buf, size_t count)
+{
+	u8 data[] = { 0x01, (rumble_addr[0] >> 8) & 0xff, rumble_addr[0] & 0xff, 0x01, 0x00 };
+	struct hid_device *hdev = to_hid_device(dev);
+	struct claw_drvdata *drvdata = hid_get_drvdata(hdev);
+	u8 val;
+	int ret;
+
+	ret = kstrtou8(buf, 10, &val);
+	if (ret)
+		return ret;
+
+	if (val > 100)
+		return -EINVAL;
+
+	data[4] = val;
+
+	ret = mcu_property_out(hdev, CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA,
+			       data, ARRAY_SIZE(data), 8);
+	if (ret)
+		return ret;
+
+	ret = mcu_property_out(hdev, CLAW_COMMAND_TYPE_SYNC_TO_ROM, NULL, 0, 0);
+	if (ret)
+		return ret;
+
+	drvdata->rumble_intensity_left = val;
+
+	return count;
+}
+
+static ssize_t rumble_intensity_left_show(struct device *dev,
+					  struct device_attribute *attr,
+					  char *buf)
+{
+	u8 data[4] = { 0x01, (rumble_addr[0] >> 8) & 0xff, rumble_addr[0] & 0xff, 0x01 };
+	struct hid_device *hdev = to_hid_device(dev);
+	struct claw_drvdata *drvdata = hid_get_drvdata(hdev);
+	int ret;
+
+	drvdata->profile_pending = CLAW_RUMBLE_LEFT_PENDING;
+	ret = mcu_property_out(hdev, CLAW_COMMAND_TYPE_READ_PROFILE, data, ARRAY_SIZE(data), 8);
+	if (ret)
+		return ret;
+
+	return sysfs_emit(buf, "%u\n", drvdata->rumble_intensity_left);
+}
+static DEVICE_ATTR_RW(rumble_intensity_left);
+
+static ssize_t rumble_intensity_right_store(struct device *dev,
+					    struct device_attribute *attr,
+					    const char *buf, size_t count)
+{
+	u8 data[] = { 0x01, (rumble_addr[1] >> 8) & 0xff, rumble_addr[1] & 0xff, 0x01, 0x00 };
+	struct hid_device *hdev = to_hid_device(dev);
+	struct claw_drvdata *drvdata = hid_get_drvdata(hdev);
+	u8 val;
+	int ret;
+
+	ret = kstrtou8(buf, 10, &val);
+	if (ret)
+		return ret;
+
+	if (val > 100)
+		return -EINVAL;
+
+	data[4] = val;
+
+	ret = mcu_property_out(hdev, CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA,
+			       data, ARRAY_SIZE(data), 8);
+	if (ret)
+		return ret;
+
+	ret = mcu_property_out(hdev, CLAW_COMMAND_TYPE_SYNC_TO_ROM, NULL, 0, 0);
+	if (ret)
+		return ret;
+
+	drvdata->rumble_intensity_right = val;
+
+	return count;
+}
+
+static ssize_t rumble_intensity_right_show(struct device *dev,
+					   struct device_attribute *attr,
+					   char *buf)
+{
+	u8 data[4] = { 0x01, (rumble_addr[1] >> 8) & 0xff, rumble_addr[1] & 0xff, 0x01 };
+	struct hid_device *hdev = to_hid_device(dev);
+	struct claw_drvdata *drvdata = hid_get_drvdata(hdev);
+	int ret;
+
+	drvdata->profile_pending = CLAW_RUMBLE_RIGHT_PENDING;
+	ret = mcu_property_out(hdev, CLAW_COMMAND_TYPE_READ_PROFILE, data, ARRAY_SIZE(data), 8);
+	if (ret)
+		return ret;
+
+	return sysfs_emit(buf, "%u\n", drvdata->rumble_intensity_right);
+}
+static DEVICE_ATTR_RW(rumble_intensity_right);
+
+static ssize_t rumble_intensity_range_show(struct device *dev,
+					   struct device_attribute *attr,
+					   char *buf)
+{
+	return sysfs_emit(buf, "0-100\n");
+}
+static DEVICE_ATTR_RO(rumble_intensity_range);
+
 static umode_t claw_gamepad_attr_is_visible(struct kobject *kobj, struct attribute *attr,
 					    int n)
 {
@@ -815,6 +941,12 @@ static umode_t claw_gamepad_attr_is_visible(struct kobject *kobj, struct attribu
 	    attr == &dev_attr_reset.attr)
 		return attr->mode;
 
+	/* Hide rumble attrs if not supported */
+	if (attr == &dev_attr_rumble_intensity_left.attr ||
+	    attr == &dev_attr_rumble_intensity_right.attr ||
+	    attr == &dev_attr_rumble_intensity_range.attr)
+		return drvdata->rumble_support ? attr->mode : 0;
+
 	/* Hide button mapping attrs if it isn't supported */
 	return drvdata->bmap_support ? attr->mode : 0;
 }
@@ -828,6 +960,9 @@ static struct attribute *claw_gamepad_attrs[] = {
 	&dev_attr_mkeys_function.attr,
 	&dev_attr_mkeys_function_index.attr,
 	&dev_attr_reset.attr,
+	&dev_attr_rumble_intensity_left.attr,
+	&dev_attr_rumble_intensity_right.attr,
+	&dev_attr_rumble_intensity_range.attr,
 	NULL,
 };
 
@@ -1286,9 +1421,11 @@ static void claw_features_supported(struct claw_drvdata *drvdata)
 		drvdata->bmap_support = true;
 		if (minor >= 0x66) {
 			drvdata->bmap_addr = button_mapping_addr_new;
+			drvdata->rumble_support = true;
 			drvdata->rgb_addr = rgb_addr_new;
 		} else {
 			drvdata->bmap_addr = button_mapping_addr_old;
+			drvdata->rumble_support = false;
 			drvdata->rgb_addr = rgb_addr_old;
 		}
 		return;
@@ -1297,11 +1434,13 @@ static void claw_features_supported(struct claw_drvdata *drvdata)
 	if ((major == 0x02 && minor >= 0x17) || major >= 0x03) {
 		drvdata->bmap_support = true;
 		drvdata->bmap_addr = button_mapping_addr_new;
+		drvdata->rumble_support = true;
 		drvdata->rgb_addr = rgb_addr_new;
 		return;
 	}
 
 	drvdata->bmap_support = false;
+	drvdata->rumble_support = false;
 	drvdata->rgb_addr = rgb_addr_old;
 }
 
Signature · 1 line
2.53.0
#9 lore raw

Re: [PATCH 4/4] HID: hid-msi-claw: Add Rumble Intensity Attributes

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Critical] The underlying `mcu_property_out()` function uses an improperly scoped `guard(mutex)`…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Critical] The underlying mcu_property_out() function uses an improperly scoped guard(mutex), breaking all driver synchronization.
- [High] Unprotected modification of drvdata->profile_pending causes race conditions during concurrent sysfs reads.
- [High] Torn transactions in sysfs store handlers lead to state mismatch between hardware and software.
- [Medium] Failure to reset drvdata->profile_pending on mcu_property_out() error paths corrupts state machine. --

commit 2a92587430af9e91f5af5fbf2205fea99b2d1000
Author: Derek J. Clark <[email protected]>

HID: hid-msi-claw: Add Rumble Intensity Attributes

This commit adds intensity adjustment for the left and right rumble motors via new sysfs attributes.

Quoted 4 lines
drivers/hid/hid-msi-claw.c +0 -0
index f4fe74a784c29..6d089f49abdb4 100644

[ ... ]

Quoted 8 lines
drivers/hid/hid-msi-claw.c +4 -0
@@ -795,6 +811,116 @@ static ssize_t button_mapping_options_show(struct device *dev,
 }
 static DEVICE_ATTR_RO(button_mapping_options);
 
+static ssize_t rumble_intensity_left_store(struct device *dev,
+					   struct device_attribute *attr,
+					   const char *buf, size_t count)
+{

[ ... ]

drivers/hid/hid-msi-claw.c static ssize_t button_mapping_options_show(struct device *dev, · line 845

Quoted 13 lines
drivers/hid/hid-msi-claw.c +13 -0
+	ret = mcu_property_out(hdev, CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA,
+			       data, ARRAY_SIZE(data), 8);
+	if (ret)
+		return ret;
+
+	ret = mcu_property_out(hdev, CLAW_COMMAND_TYPE_SYNC_TO_ROM, NULL, 0, 0);
+	if (ret)
+		return ret;
+
+	drvdata->rumble_intensity_left = val;
+
+	return count;
+}

Could this sequence introduce a regression if interrupted by concurrent sysfs stores?

Since there doesn't appear to be a lock held across this sequence, concurrent writes could interleave their WRITE_PROFILE_DATA and SYNC_TO_ROM commands. This could cause the hardware to synchronize an unintended state or cause the local driver state to fall out of sync with the actual hardware value.

Also, regarding the underlying mcu_property_out() function: does it use an improperly scoped guard(mutex)? If the configuration mutex is acquired strictly inside a limited scope, the lock would be released before hid_hw_output_report() and wait_for_completion_interruptible_timeout() are executed. This could leave all concurrent sysfs calls unprotected, causing a regression with overlapping USB output reports.

Quoted 4 lines
drivers/hid/hid-msi-claw.c +4 -0
+static ssize_t rumble_intensity_left_show(struct device *dev,
+					  struct device_attribute *attr,
+					  char *buf)
+{

[ ... ]

drivers/hid/hid-msi-claw.c static ssize_t button_mapping_options_show(struct device *dev, · line 859

Quoted 4 lines
drivers/hid/hid-msi-claw.c +4 -0
+	drvdata->profile_pending = CLAW_RUMBLE_LEFT_PENDING;
+	ret = mcu_property_out(hdev, CLAW_COMMAND_TYPE_READ_PROFILE, data, ARRAY_SIZE(data), 8);
+	if (ret)
+		return ret;

Is it possible for concurrent sysfs reads to cause a regression here?

Since drvdata->profile_pending is modified without any locking, if two sysfs attributes are read concurrently, one thread's modification could overwrite the other's state. When the asynchronous USB response later arrives, claw_profile_event() might rely on this clobbered value to route the incoming data, which could result in userspace reading incorrect data.

Additionally, if mcu_property_out() returns an error, the function returns immediately but leaves drvdata->profile_pending set to the pending state. Could this stale state cause a regression where a subsequent unrelated asynchronous profile event is incorrectly intercepted by claw_profile_event()?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
maintainer
#10 lore raw
In reply to Derek J. Clark
Quoted 29 lines · Derek J. Clark

This series adds and HID Configuration driver for the MSI Claw line of Handheld Gaming PC's. The MSI Claw HID interface provides multiple features, such as the ability to switch between xinput, dinput, and a desktop mode, RGB control, rumble intensity, and mapping of the rear "M" keys. There are additional gamepad modes that are not included in this driver as they appear to be used in assembly line testing or are incomplete in the firmware. During my testing I found them to be unstable.

The initial version of this driver was written by Denis Benato, which contained the initial reverse-engineering and implementation for the gamepad mode switching. This work was later expanded by Zhouwang Huang to include more gamepad modes and additional features. Finally, I refactored the entire driver, fixed multiple bugs, and refined the overall format to conform to kernel driver best practices and style guide.

Claude was used initially by Zhouwang Huang to quickly parse HID captures during the reverse-engineering of some of the features. Since Claude had already been used, as a test of its capabilities I had it implement the rumble intensity attribute after I had already rewritten most of the driver, which I then manually edited to fix some mistakes. I also used Claude to review the driver and these patches for any mistakes and bugs.

Quoted 5 lines

Derek J. Clark (4):
HID: hid-msi-claw: Add MSI Claw configuration driver
HID: hid-msi-claw: Add M-key mapping attributes
HID: hid-msi-claw: Add RGB control interface
HID: hid-msi-claw: Add Rumble Intensity Attributes

The driver looks reasonable, I'd just like to propose that we name it just hid-msi to follow the usual HID subsystem driver naming standards, so that it can later be extended with supporting other MSI devices.

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs
#11 lore raw
Quoted 38 lines · Jiri Kosina
Quoted 29 lines · Derek J. Clark

This series adds and HID Configuration driver for the MSI Claw line of Handheld Gaming PC's. The MSI Claw HID interface provides multiple features, such as the ability to switch between xinput, dinput, and a desktop mode, RGB control, rumble intensity, and mapping of the rear "M" keys. There are additional gamepad modes that are not included in this driver as they appear to be used in assembly line testing or are incomplete in the firmware. During my testing I found them to be unstable.

The initial version of this driver was written by Denis Benato, which contained the initial reverse-engineering and implementation for the gamepad mode switching. This work was later expanded by Zhouwang Huang to include more gamepad modes and additional features. Finally, I refactored the entire driver, fixed multiple bugs, and refined the overall format to conform to kernel driver best practices and style guide.

Claude was used initially by Zhouwang Huang to quickly parse HID captures during the reverse-engineering of some of the features. Since Claude had already been used, as a test of its capabilities I had it implement the rumble intensity attribute after I had already rewritten most of the driver, which I then manually edited to fix some mistakes. I also used Claude to review the driver and these patches for any mistakes and bugs.

Quoted 5 lines

Derek J. Clark (4):
HID: hid-msi-claw: Add MSI Claw configuration driver
HID: hid-msi-claw: Add M-key mapping attributes
HID: hid-msi-claw: Add RGB control interface
HID: hid-msi-claw: Add Rumble Intensity Attributes

Quoted 4 lines

The driver looks reasonable, I'd just like to propose that we name it just hid-msi to follow the usual HID subsystem driver naming standards, so that it can later be extended with supporting other MSI devices.

Hi Jiri,
Sounds good. I'll do that when I fix the issues flagged by the bot in v2 and I'll try to have it out some time this week.

Thanks,
Derek

Thanks

Quoted 6 lines

Thanks,

--
Jiri Kosina
SUSE Labs