Skip to content

Add MSI Claw HID Configuration Driver

Derek J. Clark linux-doc linux-input lkml 13 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v13 Latest4 patches 1 replyIn review
  2. v124 patches 2 repliesSuperseded
  3. v114 patches 1 replySuperseded
Earlier versions · 10
VersionPostedPatchesRepliesStatus
v104 patches5 repliesSuperseded
v94 patches2 repliesSuperseded
v84 patches0 repliesSuperseded
v74 patches0 repliesSuperseded
v64 patches0 repliesSuperseded
v54 patches0 repliesSuperseded
v44 patches0 repliesSuperseded
v34 patches0 repliesSuperseded
v24 patches3 repliesSuperseded
v14 patches4 repliesSuperseded

Changes in v13 from the cover letter · 59 lines

- Add pm_ptr() to driver resume pointer.
- Refactor handling of pending acks with no timeout, avoids collisions
by arming a completion to wait on the next command, allowing the URB
to be dropped (so the ACK will fire) while forcing serial events.
v12: https://lore.kernel.org/linux-input/[email protected]/
- Fix race condition with mode events. Makes claw_hw_output_report() a wrapper that holds cfg_mutex and adds __claw_hw_output_report(),
which has the old functionality but uses lockdep_assert_held(), and claw_switch_mode(), which handles cfg_mutex holding and spinlock
holding for all mode functions, preventing the multiple callers issue.
- Remove goto based cleanup in __claw_hw_output_report(), adds some duplicated code but avoids anti-pattern for cleanup.
- Use spinlock_irqsave for raw_event-reachable locks since completion context isn't guaranteed softirq-only across all HCDs.
- Add bool to track if the driver is waiting on a sync ack with no
timeout to prevent those acks from clearing timeout acks.
- On address mismatch, assume stale message return and keep waiting for correct message.
- On remove, cancel the rgb_queue last to avoid a re-arming, and in rgb_queue_fn check rgb_registered to avoid running after the sysfs
group has been removed.
- Add note to led_cdev.name to (hopefully) silence sashiko-bot about
an impossible double device collision.
- Explicitly check if the current address is less than the rgb_address
to avoid invalid overflow math.
v11: https://lore.kernel.org/linux-input/[email protected]/#r
- Restore dropped changes from v10.
v10: https://lore.kernel.org/linux-input/[email protected]/
- Remove additional gamepad_registered variable left over after rename
to gp_registered.
v9: https://lore.kernel.org/linux-input/[email protected]/
- Don't use devm_* functions in cfg_setup_fn, do manual adding
and cleanup to prevent possible use after free.
- Use scoped_guard instead of guard in claw_remove.
- Rename gamepad_registered to gp_registered for brevity.
- Check for drvdata in rgb_queue_fn to avoid use after free during teardown.
- Ensure rgb_queue work is canceled during suspend.
- Limit guard usage in cfg_setup_fn to avoid holding a lock during registration and add group events.
v8: https://lore.kernel.org/linux-input/[email protected]/
- Use spinlock when accessing gamepad_registered.
- Clear state machine on all errors in claw_hw_output_report.
- Wrap all branches under single cmd_lock guard in claw_raw_event.
- Reject generic ACK in claw_raw_event if waiting_cmd is for another branch.
- Wrap all branches under single cmd_lock guard in claw_raw_event.
- Reject generic ACK in claw_raw_event if waiting_cmd is for another branch.
- Don't close hid devices that couldn't have been opened.
- Ensure led_classdev is unregistered if adding attribute group fails.
- Reorder remove actions to ensure no use-after free or rearming cleared flags.
v7: https://lore.kernel.org/linux-input/[email protected]/
- Use smp_[store_release|load_acquire] pattern for checking gamepad_registered and rgb_registered to avoid possible races during teardown.
- Reorder reinit_completion in claw_hw_output_report to avoid race
with possible incoming ACKs.
- Reorder cancel_delayed_work_sync to ensure setup can't be re-armed
after cancel.
- Reset command state machine if hw_output_report has an error.
- Add comments to (hopefully) silence sashinko-bot warnings about the
use of endpoint matching and the impossible scenario of switching to
the alternate endpoint from userspace while the driver is bound.
- Don't use spinlock_irqsave when already in irq context.
- Add profile_lock for read/write profile_pending.
- Use struct for mkey reports and rumble reports, following the
pattern established by rgb reports previously.
- Add gating to cfg_setup_fn, allowing either gamepad settings or rgb settings to populate if the other fails for any reason.
- Match on write address for rumble and mkey reports to prevent late
ACK from causing synchronization errors.

