1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * HID driver for MSI Claw Handheld PC gamepads. 4 * 5 * Provides configuration support for the MSI Claw series of handheld PC 6 * gamepads. Multiple iterations of the device firmware has led to some 7 * quirks for how certain attributes are handled. The original firmware 8 * did not support remapping of the M1 (right) and M2 (left) rear paddles. 9 * Additionally, the MCU RAM address for writing configuration data has 10 * changed twice. Checks are done during probe to enumerate these variances. 11 * 12 * Copyright (c) 2026 Zhouwang Huang <honjow311@gmail.com> 13 * Copyright (c) 2026 Denis Benato <denis.benato@linux.dev> 14 * Copyright (c) 2026 Valve Corporation 15 */ 16 17 #include <linux/array_size.h> 18 #include <linux/cleanup.h> 19 #include <linux/completion.h> 20 #include <linux/container_of.h> 21 #include <linux/delay.h> 22 #include <linux/device.h> 23 #include <linux/hid.h> 24 #include <linux/kobject.h> 25 #include <linux/led-class-multicolor.h> 26 #include <linux/leds.h> 27 #include <linux/module.h> 28 #include <linux/mutex.h> 29 #include <linux/pm.h> 30 #include <linux/spinlock.h> 31 #include <linux/sysfs.h> 32 #include <linux/types.h> 33 #include <linux/unaligned.h> 34 #include <linux/usb.h> 35 #include <linux/workqueue.h> 36 37 #include "hid-ids.h" 38 39 #define CLAW_OUTPUT_REPORT_ID 0x0f 40 #define CLAW_INPUT_REPORT_ID 0x10 41 42 #define CLAW_PACKET_SIZE 64 43 44 #define CLAW_DINPUT_CFG_INTF_IN 0x82 45 #define CLAW_XINPUT_CFG_INTF_IN 0x83 46 47 #define CLAW_KEYS_MAX 5 48 49 #define CLAW_RGB_ZONES 9 50 #define CLAW_RGB_MAX_FRAMES 8 51 #define CLAW_RGB_FRAME_OFFSET 0x24 52 53 enum claw_command_index { 54 CLAW_COMMAND_TYPE_NONE = 0x00, 55 CLAW_COMMAND_TYPE_READ_PROFILE = 0x04, 56 CLAW_COMMAND_TYPE_READ_PROFILE_ACK = 0x05, 57 CLAW_COMMAND_TYPE_ACK = 0x06, 58 CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA = 0x21, 59 CLAW_COMMAND_TYPE_SYNC_TO_ROM = 0x22, 60 CLAW_COMMAND_TYPE_SWITCH_MODE = 0x24, 61 CLAW_COMMAND_TYPE_READ_GAMEPAD_MODE = 0x26, 62 CLAW_COMMAND_TYPE_GAMEPAD_MODE_ACK = 0x27, 63 CLAW_COMMAND_TYPE_RESET_DEVICE = 0x28, 64 }; 65 66 enum claw_gamepad_mode_index { 67 CLAW_GAMEPAD_MODE_XINPUT = 0x01, 68 CLAW_GAMEPAD_MODE_DINPUT = 0x02, 69 CLAW_GAMEPAD_MODE_DESKTOP = 0x04, 70 }; 71 72 static const char * const claw_gamepad_mode_text[] = { 73 [CLAW_GAMEPAD_MODE_XINPUT] = "xinput", 74 [CLAW_GAMEPAD_MODE_DINPUT] = "dinput", 75 [CLAW_GAMEPAD_MODE_DESKTOP] = "desktop", 76 }; 77 78 enum claw_profile_ack_pending { 79 CLAW_NO_PENDING, 80 CLAW_M1_PENDING, 81 CLAW_M2_PENDING, 82 CLAW_RGB_PENDING, 83 CLAW_RUMBLE_LEFT_PENDING, 84 CLAW_RUMBLE_RIGHT_PENDING, 85 }; 86 87 enum claw_key_index { 88 CLAW_KEY_M1, 89 CLAW_KEY_M2, 90 }; 91 92 enum claw_mkeys_function_index { 93 CLAW_MKEY_FUNCTION_MACRO, 94 CLAW_MKEY_FUNCTION_DISABLED, 95 CLAW_MKEY_FUNCTION_COMBO, 96 }; 97 98 enum claw_mode_field { 99 CLAW_FIELD_GAMEPAD_MODE, 100 CLAW_FIELD_MKEYS_FUNCTION, 101 }; 102 103 static const char * const claw_mkeys_function_text[] = { 104 [CLAW_MKEY_FUNCTION_MACRO] = "macro", 105 [CLAW_MKEY_FUNCTION_DISABLED] = "disabled", 106 [CLAW_MKEY_FUNCTION_COMBO] = "combination", 107 }; 108 109 static const struct { 110 u8 code; 111 const char *name; 112 } claw_button_mapping_key_map[] = { 113 /* Gamepad buttons */ 114 { 0x01, "ABS_HAT0Y_UP" }, 115 { 0x02, "ABS_HAT0Y_DOWN" }, 116 { 0x03, "ABS_HAT0X_LEFT" }, 117 { 0x04, "ABS_HAT0X_RIGHT" }, 118 { 0x05, "BTN_TL" }, 119 { 0x06, "BTN_TR" }, 120 { 0x07, "BTN_THUMBL" }, 121 { 0x08, "BTN_THUMBR" }, 122 { 0x09, "BTN_SOUTH" }, 123 { 0x0a, "BTN_EAST" }, 124 { 0x0b, "BTN_NORTH" }, 125 { 0x0c, "BTN_WEST" }, 126 { 0x0d, "BTN_MODE" }, 127 { 0x0e, "BTN_SELECT" }, 128 { 0x0f, "BTN_START" }, 129 { 0x13, "BTN_TL2"}, 130 { 0x14, "BTN_TR2"}, 131 { 0x15, "ABS_Y_UP"}, 132 { 0x16, "ABS_Y_DOWN"}, 133 { 0x17, "ABS_X_LEFT"}, 134 { 0x18, "ABS_X_RIGHT"}, 135 { 0x19, "ABS_RY_UP"}, 136 { 0x1a, "ABS_RY_DOWN"}, 137 { 0x1b, "ABS_RX_LEFT"}, 138 { 0x1c, "ABS_RX_RIGHT"}, 139 /* Keyboard keys */ 140 { 0x32, "KEY_ESC" }, 141 { 0x33, "KEY_F1" }, 142 { 0x34, "KEY_F2" }, 143 { 0x35, "KEY_F3" }, 144 { 0x36, "KEY_F4" }, 145 { 0x37, "KEY_F5" }, 146 { 0x38, "KEY_F6" }, 147 { 0x39, "KEY_F7" }, 148 { 0x3a, "KEY_F8" }, 149 { 0x3b, "KEY_F9" }, 150 { 0x3c, "KEY_F10" }, 151 { 0x3d, "KEY_F11" }, 152 { 0x3e, "KEY_F12" }, 153 { 0x3f, "KEY_GRAVE" }, 154 { 0x40, "KEY_1" }, 155 { 0x41, "KEY_2" }, 156 { 0x42, "KEY_3" }, 157 { 0x43, "KEY_4" }, 158 { 0x44, "KEY_5" }, 159 { 0x45, "KEY_6" }, 160 { 0x46, "KEY_7" }, 161 { 0x47, "KEY_8" }, 162 { 0x48, "KEY_9" }, 163 { 0x49, "KEY_0" }, 164 { 0x4a, "KEY_MINUS" }, 165 { 0x4b, "KEY_EQUAL" }, 166 { 0x4c, "KEY_BACKSPACE" }, 167 { 0x4d, "KEY_TAB" }, 168 { 0x4e, "KEY_Q" }, 169 { 0x4f, "KEY_W" }, 170 { 0x50, "KEY_E" }, 171 { 0x51, "KEY_R" }, 172 { 0x52, "KEY_T" }, 173 { 0x53, "KEY_Y" }, 174 { 0x54, "KEY_U" }, 175 { 0x55, "KEY_I" }, 176 { 0x56, "KEY_O" }, 177 { 0x57, "KEY_P" }, 178 { 0x58, "KEY_LEFTBRACE" }, 179 { 0x59, "KEY_RIGHTBRACE" }, 180 { 0x5a, "KEY_BACKSLASH" }, 181 { 0x5b, "KEY_CAPSLOCK" }, 182 { 0x5c, "KEY_A" }, 183 { 0x5d, "KEY_S" }, 184 { 0x5e, "KEY_D" }, 185 { 0x5f, "KEY_F" }, 186 { 0x60, "KEY_G" }, 187 { 0x61, "KEY_H" }, 188 { 0x62, "KEY_J" }, 189 { 0x63, "KEY_K" }, 190 { 0x64, "KEY_L" }, 191 { 0x65, "KEY_SEMICOLON" }, 192 { 0x66, "KEY_APOSTROPHE" }, 193 { 0x67, "KEY_ENTER" }, 194 { 0x68, "KEY_LEFTSHIFT" }, 195 { 0x69, "KEY_Z" }, 196 { 0x6a, "KEY_X" }, 197 { 0x6b, "KEY_C" }, 198 { 0x6c, "KEY_V" }, 199 { 0x6d, "KEY_B" }, 200 { 0x6e, "KEY_N" }, 201 { 0x6f, "KEY_M" }, 202 { 0x70, "KEY_COMMA" }, 203 { 0x71, "KEY_DOT" }, 204 { 0x72, "KEY_SLASH" }, 205 { 0x73, "KEY_RIGHTSHIFT" }, 206 { 0x74, "KEY_LEFTCTRL" }, 207 { 0x75, "KEY_LEFTMETA" }, 208 { 0x76, "KEY_LEFTALT" }, 209 { 0x77, "KEY_SPACE" }, 210 { 0x78, "KEY_RIGHTALT" }, 211 { 0x79, "KEY_RIGHTCTRL" }, 212 { 0x7a, "KEY_INSERT" }, 213 { 0x7b, "KEY_HOME" }, 214 { 0x7c, "KEY_PAGEUP" }, 215 { 0x7d, "KEY_DELETE" }, 216 { 0x7e, "KEY_END" }, 217 { 0x7f, "KEY_PAGEDOWN" }, 218 { 0x8a, "KEY_KPENTER" }, 219 { 0x8b, "KEY_KP0" }, 220 { 0x8c, "KEY_KP1" }, 221 { 0x8d, "KEY_KP2" }, 222 { 0x8e, "KEY_KP3" }, 223 { 0x8f, "KEY_KP4" }, 224 { 0x90, "KEY_KP5" }, 225 { 0x91, "KEY_KP6" }, 226 { 0x92, "KEY_KP7" }, 227 { 0x93, "KEY_KP8" }, 228 { 0x94, "KEY_KP9" }, 229 { 0x95, "MD_PLAY" }, 230 { 0x96, "MD_STOP" }, 231 { 0x97, "MD_NEXT" }, 232 { 0x98, "MD_PREV" }, 233 { 0x99, "MD_VOL_UP" }, 234 { 0x9a, "MD_VOL_DOWN" }, 235 { 0x9b, "MD_VOL_MUTE" }, 236 { 0x9c, "KEY_F23" }, 237 /* Mouse events */ 238 { 0xc8, "BTN_LEFT" }, 239 { 0xc9, "BTN_MIDDLE" }, 240 { 0xca, "BTN_RIGHT" }, 241 { 0xcb, "BTN_SIDE" }, 242 { 0xcc, "BTN_EXTRA" }, 243 { 0xcd, "REL_WHEEL_UP" }, 244 { 0xce, "REL_WHEEL_DOWN" }, 245 { 0xff, "DISABLED" }, 246 }; 247 248 enum claw_rgb_effect_index { 249 CLAW_RGB_EFFECT_MONOCOLOR, 250 CLAW_RGB_EFFECT_BREATHE, 251 CLAW_RGB_EFFECT_CHROMA, 252 CLAW_RGB_EFFECT_RAINBOW, 253 CLAW_RGB_EFFECT_FROSTFIRE, 254 }; 255 256 static const char * const claw_rgb_effect_text[] = { 257 [CLAW_RGB_EFFECT_MONOCOLOR] = "monocolor", 258 [CLAW_RGB_EFFECT_BREATHE] = "breathe", 259 [CLAW_RGB_EFFECT_CHROMA] = "chroma", 260 [CLAW_RGB_EFFECT_RAINBOW] = "rainbow", 261 [CLAW_RGB_EFFECT_FROSTFIRE] = "frostfire", 262 }; 263 264 static const u16 button_mapping_addr_old[] = { 265 0x007a, /* M1 */ 266 0x011f, /* M2 */ 267 }; 268 269 static const u16 button_mapping_addr_new[] = { 270 0x00bb, /* M1 */ 271 0x0164, /* M2 */ 272 }; 273 274 static const u16 rgb_addr_old = 0x01fa; 275 static const u16 rgb_addr_new = 0x024a; 276 277 static const u16 rumble_addr[] = { 278 0x0022, /* left */ 279 0x0023, /* right */ 280 }; 281 282 struct claw_command_report { 283 u8 report_id; 284 u8 padding[2]; 285 u8 header_tail; 286 u8 cmd; 287 u8 data[59]; 288 } __packed; 289 290 struct claw_profile_report { 291 u8 profile; 292 __be16 read_addr; 293 } __packed; 294 295 struct claw_mkey_report { 296 struct claw_profile_report; 297 u8 padding_0; 298 u8 padding_1; 299 u8 padding_2; 300 u8 codes[5]; 301 } __packed; 302 303 struct rgb_zone { 304 u8 red; 305 u8 green; 306 u8 blue; 307 }; 308 309 struct rgb_frame { 310 struct rgb_zone zone[CLAW_RGB_ZONES]; 311 }; 312 313 struct claw_rgb_report { 314 struct claw_profile_report; 315 u8 frame_bytes; 316 u8 padding; 317 u8 frame_count; 318 u8 state; /* Always 0x09 */ 319 u8 speed; 320 u8 brightness; 321 struct rgb_frame zone_data; 322 } __packed; 323 324 struct claw_rumble_report { 325 struct claw_profile_report; 326 u8 padding; 327 u8 intensity; 328 } __packed; 329 330 struct claw_drvdata { 331 /* MCU General Variables */ 332 enum claw_profile_ack_pending profile_pending; 333 struct completion orphan_ack_complete; 334 struct completion send_cmd_complete; 335 struct delayed_work cfg_resume; 336 struct delayed_work cfg_setup; 337 spinlock_t registration_lock; /* Lock for registration read/write */ 338 struct mutex profile_mutex; /* mutex for profile_pending calls */ 339 spinlock_t profile_lock; /* Lock for profile_pending read/write */ 340 struct hid_device *hdev; 341 bool orphan_ack_pending; 342 struct mutex cfg_mutex; /* mutex for synchronous data */ 343 struct mutex rom_mutex; /* mutex for SYNC_TO_ROM calls */ 344 spinlock_t cmd_lock; /* Lock for cmd data read/write */ 345 u8 waiting_cmd; 346 int cmd_status; 347 u16 bcd_device; 348 u8 ep; 349 350 /* Gamepad Variables */ 351 enum claw_mkeys_function_index mkeys_function; 352 enum claw_gamepad_mode_index gamepad_mode; 353 u8 m1_codes[CLAW_KEYS_MAX]; 354 u8 m2_codes[CLAW_KEYS_MAX]; 355 u8 rumble_intensity_right; 356 u8 rumble_intensity_left; 357 const u16 *bmap_addr; 358 spinlock_t rumble_lock; /* lock for rumble_intensity read/write */ 359 spinlock_t mode_lock; /* Lock for mode data read/write */ 360 bool rumble_support; 361 bool gp_registered; 362 bool bmap_support; 363 364 /* RGB Variables */ 365 struct rgb_frame rgb_frames[CLAW_RGB_MAX_FRAMES]; 366 enum claw_rgb_effect_index rgb_effect; 367 struct led_classdev_mc led_mc; 368 struct delayed_work rgb_queue; 369 spinlock_t frame_lock; /* lock for rgb_frames read/write */ 370 bool rgb_registered; 371 u8 rgb_frame_count; 372 bool rgb_enabled; 373 u8 rgb_speed; 374 u16 rgb_addr; 375 }; 376 377 static int get_endpoint_address(struct hid_device *hdev) 378 { 379 struct usb_host_endpoint *ep; 380 struct usb_interface *intf; 381 382 intf = to_usb_interface(hdev->dev.parent); 383 ep = intf->cur_altsetting->endpoint; 384 if (ep) 385 return ep->desc.bEndpointAddress; 386 387 return -ENODEV; 388 } 389 390 static int claw_gamepad_mode_event(struct claw_drvdata *drvdata, 391 struct claw_command_report *cmd_rep) 392 { 393 if (cmd_rep->data[0] >= ARRAY_SIZE(claw_gamepad_mode_text) || 394 !claw_gamepad_mode_text[cmd_rep->data[0]] || 395 cmd_rep->data[1] >= ARRAY_SIZE(claw_mkeys_function_text)) 396 return -EINVAL; 397 398 scoped_guard(spinlock_irqsave, &drvdata->mode_lock) { 399 drvdata->gamepad_mode = cmd_rep->data[0]; 400 drvdata->mkeys_function = cmd_rep->data[1]; 401 } 402 403 return 0; 404 } 405 406 static int claw_profile_event(struct claw_drvdata *drvdata, struct claw_command_report *cmd_rep) 407 { 408 enum claw_profile_ack_pending profile; 409 struct claw_rumble_report *rumble; 410 struct claw_mkey_report *mkeys; 411 struct claw_rgb_report *frame; 412 u16 rgb_addr, read_addr; 413 u8 *codes, key, f_idx; 414 u16 frame_calc; 415 int i; 416 417 scoped_guard(spinlock_irqsave, &drvdata->profile_lock) 418 profile = drvdata->profile_pending; 419 420 switch (profile) { 421 case CLAW_M1_PENDING: 422 case CLAW_M2_PENDING: 423 key = (profile == CLAW_M1_PENDING) ? CLAW_KEY_M1 : CLAW_KEY_M2; 424 mkeys = (struct claw_mkey_report *)cmd_rep->data; 425 if (be16_to_cpu(mkeys->read_addr) != drvdata->bmap_addr[key]) 426 return -EAGAIN; 427 codes = (profile == CLAW_M1_PENDING) ? drvdata->m1_codes : drvdata->m2_codes; 428 for (i = 0; i < CLAW_KEYS_MAX; i++) 429 codes[i] = (mkeys->codes[i]); 430 break; 431 case CLAW_RGB_PENDING: 432 frame = (struct claw_rgb_report *)cmd_rep->data; 433 rgb_addr = drvdata->rgb_addr; 434 read_addr = be16_to_cpu(frame->read_addr); 435 436 if (read_addr < drvdata->rgb_addr) 437 return -EAGAIN; 438 439 frame_calc = (read_addr - rgb_addr) / CLAW_RGB_FRAME_OFFSET; 440 if (frame_calc >= CLAW_RGB_MAX_FRAMES) { 441 dev_err(&drvdata->hdev->dev, "Got unsupported frame index: %x\n", 442 frame_calc); 443 return -EAGAIN; 444 } 445 f_idx = frame_calc; 446 447 scoped_guard(spinlock_irqsave, &drvdata->frame_lock) { 448 memcpy(&drvdata->rgb_frames[f_idx], &frame->zone_data, 449 sizeof(struct rgb_frame)); 450 451 /* Only use frame 0 for remaining variable assignment */ 452 if (f_idx != 0) 453 break; 454 455 drvdata->rgb_speed = frame->speed; 456 drvdata->led_mc.led_cdev.brightness = frame->brightness; 457 drvdata->led_mc.subled_info[0].intensity = frame->zone_data.zone[0].red; 458 drvdata->led_mc.subled_info[1].intensity = frame->zone_data.zone[0].green; 459 drvdata->led_mc.subled_info[2].intensity = frame->zone_data.zone[0].blue; 460 } 461 462 break; 463 case CLAW_RUMBLE_LEFT_PENDING: 464 rumble = (struct claw_rumble_report *)cmd_rep->data; 465 if (be16_to_cpu(rumble->read_addr) != rumble_addr[0]) 466 return -EAGAIN; 467 scoped_guard(spinlock_irqsave, &drvdata->rumble_lock) 468 drvdata->rumble_intensity_left = rumble->intensity; 469 break; 470 case CLAW_RUMBLE_RIGHT_PENDING: 471 rumble = (struct claw_rumble_report *)cmd_rep->data; 472 if (be16_to_cpu(rumble->read_addr) != rumble_addr[1]) 473 return -EAGAIN; 474 scoped_guard(spinlock_irqsave, &drvdata->rumble_lock) 475 drvdata->rumble_intensity_right = rumble->intensity; 476 break; 477 default: 478 dev_dbg(&drvdata->hdev->dev, 479 "Got profile event without changes pending from command: %x\n", 480 cmd_rep->cmd); 481 return -EINVAL; 482 } 483 scoped_guard(spinlock_irqsave, &drvdata->profile_lock) 484 drvdata->profile_pending = CLAW_NO_PENDING; 485 486 return 0; 487 } 488 489 static int claw_raw_event(struct claw_drvdata *drvdata, struct hid_report *report, 490 u8 *data, int size) 491 { 492 struct claw_command_report *cmd_rep; 493 int ret = 0; 494 495 if (size != CLAW_PACKET_SIZE) 496 return 0; 497 498 cmd_rep = (struct claw_command_report *)data; 499 500 if (cmd_rep->report_id != CLAW_INPUT_REPORT_ID || cmd_rep->header_tail != 0x3c) 501 return 0; 502 503 dev_dbg(&drvdata->hdev->dev, "Rx data as raw input report: [%*ph]\n", 504 CLAW_PACKET_SIZE, data); 505 506 guard(spinlock_irqsave)(&drvdata->cmd_lock); 507 switch (cmd_rep->cmd) { 508 case CLAW_COMMAND_TYPE_GAMEPAD_MODE_ACK: 509 ret = claw_gamepad_mode_event(drvdata, cmd_rep); 510 if (drvdata->waiting_cmd == CLAW_COMMAND_TYPE_READ_GAMEPAD_MODE) { 511 drvdata->cmd_status = ret; 512 complete(&drvdata->send_cmd_complete); 513 } 514 515 break; 516 case CLAW_COMMAND_TYPE_READ_PROFILE_ACK: 517 ret = claw_profile_event(drvdata, cmd_rep); 518 /* Stale address received, ignore and keep waiting */ 519 if (ret == -EAGAIN) 520 return 0; 521 if (drvdata->waiting_cmd == CLAW_COMMAND_TYPE_READ_PROFILE) { 522 drvdata->cmd_status = ret; 523 complete(&drvdata->send_cmd_complete); 524 } 525 526 break; 527 case CLAW_COMMAND_TYPE_ACK: 528 if (drvdata->orphan_ack_pending) { 529 drvdata->orphan_ack_pending = false; 530 complete(&drvdata->orphan_ack_complete); 531 break; 532 } 533 534 if (drvdata->waiting_cmd == CLAW_COMMAND_TYPE_NONE) { 535 dev_warn(&drvdata->hdev->dev, "Got unexpected ACK from MCU, ignoring\n"); 536 break; 537 } 538 539 drvdata->cmd_status = 0; 540 complete(&drvdata->send_cmd_complete); 541 542 dev_dbg(&drvdata->hdev->dev, "Waiting CMD: %x\n", drvdata->waiting_cmd); 543 544 break; 545 default: 546 dev_dbg(&drvdata->hdev->dev, "Unknown command: %x\n", cmd_rep->cmd); 547 return 0; 548 } 549 550 return ret; 551 } 552 553 static int msi_raw_event(struct hid_device *hdev, struct hid_report *report, 554 u8 *data, int size) 555 { 556 struct claw_drvdata *drvdata = hid_get_drvdata(hdev); 557 558 if (!drvdata || (drvdata->ep != CLAW_XINPUT_CFG_INTF_IN && 559 drvdata->ep != CLAW_DINPUT_CFG_INTF_IN)) 560 return 0; 561 562 return claw_raw_event(drvdata, report, data, size); 563 } 564 565 /* Caller must hold drvdata->cfg_mutex. */ 566 static int __claw_hw_output_report(struct hid_device *hdev, u8 index, u8 *data, 567 size_t len, unsigned int timeout) 568 { 569 unsigned char *dmabuf __free(kfree) = NULL; 570 u8 header[] = { CLAW_OUTPUT_REPORT_ID, 0, 0, 0x3c, index }; 571 struct claw_drvdata *drvdata = hid_get_drvdata(hdev); 572 size_t header_size = ARRAY_SIZE(header); 573 bool orphaned; 574 int ret; 575 576 lockdep_assert_held(&drvdata->cfg_mutex); 577 578 /* If expecting an orphan ack, hold next event until MCU has time to clear it */ 579 scoped_guard(spinlock_irqsave, &drvdata->cmd_lock) 580 orphaned = drvdata->orphan_ack_pending; 581 582 if (orphaned) { 583 wait_for_completion_timeout(&drvdata->orphan_ack_complete, msecs_to_jiffies(25)); 584 scoped_guard(spinlock_irqsave, &drvdata->cmd_lock) 585 drvdata->orphan_ack_pending = false; 586 } 587 588 if (header_size + len > CLAW_PACKET_SIZE) 589 return -EINVAL; 590 591 /* We can't use a devm_alloc reusable buffer without side effects during suspend */ 592 dmabuf = kzalloc(CLAW_PACKET_SIZE, GFP_KERNEL); 593 if (!dmabuf) 594 return -ENOMEM; 595 596 memcpy(dmabuf, header, header_size); 597 if (data && len) 598 memcpy(dmabuf + header_size, data, len); 599 600 reinit_completion(&drvdata->send_cmd_complete); 601 602 scoped_guard(spinlock_irqsave, &drvdata->cmd_lock) { 603 if (timeout) { 604 drvdata->waiting_cmd = index; 605 drvdata->cmd_status = -ETIMEDOUT; 606 } else { 607 reinit_completion(&drvdata->orphan_ack_complete); 608 drvdata->waiting_cmd = CLAW_COMMAND_TYPE_NONE; 609 drvdata->orphan_ack_pending = true; 610 } 611 } 612 613 dev_dbg(&hdev->dev, "Send data as raw output report: [%*ph]\n", 614 CLAW_PACKET_SIZE, dmabuf); 615 616 ret = hid_hw_output_report(hdev, dmabuf, CLAW_PACKET_SIZE); 617 if (ret < 0) 618 goto err; 619 620 ret = ret == CLAW_PACKET_SIZE ? 0 : -EIO; 621 if (ret) 622 goto err; 623 624 if (timeout) { 625 ret = wait_for_completion_interruptible_timeout(&drvdata->send_cmd_complete, 626 msecs_to_jiffies(timeout)); 627 628 dev_dbg(&hdev->dev, "Remaining timeout: %u\n", ret); 629 ret = ret > 0 ? drvdata->cmd_status : ret ?: -EBUSY; 630 if (ret) 631 goto err; 632 } 633 634 scoped_guard(spinlock_irqsave, &drvdata->cmd_lock) 635 drvdata->waiting_cmd = CLAW_COMMAND_TYPE_NONE; 636 637 return ret; 638 639 err: 640 scoped_guard(spinlock_irqsave, &drvdata->cmd_lock) { 641 drvdata->waiting_cmd = CLAW_COMMAND_TYPE_NONE; 642 drvdata->orphan_ack_pending = false; 643 } 644 return ret; 645 } 646 647 static int claw_hw_output_report(struct hid_device *hdev, u8 index, u8 *data, 648 size_t len, unsigned int timeout) 649 { 650 struct claw_drvdata *drvdata = hid_get_drvdata(hdev); 651 652 guard(mutex)(&drvdata->cfg_mutex); 653 return __claw_hw_output_report(hdev, index, data, len, timeout); 654 } 655 656 static int claw_switch_mode(struct hid_device *hdev, enum claw_mode_field field, u8 val) 657 { 658 struct claw_drvdata *drvdata = hid_get_drvdata(hdev); 659 u8 data[2]; 660 661 guard(mutex)(&drvdata->cfg_mutex); 662 663 scoped_guard(spinlock_irqsave, &drvdata->mode_lock) { 664 switch (field) { 665 case CLAW_FIELD_GAMEPAD_MODE: 666 data[0] = val; 667 data[1] = drvdata->mkeys_function; 668 break; 669 case CLAW_FIELD_MKEYS_FUNCTION: 670 data[0] = drvdata->gamepad_mode; 671 data[1] = val; 672 break; 673 } 674 } 675 676 return __claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_SWITCH_MODE, data, 677 ARRAY_SIZE(data), 0); 678 } 679 680 static ssize_t gamepad_mode_store(struct device *dev, struct device_attribute *attr, 681 const char *buf, size_t count) 682 { 683 struct hid_device *hdev = to_hid_device(dev); 684 struct claw_drvdata *drvdata = hid_get_drvdata(hdev); 685 int i, ret = -EINVAL; 686 687 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) { 688 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 689 if (!smp_load_acquire(&drvdata->gp_registered)) 690 return -ENODEV; 691 } 692 693 for (i = 0; i < ARRAY_SIZE(claw_gamepad_mode_text); i++) { 694 if (claw_gamepad_mode_text[i] && sysfs_streq(buf, claw_gamepad_mode_text[i])) { 695 ret = i; 696 break; 697 } 698 } 699 if (ret < 0) 700 return ret; 701 702 ret = claw_switch_mode(hdev, CLAW_FIELD_GAMEPAD_MODE, ret); 703 if (ret) 704 return ret; 705 706 return count; 707 } 708 709 static ssize_t gamepad_mode_show(struct device *dev, 710 struct device_attribute *attr, char *buf) 711 { 712 struct hid_device *hdev = to_hid_device(dev); 713 struct claw_drvdata *drvdata = hid_get_drvdata(hdev); 714 int ret, i; 715 716 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) { 717 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 718 if (!smp_load_acquire(&drvdata->gp_registered)) 719 return -ENODEV; 720 } 721 722 ret = claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_READ_GAMEPAD_MODE, NULL, 0, 25); 723 if (ret) 724 return ret; 725 726 scoped_guard(spinlock_irqsave, &drvdata->mode_lock) 727 i = drvdata->gamepad_mode; 728 729 if (!claw_gamepad_mode_text[i] || claw_gamepad_mode_text[i][0] == '\0') 730 return sysfs_emit(buf, "unsupported\n"); 731 732 return sysfs_emit(buf, "%s\n", claw_gamepad_mode_text[i]); 733 } 734 static DEVICE_ATTR_RW(gamepad_mode); 735 736 static ssize_t gamepad_mode_index_show(struct device *dev, 737 struct device_attribute *attr, char *buf) 738 { 739 ssize_t count = 0; 740 int i; 741 742 for (i = 0; i < ARRAY_SIZE(claw_gamepad_mode_text); i++) { 743 if (!claw_gamepad_mode_text[i] || claw_gamepad_mode_text[i][0] == '\0') 744 continue; 745 count += sysfs_emit_at(buf, count, "%s ", claw_gamepad_mode_text[i]); 746 } 747 748 if (count) 749 buf[count - 1] = '\n'; 750 751 return count; 752 } 753 static DEVICE_ATTR_RO(gamepad_mode_index); 754 755 static ssize_t mkeys_function_store(struct device *dev, struct device_attribute *attr, 756 const char *buf, size_t count) 757 { 758 struct hid_device *hdev = to_hid_device(dev); 759 struct claw_drvdata *drvdata = hid_get_drvdata(hdev); 760 int i, ret = -EINVAL; 761 762 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) { 763 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 764 if (!smp_load_acquire(&drvdata->gp_registered)) 765 return -ENODEV; 766 } 767 768 for (i = 0; i < ARRAY_SIZE(claw_mkeys_function_text); i++) { 769 if (claw_mkeys_function_text[i] && sysfs_streq(buf, claw_mkeys_function_text[i])) { 770 ret = i; 771 break; 772 } 773 } 774 if (ret < 0) 775 return ret; 776 777 ret = claw_switch_mode(hdev, CLAW_FIELD_MKEYS_FUNCTION, ret); 778 if (ret) 779 return ret; 780 781 return count; 782 } 783 784 static ssize_t mkeys_function_show(struct device *dev, struct device_attribute *attr, 785 char *buf) 786 { 787 struct hid_device *hdev = to_hid_device(dev); 788 struct claw_drvdata *drvdata = hid_get_drvdata(hdev); 789 int ret, i; 790 791 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) { 792 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 793 if (!smp_load_acquire(&drvdata->gp_registered)) 794 return -ENODEV; 795 } 796 797 ret = claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_READ_GAMEPAD_MODE, NULL, 0, 25); 798 if (ret) 799 return ret; 800 801 scoped_guard(spinlock_irqsave, &drvdata->mode_lock) 802 i = drvdata->mkeys_function; 803 804 if (i >= ARRAY_SIZE(claw_mkeys_function_text)) 805 return sysfs_emit(buf, "unsupported\n"); 806 807 return sysfs_emit(buf, "%s\n", claw_mkeys_function_text[i]); 808 } 809 static DEVICE_ATTR_RW(mkeys_function); 810 811 static ssize_t mkeys_function_index_show(struct device *dev, 812 struct device_attribute *attr, char *buf) 813 { 814 int i, count = 0; 815 816 for (i = 0; i < ARRAY_SIZE(claw_mkeys_function_text); i++) 817 count += sysfs_emit_at(buf, count, "%s ", claw_mkeys_function_text[i]); 818 819 if (count) 820 buf[count - 1] = '\n'; 821 822 return count; 823 } 824 static DEVICE_ATTR_RO(mkeys_function_index); 825 826 static ssize_t reset_store(struct device *dev, struct device_attribute *attr, 827 const char *buf, size_t count) 828 { 829 struct hid_device *hdev = to_hid_device(dev); 830 struct claw_drvdata *drvdata = hid_get_drvdata(hdev); 831 bool val; 832 int ret; 833 834 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) { 835 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 836 if (!smp_load_acquire(&drvdata->gp_registered)) 837 return -ENODEV; 838 } 839 840 ret = kstrtobool(buf, &val); 841 if (ret) 842 return ret; 843 844 if (!val) 845 return -EINVAL; 846 847 ret = claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_RESET_DEVICE, NULL, 0, 0); 848 if (ret) 849 return ret; 850 851 return count; 852 } 853 static DEVICE_ATTR_WO(reset); 854 855 static int mkey_mapping_name_to_code(const char *name) 856 { 857 int i; 858 859 for (i = 0; i < ARRAY_SIZE(claw_button_mapping_key_map); i++) { 860 if (!strcmp(name, claw_button_mapping_key_map[i].name)) 861 return claw_button_mapping_key_map[i].code; 862 } 863 864 return -EINVAL; 865 } 866 867 static const char *mkey_mapping_code_to_name(u8 code) 868 { 869 int i; 870 871 if (code == 0xff) 872 return NULL; 873 874 for (i = 0; i < ARRAY_SIZE(claw_button_mapping_key_map); i++) { 875 if (claw_button_mapping_key_map[i].code == code) 876 return claw_button_mapping_key_map[i].name; 877 } 878 879 return NULL; 880 } 881 882 static int claw_mkey_store(struct device *dev, const char *buf, u8 mkey) 883 { 884 struct hid_device *hdev = to_hid_device(dev); 885 struct claw_drvdata *drvdata = hid_get_drvdata(hdev); 886 struct claw_mkey_report report = { {0x01, cpu_to_be16(drvdata->bmap_addr[mkey])}, 887 0x07, 0x04, 0x00, {0xff, 0xff, 0xff, 0xff, 0xff} }; 888 char **raw_keys __free(argv_free) = NULL; 889 int ret, key_count, i; 890 891 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) { 892 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 893 if (!smp_load_acquire(&drvdata->gp_registered)) 894 return -ENODEV; 895 } 896 897 raw_keys = argv_split(GFP_KERNEL, buf, &key_count); 898 if (!raw_keys) 899 return -ENOMEM; 900 901 if (key_count > CLAW_KEYS_MAX) 902 return -EINVAL; 903 904 if (key_count == 0) 905 goto set_buttons; 906 907 for (i = 0; i < key_count; i++) { 908 ret = mkey_mapping_name_to_code(raw_keys[i]); 909 if (ret < 0) 910 return ret; 911 912 report.codes[i] = ret; 913 } 914 915 set_buttons: 916 scoped_guard(mutex, &drvdata->rom_mutex) { 917 ret = claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA, 918 (u8 *)&report, sizeof(report), 25); 919 if (ret) 920 return ret; 921 /* MCU will not send ACK until the USB transaction completes. ACK is sent 922 * immediately after and will hit the stale state machine, before the next 923 * command re-arms the state machine. Timeout 0 ensures no deadlock waiting 924 * for ACK that ill never come. 925 */ 926 ret = claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_SYNC_TO_ROM, NULL, 0, 0); 927 } 928 929 return ret; 930 } 931 932 static int claw_mkey_show(struct device *dev, char *buf, enum claw_key_index m_key) 933 { 934 struct hid_device *hdev = to_hid_device(dev); 935 struct claw_drvdata *drvdata = hid_get_drvdata(hdev); 936 struct claw_mkey_report report = { {0x01, cpu_to_be16(drvdata->bmap_addr[m_key])}, 0x07 }; 937 int i, ret, count = 0; 938 const char *name; 939 u8 *codes; 940 941 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) { 942 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 943 if (!smp_load_acquire(&drvdata->gp_registered)) 944 return -ENODEV; 945 } 946 947 codes = (m_key == CLAW_KEY_M1) ? drvdata->m1_codes : drvdata->m2_codes; 948 949 guard(mutex)(&drvdata->profile_mutex); 950 scoped_guard(spinlock_irqsave, &drvdata->profile_lock) 951 drvdata->profile_pending = (m_key == CLAW_KEY_M1) ? CLAW_M1_PENDING 952 : CLAW_M2_PENDING; 953 954 ret = claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_READ_PROFILE, 955 (u8 *)&report, sizeof(report), 25); 956 if (ret) 957 return ret; 958 959 for (i = 0; i < CLAW_KEYS_MAX; i++) { 960 name = mkey_mapping_code_to_name(codes[i]); 961 if (name) 962 count += sysfs_emit_at(buf, count, "%s ", name); 963 } 964 965 if (!count) 966 return sysfs_emit(buf, "(not set)\n"); 967 968 buf[count - 1] = '\n'; 969 970 return count; 971 } 972 973 static ssize_t button_m1_store(struct device *dev, struct device_attribute *attr, 974 const char *buf, size_t count) 975 { 976 int ret; 977 978 ret = claw_mkey_store(dev, buf, CLAW_KEY_M1); 979 if (ret) 980 return ret; 981 982 return count; 983 } 984 985 static ssize_t button_m1_show(struct device *dev, struct device_attribute *attr, 986 char *buf) 987 { 988 return claw_mkey_show(dev, buf, CLAW_KEY_M1); 989 } 990 static DEVICE_ATTR_RW(button_m1); 991 992 static ssize_t button_m2_store(struct device *dev, struct device_attribute *attr, 993 const char *buf, size_t count) 994 { 995 int ret; 996 997 ret = claw_mkey_store(dev, buf, CLAW_KEY_M2); 998 if (ret) 999 return ret; 1000 1001 return count; 1002 } 1003 1004 static ssize_t button_m2_show(struct device *dev, struct device_attribute *attr, 1005 char *buf) 1006 { 1007 return claw_mkey_show(dev, buf, CLAW_KEY_M2); 1008 } 1009 static DEVICE_ATTR_RW(button_m2); 1010 1011 static ssize_t button_mapping_options_show(struct device *dev, 1012 struct device_attribute *attr, char *buf) 1013 { 1014 int i, count = 0; 1015 1016 for (i = 0; i < ARRAY_SIZE(claw_button_mapping_key_map); i++) 1017 count += sysfs_emit_at(buf, count, "%s ", claw_button_mapping_key_map[i].name); 1018 1019 if (count) 1020 buf[count - 1] = '\n'; 1021 1022 return count; 1023 } 1024 static DEVICE_ATTR_RO(button_mapping_options); 1025 1026 static ssize_t rumble_intensity_left_store(struct device *dev, 1027 struct device_attribute *attr, 1028 const char *buf, size_t count) 1029 { 1030 struct claw_rumble_report report = { {0x01, cpu_to_be16(rumble_addr[0])}, 0x01 }; 1031 struct hid_device *hdev = to_hid_device(dev); 1032 struct claw_drvdata *drvdata = hid_get_drvdata(hdev); 1033 u8 val; 1034 int ret; 1035 1036 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) { 1037 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 1038 if (!smp_load_acquire(&drvdata->gp_registered)) 1039 return -ENODEV; 1040 } 1041 1042 ret = kstrtou8(buf, 10, &val); 1043 if (ret) 1044 return ret; 1045 1046 if (val > 100) 1047 return -EINVAL; 1048 1049 report.intensity = val; 1050 1051 guard(mutex)(&drvdata->rom_mutex); 1052 ret = claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA, 1053 (u8 *)&report, sizeof(report), 25); 1054 if (ret) 1055 return ret; 1056 1057 /* MCU will not send ACK until the USB transaction completes. ACK is sent 1058 * immediately after and will hit the stale state machine, before the next 1059 * command re-arms the state machine. Timeout 0 ensures no deadlock waiting 1060 * for ACK that ill never come. 1061 */ 1062 ret = claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_SYNC_TO_ROM, NULL, 0, 0); 1063 if (ret) 1064 return ret; 1065 1066 return count; 1067 } 1068 1069 static ssize_t rumble_intensity_left_show(struct device *dev, 1070 struct device_attribute *attr, 1071 char *buf) 1072 { 1073 struct claw_rumble_report report = { {0x01, cpu_to_be16(rumble_addr[0])}, 0x01 }; 1074 struct hid_device *hdev = to_hid_device(dev); 1075 struct claw_drvdata *drvdata = hid_get_drvdata(hdev); 1076 int ret; 1077 u8 val; 1078 1079 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) { 1080 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 1081 if (!smp_load_acquire(&drvdata->gp_registered)) 1082 return -ENODEV; 1083 } 1084 1085 guard(mutex)(&drvdata->profile_mutex); 1086 scoped_guard(spinlock_irqsave, &drvdata->profile_lock) 1087 drvdata->profile_pending = CLAW_RUMBLE_LEFT_PENDING; 1088 ret = claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_READ_PROFILE, 1089 (u8 *)&report, sizeof(report), 25); 1090 if (ret) 1091 return ret; 1092 1093 scoped_guard(spinlock_irqsave, &drvdata->rumble_lock) 1094 val = drvdata->rumble_intensity_left; 1095 1096 return sysfs_emit(buf, "%u\n", val); 1097 } 1098 static DEVICE_ATTR_RW(rumble_intensity_left); 1099 1100 static ssize_t rumble_intensity_right_store(struct device *dev, 1101 struct device_attribute *attr, 1102 const char *buf, size_t count) 1103 { 1104 struct claw_rumble_report report = { {0x01, cpu_to_be16(rumble_addr[1])}, 0x01 }; 1105 struct hid_device *hdev = to_hid_device(dev); 1106 struct claw_drvdata *drvdata = hid_get_drvdata(hdev); 1107 u8 val; 1108 int ret; 1109 1110 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) { 1111 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 1112 if (!smp_load_acquire(&drvdata->gp_registered)) 1113 return -ENODEV; 1114 } 1115 1116 ret = kstrtou8(buf, 10, &val); 1117 if (ret) 1118 return ret; 1119 1120 if (val > 100) 1121 return -EINVAL; 1122 1123 report.intensity = val; 1124 1125 guard(mutex)(&drvdata->rom_mutex); 1126 ret = claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA, 1127 (u8 *)&report, sizeof(report), 25); 1128 if (ret) 1129 return ret; 1130 1131 /* MCU will not send ACK until the USB transaction completes. ACK is sent 1132 * immediately after and will hit the stale state machine, before the next 1133 * command re-arms the state machine. Timeout 0 ensures no deadlock waiting 1134 * for ACK that ill never come. 1135 */ 1136 ret = claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_SYNC_TO_ROM, NULL, 0, 0); 1137 if (ret) 1138 return ret; 1139 1140 return count; 1141 } 1142 1143 static ssize_t rumble_intensity_right_show(struct device *dev, 1144 struct device_attribute *attr, 1145 char *buf) 1146 { 1147 struct claw_rumble_report report = { {0x01, cpu_to_be16(rumble_addr[1])}, 0x01 }; 1148 struct hid_device *hdev = to_hid_device(dev); 1149 struct claw_drvdata *drvdata = hid_get_drvdata(hdev); 1150 int ret; 1151 u8 val; 1152 1153 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) { 1154 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 1155 if (!smp_load_acquire(&drvdata->gp_registered)) 1156 return -ENODEV; 1157 } 1158 1159 guard(mutex)(&drvdata->profile_mutex); 1160 scoped_guard(spinlock_irqsave, &drvdata->profile_lock) 1161 drvdata->profile_pending = CLAW_RUMBLE_RIGHT_PENDING; 1162 ret = claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_READ_PROFILE, 1163 (u8 *)&report, sizeof(report), 25); 1164 if (ret) 1165 return ret; 1166 1167 scoped_guard(spinlock_irqsave, &drvdata->rumble_lock) 1168 val = drvdata->rumble_intensity_right; 1169 1170 return sysfs_emit(buf, "%u\n", val); 1171 } 1172 static DEVICE_ATTR_RW(rumble_intensity_right); 1173 1174 static ssize_t rumble_intensity_range_show(struct device *dev, 1175 struct device_attribute *attr, 1176 char *buf) 1177 { 1178 return sysfs_emit(buf, "0-100\n"); 1179 } 1180 static DEVICE_ATTR_RO(rumble_intensity_range); 1181 1182 static umode_t claw_gamepad_attr_is_visible(struct kobject *kobj, struct attribute *attr, 1183 int n) 1184 { 1185 struct hid_device *hdev = to_hid_device(kobj_to_dev(kobj)); 1186 struct claw_drvdata *drvdata = hid_get_drvdata(hdev); 1187 1188 if (!drvdata) { 1189 dev_warn(&hdev->dev, 1190 "Failed to get drvdata from kobj. Gamepad attributes are not available.\n"); 1191 return 0; 1192 } 1193 1194 /* Always show attrs available on all firmware */ 1195 if (attr == &dev_attr_gamepad_mode.attr || 1196 attr == &dev_attr_gamepad_mode_index.attr || 1197 attr == &dev_attr_mkeys_function.attr || 1198 attr == &dev_attr_mkeys_function_index.attr || 1199 attr == &dev_attr_reset.attr) 1200 return attr->mode; 1201 1202 /* Hide rumble attrs if not supported */ 1203 if (attr == &dev_attr_rumble_intensity_left.attr || 1204 attr == &dev_attr_rumble_intensity_right.attr || 1205 attr == &dev_attr_rumble_intensity_range.attr) 1206 return drvdata->rumble_support ? attr->mode : 0; 1207 1208 /* Hide button mapping attrs if it isn't supported */ 1209 return drvdata->bmap_support ? attr->mode : 0; 1210 } 1211 1212 static struct attribute *claw_gamepad_attrs[] = { 1213 &dev_attr_button_m1.attr, 1214 &dev_attr_button_m2.attr, 1215 &dev_attr_button_mapping_options.attr, 1216 &dev_attr_gamepad_mode.attr, 1217 &dev_attr_gamepad_mode_index.attr, 1218 &dev_attr_mkeys_function.attr, 1219 &dev_attr_mkeys_function_index.attr, 1220 &dev_attr_reset.attr, 1221 &dev_attr_rumble_intensity_left.attr, 1222 &dev_attr_rumble_intensity_right.attr, 1223 &dev_attr_rumble_intensity_range.attr, 1224 NULL, 1225 }; 1226 1227 static const struct attribute_group claw_gamepad_attr_group = { 1228 .attrs = claw_gamepad_attrs, 1229 .is_visible = claw_gamepad_attr_is_visible, 1230 }; 1231 1232 /* Read RGB config from device */ 1233 static int claw_read_rgb_config(struct hid_device *hdev) 1234 { 1235 u8 data[4] = { 0x01, 0x00, 0x00, CLAW_RGB_FRAME_OFFSET }; 1236 struct claw_drvdata *drvdata = hid_get_drvdata(hdev); 1237 u16 read_addr = drvdata->rgb_addr; 1238 size_t len = ARRAY_SIZE(data); 1239 int ret, i; 1240 1241 if (!drvdata->rgb_addr) 1242 return -ENODEV; 1243 1244 /* Loop through all 8 pages of RGB data */ 1245 guard(mutex)(&drvdata->profile_mutex); 1246 for (i = 0; i < CLAW_RGB_MAX_FRAMES; i++) { 1247 scoped_guard(spinlock_irqsave, &drvdata->profile_lock) 1248 drvdata->profile_pending = CLAW_RGB_PENDING; 1249 data[1] = (read_addr >> 8) & 0xff; 1250 data[2] = read_addr & 0x00ff; 1251 ret = claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_READ_PROFILE, data, len, 25); 1252 if (ret) 1253 return ret; 1254 1255 read_addr += CLAW_RGB_FRAME_OFFSET; 1256 } 1257 1258 return 0; 1259 } 1260 1261 /* Send RGB configuration to device */ 1262 static int claw_write_rgb_state(struct claw_drvdata *drvdata) 1263 { 1264 struct claw_rgb_report report = { {0x01, 0}, CLAW_RGB_FRAME_OFFSET, 0x00, 1265 drvdata->rgb_frame_count, 0x09, drvdata->rgb_speed, 1266 drvdata->led_mc.led_cdev.brightness }; 1267 u16 write_addr = drvdata->rgb_addr; 1268 int f, ret; 1269 1270 if (!drvdata->rgb_addr) 1271 return -ENODEV; 1272 1273 if (!drvdata->rgb_frame_count) 1274 return -EINVAL; 1275 1276 guard(mutex)(&drvdata->rom_mutex); 1277 /* Loop through (up to) 8 pages of RGB data */ 1278 for (f = 0; f < drvdata->rgb_frame_count; f++) { 1279 scoped_guard(spinlock_irqsave, &drvdata->frame_lock) 1280 report.zone_data = drvdata->rgb_frames[f]; 1281 1282 /* Set the MCU address to write the frame data to */ 1283 report.read_addr = cpu_to_be16(write_addr); 1284 1285 /* Serialize the rgb_report and write it to MCU */ 1286 ret = claw_hw_output_report(drvdata->hdev, CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA, 1287 (u8 *)&report, sizeof(report), 25); 1288 if (ret) 1289 return ret; 1290 1291 /* Increment the write addr by the offset for the next frame */ 1292 write_addr += CLAW_RGB_FRAME_OFFSET; 1293 } 1294 1295 /* MCU will not send ACK until the USB transaction completes. ACK is sent 1296 * immediately after and will hit the stale state machine, before the next 1297 * command re-arms the state machine. Timeout 0 ensures no deadlock waiting 1298 * for ACK that ill never come. 1299 */ 1300 ret = claw_hw_output_report(drvdata->hdev, CLAW_COMMAND_TYPE_SYNC_TO_ROM, NULL, 0, 0); 1301 1302 return ret; 1303 } 1304 1305 /* Fill all zones with the same color */ 1306 static void claw_frame_fill_solid(struct rgb_frame *frame, struct rgb_zone zone) 1307 { 1308 int z; 1309 1310 for (z = 0; z < CLAW_RGB_ZONES; z++) 1311 frame->zone[z] = zone; 1312 } 1313 1314 /* Apply solid effect (1 frame, no color) */ 1315 static int claw_apply_disabled(struct claw_drvdata *drvdata) 1316 { 1317 struct rgb_zone off = { 0x00, 0x00, 0x00}; 1318 1319 scoped_guard(spinlock_irqsave, &drvdata->frame_lock) { 1320 drvdata->rgb_frame_count = 1; 1321 claw_frame_fill_solid(&drvdata->rgb_frames[0], off); 1322 } 1323 1324 return claw_write_rgb_state(drvdata); 1325 } 1326 1327 /* Apply solid effect (1 frame, all zones same color) */ 1328 static int claw_apply_monocolor(struct claw_drvdata *drvdata) 1329 { 1330 struct mc_subled *subleds = drvdata->led_mc.subled_info; 1331 struct rgb_zone zone = { subleds[0].intensity, subleds[1].intensity, 1332 subleds[2].intensity }; 1333 1334 scoped_guard(spinlock_irqsave, &drvdata->frame_lock) { 1335 drvdata->rgb_frame_count = 1; 1336 claw_frame_fill_solid(&drvdata->rgb_frames[0], zone); 1337 } 1338 1339 return claw_write_rgb_state(drvdata); 1340 } 1341 1342 /* Apply breathe effect (2 frames: color -> off) */ 1343 static int claw_apply_breathe(struct claw_drvdata *drvdata) 1344 { 1345 struct mc_subled *subleds = drvdata->led_mc.subled_info; 1346 struct rgb_zone zone = { subleds[0].intensity, subleds[1].intensity, 1347 subleds[2].intensity }; 1348 static const struct rgb_zone off = { 0, 0, 0 }; 1349 1350 scoped_guard(spinlock_irqsave, &drvdata->frame_lock) { 1351 drvdata->rgb_frame_count = 2; 1352 claw_frame_fill_solid(&drvdata->rgb_frames[0], zone); 1353 claw_frame_fill_solid(&drvdata->rgb_frames[1], off); 1354 } 1355 1356 return claw_write_rgb_state(drvdata); 1357 } 1358 1359 /* Apply chroma effect (6 frames: rainbow cycle, all zones sync) */ 1360 static int claw_apply_chroma(struct claw_drvdata *drvdata) 1361 { 1362 static const struct rgb_zone colors[] = { 1363 {255, 0, 0}, /* red */ 1364 {255, 255, 0}, /* yellow */ 1365 { 0, 255, 0}, /* green */ 1366 { 0, 255, 255}, /* cyan */ 1367 { 0, 0, 255}, /* blue */ 1368 {255, 0, 255}, /* magenta */ 1369 }; 1370 u8 frame_count = ARRAY_SIZE(colors); 1371 int f; 1372 1373 scoped_guard(spinlock_irqsave, &drvdata->frame_lock) { 1374 drvdata->rgb_frame_count = frame_count; 1375 1376 for (f = 0; f < frame_count; f++) 1377 claw_frame_fill_solid(&drvdata->rgb_frames[f], colors[f]); 1378 } 1379 1380 return claw_write_rgb_state(drvdata); 1381 } 1382 1383 /* Apply rainbow effect (4 frames: rotating colors around joysticks) */ 1384 static int claw_apply_rainbow(struct claw_drvdata *drvdata) 1385 { 1386 static const struct rgb_zone colors[] = { 1387 {255, 0, 0}, /* red */ 1388 { 0, 255, 0}, /* green */ 1389 { 0, 255, 255}, /* cyan */ 1390 { 0, 0, 255}, /* blue */ 1391 }; 1392 u8 frame_count = ARRAY_SIZE(colors); 1393 int f, z; 1394 1395 scoped_guard(spinlock_irqsave, &drvdata->frame_lock) { 1396 drvdata->rgb_frame_count = frame_count; 1397 1398 for (f = 0; f < frame_count; f++) { 1399 for (z = 0; z < 4; z++) { 1400 drvdata->rgb_frames[f].zone[z] = colors[(z + f) % 4]; 1401 drvdata->rgb_frames[f].zone[z + 4] = colors[(z + f) % 4]; 1402 } 1403 drvdata->rgb_frames[f].zone[8] = colors[f]; 1404 } 1405 } 1406 1407 return claw_write_rgb_state(drvdata); 1408 } 1409 1410 /* 1411 * Apply frostfire effect (4 frames: fire vs ice rotating) 1412 * Right joystick: fire red -> dark -> ice blue -> dark (clockwise) 1413 * Left joystick: ice blue -> dark -> fire red -> dark (counter-clockwise) 1414 * ABXY: fire red -> dark -> ice blue -> dark 1415 */ 1416 static int claw_apply_frostfire(struct claw_drvdata *drvdata) 1417 { 1418 static const struct rgb_zone colors[] = { 1419 {255, 0, 0}, /* fire red */ 1420 { 0, 0, 0}, /* dark */ 1421 { 0, 0, 255}, /* ice blue */ 1422 { 0, 0, 0}, /* dark */ 1423 }; 1424 u8 frame_count = ARRAY_SIZE(colors); 1425 int f, z; 1426 1427 scoped_guard(spinlock_irqsave, &drvdata->frame_lock) { 1428 drvdata->rgb_frame_count = frame_count; 1429 1430 for (f = 0; f < frame_count; f++) { 1431 for (z = 0; z < 4; z++) { 1432 drvdata->rgb_frames[f].zone[z] = colors[(z + f) % 4]; 1433 drvdata->rgb_frames[f].zone[z + 4] = colors[(z - f + 6) % 4]; 1434 } 1435 drvdata->rgb_frames[f].zone[8] = colors[f]; 1436 } 1437 } 1438 1439 return claw_write_rgb_state(drvdata); 1440 } 1441 1442 /* Apply current state to device */ 1443 static int claw_apply_rgb_state(struct claw_drvdata *drvdata) 1444 { 1445 if (!drvdata->rgb_enabled) 1446 return claw_apply_disabled(drvdata); 1447 1448 switch (drvdata->rgb_effect) { 1449 case CLAW_RGB_EFFECT_MONOCOLOR: 1450 return claw_apply_monocolor(drvdata); 1451 case CLAW_RGB_EFFECT_BREATHE: 1452 return claw_apply_breathe(drvdata); 1453 case CLAW_RGB_EFFECT_CHROMA: 1454 return claw_apply_chroma(drvdata); 1455 case CLAW_RGB_EFFECT_RAINBOW: 1456 return claw_apply_rainbow(drvdata); 1457 case CLAW_RGB_EFFECT_FROSTFIRE: 1458 return claw_apply_frostfire(drvdata); 1459 default: 1460 dev_err(&drvdata->hdev->dev, "No supported rgb_effect selected\n"); 1461 return -EINVAL; 1462 } 1463 } 1464 1465 static void claw_rgb_queue_fn(struct work_struct *work) 1466 { 1467 struct delayed_work *dwork = container_of(work, struct delayed_work, work); 1468 struct claw_drvdata *drvdata = container_of(dwork, struct claw_drvdata, rgb_queue); 1469 int ret; 1470 1471 if (!drvdata) 1472 return; 1473 1474 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) { 1475 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 1476 if (!smp_load_acquire(&drvdata->rgb_registered)) 1477 return; 1478 } 1479 1480 ret = claw_apply_rgb_state(drvdata); 1481 if (ret) 1482 dev_err(&drvdata->hdev->dev, "Failed to apply RGB state: %d\n", ret); 1483 } 1484 1485 static ssize_t effect_store(struct device *dev, 1486 struct device_attribute *attr, 1487 const char *buf, size_t count) 1488 { 1489 struct led_classdev *led_cdev = dev_get_drvdata(dev); 1490 struct led_classdev_mc *led_mc = container_of(led_cdev, struct led_classdev_mc, led_cdev); 1491 struct claw_drvdata *drvdata = container_of(led_mc, struct claw_drvdata, led_mc); 1492 int ret; 1493 1494 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) { 1495 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 1496 if (!smp_load_acquire(&drvdata->rgb_registered)) 1497 return -ENODEV; 1498 } 1499 1500 ret = sysfs_match_string(claw_rgb_effect_text, buf); 1501 if (ret < 0) 1502 return ret; 1503 1504 scoped_guard(spinlock_irqsave, &drvdata->profile_lock) 1505 drvdata->rgb_effect = ret; 1506 1507 mod_delayed_work(system_wq, &drvdata->rgb_queue, msecs_to_jiffies(50)); 1508 1509 return count; 1510 } 1511 1512 static ssize_t effect_show(struct device *dev, 1513 struct device_attribute *attr, char *buf) 1514 { 1515 struct led_classdev *led_cdev = dev_get_drvdata(dev); 1516 struct led_classdev_mc *led_mc = container_of(led_cdev, struct led_classdev_mc, led_cdev); 1517 struct claw_drvdata *drvdata = container_of(led_mc, struct claw_drvdata, led_mc); 1518 1519 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) { 1520 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 1521 if (!smp_load_acquire(&drvdata->rgb_registered)) 1522 return -ENODEV; 1523 } 1524 1525 if (drvdata->rgb_effect >= ARRAY_SIZE(claw_rgb_effect_text)) 1526 return -EINVAL; 1527 1528 return sysfs_emit(buf, "%s\n", claw_rgb_effect_text[drvdata->rgb_effect]); 1529 } 1530 1531 static DEVICE_ATTR_RW(effect); 1532 1533 static ssize_t effect_index_show(struct device *dev, 1534 struct device_attribute *attr, char *buf) 1535 { 1536 int i, count = 0; 1537 1538 for (i = 0; i < ARRAY_SIZE(claw_rgb_effect_text); i++) 1539 count += sysfs_emit_at(buf, count, "%s ", claw_rgb_effect_text[i]); 1540 1541 if (count) 1542 buf[count - 1] = '\n'; 1543 1544 return count; 1545 } 1546 static DEVICE_ATTR_RO(effect_index); 1547 1548 static ssize_t enabled_store(struct device *dev, 1549 struct device_attribute *attr, 1550 const char *buf, size_t count) 1551 { 1552 struct led_classdev *led_cdev = dev_get_drvdata(dev); 1553 struct led_classdev_mc *led_mc = container_of(led_cdev, struct led_classdev_mc, led_cdev); 1554 struct claw_drvdata *drvdata = container_of(led_mc, struct claw_drvdata, led_mc); 1555 bool val; 1556 int ret; 1557 1558 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) { 1559 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 1560 if (!smp_load_acquire(&drvdata->rgb_registered)) 1561 return -ENODEV; 1562 } 1563 1564 ret = kstrtobool(buf, &val); 1565 if (ret) 1566 return ret; 1567 1568 scoped_guard(spinlock_irqsave, &drvdata->profile_lock) 1569 drvdata->rgb_enabled = val; 1570 1571 mod_delayed_work(system_wq, &drvdata->rgb_queue, msecs_to_jiffies(50)); 1572 1573 return count; 1574 } 1575 1576 static ssize_t enabled_show(struct device *dev, 1577 struct device_attribute *attr, char *buf) 1578 { 1579 struct led_classdev *led_cdev = dev_get_drvdata(dev); 1580 struct led_classdev_mc *led_mc = container_of(led_cdev, struct led_classdev_mc, led_cdev); 1581 struct claw_drvdata *drvdata = container_of(led_mc, struct claw_drvdata, led_mc); 1582 1583 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) { 1584 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 1585 if (!smp_load_acquire(&drvdata->rgb_registered)) 1586 return -ENODEV; 1587 } 1588 1589 return sysfs_emit(buf, "%s\n", drvdata->rgb_enabled ? "true" : "false"); 1590 } 1591 static DEVICE_ATTR_RW(enabled); 1592 1593 static ssize_t enabled_index_show(struct device *dev, 1594 struct device_attribute *attr, char *buf) 1595 { 1596 return sysfs_emit(buf, "true false\n"); 1597 } 1598 static DEVICE_ATTR_RO(enabled_index); 1599 1600 static ssize_t speed_store(struct device *dev, struct device_attribute *attr, 1601 const char *buf, size_t count) 1602 { 1603 struct led_classdev *led_cdev = dev_get_drvdata(dev); 1604 struct led_classdev_mc *led_mc = container_of(led_cdev, struct led_classdev_mc, led_cdev); 1605 struct claw_drvdata *drvdata = container_of(led_mc, struct claw_drvdata, led_mc); 1606 unsigned int val, speed; 1607 int ret; 1608 1609 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) { 1610 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 1611 if (!smp_load_acquire(&drvdata->rgb_registered)) 1612 return -ENODEV; 1613 } 1614 1615 ret = kstrtouint(buf, 10, &val); 1616 if (ret) 1617 return ret; 1618 1619 if (val > 20) 1620 return -EINVAL; 1621 1622 /* 0 is fastest, invert value for intuitive userspace speed */ 1623 speed = 20 - val; 1624 1625 scoped_guard(spinlock_irqsave, &drvdata->profile_lock) 1626 drvdata->rgb_speed = speed; 1627 1628 mod_delayed_work(system_wq, &drvdata->rgb_queue, msecs_to_jiffies(50)); 1629 1630 return count; 1631 } 1632 1633 static ssize_t speed_show(struct device *dev, struct device_attribute *attr, 1634 char *buf) 1635 { 1636 struct led_classdev *led_cdev = dev_get_drvdata(dev); 1637 struct led_classdev_mc *led_mc = container_of(led_cdev, struct led_classdev_mc, led_cdev); 1638 struct claw_drvdata *drvdata = container_of(led_mc, struct claw_drvdata, led_mc); 1639 u8 speed = 20 - drvdata->rgb_speed; 1640 1641 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) { 1642 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 1643 if (!smp_load_acquire(&drvdata->rgb_registered)) 1644 return -ENODEV; 1645 } 1646 1647 return sysfs_emit(buf, "%u\n", speed); 1648 } 1649 static DEVICE_ATTR_RW(speed); 1650 1651 static ssize_t speed_range_show(struct device *dev, 1652 struct device_attribute *attr, char *buf) 1653 { 1654 return sysfs_emit(buf, "0-20\n"); 1655 } 1656 static DEVICE_ATTR_RO(speed_range); 1657 1658 static void claw_led_brightness_set(struct led_classdev *led_cdev, 1659 enum led_brightness _brightness) 1660 { 1661 struct led_classdev_mc *led_mc = container_of(led_cdev, struct led_classdev_mc, led_cdev); 1662 struct claw_drvdata *drvdata = container_of(led_mc, struct claw_drvdata, led_mc); 1663 1664 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) { 1665 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 1666 if (!smp_load_acquire(&drvdata->rgb_registered)) 1667 return; 1668 } 1669 1670 mod_delayed_work(system_wq, &drvdata->rgb_queue, msecs_to_jiffies(50)); 1671 } 1672 1673 static struct attribute *claw_rgb_attrs[] = { 1674 &dev_attr_effect.attr, 1675 &dev_attr_effect_index.attr, 1676 &dev_attr_enabled.attr, 1677 &dev_attr_enabled_index.attr, 1678 &dev_attr_speed.attr, 1679 &dev_attr_speed_range.attr, 1680 NULL, 1681 }; 1682 1683 static const struct attribute_group claw_rgb_attr_group = { 1684 .attrs = claw_rgb_attrs, 1685 }; 1686 1687 static struct mc_subled claw_rgb_subled_info[] = { 1688 { 1689 .color_index = LED_COLOR_ID_RED, 1690 .channel = 0x1, 1691 }, 1692 { 1693 .color_index = LED_COLOR_ID_GREEN, 1694 .channel = 0x2, 1695 }, 1696 { 1697 .color_index = LED_COLOR_ID_BLUE, 1698 .channel = 0x3, 1699 }, 1700 }; 1701 1702 static void cfg_setup_fn(struct work_struct *work) 1703 { 1704 struct delayed_work *dwork = container_of(work, struct delayed_work, work); 1705 struct claw_drvdata *drvdata = container_of(dwork, struct claw_drvdata, cfg_setup); 1706 bool gamepad_ready = false, rgb_ready = false, gp_registered, rgb_registered; 1707 int ret; 1708 1709 ret = claw_hw_output_report(drvdata->hdev, CLAW_COMMAND_TYPE_READ_GAMEPAD_MODE, 1710 NULL, 0, 25); 1711 if (ret) { 1712 dev_err(&drvdata->hdev->dev, 1713 "Failed to read gamepad mode: %d\n", ret); 1714 goto prep_rgb; 1715 } 1716 gamepad_ready = true; 1717 1718 prep_rgb: 1719 ret = claw_read_rgb_config(drvdata->hdev); 1720 if (ret) { 1721 dev_err(&drvdata->hdev->dev, 1722 "Failed to read RGB config: %d\n", ret); 1723 goto try_gamepad; 1724 } 1725 rgb_ready = true; 1726 1727 /* Add sysfs attributes after we get the device state */ 1728 try_gamepad: 1729 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) 1730 /* Pairs with smp_store_release from below */ 1731 gp_registered = smp_load_acquire(&drvdata->gp_registered); 1732 1733 if (!gp_registered && gamepad_ready) { 1734 ret = device_add_group(&drvdata->hdev->dev, &claw_gamepad_attr_group); 1735 if (ret) { 1736 dev_err(&drvdata->hdev->dev, 1737 "Failed to create gamepad attrs: %d\n", ret); 1738 goto try_rgb; 1739 } 1740 1741 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) { 1742 /* Pairs with smp_load_acquire in attribute show/store functions */ 1743 smp_store_release(&drvdata->gp_registered, true); 1744 gp_registered = true; 1745 } 1746 } 1747 1748 try_rgb: 1749 /* Add and enable RGB interface once we have the device state */ 1750 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) 1751 /* Pairs with smp_store_release from below */ 1752 rgb_registered = smp_load_acquire(&drvdata->rgb_registered); 1753 1754 if (!rgb_registered && rgb_ready) { 1755 ret = led_classdev_multicolor_register(&drvdata->hdev->dev, 1756 &drvdata->led_mc); 1757 if (ret) { 1758 dev_err(&drvdata->hdev->dev, "Failed to create led device: %d\n", ret); 1759 goto update_kobjects; 1760 } 1761 1762 ret = device_add_group(drvdata->led_mc.led_cdev.dev, &claw_rgb_attr_group); 1763 if (ret) { 1764 dev_err(&drvdata->hdev->dev, "Failed to create RGB attrs: %d\n", ret); 1765 led_classdev_multicolor_unregister(&drvdata->led_mc); 1766 goto update_kobjects; 1767 } 1768 1769 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) { 1770 /* Pairs with smp_load_acquire in attribute show/store functions */ 1771 smp_store_release(&drvdata->rgb_registered, true); 1772 rgb_registered = true; 1773 } 1774 } 1775 1776 update_kobjects: 1777 if (gp_registered) 1778 kobject_uevent(&drvdata->hdev->dev.kobj, KOBJ_CHANGE); 1779 if (rgb_registered) 1780 kobject_uevent(&drvdata->led_mc.led_cdev.dev->kobj, KOBJ_CHANGE); 1781 } 1782 1783 static void cfg_resume_fn(struct work_struct *work) 1784 { 1785 struct delayed_work *dwork = container_of(work, struct delayed_work, work); 1786 struct claw_drvdata *drvdata = container_of(dwork, struct claw_drvdata, cfg_resume); 1787 1788 guard(spinlock_irqsave)(&drvdata->registration_lock); 1789 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 1790 if (!smp_load_acquire(&drvdata->gp_registered) || 1791 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 1792 !smp_load_acquire(&drvdata->rgb_registered)) 1793 schedule_delayed_work(&drvdata->cfg_setup, msecs_to_jiffies(500)); 1794 } 1795 1796 static void claw_features_supported(struct claw_drvdata *drvdata) 1797 { 1798 u8 major = (drvdata->bcd_device >> 8) & 0xff; 1799 u8 minor = drvdata->bcd_device & 0xff; 1800 1801 if (major == 0x01) { 1802 drvdata->bmap_support = true; 1803 if (minor >= 0x66) { 1804 drvdata->bmap_addr = button_mapping_addr_new; 1805 drvdata->rumble_support = true; 1806 drvdata->rgb_addr = rgb_addr_new; 1807 } else { 1808 drvdata->bmap_addr = button_mapping_addr_old; 1809 drvdata->rgb_addr = rgb_addr_old; 1810 } 1811 return; 1812 } 1813 1814 if ((major == 0x02 && minor >= 0x17) || major >= 0x03) { 1815 drvdata->bmap_support = true; 1816 drvdata->bmap_addr = button_mapping_addr_new; 1817 drvdata->rumble_support = true; 1818 drvdata->rgb_addr = rgb_addr_new; 1819 return; 1820 } 1821 1822 drvdata->rgb_addr = rgb_addr_old; 1823 } 1824 1825 static int claw_probe(struct hid_device *hdev, u8 ep) 1826 { 1827 struct usb_interface *intf = to_usb_interface(hdev->dev.parent); 1828 struct usb_device *udev = interface_to_usbdev(intf); 1829 struct claw_drvdata *drvdata; 1830 int ret; 1831 1832 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL); 1833 if (!drvdata) 1834 return -ENOMEM; 1835 1836 drvdata->gamepad_mode = CLAW_GAMEPAD_MODE_XINPUT; 1837 drvdata->rgb_enabled = true; 1838 drvdata->hdev = hdev; 1839 drvdata->ep = ep; 1840 1841 /* Determine feature level from firmware version */ 1842 drvdata->bcd_device = le16_to_cpu(udev->descriptor.bcdDevice); 1843 claw_features_supported(drvdata); 1844 1845 if (!drvdata->bmap_support) 1846 dev_dbg(&hdev->dev, "M-Key mapping is not supported. Update firmware to enable.\n"); 1847 1848 /* Device is hardwired and name is guaranteed to be unique */ 1849 drvdata->led_mc.led_cdev.name = "msi_claw:rgb:joystick_rings"; 1850 drvdata->led_mc.led_cdev.brightness = 0x50; 1851 drvdata->led_mc.led_cdev.max_brightness = 0x64; 1852 drvdata->led_mc.led_cdev.color = LED_COLOR_ID_RGB; 1853 drvdata->led_mc.led_cdev.brightness_set = claw_led_brightness_set; 1854 drvdata->led_mc.num_colors = 3; 1855 drvdata->led_mc.subled_info = devm_kmemdup(&hdev->dev, claw_rgb_subled_info, 1856 sizeof(claw_rgb_subled_info), GFP_KERNEL); 1857 if (!drvdata->led_mc.subled_info) 1858 return -ENOMEM; 1859 1860 mutex_init(&drvdata->cfg_mutex); 1861 mutex_init(&drvdata->profile_mutex); 1862 mutex_init(&drvdata->rom_mutex); 1863 spin_lock_init(&drvdata->registration_lock); 1864 spin_lock_init(&drvdata->cmd_lock); 1865 spin_lock_init(&drvdata->mode_lock); 1866 spin_lock_init(&drvdata->profile_lock); 1867 spin_lock_init(&drvdata->frame_lock); 1868 spin_lock_init(&drvdata->rumble_lock); 1869 init_completion(&drvdata->orphan_ack_complete); 1870 init_completion(&drvdata->send_cmd_complete); 1871 INIT_DELAYED_WORK(&drvdata->cfg_resume, &cfg_resume_fn); 1872 INIT_DELAYED_WORK(&drvdata->cfg_setup, &cfg_setup_fn); 1873 INIT_DELAYED_WORK(&drvdata->rgb_queue, &claw_rgb_queue_fn); 1874 1875 /* For control interface: open the HID transport for sending commands. */ 1876 ret = hid_hw_open(hdev); 1877 if (ret) 1878 return ret; 1879 1880 hid_set_drvdata(hdev, drvdata); 1881 schedule_delayed_work(&drvdata->cfg_setup, msecs_to_jiffies(500)); 1882 1883 return 0; 1884 } 1885 1886 static int msi_probe(struct hid_device *hdev, const struct hid_device_id *id) 1887 { 1888 int ret; 1889 u8 ep; 1890 1891 if (!hid_is_usb(hdev)) { 1892 ret = -ENODEV; 1893 goto err_probe; 1894 } 1895 1896 ret = hid_parse(hdev); 1897 if (ret) 1898 goto err_probe; 1899 1900 /* Set quirk to create separate input devices per HID application */ 1901 hdev->quirks |= HID_QUIRK_INPUT_PER_APP | HID_QUIRK_MULTI_INPUT; 1902 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); 1903 if (ret) 1904 goto err_probe; 1905 1906 /* For non-control interfaces (keyboard/mouse), allow userspace to grab the devices. */ 1907 ret = get_endpoint_address(hdev); 1908 if (ret < 0) 1909 goto err_stop_hw; 1910 1911 ep = ret; 1912 if (ep == CLAW_XINPUT_CFG_INTF_IN || ep == CLAW_DINPUT_CFG_INTF_IN) { 1913 ret = claw_probe(hdev, ep); 1914 if (ret) 1915 goto err_stop_hw; 1916 } 1917 1918 return 0; 1919 1920 err_stop_hw: 1921 hid_hw_stop(hdev); 1922 err_probe: 1923 return dev_err_probe(&hdev->dev, ret, "Failed to init device\n"); 1924 } 1925 1926 static void claw_remove(struct hid_device *hdev) 1927 { 1928 struct claw_drvdata *drvdata = hid_get_drvdata(hdev); 1929 bool gp_registered; 1930 bool rgb_registered; 1931 1932 if (!drvdata) 1933 return; 1934 1935 cancel_delayed_work_sync(&drvdata->cfg_resume); 1936 cancel_delayed_work_sync(&drvdata->cfg_setup); 1937 1938 scoped_guard(spinlock_irqsave, &drvdata->registration_lock) { 1939 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 1940 gp_registered = smp_load_acquire(&drvdata->gp_registered); 1941 /* Pairs with smp_load_acquire in attribute show/store functions */ 1942 smp_store_release(&drvdata->gp_registered, false); 1943 /* Pairs with smp_store_release from cfg_setup_fn in system_wq context */ 1944 rgb_registered = smp_load_acquire(&drvdata->rgb_registered); 1945 /* Pairs with smp_load_acquire in attribute show/store functions */ 1946 smp_store_release(&drvdata->rgb_registered, false); 1947 } 1948 1949 if (gp_registered) 1950 device_remove_group(&hdev->dev, &claw_gamepad_attr_group); 1951 1952 if (rgb_registered) { 1953 device_remove_group(drvdata->led_mc.led_cdev.dev, &claw_rgb_attr_group); 1954 led_classdev_multicolor_unregister(&drvdata->led_mc); 1955 } 1956 cancel_delayed_work_sync(&drvdata->rgb_queue); 1957 1958 hid_hw_close(hdev); 1959 } 1960 1961 static void msi_remove(struct hid_device *hdev) 1962 { 1963 int ret; 1964 u8 ep; 1965 1966 /* Safe assumption. SET_INTERFACE ioctl can't be used while driver is bound */ 1967 ret = get_endpoint_address(hdev); 1968 if (ret <= 0) 1969 goto hw_stop; 1970 1971 ep = ret; 1972 if (ep == CLAW_XINPUT_CFG_INTF_IN || ep == CLAW_DINPUT_CFG_INTF_IN) 1973 claw_remove(hdev); 1974 1975 hw_stop: 1976 hid_hw_stop(hdev); 1977 } 1978 1979 static int claw_resume(struct hid_device *hdev) 1980 { 1981 struct claw_drvdata *drvdata = hid_get_drvdata(hdev); 1982 1983 if (!drvdata) 1984 return -ENODEV; 1985 1986 /* MCU can take up to 500ms to be ready after resume */ 1987 schedule_delayed_work(&drvdata->cfg_resume, msecs_to_jiffies(500)); 1988 return 0; 1989 } 1990 1991 static int msi_resume(struct hid_device *hdev) 1992 { 1993 int ret; 1994 u8 ep; 1995 1996 /* Safe assumption. SET_INTERFACE ioctl can't be used while driver is bound */ 1997 ret = get_endpoint_address(hdev); 1998 if (ret <= 0) 1999 return 0; 2000 2001 ep = ret; 2002 if (ep == CLAW_XINPUT_CFG_INTF_IN || ep == CLAW_DINPUT_CFG_INTF_IN) 2003 return claw_resume(hdev); 2004 2005 return 0; 2006 } 2007 2008 static int claw_suspend(struct hid_device *hdev) 2009 { 2010 struct claw_drvdata *drvdata = hid_get_drvdata(hdev); 2011 2012 if (!drvdata) 2013 return -ENODEV; 2014 2015 cancel_delayed_work_sync(&drvdata->cfg_resume); 2016 cancel_delayed_work_sync(&drvdata->cfg_setup); 2017 cancel_delayed_work_sync(&drvdata->rgb_queue); 2018 2019 return 0; 2020 } 2021 2022 static int msi_suspend(struct hid_device *hdev, pm_message_t msg) 2023 { 2024 int ret; 2025 u8 ep; 2026 2027 /* Safe assumption. SET_INTERFACE ioctl can't be used while driver is bound */ 2028 ret = get_endpoint_address(hdev); 2029 if (ret <= 0) 2030 return 0; 2031 2032 ep = ret; 2033 if (ep == CLAW_XINPUT_CFG_INTF_IN || ep == CLAW_DINPUT_CFG_INTF_IN) 2034 return claw_suspend(hdev); 2035 2036 return 0; 2037 } 2038 2039 static const struct hid_device_id msi_devices[] = { 2040 { HID_USB_DEVICE(USB_VENDOR_ID_MSI_2, USB_DEVICE_ID_MSI_CLAW_XINPUT) }, 2041 { HID_USB_DEVICE(USB_VENDOR_ID_MSI_2, USB_DEVICE_ID_MSI_CLAW_DINPUT) }, 2042 { HID_USB_DEVICE(USB_VENDOR_ID_MSI_2, USB_DEVICE_ID_MSI_CLAW_DESKTOP) }, 2043 { HID_USB_DEVICE(USB_VENDOR_ID_MSI_2, USB_DEVICE_ID_MSI_CLAW_BIOS) }, 2044 { } 2045 }; 2046 MODULE_DEVICE_TABLE(hid, msi_devices); 2047 2048 static struct hid_driver msi_driver = { 2049 .name = "hid-msi", 2050 .id_table = msi_devices, 2051 .raw_event = msi_raw_event, 2052 .probe = msi_probe, 2053 .remove = msi_remove, 2054 .resume = pm_ptr(msi_resume), 2055 .suspend = pm_ptr(msi_suspend), 2056 }; 2057 module_hid_driver(msi_driver); 2058 2059 MODULE_LICENSE("GPL"); 2060 MODULE_AUTHOR("Denis Benato <denis.benato@linux.dev>"); 2061 MODULE_AUTHOR("Zhouwang Huang <honjow311@gmail.com>"); 2062 MODULE_AUTHOR("Derek J. Clark <derekjohn.clark@gmail.com>"); 2063 MODULE_DESCRIPTION("HID driver for MSI Claw Handheld PC gamepads"); 2064