Patches in v13 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4HID: hid-msi: Add MSI Claw configuration driverMAINTAINERS drivers/hid/Kconfig drivers/hid/Makefile +2 more+793 -00 replies
2/4HID: hid-msi: Add M-key mapping attributesdrivers/hid/hid-msi.c+445 -10 replies
3/4HID: hid-msi: Add RGB control interfacedrivers/hid/hid-msi.c+666 -150 replies
4/4HID: hid-msi: Add Rumble Intensity Attributesdrivers/hid/hid-msi.c+201 -10 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v13 yet.

Read the v13 thread

Cover letter [PATCH v13 0/4] Add MSI Claw HID Configuration Driver

This series adds an 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.

---
v13:
- Add pm_ptr() to driver resume pointer.
- Refactor handling of pending acks with no timeout, avoids collisions
by arming a completion to wait on the next command, allowing the URB
to be dropped (so the ACK will fire) while forcing serial events.
v12: https://lore.kernel.org/linux-input/[email protected]/
- Fix race condition with mode events. Makes claw_hw_output_report() a wrapper that holds cfg_mutex and adds __claw_hw_output_report(),
which has the old functionality but uses lockdep_assert_held(), and claw_switch_mode(), which handles cfg_mutex holding and spinlock
holding for all mode functions, preventing the multiple callers issue.
- Remove goto based cleanup in __claw_hw_output_report(), adds some duplicated code but avoids anti-pattern for cleanup.
- Use spinlock_irqsave for raw_event-reachable locks since completion context isn't guaranteed softirq-only across all HCDs.
- Add bool to track if the driver is waiting on a sync ack with no
timeout to prevent those acks from clearing timeout acks.
- On address mismatch, assume stale message return and keep waiting for correct message.
- On remove, cancel the rgb_queue last to avoid a re-arming, and in rgb_queue_fn check rgb_registered to avoid running after the sysfs
group has been removed.
- Add note to led_cdev.name to (hopefully) silence sashiko-bot about
an impossible double device collision.
- Explicitly check if the current address is less than the rgb_address
to avoid invalid overflow math.
v11: https://lore.kernel.org/linux-input/[email protected]/#r
- Restore dropped changes from v10.
v10: https://lore.kernel.org/linux-input/[email protected]/
- Remove additional gamepad_registered variable left over after rename
to gp_registered.
v9: https://lore.kernel.org/linux-input/[email protected]/
- Don't use devm_* functions in cfg_setup_fn, do manual adding
and cleanup to prevent possible use after free.
- Use scoped_guard instead of guard in claw_remove.
- Rename gamepad_registered to gp_registered for brevity.
- Check for drvdata in rgb_queue_fn to avoid use after free during teardown.
- Ensure rgb_queue work is canceled during suspend.
- Limit guard usage in cfg_setup_fn to avoid holding a lock during registration and add group events.
v8: https://lore.kernel.org/linux-input/[email protected]/
- Use spinlock when accessing gamepad_registered.
- Clear state machine on all errors in claw_hw_output_report.
- Wrap all branches under single cmd_lock guard in claw_raw_event.
- Reject generic ACK in claw_raw_event if waiting_cmd is for another branch.
- Wrap all branches under single cmd_lock guard in claw_raw_event.
- Reject generic ACK in claw_raw_event if waiting_cmd is for another branch.
- Don't close hid devices that couldn't have been opened.
- Ensure led_classdev is unregistered if adding attribute group fails.
- Reorder remove actions to ensure no use-after free or rearming cleared flags.
v7: https://lore.kernel.org/linux-input/[email protected]/
- Use smp_[store_release|load_acquire] pattern for checking gamepad_registered and rgb_registered to avoid possible races during teardown.
- Reorder reinit_completion in claw_hw_output_report to avoid race
with possible incoming ACKs.
- Reorder cancel_delayed_work_sync to ensure setup can't be re-armed
after cancel.
- Reset command state machine if hw_output_report has an error.
- Add comments to (hopefully) silence sashinko-bot warnings about the
use of endpoint matching and the impossible scenario of switching to
the alternate endpoint from userspace while the driver is bound.
- Don't use spinlock_irqsave when already in irq context.
- Add profile_lock for read/write profile_pending.
- Use struct for mkey reports and rumble reports, following the
pattern established by rgb reports previously.
- Add gating to cfg_setup_fn, allowing either gamepad settings or rgb settings to populate if the other fails for any reason.
- Match on write address for rumble and mkey reports to prevent late
ACK from causing synchronization errors.

v6: https://lore.kernel.org/linux-input/[email protected]/
- Add send/ack pattern to ensure synchronous acks.
- Use spinlock_irqsave instead of mutex for read/write MODE event
data.
- add select NEW_LEDS to kconfig.
- Make all timeouts 25ms to ensure at least 2 jiffies in a 100Hz
config.
- Gate all attribute show/store functions with gamepad_registered or rgb_registered, enabling use of devm_device_add_group and ending
the need to hold a mutex during remove.
- Don't set gamepad_mode on resume, MCU preserves state.
- Ensure all count variables are checked for > 0 characters before
setting buf - 1 to \n.
- Re-arm cfg_setup in resume if it was canceled in an early suspend.
- Remove duplicated argv_free macro.
- Add spinlock_irqsave vice mutex for read/write access on attribute variables.
v5: https://lore.kernel.org/linux-input/[email protected]/
- Swap disabled & combination mkeys_function enum values.
- Fix bug introduced in v5 where claw_buttons_store would return
-EINVAL on all valid key entries.
- Ensure mode_mutex is properly init.
- Ensure claw_remove is calling hid_hw_close and not hid_hw_stop for
all paths.
- Ensure adding "DISABLED" key to valid entries is done in the correct patch.
- Re-enable sending an empty string to clear button mappings in
addition to setting DISABLED.
- Move adding the RGB device into cfg_setup to prevent led core
attributes from being written to prior to setup completing.
- Ensure frame_lock is properly init.
- Change variable names in RGB functions from frame and zone to f and
z respectively to fit all scoped_guard actions in 100 columns.
v4: https://lore.kernel.org/linux-input/[email protected]/
- Add msi_suspend/claw_suspend.
- Reorder claw_remove to cancel all work before removing sysfs.
- Add mutex lock for removing sysfs attributes.
- Add mutex lock for MODE command data read/write.
- Change dev_warn to dev_dbg in claw_profile_event.
- use __free with DEFINE_FREE macro for argv instead of manually
running argv_free, cleaining up scoped_guard goto.
- Fix frame_calc validity check to use >=.
- Use spinlock instead of mutex in raw_event and related attribute
_store function.
- Ensure delayed work is canceled in suspend & canceled before sysfs attribute removal.
v3: https://lore.kernel.org/linux-input/[email protected]/
- Add mutex for read/write if rgb frame data.
- Ensure claw_hw_output_report is properly guarded.
- Remove setting rgb_frame_count when reading rgb profiles as it always returns garbage data.
- Ensure rgb_speed is getting drvdata from a valid lookup (not hdev).
- Use scoped_guard where necessary.
- Reoder claw_probe to ensure all mutex, completion, and variable assignments are in place prior to setting drvdata.
- Ensure gamepad_mode is set to a valid enum value in claw_probe.
v2: https://lore.kernel.org/linux-input/[email protected]/
- Use mutexes to guard SYNC_TO_ROM calls and pending_profile calls.
- Rename driver to hid-msi and add generic entrypoints for probe/resume/remove that call claw specific functions in order to
future proof the driver for other MSI HID interfaces.
- Fix various bugs and formatting issues.
v1: https://lore.kernel.org/linux-input/[email protected]/

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

 MAINTAINERS           |    6 +
 drivers/hid/Kconfig   |   13 +
 drivers/hid/Makefile  |    1 +
 drivers/hid/hid-ids.h |    5 +
 drivers/hid/hid-msi.c | 2054 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 2079 insertions(+)
 create mode 100644 drivers/hid/hid-msi.c
Signature · 1 line
2.54.0