1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * HID driver for Valve Steam Controller 4 * 5 * Copyright (c) 2018 Rodrigo Rivas Costa <rodrigorivascosta@gmail.com> 6 * Copyright (c) 2022 Valve Software 7 * 8 * Supports both the wired and wireless interfaces. 9 * 10 * This controller has a builtin emulation of mouse and keyboard: the right pad 11 * can be used as a mouse, the shoulder buttons are mouse buttons, A and B 12 * buttons are ENTER and ESCAPE, and so on. This is implemented as additional 13 * HID interfaces. 14 * 15 * This is known as the "lizard mode", because apparently lizards like to use 16 * the computer from the coach, without a proper mouse and keyboard. 17 * 18 * This driver will disable the lizard mode when the input device is opened 19 * and re-enable it when the input device is closed, so as not to break user 20 * mode behaviour. The lizard_mode parameter can be used to change that. 21 * 22 * There are a few user space applications (notably Steam Client) that use 23 * the hidraw interface directly to create input devices (XTest, uinput...). 24 * In order to avoid breaking them this driver creates a layered hidraw device, 25 * so it can detect when the client is running and then: 26 * - it will not send any command to the controller. 27 * - this input device will be removed, to avoid double input of the same 28 * user action. 29 * When the client is closed, this input device will be created again. 30 * 31 * For additional functions, such as changing the right-pad margin or switching 32 * the led, you can use the user-space tool at: 33 * 34 * https://github.com/rodrigorc/steamctrl 35 */ 36 37 #include <linux/device.h> 38 #include <linux/input.h> 39 #include <linux/hid.h> 40 #include <linux/module.h> 41 #include <linux/workqueue.h> 42 #include <linux/mutex.h> 43 #include <linux/rcupdate.h> 44 #include <linux/delay.h> 45 #include <linux/power_supply.h> 46 #include <linux/unaligned.h> 47 #include <linux/usb.h> 48 #include "hid-ids.h" 49 50 MODULE_DESCRIPTION("HID driver for Valve Steam Controller"); 51 MODULE_LICENSE("GPL"); 52 MODULE_AUTHOR("Rodrigo Rivas Costa <rodrigorivascosta@gmail.com>"); 53 MODULE_AUTHOR("Vicki Pfau <vi@endrift.com>"); 54 55 static bool lizard_mode = true; 56 57 static DEFINE_MUTEX(steam_devices_lock); 58 static LIST_HEAD(steam_devices); 59 60 #define STEAM_QUIRK_WIRELESS BIT(0) 61 #define STEAM_QUIRK_DECK BIT(1) 62 #define STEAM_QUIRK_IBEX BIT(2) 63 #define STEAM_QUIRK_BLE BIT(3) 64 65 /* Touch pads are 40 mm in diameter and 65535 units */ 66 #define STEAM_PAD_RESOLUTION 1638 67 /* Trigger runs are about 5 mm and 256 units */ 68 #define STEAM_TRIGGER_RESOLUTION 51 69 /* Joystick runs are about 5 mm and 256 units */ 70 #define STEAM_JOYSTICK_RESOLUTION 51 71 /* Trigger runs are about 6 mm and 32768 units */ 72 #define STEAM_DECK_TRIGGER_RESOLUTION 5461 73 /* Joystick runs are about 5 mm and 32768 units */ 74 #define STEAM_DECK_JOYSTICK_RESOLUTION 6553 75 /* Accelerometer has 16 bit resolution and a range of +/- 2g */ 76 #define STEAM_ACCEL_RES_PER_G 16384 77 #define STEAM_ACCEL_RANGE 32768 78 #define STEAM_ACCEL_FUZZ 128 79 #define STEAM_DECK_ACCEL_FUZZ 32 80 /* Gyroscope has 16 bit resolution and a range of +/- 2000 dps */ 81 #define STEAM_GYRO_RES_PER_DPS 16 82 #define STEAM_GYRO_RANGE 32768 83 #define STEAM_GYRO_FUZZ 0 84 85 #define STEAM_PAD_FUZZ 256 86 87 /* 88 * Commands that can be sent in a feature report. 89 * Thanks to Valve and SDL for the names. 90 */ 91 enum { 92 ID_SET_DIGITAL_MAPPINGS = 0x80, 93 ID_CLEAR_DIGITAL_MAPPINGS = 0x81, 94 ID_GET_DIGITAL_MAPPINGS = 0x82, 95 ID_GET_ATTRIBUTES_VALUES = 0x83, 96 ID_GET_ATTRIBUTE_LABEL = 0x84, 97 ID_SET_DEFAULT_DIGITAL_MAPPINGS = 0x85, 98 ID_FACTORY_RESET = 0x86, 99 ID_SET_SETTINGS_VALUES = 0x87, 100 ID_CLEAR_SETTINGS_VALUES = 0x88, 101 ID_GET_SETTINGS_VALUES = 0x89, 102 ID_GET_SETTING_LABEL = 0x8A, 103 ID_GET_SETTINGS_MAXS = 0x8B, 104 ID_GET_SETTINGS_DEFAULTS = 0x8C, 105 ID_SET_CONTROLLER_MODE = 0x8D, 106 ID_LOAD_DEFAULT_SETTINGS = 0x8E, 107 ID_TRIGGER_HAPTIC_PULSE = 0x8F, 108 ID_TURN_OFF_CONTROLLER = 0x9F, 109 110 ID_GET_DEVICE_INFO = 0xA1, 111 112 ID_CALIBRATE_TRACKPADS = 0xA7, 113 ID_RESERVED_0 = 0xA8, 114 ID_SET_SERIAL_NUMBER = 0xA9, 115 ID_GET_TRACKPAD_CALIBRATION = 0xAA, 116 ID_GET_TRACKPAD_FACTORY_CALIBRATION = 0xAB, 117 ID_GET_TRACKPAD_RAW_DATA = 0xAC, 118 ID_ENABLE_PAIRING = 0xAD, 119 ID_GET_STRING_ATTRIBUTE = 0xAE, 120 ID_RADIO_ERASE_RECORDS = 0xAF, 121 ID_RADIO_WRITE_RECORD = 0xB0, 122 ID_SET_DONGLE_SETTING = 0xB1, 123 ID_DONGLE_DISCONNECT_DEVICE = 0xB2, 124 ID_DONGLE_COMMIT_DEVICE = 0xB3, 125 ID_DONGLE_GET_WIRELESS_STATE = 0xB4, 126 ID_CALIBRATE_GYRO = 0xB5, 127 ID_PLAY_AUDIO = 0xB6, 128 ID_AUDIO_UPDATE_START = 0xB7, 129 ID_AUDIO_UPDATE_DATA = 0xB8, 130 ID_AUDIO_UPDATE_COMPLETE = 0xB9, 131 ID_GET_CHIPID = 0xBA, 132 133 ID_CALIBRATE_JOYSTICK = 0xBF, 134 ID_CALIBRATE_ANALOG_TRIGGERS = 0xC0, 135 ID_SET_AUDIO_MAPPING = 0xC1, 136 ID_CHECK_GYRO_FW_LOAD = 0xC2, 137 ID_CALIBRATE_ANALOG = 0xC3, 138 ID_DONGLE_GET_CONNECTED_SLOTS = 0xC4, 139 140 ID_RESET_IMU = 0xCE, 141 142 ID_TRIGGER_HAPTIC_CMD = 0xEA, 143 ID_TRIGGER_RUMBLE_CMD = 0xEB, 144 }; 145 146 /* Settings IDs */ 147 enum { 148 /* 0 */ 149 SETTING_MOUSE_SENSITIVITY, 150 SETTING_MOUSE_ACCELERATION, 151 SETTING_TRACKBALL_ROTATION_ANGLE, 152 SETTING_HAPTIC_INTENSITY_UNUSED, 153 SETTING_LEFT_GAMEPAD_STICK_ENABLED, 154 SETTING_RIGHT_GAMEPAD_STICK_ENABLED, 155 SETTING_USB_DEBUG_MODE, 156 SETTING_LEFT_TRACKPAD_MODE, 157 SETTING_RIGHT_TRACKPAD_MODE, 158 SETTING_LIZARD_MODE, 159 160 /* 10 */ 161 SETTING_DPAD_DEADZONE, 162 SETTING_MINIMUM_MOMENTUM_VEL, 163 SETTING_MOMENTUM_DECAY_AMMOUNT, 164 SETTING_TRACKPAD_RELATIVE_MODE_TICKS_PER_PIXEL, 165 SETTING_HAPTIC_INCREMENT, 166 SETTING_DPAD_ANGLE_SIN, 167 SETTING_DPAD_ANGLE_COS, 168 SETTING_MOMENTUM_VERTICAL_DIVISOR, 169 SETTING_MOMENTUM_MAXIMUM_VELOCITY, 170 SETTING_TRACKPAD_Z_ON, 171 172 /* 20 */ 173 SETTING_TRACKPAD_Z_OFF, 174 SETTING_SENSITIVY_SCALE_AMMOUNT, 175 SETTING_LEFT_TRACKPAD_SECONDARY_MODE, 176 SETTING_RIGHT_TRACKPAD_SECONDARY_MODE, 177 SETTING_SMOOTH_ABSOLUTE_MOUSE, 178 SETTING_STEAMBUTTON_POWEROFF_TIME, 179 SETTING_UNUSED_1, 180 SETTING_TRACKPAD_OUTER_RADIUS, 181 SETTING_TRACKPAD_Z_ON_LEFT, 182 SETTING_TRACKPAD_Z_OFF_LEFT, 183 184 /* 30 */ 185 SETTING_TRACKPAD_OUTER_SPIN_VEL, 186 SETTING_TRACKPAD_OUTER_SPIN_RADIUS, 187 SETTING_TRACKPAD_OUTER_SPIN_HORIZONTAL_ONLY, 188 SETTING_TRACKPAD_RELATIVE_MODE_DEADZONE, 189 SETTING_TRACKPAD_RELATIVE_MODE_MAX_VEL, 190 SETTING_TRACKPAD_RELATIVE_MODE_INVERT_Y, 191 SETTING_TRACKPAD_DOUBLE_TAP_BEEP_ENABLED, 192 SETTING_TRACKPAD_DOUBLE_TAP_BEEP_PERIOD, 193 SETTING_TRACKPAD_DOUBLE_TAP_BEEP_COUNT, 194 SETTING_TRACKPAD_OUTER_RADIUS_RELEASE_ON_TRANSITION, 195 196 /* 40 */ 197 SETTING_RADIAL_MODE_ANGLE, 198 SETTING_HAPTIC_INTENSITY_MOUSE_MODE, 199 SETTING_LEFT_DPAD_REQUIRES_CLICK, 200 SETTING_RIGHT_DPAD_REQUIRES_CLICK, 201 SETTING_LED_BASELINE_BRIGHTNESS, 202 SETTING_LED_USER_BRIGHTNESS, 203 SETTING_ENABLE_RAW_JOYSTICK, 204 SETTING_ENABLE_FAST_SCAN, 205 SETTING_IMU_MODE, 206 SETTING_WIRELESS_PACKET_VERSION, 207 208 /* 50 */ 209 SETTING_SLEEP_INACTIVITY_TIMEOUT, 210 SETTING_TRACKPAD_NOISE_THRESHOLD, 211 SETTING_LEFT_TRACKPAD_CLICK_PRESSURE, 212 SETTING_RIGHT_TRACKPAD_CLICK_PRESSURE, 213 SETTING_LEFT_BUMPER_CLICK_PRESSURE, 214 SETTING_RIGHT_BUMPER_CLICK_PRESSURE, 215 SETTING_LEFT_GRIP_CLICK_PRESSURE, 216 SETTING_RIGHT_GRIP_CLICK_PRESSURE, 217 SETTING_LEFT_GRIP2_CLICK_PRESSURE, 218 SETTING_RIGHT_GRIP2_CLICK_PRESSURE, 219 220 /* 60 */ 221 SETTING_PRESSURE_MODE, 222 SETTING_CONTROLLER_TEST_MODE, 223 SETTING_TRIGGER_MODE, 224 SETTING_TRACKPAD_Z_THRESHOLD, 225 SETTING_FRAME_RATE, 226 SETTING_TRACKPAD_FILT_CTRL, 227 SETTING_TRACKPAD_CLIP, 228 SETTING_DEBUG_OUTPUT_SELECT, 229 SETTING_TRIGGER_THRESHOLD_PERCENT, 230 SETTING_TRACKPAD_FREQUENCY_HOPPING, 231 232 /* 70 */ 233 SETTING_HAPTICS_ENABLED, 234 SETTING_STEAM_WATCHDOG_ENABLE, 235 SETTING_TIMP_TOUCH_THRESHOLD_ON, 236 SETTING_TIMP_TOUCH_THRESHOLD_OFF, 237 SETTING_FREQ_HOPPING, 238 SETTING_TEST_CONTROL, 239 SETTING_HAPTIC_MASTER_GAIN_DB, 240 SETTING_THUMB_TOUCH_THRESH, 241 SETTING_DEVICE_POWER_STATUS, 242 SETTING_HAPTIC_INTENSITY, 243 244 /* 80 */ 245 SETTING_STABILIZER_ENABLED, 246 SETTING_TIMP_MODE_MTE, 247 }; 248 249 /* Input report identifiers */ 250 enum 251 { 252 ID_CONTROLLER_STATE = 1, 253 ID_CONTROLLER_DEBUG = 2, 254 ID_CONTROLLER_WIRELESS = 3, 255 ID_CONTROLLER_STATUS = 4, 256 ID_CONTROLLER_DEBUG2 = 5, 257 ID_CONTROLLER_SECONDARY_STATE = 6, 258 ID_CONTROLLER_BLE_STATE = 7, 259 ID_CONTROLLER_DECK_STATE = 9, 260 }; 261 262 /* Read-only attributes */ 263 enum { 264 ATTRIB_UNIQUE_ID, // deprecated 265 ATTRIB_PRODUCT_ID, 266 ATTRIB_PRODUCT_REVISON, // deprecated 267 ATTRIB_CAPABILITIES = ATTRIB_PRODUCT_REVISON, // intentional aliasing 268 ATTRIB_FIRMWARE_VERSION, // deprecated 269 ATTRIB_FIRMWARE_BUILD_TIME, 270 ATTRIB_RADIO_FIRMWARE_BUILD_TIME, 271 ATTRIB_RADIO_DEVICE_ID0, 272 ATTRIB_RADIO_DEVICE_ID1, 273 ATTRIB_DONGLE_FIRMWARE_BUILD_TIME, 274 ATTRIB_HW_ID, // AKA BOARD_REVISION, 275 ATTRIB_BOOTLOADER_BUILD_TIME, 276 ATTRIB_CONNECTION_INTERVAL_IN_US, 277 ATTRIB_SECONDARY_FIRMWARE_BUILD_TIME, 278 ATTRIB_SECONDARY_BOOTLOADER_BUILD_TIME, 279 ATTRIB_SECONDARY_HW_ID, // AKA BOARD_REVISION, 280 ATTRIB_STREAMING, 281 ATTRIB_TRACKPAD_ID, 282 ATTRIB_SECONDARY_TRACKPAD_ID, 283 284 ATTRIB_COUNT 285 }; 286 287 /* String attribute identifiers */ 288 enum { 289 ATTRIB_STR_BOARD_SERIAL, 290 ATTRIB_STR_UNIT_SERIAL, 291 }; 292 293 /* Values for IMU_MODE (bitmask) */ 294 enum { 295 SETTING_IMU_MODE_OFF = 0, 296 SETTING_IMU_MODE_STEERING = BIT(0), 297 SETTING_IMU_MODE_TILT = BIT(1), 298 SETTING_IMU_MODE_SEND_ORIENTATION = BIT(2), 299 SETTING_IMU_MODE_SEND_RAW_ACCEL = BIT(3), 300 SETTING_IMU_MODE_SEND_RAW_GYRO = BIT(4), 301 }; 302 303 /* Trackpad modes */ 304 enum { 305 TRACKPAD_ABSOLUTE_MOUSE, 306 TRACKPAD_RELATIVE_MOUSE, 307 TRACKPAD_DPAD_FOUR_WAY_DISCRETE, 308 TRACKPAD_DPAD_FOUR_WAY_OVERLAP, 309 TRACKPAD_DPAD_EIGHT_WAY, 310 TRACKPAD_RADIAL_MODE, 311 TRACKPAD_ABSOLUTE_DPAD, 312 TRACKPAD_NONE, 313 TRACKPAD_GESTURE_KEYBOARD, 314 }; 315 316 /* Report identifiers (Ibex only) */ 317 enum { 318 /* Feature */ 319 REPORT_ID_FEATURES_CONTROLLER = 1, 320 REPORT_ID_FEATURES_DONGLE = 2, 321 322 /* Input */ 323 REPORT_ID_INPUT = 0x42, 324 REPORT_ID_BATTERY = 0x43, 325 REPORT_ID_INPUT2 = 0x45, 326 REPORT_ID_WIRELESS_EVENT = 0x79, 327 328 /* Output */ 329 REPORT_ID_HAPTIC_RUMBLE = 0x80, 330 REPORT_ID_HAPTIC_PULSE = 0x81, 331 REPORT_ID_HAPTIC_COMMAND = 0x82, 332 REPORT_ID_HAPTIC_LFO_TONE = 0x83, 333 REPORT_ID_HAPTIC_LOG_SWEEP = 0x84, 334 REPORT_ID_HAPTIC_SCRIPT = 0x85, 335 }; 336 337 /* Ibex charge state */ 338 enum { 339 CHARGE_STATE_DISCHARGING = 1, 340 CHARGE_STATE_CHARGING = 2, 341 CHARGE_STATE_CHARGING_DONE = 4, 342 }; 343 344 /* Ibex wireless events */ 345 enum { 346 WIRELESS_EVENT_DISCONNECT = 1, 347 WIRELESS_EVENT_CONNECT = 2, 348 WIRELESS_EVENT_PAIR = 3, 349 }; 350 351 struct steam_controller_attribute { 352 unsigned char tag; 353 __le32 value; 354 } __packed; 355 356 struct steam_ibex_battery_status { 357 u8 charge_state; 358 u8 battery_level; 359 __le16 battery_voltage; 360 __le16 system_voltage; 361 __le16 input_voltage; 362 __le16 battery_current; 363 __le16 input_current; 364 __le16 temperature; 365 }; 366 367 struct steam_ibex_haptic_rumble { 368 u8 type; 369 __le16 intensity; 370 struct { 371 __le16 speed; 372 u8 gain; 373 } __packed left, right; 374 } __packed; 375 static_assert(sizeof(struct steam_ibex_haptic_rumble) == 9); 376 377 struct steam_ibex_haptic_pulse { 378 u8 side; 379 __le16 on_us; 380 __le16 off_us; 381 __le16 repeat_count; 382 } __packed; 383 static_assert(sizeof(struct steam_ibex_haptic_pulse) == 7); 384 385 struct steam_ibex_output_report { 386 u8 id; 387 union { 388 struct steam_ibex_haptic_rumble rumble; 389 struct steam_ibex_haptic_pulse pulse; 390 }; 391 } __packed; 392 393 /* Pad identifiers for the deck */ 394 #define STEAM_PAD_LEFT 0 395 #define STEAM_PAD_RIGHT 1 396 #define STEAM_PAD_BOTH 2 397 398 /* Other random constants */ 399 #define STEAM_SERIAL_LEN 0x15 400 401 struct steam_device { 402 struct list_head list; 403 spinlock_t lock; 404 struct hid_device *hdev, *client_hdev; 405 struct mutex report_mutex; 406 struct mutex registration_mutex; 407 unsigned long client_opened; 408 struct input_dev __rcu *input; 409 struct input_dev __rcu *sensors; 410 unsigned long quirks; 411 struct work_struct work_connect; 412 bool connected; 413 bool registered; 414 char serial_no[STEAM_SERIAL_LEN + 1]; 415 struct power_supply_desc battery_desc; 416 struct power_supply __rcu *battery; 417 u8 battery_charge; 418 u8 battery_status; 419 u16 battery_temp; 420 u16 battery_voltage; 421 u16 battery_current; 422 struct delayed_work mode_switch; 423 bool did_mode_switch; 424 bool gamepad_mode; 425 struct work_struct rumble_work; 426 struct delayed_work coalesce_rumble_work; 427 u16 rumble_left; 428 u16 rumble_right; 429 unsigned int sensor_timestamp_us; 430 unsigned int sensor_update_rate_us; 431 }; 432 433 static int steam_recv_report_id(struct steam_device *steam, 434 u8 *data, int size, u8 report_id) 435 { 436 struct hid_report *r; 437 u8 *buf; 438 unsigned int retries = 50; 439 int ret; 440 u32 len; 441 442 /* 443 * All reports start with a two byte header. 444 * We must read at least two bytes to get a sensible output. 445 */ 446 if (size < 2) 447 return -EINVAL; 448 449 r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[report_id]; 450 if (!r) { 451 hid_err(steam->hdev, "No HID_FEATURE_REPORT present for ID %u\n", report_id); 452 return -EINVAL; 453 } 454 455 len = hid_report_len(r); 456 if (len < 64) 457 return -EINVAL; 458 459 buf = hid_alloc_report_buf(r, GFP_KERNEL); 460 if (!buf) 461 return -ENOMEM; 462 463 /* 464 * The report ID is consistent, so strip the first byte from the output. 465 * hid_report_len() is not counting the report ID, so +1 to the length 466 * or else we get a EOVERFLOW. We are safe from a buffer overflow 467 * because hid_alloc_report_buf() allocates +7 bytes. 468 */ 469 if (!(steam->quirks & STEAM_QUIRK_IBEX)) 470 len += 1; 471 472 /* 473 * Sometimes the wireless controller fails with EPIPE 474 * when sending a feature report. 475 * Doing a HID_REQ_GET_REPORT and waiting for a while 476 * seems to fix that. 477 */ 478 do { 479 ret = hid_hw_raw_request(steam->hdev, report_id, 480 buf, len, 481 HID_FEATURE_REPORT, HID_REQ_GET_REPORT); 482 if (ret != -EPIPE) 483 break; 484 msleep(20); 485 } while (--retries); 486 if (ret > 0) { 487 /* Remove the report ID from the return buffer */ 488 ret--; 489 size = min(size, ret); 490 memcpy(data, buf + 1, size); 491 } 492 493 kfree(buf); 494 /* 495 * Don't log if the failure is -ENODEV, as this 496 * can happen normally on disconnect. 497 */ 498 if (ret < 0 && ret != -ENODEV) 499 hid_err(steam->hdev, "%s: error %d\n", __func__, ret); 500 else if (ret > 0) 501 hid_dbg(steam->hdev, "Received report %*ph\n", size, data); 502 if (ret < 0) 503 return ret; 504 505 if (ret < 2) { 506 hid_err(steam->hdev, "%s: reply too short\n", __func__); 507 return -EPROTO; 508 } 509 if (ret < data[1] + 2) { 510 hid_err(steam->hdev, "%s: expected %u bytes, read %i\n", 511 __func__, data[1] + 2, ret); 512 return -EPROTO; 513 } 514 return size; 515 } 516 517 static int steam_send_report_id(struct steam_device *steam, 518 u8 *cmd, int size, u8 report_id) 519 { 520 struct hid_report *r; 521 u8 *buf; 522 unsigned int retries = 50; 523 int ret; 524 u32 len; 525 526 r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[report_id]; 527 if (!r) { 528 hid_err(steam->hdev, "No HID_FEATURE_REPORT present for ID %u\n", report_id); 529 return -EINVAL; 530 } 531 532 len = hid_report_len(r); 533 if (len < 64) 534 return -EINVAL; 535 536 buf = hid_alloc_report_buf(r, GFP_KERNEL); 537 if (!buf) 538 return -ENOMEM; 539 540 /* The report ID is always consistent */ 541 buf[0] = report_id; 542 memcpy(buf + 1, cmd, size); 543 544 hid_dbg(steam->hdev, "Sending report %*ph\n", size, cmd); 545 546 if (!(steam->quirks & STEAM_QUIRK_IBEX)) 547 len += 1; 548 549 /* 550 * Sometimes the wireless controller fails with EPIPE 551 * when sending a feature report. 552 * Doing a HID_REQ_SET_REPORT and waiting for a while 553 * seems to fix that. 554 */ 555 do { 556 ret = hid_hw_raw_request(steam->hdev, report_id, 557 buf, max(size + 1, len), 558 HID_FEATURE_REPORT, HID_REQ_SET_REPORT); 559 if (ret != -EPIPE) 560 break; 561 msleep(20); 562 } while (--retries); 563 564 kfree(buf); 565 /* 566 * Don't log if the failure is -ENODEV, as this 567 * can happen normally on disconnect. 568 */ 569 if (ret < 0 && ret != -ENODEV) 570 hid_err(steam->hdev, "%s: error %d (%*ph)\n", __func__, 571 ret, size, cmd); 572 return ret; 573 } 574 575 static int steam_send_report(struct steam_device *steam, 576 u8 *cmd, int size) 577 { 578 u8 report_id; 579 580 if (steam->quirks & STEAM_QUIRK_IBEX) 581 report_id = REPORT_ID_FEATURES_CONTROLLER; 582 else 583 report_id = 0; 584 585 return steam_send_report_id(steam, cmd, size, report_id); 586 } 587 588 static inline int steam_send_report_byte(struct steam_device *steam, u8 cmd) 589 { 590 return steam_send_report(steam, &cmd, 1); 591 } 592 593 static int steam_write_settings(struct steam_device *steam, 594 /* u8 reg, u16 val */...) 595 { 596 /* Send: 0x87 len (reg valLo valHi)* */ 597 u8 reg; 598 u16 val; 599 u8 cmd[64] = {ID_SET_SETTINGS_VALUES, 0x00}; 600 va_list args; 601 602 va_start(args, steam); 603 for (;;) { 604 reg = va_arg(args, int); 605 if (reg == 0) 606 break; 607 val = va_arg(args, int); 608 cmd[cmd[1] + 2] = reg; 609 cmd[cmd[1] + 3] = val & 0xff; 610 cmd[cmd[1] + 4] = val >> 8; 611 cmd[1] += 3; 612 } 613 va_end(args); 614 615 return steam_send_report(steam, cmd, 2 + cmd[1]); 616 } 617 618 static int steam_exchange_report_id(struct steam_device *steam, u8 *cmd, int csize, 619 u8 *reply, int rsize, u8 report_id) 620 { 621 unsigned int retries = 5; 622 int ret; 623 624 do { 625 ret = steam_send_report_id(steam, cmd, csize, report_id); 626 if (ret < 0) 627 return ret; 628 ret = steam_recv_report_id(steam, reply, rsize, report_id); 629 /* 630 * Sometimes this can fail on the first few tries on the Steam 631 * Controller (2015). It appears to be a firmware bug, and Steam 632 * itself just retries, so we should also retry a few times to 633 * see if we get it. 634 */ 635 if (ret == -EPROTO) 636 continue; 637 if (ret < 0) { 638 hid_err(steam->hdev, "%s: error reading reply (%*ph)\n", 639 __func__, csize, cmd); 640 return ret; 641 } 642 if (reply[0] == cmd[0] && reply[1] >= 1) 643 break; 644 if (retries > 0) 645 continue; 646 hid_err(steam->hdev, "%s: invalid reply (%*ph)\n", __func__, 647 rsize, reply); 648 return -EPROTO; 649 } while (retries--); 650 651 return ret; 652 } 653 654 static int steam_exchange_report(struct steam_device *steam, u8 *cmd, int csize, 655 u8 *reply, int rsize) 656 { 657 u8 report_id; 658 659 if (steam->quirks & STEAM_QUIRK_IBEX) 660 report_id = REPORT_ID_FEATURES_CONTROLLER; 661 else 662 report_id = 0; 663 664 return steam_exchange_report_id(steam, cmd, csize, reply, rsize, report_id); 665 } 666 667 static int steam_get_serial(struct steam_device *steam) 668 { 669 /* 670 * Send: 0xae 0x15 0x01 671 * Recv: 0xae 0x15 0x01 serialnumber 672 */ 673 int ret = 0; 674 u8 cmd[] = {ID_GET_STRING_ATTRIBUTE, sizeof(steam->serial_no), ATTRIB_STR_UNIT_SERIAL}; 675 u8 reply[3 + STEAM_SERIAL_LEN + 1] = {0}; 676 677 ret = steam_exchange_report(steam, cmd, sizeof(cmd), reply, sizeof(reply)); 678 if (ret < 0) 679 return ret; 680 if (reply[1] > sizeof(steam->serial_no) || reply[2] != ATTRIB_STR_UNIT_SERIAL) { 681 hid_err(steam->hdev, "%s: invalid reply (%*ph)\n", __func__, 682 (int)sizeof(reply), reply); 683 return -EIO; 684 } 685 strscpy(steam->serial_no, reply + 3, reply[1]); 686 return ret; 687 } 688 689 static int steam_get_attributes(struct steam_device *steam) 690 { 691 int ret = 0; 692 u8 cmd[] = {ID_GET_ATTRIBUTES_VALUES, 0}; 693 u8 reply[64] = {}; 694 u8 size; 695 int i; 696 struct steam_controller_attribute *attr; 697 698 ret = steam_exchange_report(steam, cmd, sizeof(cmd), reply, sizeof(reply)); 699 if (ret < 0) 700 return ret; 701 if (reply[1] < 2) { 702 hid_err(steam->hdev, "%s: invalid reply (%*ph)\n", __func__, 703 (int)sizeof(reply), reply); 704 return -EIO; 705 } 706 707 size = min(reply[1], sizeof(reply) - 2); 708 for (i = 0; i + sizeof(*attr) <= size; i += sizeof(*attr)) { 709 attr = (struct steam_controller_attribute *)&reply[i + 2]; 710 if (attr->tag == ATTRIB_CONNECTION_INTERVAL_IN_US) { 711 steam->sensor_update_rate_us = get_unaligned_le32(&attr->value); 712 hid_dbg(steam->hdev, "Sensor update rate: %uus\n", 713 steam->sensor_update_rate_us); 714 } 715 } 716 717 return 0; 718 } 719 720 static int steam_get_conn_status(struct steam_device *steam) 721 { 722 int ret = 0; 723 u8 cmd[] = {ID_DONGLE_GET_WIRELESS_STATE}; 724 u8 reply[3] = {}; 725 u8 report_id; 726 727 if (steam->quirks & STEAM_QUIRK_IBEX) 728 report_id = REPORT_ID_FEATURES_DONGLE; 729 else 730 report_id = 0; 731 732 guard(mutex)(&steam->report_mutex); 733 ret = steam_exchange_report_id(steam, cmd, sizeof(cmd), reply, sizeof(reply), report_id); 734 if (ret < 0) 735 return ret; 736 if (reply[0] != ID_DONGLE_GET_WIRELESS_STATE || reply[1] < 1) { 737 hid_err(steam->hdev, "%s: invalid reply (%*ph)\n", __func__, 738 (int)sizeof(reply), reply); 739 return -EIO; 740 } 741 742 return reply[2]; 743 } 744 745 /* 746 * Send a haptic pulse to the trackpads 747 * Duration and interval are measured in microseconds, count is the number 748 * of pulses to send for duration time with interval microseconds between them 749 * and gain is measured in decibels, ranging from -24 to +6 750 */ 751 static inline int steam_haptic_pulse(struct steam_device *steam, u8 pad, 752 u16 duration, u16 interval, u16 count, u8 gain) 753 { 754 int ret; 755 756 /* Left and right are swapped on this report for legacy reasons */ 757 if (pad < STEAM_PAD_BOTH) 758 pad ^= 1; 759 760 if (steam->quirks & STEAM_QUIRK_IBEX) { 761 struct steam_ibex_output_report *report = 762 kzalloc(sizeof(struct steam_ibex_output_report), GFP_KERNEL); 763 764 if (!report) 765 return -ENOMEM; 766 767 report->id = REPORT_ID_HAPTIC_PULSE; 768 report->pulse.side = pad; 769 put_unaligned_le16(duration, &report->pulse.on_us); 770 put_unaligned_le16(interval, &report->pulse.off_us); 771 put_unaligned_le16(count, &report->pulse.repeat_count); 772 773 ret = hid_hw_output_report(steam->hdev, (u8 *)report, 8); 774 775 kfree(report); 776 } else { 777 u8 report[10] = {ID_TRIGGER_HAPTIC_PULSE, 8, pad}; 778 779 report[3] = duration & 0xFF; 780 report[4] = duration >> 8; 781 report[5] = interval & 0xFF; 782 report[6] = interval >> 8; 783 report[7] = count & 0xFF; 784 report[8] = count >> 8; 785 report[9] = gain; 786 787 ret = steam_send_report(steam, report, 10); 788 } 789 790 return ret; 791 } 792 793 static inline int steam_haptic_rumble(struct steam_device *steam, 794 u16 intensity, u16 left_speed, u16 right_speed, 795 u8 left_gain, u8 right_gain) 796 { 797 int ret; 798 799 if (steam->quirks & STEAM_QUIRK_IBEX) { 800 struct steam_ibex_output_report *report = 801 kzalloc(sizeof(struct steam_ibex_output_report), GFP_KERNEL); 802 803 if (!report) 804 return -ENOMEM; 805 806 report->id = REPORT_ID_HAPTIC_RUMBLE; 807 put_unaligned_le16(intensity, &report->rumble.intensity); 808 put_unaligned_le16(left_speed, &report->rumble.left.speed); 809 report->rumble.left.gain = left_gain; 810 put_unaligned_le16(right_speed, &report->rumble.right.speed); 811 report->rumble.right.gain = right_gain; 812 813 ret = hid_hw_output_report(steam->hdev, (u8 *)report, 10); 814 815 kfree(report); 816 } else { 817 u8 report[11] = {ID_TRIGGER_RUMBLE_CMD, 9}; 818 819 report[3] = intensity & 0xFF; 820 report[4] = intensity >> 8; 821 report[5] = left_speed & 0xFF; 822 report[6] = left_speed >> 8; 823 report[7] = right_speed & 0xFF; 824 report[8] = right_speed >> 8; 825 report[9] = left_gain; 826 report[10] = right_gain; 827 828 ret = steam_send_report(steam, report, sizeof(report)); 829 } 830 return ret; 831 } 832 833 static void steam_haptic_rumble_cb(struct work_struct *work) 834 { 835 struct steam_device *steam = container_of(work, struct steam_device, 836 rumble_work); 837 838 mutex_lock(&steam->report_mutex); 839 steam_haptic_rumble(steam, 0, steam->rumble_left, 840 steam->rumble_right, 2, 0); 841 mutex_unlock(&steam->report_mutex); 842 } 843 844 static void steam_coalesce_rumble_cb(struct work_struct *work) 845 { 846 struct steam_device *steam = container_of(to_delayed_work(work), 847 struct steam_device, 848 coalesce_rumble_work); 849 850 mutex_lock(&steam->report_mutex); 851 steam_haptic_rumble(steam, 0, steam->rumble_left, 852 steam->rumble_right, 2, 0); 853 mutex_unlock(&steam->report_mutex); 854 855 if (steam->rumble_left || steam->rumble_right) 856 schedule_delayed_work(&steam->coalesce_rumble_work, HZ / 20); 857 } 858 859 #ifdef CONFIG_STEAM_FF 860 static int steam_play_effect(struct input_dev *dev, void *data, 861 struct ff_effect *effect) 862 { 863 struct steam_device *steam = input_get_drvdata(dev); 864 865 steam->rumble_left = effect->u.rumble.strong_magnitude; 866 steam->rumble_right = effect->u.rumble.weak_magnitude; 867 868 /* 869 * The interface gets somewhat overloaded when too many rumble 870 * packets are sent in a row, so Steam throttles it to 20 Hz 871 */ 872 if (delayed_work_pending(&steam->coalesce_rumble_work)) 873 return 0; 874 875 schedule_delayed_work(&steam->coalesce_rumble_work, HZ / 20); 876 return schedule_work(&steam->rumble_work); 877 } 878 #endif 879 880 static void steam_set_lizard_mode(struct steam_device *steam, bool enable) 881 { 882 if (steam->gamepad_mode) 883 enable = false; 884 885 if (enable) { 886 /* enable esc, enter, cursors */ 887 steam_send_report_byte(steam, ID_SET_DEFAULT_DIGITAL_MAPPINGS); 888 /* reset settings */ 889 steam_send_report_byte(steam, ID_LOAD_DEFAULT_SETTINGS); 890 } else { 891 /* disable esc, enter, cursor */ 892 steam_send_report_byte(steam, ID_CLEAR_DIGITAL_MAPPINGS); 893 894 if (steam->quirks & (STEAM_QUIRK_DECK | STEAM_QUIRK_IBEX)) 895 steam_write_settings(steam, 896 /* disable lizard mode */ 897 SETTING_LIZARD_MODE, 0, 898 /* disable watchdog that tests if Steam is active */ 899 SETTING_STEAM_WATCHDOG_ENABLE, 0, 900 0); 901 else 902 steam_write_settings(steam, 903 SETTING_LEFT_TRACKPAD_MODE, TRACKPAD_NONE, /* disable mouse */ 904 SETTING_RIGHT_TRACKPAD_MODE, TRACKPAD_NONE, /* disable mouse */ 905 0); 906 } 907 } 908 909 static int steam_input_open(struct input_dev *dev) 910 { 911 struct steam_device *steam = input_get_drvdata(dev); 912 unsigned long flags; 913 bool client_opened; 914 915 /* 916 * Disabling lizard mode automatically is only done on the Steam 917 * Controller. On the Steam Deck, this is toggled manually by holding 918 * the options button instead, handled by steam_mode_switch_cb. 919 */ 920 if (!(steam->quirks & (STEAM_QUIRK_DECK | STEAM_QUIRK_IBEX))) { 921 spin_lock_irqsave(&steam->lock, flags); 922 client_opened = steam->client_opened; 923 spin_unlock_irqrestore(&steam->lock, flags); 924 guard(mutex)(&steam->report_mutex); 925 if (!client_opened && lizard_mode) 926 steam_set_lizard_mode(steam, false); 927 } 928 929 return 0; 930 } 931 932 static void steam_input_close(struct input_dev *dev) 933 { 934 struct steam_device *steam = input_get_drvdata(dev); 935 unsigned long flags; 936 bool client_opened; 937 938 if (!(steam->quirks & (STEAM_QUIRK_DECK | STEAM_QUIRK_IBEX))) { 939 spin_lock_irqsave(&steam->lock, flags); 940 client_opened = steam->client_opened; 941 spin_unlock_irqrestore(&steam->lock, flags); 942 guard(mutex)(&steam->report_mutex); 943 if (!client_opened && lizard_mode) 944 steam_set_lizard_mode(steam, true); 945 } 946 } 947 948 static int steam_sensor_open(struct input_dev *dev) 949 { 950 struct steam_device *steam = input_get_drvdata(dev); 951 952 scoped_guard(spinlock_irqsave, &steam->lock) { 953 if (steam->client_opened) 954 return 0; 955 } 956 957 guard(mutex)(&steam->report_mutex); 958 steam_write_settings(steam, SETTING_IMU_MODE, 959 SETTING_IMU_MODE_SEND_RAW_ACCEL | SETTING_IMU_MODE_SEND_RAW_GYRO, 960 0); 961 962 return 0; 963 } 964 965 static void steam_sensor_close(struct input_dev *dev) 966 { 967 struct steam_device *steam = input_get_drvdata(dev); 968 969 scoped_guard(spinlock_irqsave, &steam->lock) { 970 if (steam->client_opened) 971 return; 972 } 973 974 guard(mutex)(&steam->report_mutex); 975 steam_write_settings(steam, SETTING_IMU_MODE, 0, 0); 976 } 977 978 static enum power_supply_property steam_battery_props[] = { 979 POWER_SUPPLY_PROP_PRESENT, 980 POWER_SUPPLY_PROP_SCOPE, 981 POWER_SUPPLY_PROP_VOLTAGE_NOW, 982 POWER_SUPPLY_PROP_CAPACITY, 983 }; 984 985 static int steam_battery_get_property(struct power_supply *psy, 986 enum power_supply_property psp, 987 union power_supply_propval *val) 988 { 989 struct steam_device *steam = power_supply_get_drvdata(psy); 990 unsigned long flags; 991 u16 volts; 992 u16 curr; 993 u16 temp; 994 u8 batt; 995 u8 status; 996 int ret = 0; 997 998 spin_lock_irqsave(&steam->lock, flags); 999 volts = steam->battery_voltage; 1000 curr = steam->battery_current; 1001 batt = steam->battery_charge; 1002 temp = steam->battery_temp; 1003 status = steam->battery_status; 1004 spin_unlock_irqrestore(&steam->lock, flags); 1005 1006 switch (psp) { 1007 case POWER_SUPPLY_PROP_PRESENT: 1008 val->intval = 1; 1009 break; 1010 case POWER_SUPPLY_PROP_STATUS: 1011 val->intval = status; 1012 break; 1013 case POWER_SUPPLY_PROP_SCOPE: 1014 val->intval = POWER_SUPPLY_SCOPE_DEVICE; 1015 break; 1016 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 1017 val->intval = volts * 1000; /* mV -> uV */ 1018 break; 1019 case POWER_SUPPLY_PROP_CURRENT_NOW: 1020 val->intval = curr * 1000; /* mA -> uA */ 1021 break; 1022 case POWER_SUPPLY_PROP_TEMP: 1023 val->intval = temp / 100; /* thousandths °C -> tenths °C */ 1024 break; 1025 case POWER_SUPPLY_PROP_CAPACITY: 1026 val->intval = batt; 1027 break; 1028 default: 1029 ret = -EINVAL; 1030 break; 1031 } 1032 return ret; 1033 } 1034 1035 static enum power_supply_property steam_ibex_battery_props[] = { 1036 POWER_SUPPLY_PROP_PRESENT, 1037 POWER_SUPPLY_PROP_STATUS, 1038 POWER_SUPPLY_PROP_SCOPE, 1039 POWER_SUPPLY_PROP_VOLTAGE_NOW, 1040 POWER_SUPPLY_PROP_CURRENT_NOW, 1041 POWER_SUPPLY_PROP_CAPACITY, 1042 POWER_SUPPLY_PROP_TEMP, 1043 }; 1044 1045 static int steam_battery_register(struct steam_device *steam) 1046 { 1047 struct power_supply *battery; 1048 struct power_supply_config battery_cfg = { .drv_data = steam, }; 1049 unsigned long flags; 1050 int ret; 1051 1052 steam->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY; 1053 if (steam->quirks & STEAM_QUIRK_IBEX) { 1054 steam->battery_desc.properties = steam_ibex_battery_props; 1055 steam->battery_desc.num_properties = ARRAY_SIZE(steam_ibex_battery_props); 1056 /* 1057 * Ibex needs a shorter name as it has a temperature and the 1058 * thermal zone name length limit is 20 characters. It's more 1059 * ambiguous sounding, so let's only use it when needed. 1060 */ 1061 steam->battery_desc.name = devm_kasprintf(&steam->hdev->dev, 1062 GFP_KERNEL, "steam-%s", 1063 steam->serial_no); 1064 } else { 1065 steam->battery_desc.properties = steam_battery_props; 1066 steam->battery_desc.num_properties = ARRAY_SIZE(steam_battery_props); 1067 steam->battery_desc.name = devm_kasprintf(&steam->hdev->dev, 1068 GFP_KERNEL, "steam-controller-%s-battery", 1069 steam->serial_no); 1070 } 1071 steam->battery_desc.get_property = steam_battery_get_property; 1072 if (!steam->battery_desc.name) 1073 return -ENOMEM; 1074 1075 /* avoid the warning of 0% battery while waiting for the first info */ 1076 spin_lock_irqsave(&steam->lock, flags); 1077 steam->battery_voltage = 3000; 1078 steam->battery_charge = 100; 1079 steam->battery_current = 0; 1080 steam->battery_temp = 20000; 1081 steam->battery_status = POWER_SUPPLY_STATUS_UNKNOWN; 1082 spin_unlock_irqrestore(&steam->lock, flags); 1083 1084 battery = power_supply_register(&steam->hdev->dev, 1085 &steam->battery_desc, &battery_cfg); 1086 if (IS_ERR(battery)) { 1087 ret = PTR_ERR(battery); 1088 devm_kfree(&steam->hdev->dev, steam->battery_desc.name); 1089 hid_err(steam->hdev, 1090 "%s:power_supply_register failed with error %d\n", 1091 __func__, ret); 1092 return ret; 1093 } 1094 rcu_assign_pointer(steam->battery, battery); 1095 power_supply_powers(battery, &steam->hdev->dev); 1096 return 0; 1097 } 1098 1099 static int steam_input_register(struct steam_device *steam) 1100 { 1101 struct hid_device *hdev = steam->hdev; 1102 struct input_dev *input; 1103 int ret; 1104 1105 rcu_read_lock(); 1106 input = rcu_dereference(steam->input); 1107 rcu_read_unlock(); 1108 if (input) { 1109 dbg_hid("%s: already connected\n", __func__); 1110 return 0; 1111 } 1112 1113 input = input_allocate_device(); 1114 if (!input) 1115 return -ENOMEM; 1116 1117 input_set_drvdata(input, steam); 1118 input->dev.parent = &hdev->dev; 1119 input->open = steam_input_open; 1120 input->close = steam_input_close; 1121 1122 input->name = (steam->quirks & STEAM_QUIRK_WIRELESS) ? "Wireless Steam Controller" : 1123 (steam->quirks & STEAM_QUIRK_DECK) ? "Steam Deck" : 1124 "Steam Controller"; 1125 input->phys = hdev->phys; 1126 input->uniq = steam->serial_no; 1127 input->id.bustype = hdev->bus; 1128 input->id.vendor = hdev->vendor; 1129 input->id.product = hdev->product; 1130 input->id.version = hdev->version; 1131 1132 input_set_capability(input, EV_KEY, BTN_TR2); 1133 input_set_capability(input, EV_KEY, BTN_TL2); 1134 input_set_capability(input, EV_KEY, BTN_TR); 1135 input_set_capability(input, EV_KEY, BTN_TL); 1136 input_set_capability(input, EV_KEY, BTN_Y); 1137 input_set_capability(input, EV_KEY, BTN_B); 1138 input_set_capability(input, EV_KEY, BTN_X); 1139 input_set_capability(input, EV_KEY, BTN_A); 1140 input_set_capability(input, EV_KEY, BTN_DPAD_UP); 1141 input_set_capability(input, EV_KEY, BTN_DPAD_RIGHT); 1142 input_set_capability(input, EV_KEY, BTN_DPAD_LEFT); 1143 input_set_capability(input, EV_KEY, BTN_DPAD_DOWN); 1144 input_set_capability(input, EV_KEY, BTN_SELECT); 1145 input_set_capability(input, EV_KEY, BTN_MODE); 1146 input_set_capability(input, EV_KEY, BTN_START); 1147 input_set_capability(input, EV_KEY, BTN_THUMBR); 1148 input_set_capability(input, EV_KEY, BTN_THUMBL); 1149 input_set_capability(input, EV_KEY, BTN_THUMB); 1150 input_set_capability(input, EV_KEY, BTN_THUMB2); 1151 input_set_capability(input, EV_KEY, BTN_GRIPL); 1152 input_set_capability(input, EV_KEY, BTN_GRIPR); 1153 if (steam->quirks & (STEAM_QUIRK_DECK | STEAM_QUIRK_IBEX)) { 1154 input_set_capability(input, EV_KEY, BTN_BASE); 1155 input_set_capability(input, EV_KEY, BTN_GRIPL2); 1156 input_set_capability(input, EV_KEY, BTN_GRIPR2); 1157 } 1158 1159 input_set_abs_params(input, ABS_X, -32767, 32767, 0, 0); 1160 input_set_abs_params(input, ABS_Y, -32767, 32767, 0, 0); 1161 1162 input_set_abs_params(input, ABS_HAT0X, -32767, 32767, 1163 STEAM_PAD_FUZZ, 0); 1164 input_set_abs_params(input, ABS_HAT0Y, -32767, 32767, 1165 STEAM_PAD_FUZZ, 0); 1166 1167 if (steam->quirks & (STEAM_QUIRK_DECK | STEAM_QUIRK_IBEX)) { 1168 input_set_abs_params(input, ABS_HAT2Y, 0, 32767, 0, 0); 1169 input_set_abs_params(input, ABS_HAT2X, 0, 32767, 0, 0); 1170 1171 input_set_abs_params(input, ABS_RX, -32767, 32767, 0, 0); 1172 input_set_abs_params(input, ABS_RY, -32767, 32767, 0, 0); 1173 1174 input_set_abs_params(input, ABS_HAT1X, -32767, 32767, 1175 STEAM_PAD_FUZZ, 0); 1176 input_set_abs_params(input, ABS_HAT1Y, -32767, 32767, 1177 STEAM_PAD_FUZZ, 0); 1178 1179 input_abs_set_res(input, ABS_X, STEAM_DECK_JOYSTICK_RESOLUTION); 1180 input_abs_set_res(input, ABS_Y, STEAM_DECK_JOYSTICK_RESOLUTION); 1181 input_abs_set_res(input, ABS_RX, STEAM_DECK_JOYSTICK_RESOLUTION); 1182 input_abs_set_res(input, ABS_RY, STEAM_DECK_JOYSTICK_RESOLUTION); 1183 input_abs_set_res(input, ABS_HAT1X, STEAM_PAD_RESOLUTION); 1184 input_abs_set_res(input, ABS_HAT1Y, STEAM_PAD_RESOLUTION); 1185 input_abs_set_res(input, ABS_HAT2Y, STEAM_DECK_TRIGGER_RESOLUTION); 1186 input_abs_set_res(input, ABS_HAT2X, STEAM_DECK_TRIGGER_RESOLUTION); 1187 } else { 1188 input_set_abs_params(input, ABS_HAT2Y, 0, 255, 0, 0); 1189 input_set_abs_params(input, ABS_HAT2X, 0, 255, 0, 0); 1190 1191 input_set_abs_params(input, ABS_RX, -32767, 32767, 1192 STEAM_PAD_FUZZ, 0); 1193 input_set_abs_params(input, ABS_RY, -32767, 32767, 1194 STEAM_PAD_FUZZ, 0); 1195 1196 input_abs_set_res(input, ABS_X, STEAM_JOYSTICK_RESOLUTION); 1197 input_abs_set_res(input, ABS_Y, STEAM_JOYSTICK_RESOLUTION); 1198 input_abs_set_res(input, ABS_RX, STEAM_PAD_RESOLUTION); 1199 input_abs_set_res(input, ABS_RY, STEAM_PAD_RESOLUTION); 1200 input_abs_set_res(input, ABS_HAT2Y, STEAM_TRIGGER_RESOLUTION); 1201 input_abs_set_res(input, ABS_HAT2X, STEAM_TRIGGER_RESOLUTION); 1202 } 1203 input_abs_set_res(input, ABS_HAT0X, STEAM_PAD_RESOLUTION); 1204 input_abs_set_res(input, ABS_HAT0Y, STEAM_PAD_RESOLUTION); 1205 1206 #ifdef CONFIG_STEAM_FF 1207 if (steam->quirks & (STEAM_QUIRK_DECK | STEAM_QUIRK_IBEX)) { 1208 input_set_capability(input, EV_FF, FF_RUMBLE); 1209 ret = input_ff_create_memless(input, NULL, steam_play_effect); 1210 if (ret) 1211 goto input_register_fail; 1212 } 1213 #endif 1214 1215 ret = input_register_device(input); 1216 if (ret) 1217 goto input_register_fail; 1218 1219 rcu_assign_pointer(steam->input, input); 1220 return 0; 1221 1222 input_register_fail: 1223 input_free_device(input); 1224 return ret; 1225 } 1226 1227 static int steam_sensors_register(struct steam_device *steam) 1228 { 1229 struct hid_device *hdev = steam->hdev; 1230 struct input_dev *sensors; 1231 int ret; 1232 bool needs_open_close; 1233 1234 rcu_read_lock(); 1235 sensors = rcu_dereference(steam->sensors); 1236 rcu_read_unlock(); 1237 if (sensors) { 1238 dbg_hid("%s: already connected\n", __func__); 1239 return 0; 1240 } 1241 1242 sensors = input_allocate_device(); 1243 if (!sensors) 1244 return -ENOMEM; 1245 1246 input_set_drvdata(sensors, steam); 1247 sensors->dev.parent = &hdev->dev; 1248 1249 /* 1250 * The open/close calls are needed in these specific cases: 1251 * 1252 * - Steam Controller (2015): Always 1253 * - Steam Deck: Never 1254 * - Steam Controller (2026): Only when wireless/BLE 1255 */ 1256 if (steam->quirks & STEAM_QUIRK_DECK) 1257 needs_open_close = false; 1258 else if (steam->quirks & (STEAM_QUIRK_WIRELESS | STEAM_QUIRK_BLE)) 1259 /* Both wireless Steam Controller setups */ 1260 needs_open_close = true; 1261 else if (steam->quirks & STEAM_QUIRK_IBEX) 1262 /* Wired Steam Controller (2026) */ 1263 needs_open_close = false; 1264 else 1265 /* Wired Steam Controller (2015) */ 1266 needs_open_close = true; 1267 1268 if (needs_open_close) { 1269 sensors->open = steam_sensor_open; 1270 sensors->close = steam_sensor_close; 1271 } 1272 1273 sensors->name = steam->quirks & STEAM_QUIRK_DECK ? 1274 "Steam Deck Motion Sensors" : 1275 "Steam Controller Motion Sensors"; 1276 sensors->phys = hdev->phys; 1277 sensors->uniq = steam->serial_no; 1278 sensors->id.bustype = hdev->bus; 1279 sensors->id.vendor = hdev->vendor; 1280 sensors->id.product = hdev->product; 1281 sensors->id.version = hdev->version; 1282 1283 __set_bit(INPUT_PROP_ACCELEROMETER, sensors->propbit); 1284 __set_bit(EV_MSC, sensors->evbit); 1285 __set_bit(MSC_TIMESTAMP, sensors->mscbit); 1286 1287 if (steam->quirks & STEAM_QUIRK_DECK) { 1288 input_set_abs_params(sensors, ABS_X, -STEAM_ACCEL_RANGE, 1289 STEAM_ACCEL_RANGE, STEAM_DECK_ACCEL_FUZZ, 0); 1290 input_set_abs_params(sensors, ABS_Y, -STEAM_ACCEL_RANGE, 1291 STEAM_ACCEL_RANGE, STEAM_DECK_ACCEL_FUZZ, 0); 1292 input_set_abs_params(sensors, ABS_Z, -STEAM_ACCEL_RANGE, 1293 STEAM_ACCEL_RANGE, STEAM_DECK_ACCEL_FUZZ, 0); 1294 } else { 1295 input_set_abs_params(sensors, ABS_X, -STEAM_ACCEL_RANGE, 1296 STEAM_ACCEL_RANGE, STEAM_ACCEL_FUZZ, 0); 1297 input_set_abs_params(sensors, ABS_Y, -STEAM_ACCEL_RANGE, 1298 STEAM_ACCEL_RANGE, STEAM_ACCEL_FUZZ, 0); 1299 input_set_abs_params(sensors, ABS_Z, -STEAM_ACCEL_RANGE, 1300 STEAM_ACCEL_RANGE, STEAM_ACCEL_FUZZ, 0); 1301 } 1302 input_abs_set_res(sensors, ABS_X, STEAM_ACCEL_RES_PER_G); 1303 input_abs_set_res(sensors, ABS_Y, STEAM_ACCEL_RES_PER_G); 1304 input_abs_set_res(sensors, ABS_Z, STEAM_ACCEL_RES_PER_G); 1305 1306 input_set_abs_params(sensors, ABS_RX, -STEAM_GYRO_RANGE, 1307 STEAM_GYRO_RANGE, STEAM_GYRO_FUZZ, 0); 1308 input_set_abs_params(sensors, ABS_RY, -STEAM_GYRO_RANGE, 1309 STEAM_GYRO_RANGE, STEAM_GYRO_FUZZ, 0); 1310 input_set_abs_params(sensors, ABS_RZ, -STEAM_GYRO_RANGE, 1311 STEAM_GYRO_RANGE, STEAM_GYRO_FUZZ, 0); 1312 input_abs_set_res(sensors, ABS_RX, STEAM_GYRO_RES_PER_DPS); 1313 input_abs_set_res(sensors, ABS_RY, STEAM_GYRO_RES_PER_DPS); 1314 input_abs_set_res(sensors, ABS_RZ, STEAM_GYRO_RES_PER_DPS); 1315 1316 ret = input_register_device(sensors); 1317 if (ret) 1318 goto sensors_register_fail; 1319 1320 rcu_assign_pointer(steam->sensors, sensors); 1321 return 0; 1322 1323 sensors_register_fail: 1324 input_free_device(sensors); 1325 return ret; 1326 } 1327 1328 static void steam_input_unregister(struct steam_device *steam) 1329 { 1330 struct input_dev *input; 1331 rcu_read_lock(); 1332 input = rcu_dereference(steam->input); 1333 rcu_read_unlock(); 1334 if (!input) 1335 return; 1336 RCU_INIT_POINTER(steam->input, NULL); 1337 synchronize_rcu(); 1338 input_unregister_device(input); 1339 } 1340 1341 static void steam_sensors_unregister(struct steam_device *steam) 1342 { 1343 struct input_dev *sensors; 1344 1345 rcu_read_lock(); 1346 sensors = rcu_dereference(steam->sensors); 1347 rcu_read_unlock(); 1348 1349 if (!sensors) 1350 return; 1351 RCU_INIT_POINTER(steam->sensors, NULL); 1352 synchronize_rcu(); 1353 input_unregister_device(sensors); 1354 } 1355 1356 static void steam_battery_unregister(struct steam_device *steam) 1357 { 1358 struct power_supply *battery; 1359 1360 rcu_read_lock(); 1361 battery = rcu_dereference(steam->battery); 1362 rcu_read_unlock(); 1363 1364 if (!battery) 1365 return; 1366 RCU_INIT_POINTER(steam->battery, NULL); 1367 synchronize_rcu(); 1368 power_supply_unregister(battery); 1369 devm_kfree(&steam->hdev->dev, steam->battery_desc.name); 1370 } 1371 1372 static int steam_register(struct steam_device *steam) 1373 { 1374 int ret; 1375 1376 mutex_lock(&steam->registration_mutex); 1377 /* 1378 * This function can be called several times in a row with the 1379 * wireless adaptor, without steam_unregister() between them, because 1380 * another client send a get_connection_status command, for example. 1381 */ 1382 if (steam->registered) { 1383 mutex_unlock(&steam->registration_mutex); 1384 return 0; 1385 } 1386 1387 mutex_lock(&steam->report_mutex); 1388 /* 1389 * Unlikely, but getting the serial could fail, and it is not so 1390 * important, so make up a serial number and go on. 1391 */ 1392 if (steam_get_serial(steam) < 0 || !steam->serial_no[0]) 1393 strscpy(steam->serial_no, "XXXXXXXXXX", 1394 sizeof(steam->serial_no)); 1395 1396 ret = steam_get_attributes(steam); 1397 if (ret < 0) 1398 hid_err(steam->hdev, 1399 "%s:steam_get_attributes failed with error %d\n", 1400 __func__, ret); 1401 1402 hid_info(steam->hdev, "Steam %s '%s' connected", 1403 steam->quirks & STEAM_QUIRK_DECK ? "Deck" : "Controller", 1404 steam->serial_no); 1405 1406 steam_set_lizard_mode(steam, lizard_mode); 1407 mutex_unlock(&steam->report_mutex); 1408 1409 /* ignore battery errors, we can live without it */ 1410 if (steam->quirks & (STEAM_QUIRK_WIRELESS | STEAM_QUIRK_IBEX)) 1411 steam_battery_register(steam); 1412 1413 ret = steam_input_register(steam); 1414 if (ret != 0) 1415 goto steam_register_input_fail; 1416 ret = steam_sensors_register(steam); 1417 if (ret != 0) 1418 goto steam_register_sensors_fail; 1419 1420 steam->registered = true; 1421 mutex_unlock(&steam->registration_mutex); 1422 mutex_lock(&steam_devices_lock); 1423 if (list_empty(&steam->list)) 1424 list_add(&steam->list, &steam_devices); 1425 mutex_unlock(&steam_devices_lock); 1426 return 0; 1427 1428 steam_register_sensors_fail: 1429 steam_input_unregister(steam); 1430 steam_register_input_fail: 1431 steam_battery_unregister(steam); 1432 mutex_unlock(&steam->registration_mutex); 1433 cancel_work_sync(&steam->rumble_work); 1434 cancel_delayed_work_sync(&steam->mode_switch); 1435 cancel_delayed_work_sync(&steam->coalesce_rumble_work); 1436 return ret; 1437 } 1438 1439 static void steam_unregister(struct steam_device *steam) 1440 { 1441 if (!steam->registered) 1442 return; 1443 1444 hid_info(steam->hdev, "Steam %s '%s' disconnected", 1445 steam->quirks & STEAM_QUIRK_DECK ? "Deck" : "Controller", 1446 steam->serial_no); 1447 mutex_lock(&steam->registration_mutex); 1448 steam->registered = false; 1449 steam_battery_unregister(steam); 1450 steam_sensors_unregister(steam); 1451 steam_input_unregister(steam); 1452 mutex_unlock(&steam->registration_mutex); 1453 cancel_work_sync(&steam->rumble_work); 1454 cancel_delayed_work_sync(&steam->mode_switch); 1455 cancel_delayed_work_sync(&steam->coalesce_rumble_work); 1456 mutex_lock(&steam_devices_lock); 1457 list_del_init(&steam->list); 1458 mutex_unlock(&steam_devices_lock); 1459 } 1460 1461 static void steam_work_connect_cb(struct work_struct *work) 1462 { 1463 struct steam_device *steam = container_of(work, struct steam_device, 1464 work_connect); 1465 1466 unsigned long flags; 1467 bool connected; 1468 bool opened; 1469 int ret; 1470 1471 spin_lock_irqsave(&steam->lock, flags); 1472 opened = steam->client_opened; 1473 connected = steam->connected; 1474 spin_unlock_irqrestore(&steam->lock, flags); 1475 1476 if (connected && !opened) { 1477 ret = steam_register(steam); 1478 if (ret) 1479 hid_err(steam->hdev, 1480 "%s:steam_register failed with error %d\n", 1481 __func__, ret); 1482 } else { 1483 steam_unregister(steam); 1484 } 1485 } 1486 1487 static void steam_mode_switch_cb(struct work_struct *work) 1488 { 1489 struct steam_device *steam = container_of(to_delayed_work(work), 1490 struct steam_device, mode_switch); 1491 unsigned long flags; 1492 bool client_opened; 1493 bool gamepad_mode; 1494 1495 if (!lizard_mode) 1496 return; 1497 1498 spin_lock_irqsave(&steam->lock, flags); 1499 steam->gamepad_mode = !steam->gamepad_mode; 1500 gamepad_mode = steam->gamepad_mode; 1501 client_opened = steam->client_opened; 1502 spin_unlock_irqrestore(&steam->lock, flags); 1503 1504 hid_dbg(steam->hdev, "%s: switching gamepad mode to %i\n", __func__, gamepad_mode); 1505 if (gamepad_mode) { 1506 guard(mutex)(&steam->report_mutex); 1507 steam_set_lizard_mode(steam, false); 1508 } else { 1509 struct input_dev *input; 1510 struct input_dev *sensors; 1511 1512 if (!client_opened) { 1513 guard(mutex)(&steam->report_mutex); 1514 steam_set_lizard_mode(steam, lizard_mode); 1515 } 1516 1517 /* 1518 * Zero out inputs so it doesn't look like we're holding 1519 * anything indefinitely. 1520 */ 1521 guard(spinlock_irqsave)(&steam->lock); 1522 rcu_read_lock(); 1523 input = rcu_dereference(steam->input); 1524 if (likely(input)) { 1525 input_report_key(input, BTN_TR2, 0); 1526 input_report_key(input, BTN_TL2, 0); 1527 input_report_key(input, BTN_TR, 0); 1528 input_report_key(input, BTN_TL, 0); 1529 input_report_key(input, BTN_Y, 0); 1530 input_report_key(input, BTN_B, 0); 1531 input_report_key(input, BTN_X, 0); 1532 input_report_key(input, BTN_A, 0); 1533 input_report_key(input, BTN_DPAD_UP, 0); 1534 input_report_key(input, BTN_DPAD_RIGHT, 0); 1535 input_report_key(input, BTN_DPAD_LEFT, 0); 1536 input_report_key(input, BTN_DPAD_DOWN, 0); 1537 input_report_key(input, BTN_SELECT, 0); 1538 input_report_key(input, BTN_MODE, 0); 1539 input_report_key(input, BTN_START, 0); 1540 input_report_key(input, BTN_THUMBR, 0); 1541 input_report_key(input, BTN_THUMBL, 0); 1542 input_report_key(input, BTN_THUMB, 0); 1543 input_report_key(input, BTN_THUMB2, 0); 1544 input_report_key(input, BTN_GRIPL, 0); 1545 input_report_key(input, BTN_GRIPR, 0); 1546 1547 input_report_abs(input, ABS_X, 0); 1548 input_report_abs(input, ABS_Y, 0); 1549 input_report_abs(input, ABS_RX, 0); 1550 input_report_abs(input, ABS_RY, 0); 1551 input_report_abs(input, ABS_HAT0X, 0); 1552 input_report_abs(input, ABS_HAT0Y, 0); 1553 input_report_abs(input, ABS_HAT2Y, 0); 1554 input_report_abs(input, ABS_HAT2X, 0); 1555 1556 if (steam->quirks & (STEAM_QUIRK_DECK | STEAM_QUIRK_IBEX)) { 1557 input_report_key(input, BTN_BASE, 0); 1558 input_report_key(input, BTN_GRIPL2, 0); 1559 input_report_key(input, BTN_GRIPR2, 0); 1560 1561 input_report_abs(input, ABS_HAT1X, 0); 1562 input_report_abs(input, ABS_HAT1Y, 0); 1563 } 1564 1565 input_sync(input); 1566 } 1567 sensors = rcu_dereference(steam->sensors); 1568 if (likely(sensors)) { 1569 /* Skip accelerometers since 0 isn't a neutral input */ 1570 1571 input_report_abs(sensors, ABS_RX, 0); 1572 input_report_abs(sensors, ABS_RY, 0); 1573 input_report_abs(sensors, ABS_RZ, 0); 1574 1575 input_sync(sensors); 1576 } 1577 rcu_read_unlock(); 1578 } 1579 1580 guard(mutex)(&steam->report_mutex); 1581 steam_haptic_pulse(steam, STEAM_PAD_RIGHT, 0x190, 0, 1, 0); 1582 if (gamepad_mode) { 1583 steam_haptic_pulse(steam, STEAM_PAD_LEFT, 0x14D, 0x14D, 0x2D, 0); 1584 } else { 1585 steam_haptic_pulse(steam, STEAM_PAD_LEFT, 0x1F4, 0x1F4, 0x1E, 0); 1586 } 1587 } 1588 1589 static bool steam_is_valve_interface(struct hid_device *hdev, int quirks) 1590 { 1591 struct hid_report_enum *rep_enum; 1592 1593 /* 1594 * The 2015 wired controller creates 3 interfaces: 1595 * 0: emulated mouse. 1596 * 1: emulated keyboard. 1597 * 2: the real game pad. 1598 * The 2015 wireless adapter creates 5 interfaces: 1599 * 0: emulated keyboard. 1600 * 1-4: slots where up to 4 real controllers will be connected to. 1601 * The Steam Deck creates 5 interfaces: 1602 * 0: emulated mouse. 1603 * 1: emulated keyboard. 1604 * 2: the real game pad. 1605 * 3-4: internal device comms (not HID). 1606 * The 2026 wired controller creates 1 unified interface. 1607 * The 2026 wireless puck creates 7 interfaces: 1608 * 0-1: internal device comms (not HID). 1609 * 2-5: slots where up to 4 real controllers will be connected to. 1610 * 6: basic pogo pin interface. 1611 * 1612 * We know which one is the real controller interface for the pre-2026 1613 * controllers because they are the only ones with a feature report. 1614 * 1615 * The puck's pogo pin interface should be ignored as it's stripped 1616 * down. It has one collection with usage page FF00 with usage ID 2. 1617 */ 1618 if (quirks & STEAM_QUIRK_IBEX) { 1619 /* There is only one BLE HID interface */ 1620 if (quirks & STEAM_QUIRK_BLE) 1621 return true; 1622 1623 if (hdev->maxcollection < 1) 1624 return true; 1625 1626 return hdev->collection[0].usage != 0xFF000002; 1627 } 1628 1629 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT]; 1630 return !list_empty(&rep_enum->report_list); 1631 } 1632 1633 static int steam_client_ll_parse(struct hid_device *hdev) 1634 { 1635 struct steam_device *steam = hdev->driver_data; 1636 1637 return hid_parse_report(hdev, steam->hdev->dev_rdesc, 1638 steam->hdev->dev_rsize); 1639 } 1640 1641 static int steam_client_ll_start(struct hid_device *hdev) 1642 { 1643 return 0; 1644 } 1645 1646 static void steam_client_ll_stop(struct hid_device *hdev) 1647 { 1648 } 1649 1650 static int steam_client_ll_open(struct hid_device *hdev) 1651 { 1652 struct steam_device *steam = hdev->driver_data; 1653 unsigned long flags; 1654 1655 spin_lock_irqsave(&steam->lock, flags); 1656 steam->client_opened++; 1657 spin_unlock_irqrestore(&steam->lock, flags); 1658 1659 schedule_work(&steam->work_connect); 1660 1661 return 0; 1662 } 1663 1664 static void steam_client_ll_close(struct hid_device *hdev) 1665 { 1666 struct steam_device *steam = hdev->driver_data; 1667 1668 unsigned long flags; 1669 1670 spin_lock_irqsave(&steam->lock, flags); 1671 steam->client_opened--; 1672 spin_unlock_irqrestore(&steam->lock, flags); 1673 1674 schedule_work(&steam->work_connect); 1675 } 1676 1677 static int steam_client_ll_raw_request(struct hid_device *hdev, 1678 unsigned char reportnum, u8 *buf, 1679 size_t count, unsigned char report_type, 1680 int reqtype) 1681 { 1682 struct steam_device *steam = hdev->driver_data; 1683 1684 return hid_hw_raw_request(steam->hdev, reportnum, buf, count, 1685 report_type, reqtype); 1686 } 1687 1688 static int steam_client_ll_output_report(struct hid_device *hdev, 1689 u8 *buf, size_t count) 1690 { 1691 struct steam_device *steam = hdev->driver_data; 1692 1693 return hid_hw_output_report(steam->hdev, buf, count); 1694 } 1695 1696 static const struct hid_ll_driver steam_client_ll_driver = { 1697 .parse = steam_client_ll_parse, 1698 .start = steam_client_ll_start, 1699 .stop = steam_client_ll_stop, 1700 .open = steam_client_ll_open, 1701 .close = steam_client_ll_close, 1702 .raw_request = steam_client_ll_raw_request, 1703 .output_report = steam_client_ll_output_report, 1704 }; 1705 1706 static struct hid_device *steam_create_client_hid(struct hid_device *hdev) 1707 { 1708 struct hid_device *client_hdev; 1709 1710 client_hdev = hid_allocate_device(); 1711 if (IS_ERR(client_hdev)) 1712 return client_hdev; 1713 1714 client_hdev->ll_driver = &steam_client_ll_driver; 1715 client_hdev->dev.parent = hdev->dev.parent; 1716 client_hdev->bus = hdev->bus; 1717 client_hdev->vendor = hdev->vendor; 1718 client_hdev->product = hdev->product; 1719 client_hdev->version = hdev->version; 1720 client_hdev->type = hdev->type; 1721 client_hdev->country = hdev->country; 1722 strscpy(client_hdev->name, hdev->name, 1723 sizeof(client_hdev->name)); 1724 strscpy(client_hdev->phys, hdev->phys, 1725 sizeof(client_hdev->phys)); 1726 /* 1727 * Since we use the same device info than the real interface to 1728 * trick userspace, we will be calling steam_probe recursively. 1729 * We need to recognize the client interface somehow. 1730 */ 1731 client_hdev->group = HID_GROUP_STEAM; 1732 return client_hdev; 1733 } 1734 1735 static int steam_probe(struct hid_device *hdev, 1736 const struct hid_device_id *id) 1737 { 1738 struct steam_device *steam; 1739 int ret; 1740 1741 ret = hid_parse(hdev); 1742 if (ret) { 1743 hid_err(hdev, 1744 "%s:parse of hid interface failed\n", __func__); 1745 return ret; 1746 } 1747 1748 /* 1749 * The virtual client_dev is only used for hidraw. 1750 * Also avoid the recursive probe. 1751 */ 1752 if (hdev->group == HID_GROUP_STEAM) 1753 return hid_hw_start(hdev, HID_CONNECT_HIDRAW); 1754 /* 1755 * The non-valve interfaces (mouse and keyboard emulation) are 1756 * connected without changes. 1757 */ 1758 if (!steam_is_valve_interface(hdev, id->driver_data)) 1759 return hid_hw_start(hdev, HID_CONNECT_DEFAULT); 1760 1761 steam = devm_kzalloc(&hdev->dev, sizeof(*steam), GFP_KERNEL); 1762 if (!steam) 1763 return -ENOMEM; 1764 1765 steam->hdev = hdev; 1766 hid_set_drvdata(hdev, steam); 1767 spin_lock_init(&steam->lock); 1768 mutex_init(&steam->report_mutex); 1769 mutex_init(&steam->registration_mutex); 1770 steam->quirks = id->driver_data; 1771 INIT_WORK(&steam->work_connect, steam_work_connect_cb); 1772 INIT_DELAYED_WORK(&steam->mode_switch, steam_mode_switch_cb); 1773 INIT_LIST_HEAD(&steam->list); 1774 INIT_WORK(&steam->rumble_work, steam_haptic_rumble_cb); 1775 INIT_DELAYED_WORK(&steam->coalesce_rumble_work, steam_coalesce_rumble_cb); 1776 steam->sensor_timestamp_us = 0; 1777 if (steam->quirks & STEAM_QUIRK_DECK) 1778 steam->sensor_update_rate_us = 4000; 1779 else 1780 steam->sensor_update_rate_us = 9000; 1781 1782 /* 1783 * With the real steam controller interface, do not connect hidraw. 1784 * Instead, create the client_hid and connect that. 1785 */ 1786 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_HIDRAW); 1787 if (ret) 1788 goto err_cancel_work; 1789 1790 ret = hid_hw_open(hdev); 1791 if (ret) { 1792 hid_err(hdev, 1793 "%s:hid_hw_open\n", 1794 __func__); 1795 goto err_hw_stop; 1796 } 1797 1798 steam->connected = true; 1799 1800 if (steam->quirks & STEAM_QUIRK_WIRELESS) { 1801 hid_info(hdev, "Steam wireless receiver connected"); 1802 /* If using a wireless adaptor ask for connection status */ 1803 steam->connected = false; 1804 ret = steam_get_conn_status(steam); 1805 if (ret < 0) 1806 hid_err(hdev, 1807 "%s:steam_get_conn_status failed with error %d\n", 1808 __func__, ret); 1809 else if (ret == WIRELESS_EVENT_CONNECT) 1810 steam->connected = true; 1811 } 1812 if (steam->connected) { 1813 ret = steam_register(steam); 1814 if (ret) { 1815 hid_err(hdev, 1816 "%s:steam_register failed with error %d\n", 1817 __func__, ret); 1818 goto err_hw_close; 1819 } 1820 } 1821 1822 steam->client_hdev = steam_create_client_hid(hdev); 1823 if (IS_ERR(steam->client_hdev)) { 1824 ret = PTR_ERR(steam->client_hdev); 1825 goto err_steam_unregister; 1826 } 1827 steam->client_hdev->driver_data = steam; 1828 1829 ret = hid_add_device(steam->client_hdev); 1830 if (ret) 1831 goto err_destroy; 1832 1833 return 0; 1834 1835 err_destroy: 1836 hid_destroy_device(steam->client_hdev); 1837 err_steam_unregister: 1838 if (steam->connected) 1839 steam_unregister(steam); 1840 err_hw_close: 1841 hid_hw_close(hdev); 1842 err_hw_stop: 1843 hid_hw_stop(hdev); 1844 err_cancel_work: 1845 cancel_work_sync(&steam->work_connect); 1846 cancel_delayed_work_sync(&steam->mode_switch); 1847 cancel_work_sync(&steam->rumble_work); 1848 cancel_delayed_work_sync(&steam->coalesce_rumble_work); 1849 1850 return ret; 1851 } 1852 1853 static void steam_remove(struct hid_device *hdev) 1854 { 1855 struct steam_device *steam = hid_get_drvdata(hdev); 1856 unsigned long flags; 1857 1858 if (!steam || hdev->group == HID_GROUP_STEAM) { 1859 hid_hw_stop(hdev); 1860 return; 1861 } 1862 1863 hid_hw_close(hdev); 1864 hid_destroy_device(steam->client_hdev); 1865 spin_lock_irqsave(&steam->lock, flags); 1866 steam->client_opened = 0; 1867 spin_unlock_irqrestore(&steam->lock, flags); 1868 cancel_work_sync(&steam->work_connect); 1869 if (steam->quirks & STEAM_QUIRK_WIRELESS) { 1870 hid_info(hdev, "Steam wireless receiver disconnected"); 1871 } 1872 steam_unregister(steam); 1873 hid_hw_stop(hdev); 1874 } 1875 1876 static void steam_do_connect_event(struct steam_device *steam, bool connected) 1877 { 1878 bool changed; 1879 1880 changed = steam->connected != connected; 1881 steam->connected = connected; 1882 1883 if (changed && schedule_work(&steam->work_connect) == 0) 1884 dbg_hid("%s: connected=%d event already queued\n", 1885 __func__, connected); 1886 } 1887 1888 /* 1889 * Some input data in the protocol has the opposite sign. 1890 * Clamp the values to 32767..-32767 so that the range is 1891 * symmetrical and can be negated safely. 1892 */ 1893 static inline s16 steam_le16(const u8 *data) 1894 { 1895 s16 x = (s16) get_unaligned_le16((const __le16 *)data); 1896 1897 return x == -32768 ? -32767 : x; 1898 } 1899 1900 struct steam_button_mapping { 1901 int code; 1902 u8 byte; 1903 u8 bit; 1904 }; 1905 1906 struct steam_axis_mapping { 1907 int code; 1908 s8 sign; 1909 u8 byte; 1910 }; 1911 1912 static void steam_map_buttons(struct input_dev *input, 1913 const struct steam_button_mapping *mappings, const u8 *data) 1914 { 1915 const struct steam_button_mapping *mapping; 1916 1917 for (mapping = mappings; mapping->code; mapping++) 1918 input_report_key(input, mapping->code, 1919 data[mapping->byte] & BIT(mapping->bit)); 1920 } 1921 1922 static void steam_map_axes(struct input_dev *input, 1923 const struct steam_axis_mapping *mappings, const u8 *data) 1924 { 1925 const struct steam_axis_mapping *mapping; 1926 1927 for (mapping = mappings; mapping->sign; mapping++) 1928 input_report_abs(input, mapping->code, 1929 mapping->sign * steam_le16(&data[mapping->byte])); 1930 } 1931 1932 /* 1933 * The size for this message payload is 60. 1934 * The known values are: 1935 * (* values are not sent through wireless) 1936 * (* accelerator/gyro is disabled by default) 1937 * Offset| Type | Mapped to |Meaning 1938 * -------+-------+-----------+-------------------------- 1939 * 4-7 | u32 | -- | sequence number 1940 * 8-10 | 24bit | see below | buttons 1941 * 11 | u8 | ABS_HAT2Y | left trigger 1942 * 12 | u8 | ABS_HAT2X | right trigger 1943 * 13-15 | -- | -- | always 0 1944 * 16-17 | s16 | ABS_X/ABS_HAT0X | X value 1945 * 18-19 | s16 | ABS_Y/ABS_HAT0Y | Y value 1946 * 20-21 | s16 | ABS_RX | right-pad X value 1947 * 22-23 | s16 | ABS_RY | right-pad Y value 1948 * 24-25 | s16 | -- | * left trigger 1949 * 26-27 | s16 | -- | * right trigger 1950 * 28-29 | s16 | -- | * accelerometer X value 1951 * 30-31 | s16 | -- | * accelerometer Y value 1952 * 32-33 | s16 | -- | * accelerometer Z value 1953 * 34-35 | s16 | -- | gyro X value 1954 * 36-36 | s16 | -- | gyro Y value 1955 * 38-39 | s16 | -- | gyro Z value 1956 * 40-41 | s16 | -- | quaternion W value 1957 * 42-43 | s16 | -- | quaternion X value 1958 * 44-45 | s16 | -- | quaternion Y value 1959 * 46-47 | s16 | -- | quaternion Z value 1960 * 48-49 | -- | -- | always 0 1961 * 50-51 | s16 | -- | * left trigger (uncalibrated) 1962 * 52-53 | s16 | -- | * right trigger (uncalibrated) 1963 * 54-55 | s16 | -- | * joystick X value (uncalibrated) 1964 * 56-57 | s16 | -- | * joystick Y value (uncalibrated) 1965 * 58-59 | s16 | -- | * left-pad X value 1966 * 60-61 | s16 | -- | * left-pad Y value 1967 * 62-63 | u16 | -- | * battery voltage 1968 * 1969 * The buttons are: 1970 * Bit | Mapped to | Description 1971 * ------+------------+-------------------------------- 1972 * 8.0 | BTN_TR2 | right trigger fully pressed 1973 * 8.1 | BTN_TL2 | left trigger fully pressed 1974 * 8.2 | BTN_TR | right shoulder 1975 * 8.3 | BTN_TL | left shoulder 1976 * 8.4 | BTN_Y | button Y 1977 * 8.5 | BTN_B | button B 1978 * 8.6 | BTN_X | button X 1979 * 8.7 | BTN_A | button A 1980 * 9.0 | BTN_DPAD_UP | left-pad up 1981 * 9.1 | BTN_DPAD_RIGHT | left-pad right 1982 * 9.2 | BTN_DPAD_LEFT | left-pad left 1983 * 9.3 | BTN_DPAD_DOWN | left-pad down 1984 * 9.4 | BTN_SELECT | view 1985 * 9.5 | BTN_MODE | steam logo 1986 * 9.6 | BTN_START | menu 1987 * 9.7 | BTN_GRIPL | left back lever 1988 * 10.0 | BTN_GRIPR | right back lever 1989 * 10.1 | -- | left-pad clicked 1990 * 10.2 | BTN_THUMBR | right-pad clicked 1991 * 10.3 | BTN_THUMB | left-pad touched (but see explanation below) 1992 * 10.4 | BTN_THUMB2 | right-pad touched 1993 * 10.5 | -- | unknown 1994 * 10.6 | BTN_THUMBL | joystick clicked 1995 * 10.7 | -- | lpad_and_joy 1996 */ 1997 1998 static const struct steam_button_mapping steam_controller_button_mappings[] = { 1999 { BTN_TR2, 8, 0 }, 2000 { BTN_TL2, 8, 1 }, 2001 { BTN_TR, 8, 2 }, 2002 { BTN_TL, 8, 3 }, 2003 { BTN_Y, 8, 4 }, 2004 { BTN_B, 8, 5 }, 2005 { BTN_X, 8, 6 }, 2006 { BTN_A, 8, 7 }, 2007 { BTN_SELECT, 9, 4 }, 2008 { BTN_MODE, 9, 5 }, 2009 { BTN_START, 9, 6 }, 2010 { BTN_GRIPL, 9, 7 }, 2011 { BTN_GRIPR, 10, 0 }, 2012 { BTN_THUMBR, 10, 2 }, 2013 { BTN_THUMBL, 10, 6 }, 2014 { BTN_THUMB2, 10, 4 }, 2015 { BTN_DPAD_UP, 9, 0 }, 2016 { BTN_DPAD_RIGHT, 9, 1 }, 2017 { BTN_DPAD_LEFT, 9, 2 }, 2018 { BTN_DPAD_DOWN, 9, 3 }, 2019 { /* sentinel */ }, 2020 }; 2021 2022 static const struct steam_axis_mapping steam_controller_axis_mappings[] = { 2023 { ABS_RX, 1, 20 }, 2024 { ABS_RY, -1, 22 }, 2025 { /* sentinel */ }, 2026 }; 2027 2028 static const struct steam_axis_mapping steam_controller_imu_mappings[] = { 2029 { ABS_X, 1, 28 }, 2030 { ABS_Z, -1, 30 }, 2031 { ABS_Y, 1, 32 }, 2032 { ABS_RX, 1, 34 }, 2033 { ABS_RZ, 1, 36 }, 2034 { ABS_RY, 1, 38 }, 2035 { /* sentinel */ }, 2036 }; 2037 2038 static void steam_do_input_event(struct steam_device *steam, 2039 struct input_dev *input, u8 *data) 2040 { 2041 s16 x, y; 2042 bool lpad_touched, lpad_and_joy; 2043 2044 input_report_abs(input, ABS_HAT2Y, data[11]); 2045 input_report_abs(input, ABS_HAT2X, data[12]); 2046 2047 /* 2048 * These two bits tells how to interpret the values X and Y. 2049 * lpad_and_joy tells that the joystick and the lpad are used at the 2050 * same time. 2051 * lpad_touched tells whether X/Y are to be read as lpad coord or 2052 * joystick values. 2053 * (lpad_touched || lpad_and_joy) tells if the lpad is really touched. 2054 */ 2055 lpad_touched = data[10] & BIT(3); 2056 lpad_and_joy = data[10] & BIT(7); 2057 x = steam_le16(data + 16); 2058 y = -steam_le16(data + 18); 2059 2060 input_report_abs(input, lpad_touched ? ABS_HAT0X : ABS_X, x); 2061 input_report_abs(input, lpad_touched ? ABS_HAT0Y : ABS_Y, y); 2062 /* Check if joystick is centered */ 2063 if (lpad_touched && !lpad_and_joy) { 2064 input_report_abs(input, ABS_X, 0); 2065 input_report_abs(input, ABS_Y, 0); 2066 } 2067 /* Check if lpad is untouched */ 2068 if (!(lpad_touched || lpad_and_joy)) { 2069 input_report_abs(input, ABS_HAT0X, 0); 2070 input_report_abs(input, ABS_HAT0Y, 0); 2071 } 2072 input_report_key(input, BTN_THUMB, lpad_touched || lpad_and_joy); 2073 2074 steam_map_buttons(input, steam_controller_button_mappings, data); 2075 steam_map_axes(input, steam_controller_axis_mappings, data); 2076 2077 input_sync(input); 2078 } 2079 2080 static void steam_do_sensors_event(struct steam_device *steam, 2081 struct input_dev *sensors, u8 *data) 2082 { 2083 steam->sensor_timestamp_us += steam->sensor_update_rate_us; 2084 2085 input_event(sensors, EV_MSC, MSC_TIMESTAMP, steam->sensor_timestamp_us); 2086 steam_map_axes(sensors, steam_controller_imu_mappings, data); 2087 2088 input_sync(sensors); 2089 } 2090 2091 /* 2092 * The size for this message payload is 56. 2093 * The known values are: 2094 * Offset| Type | Mapped to |Meaning 2095 * -------+-------+-----------+-------------------------- 2096 * 4-7 | u32 | -- | sequence number 2097 * 8-15 | u64 | see below | buttons 2098 * 16-17 | s16 | ABS_HAT0X | left-pad X value 2099 * 18-19 | s16 | ABS_HAT0Y | left-pad Y value 2100 * 20-21 | s16 | ABS_HAT1X | right-pad X value 2101 * 22-23 | s16 | ABS_HAT1Y | right-pad Y value 2102 * 24-25 | s16 | IMU ABS_X | accelerometer X value 2103 * 26-27 | s16 | IMU ABS_Z | accelerometer Y value 2104 * 28-29 | s16 | IMU ABS_Y | accelerometer Z value 2105 * 30-31 | s16 | IMU ABS_RX | gyro X value 2106 * 32-33 | s16 | IMU ABS_RZ | gyro Y value 2107 * 34-35 | s16 | IMU ABS_RY | gyro Z value 2108 * 36-37 | s16 | -- | quaternion W value 2109 * 38-39 | s16 | -- | quaternion X value 2110 * 40-41 | s16 | -- | quaternion Y value 2111 * 42-43 | s16 | -- | quaternion Z value 2112 * 44-45 | u16 | ABS_HAT2Y | left trigger (uncalibrated) 2113 * 46-47 | u16 | ABS_HAT2X | right trigger (uncalibrated) 2114 * 48-49 | s16 | ABS_X | left joystick X 2115 * 50-51 | s16 | ABS_Y | left joystick Y 2116 * 52-53 | s16 | ABS_RX | right joystick X 2117 * 54-55 | s16 | ABS_RY | right joystick Y 2118 * 56-57 | u16 | -- | left pad pressure 2119 * 58-59 | u16 | -- | right pad pressure 2120 * 2121 * The buttons are: 2122 * Bit | Mapped to | Description 2123 * ------+------------+-------------------------------- 2124 * 8.0 | BTN_TR2 | right trigger fully pressed 2125 * 8.1 | BTN_TL2 | left trigger fully pressed 2126 * 8.2 | BTN_TR | right shoulder 2127 * 8.3 | BTN_TL | left shoulder 2128 * 8.4 | BTN_Y | button Y 2129 * 8.5 | BTN_B | button B 2130 * 8.6 | BTN_X | button X 2131 * 8.7 | BTN_A | button A 2132 * 9.0 | BTN_DPAD_UP | left-pad up 2133 * 9.1 | BTN_DPAD_RIGHT | left-pad right 2134 * 9.2 | BTN_DPAD_LEFT | left-pad left 2135 * 9.3 | BTN_DPAD_DOWN | left-pad down 2136 * 9.4 | BTN_SELECT | view 2137 * 9.5 | BTN_MODE | steam logo 2138 * 9.6 | BTN_START | menu 2139 * 9.7 | BTN_GRIPL2 | left bottom grip button 2140 * 10.0 | BTN_GRIPR2 | right bottom grip button 2141 * 10.1 | BTN_THUMB | left pad pressed 2142 * 10.2 | BTN_THUMB2 | right pad pressed 2143 * 10.3 | -- | left pad touched 2144 * 10.4 | -- | right pad touched 2145 * 10.5 | -- | unknown 2146 * 10.6 | BTN_THUMBL | left joystick clicked 2147 * 10.7 | -- | unknown 2148 * 11.0 | -- | unknown 2149 * 11.1 | -- | unknown 2150 * 11.2 | BTN_THUMBR | right joystick clicked 2151 * 11.3 | -- | unknown 2152 * 11.4 | -- | unknown 2153 * 11.5 | -- | unknown 2154 * 11.6 | -- | unknown 2155 * 11.7 | -- | unknown 2156 * 12.0 | -- | unknown 2157 * 12.1 | -- | unknown 2158 * 12.2 | -- | unknown 2159 * 12.3 | -- | unknown 2160 * 12.4 | -- | unknown 2161 * 12.5 | -- | unknown 2162 * 12.6 | -- | unknown 2163 * 12.7 | -- | unknown 2164 * 13.0 | -- | unknown 2165 * 13.1 | BTN_GRIPL | left top grip button 2166 * 13.2 | BTN_GRIPR | right top grip button 2167 * 13.3 | -- | unknown 2168 * 13.4 | -- | unknown 2169 * 13.5 | -- | unknown 2170 * 13.6 | -- | left joystick touched 2171 * 13.7 | -- | right joystick touched 2172 * 14.0 | -- | unknown 2173 * 14.1 | -- | unknown 2174 * 14.2 | BTN_BASE | quick access button 2175 * 14.3 | -- | unknown 2176 * 14.4 | -- | unknown 2177 * 14.5 | -- | unknown 2178 * 14.6 | -- | unknown 2179 * 14.7 | -- | unknown 2180 * 15.0 | -- | unknown 2181 * 15.1 | -- | unknown 2182 * 15.2 | -- | unknown 2183 * 15.3 | -- | unknown 2184 * 15.4 | -- | unknown 2185 * 15.5 | -- | unknown 2186 * 15.6 | -- | unknown 2187 * 15.7 | -- | unknown 2188 */ 2189 2190 static const struct steam_button_mapping steam_deck_button_mappings[] = { 2191 { BTN_TR2, 8, 0 }, 2192 { BTN_TL2, 8, 1 }, 2193 { BTN_TR, 8, 2 }, 2194 { BTN_TL, 8, 3 }, 2195 { BTN_Y, 8, 4 }, 2196 { BTN_B, 8, 5 }, 2197 { BTN_X, 8, 6 }, 2198 { BTN_A, 8, 7 }, 2199 { BTN_SELECT, 9, 4 }, 2200 { BTN_MODE, 9, 5 }, 2201 { BTN_START, 9, 6 }, 2202 { BTN_GRIPL2, 9, 7 }, 2203 { BTN_GRIPR2, 10, 0 }, 2204 { BTN_THUMBL, 10, 6 }, 2205 { BTN_THUMBR, 11, 2 }, 2206 { BTN_DPAD_UP, 9, 0 }, 2207 { BTN_DPAD_RIGHT, 9, 1 }, 2208 { BTN_DPAD_LEFT, 9, 2 }, 2209 { BTN_DPAD_DOWN, 9, 3 }, 2210 { BTN_THUMB, 10, 1 }, 2211 { BTN_THUMB2, 10, 2 }, 2212 { BTN_GRIPL, 13, 1 }, 2213 { BTN_GRIPR, 13, 2 }, 2214 { BTN_BASE, 14, 2 }, 2215 { /* sentinel */ }, 2216 }; 2217 2218 static const struct steam_axis_mapping steam_deck_axis_mappings[] = { 2219 { ABS_X, 1, 48 }, 2220 { ABS_Y, -1, 50 }, 2221 { ABS_RX, 1, 52 }, 2222 { ABS_RY, -1, 54 }, 2223 { ABS_HAT2Y, 1, 44 }, 2224 { ABS_HAT2X, 1, 46 }, 2225 { /* sentinel */ }, 2226 }; 2227 2228 static const struct steam_axis_mapping steam_deck_imu_mappings[] = { 2229 { ABS_X, 1, 24 }, 2230 { ABS_Z, -1, 26 }, 2231 { ABS_Y, 1, 28 }, 2232 { ABS_RX, 1, 30 }, 2233 { ABS_RZ, -1, 32 }, 2234 { ABS_RY, 1, 34 }, 2235 { /* sentinel */ }, 2236 }; 2237 2238 static void steam_do_deck_input_event(struct steam_device *steam, 2239 struct input_dev *input, u8 *data) 2240 { 2241 bool start_pressed; 2242 bool lpad_touched, rpad_touched; 2243 2244 start_pressed = data[9] & BIT(6); 2245 2246 if (!start_pressed && steam->did_mode_switch) { 2247 steam->did_mode_switch = false; 2248 cancel_delayed_work(&steam->mode_switch); 2249 } else if (!steam->client_opened && start_pressed && !steam->did_mode_switch) { 2250 hid_dbg(steam->hdev, "%s: doing mode switch\n", __func__); 2251 steam->did_mode_switch = true; 2252 schedule_delayed_work(&steam->mode_switch, 45 * HZ / 100); 2253 } 2254 2255 if (!steam->gamepad_mode && lizard_mode) 2256 return; 2257 2258 lpad_touched = data[10] & BIT(3); 2259 rpad_touched = data[10] & BIT(4); 2260 2261 if (lpad_touched) { 2262 input_report_abs(input, ABS_HAT0X, steam_le16(data + 16)); 2263 input_report_abs(input, ABS_HAT0Y, steam_le16(data + 18)); 2264 } else { 2265 input_report_abs(input, ABS_HAT0X, 0); 2266 input_report_abs(input, ABS_HAT0Y, 0); 2267 } 2268 2269 if (rpad_touched) { 2270 input_report_abs(input, ABS_HAT1X, steam_le16(data + 20)); 2271 input_report_abs(input, ABS_HAT1Y, steam_le16(data + 22)); 2272 } else { 2273 input_report_abs(input, ABS_HAT1X, 0); 2274 input_report_abs(input, ABS_HAT1Y, 0); 2275 } 2276 2277 steam_map_buttons(input, steam_deck_button_mappings, data); 2278 steam_map_axes(input, steam_deck_axis_mappings, data); 2279 2280 input_sync(input); 2281 } 2282 2283 static void steam_do_deck_sensors_event(struct steam_device *steam, 2284 struct input_dev *sensors, u8 *data) 2285 { 2286 steam->sensor_timestamp_us += steam->sensor_update_rate_us; 2287 2288 if (!steam->gamepad_mode && lizard_mode) 2289 return; 2290 2291 input_event(sensors, EV_MSC, MSC_TIMESTAMP, steam->sensor_timestamp_us); 2292 steam_map_axes(sensors, steam_deck_imu_mappings, data); 2293 2294 input_sync(sensors); 2295 } 2296 2297 /* 2298 * The size for this message payload is 11. 2299 * The known values are: 2300 * Offset| Type | Meaning 2301 * -------+-------+--------------------------- 2302 * 4-7 | u32 | sequence number 2303 * 8-11 | -- | always 0 2304 * 12-13 | u16 | voltage (mV) 2305 * 14 | u8 | battery percent 2306 */ 2307 static void steam_do_battery_event(struct steam_device *steam, 2308 struct power_supply *battery, u8 *data) 2309 { 2310 s16 volts = steam_le16(data + 12); 2311 u8 batt = data[14]; 2312 2313 /* Creating the battery may have failed */ 2314 rcu_read_lock(); 2315 battery = rcu_dereference(steam->battery); 2316 if (likely(battery)) { 2317 steam->battery_voltage = volts; 2318 steam->battery_charge = batt; 2319 power_supply_changed(battery); 2320 } 2321 rcu_read_unlock(); 2322 } 2323 2324 /* 2325 * The size for this message payload is 53 in REPORT_ID_INPUT and 45 in REPORT_ID_INPUT2. 2326 * The values are: 2327 * (* values only in REPORT_ID_INPUT) 2328 * Offset| Type | Mapped to |Meaning 2329 * -------+-------+-----------+-------------------------- 2330 * 1 | u8 | -- | sequence number 2331 * 2-5 | u32 | see below | buttons 2332 * 6-7 | s16 | ABS_HAT2Y | left trigger (uncalibrated) 2333 * 8-9 | s16 | ABS_HAT2X | right trigger (uncalibrated) 2334 * 10-11 | s16 | ABS_X | left joystick X 2335 * 12-13 | s16 | ABS_Y | left joystick Y 2336 * 14-15 | s16 | ABS_RX | right joystick X 2337 * 16-17 | s16 | ABS_RY | right joystick Y 2338 * 18-19 | s16 | ABS_HAT0X | left-pad X value 2339 * 20-21 | s16 | ABS_HAT0Y | left-pad Y value 2340 * 22-23 | u16 | -- | left pad pressure 2341 * 24-25 | s16 | ABS_HAT1X | right-pad X value 2342 * 26-27 | s16 | ABS_HAT1Y | right-pad Y value 2343 * 28-29 | u16 | -- | right pad pressure 2344 * 30-33 | u32 | IMU MSC_TIMESTAMP | IMU timestamp 2345 * 34-35 | s16 | IMU ABS_X | accelerometer X value 2346 * 36-37 | s16 | IMU ABS_Z | accelerometer Y value 2347 * 38-39 | s16 | IMU ABS_Y | accelerometer Z value 2348 * 40-41 | s16 | IMU ABS_RX | gyro X value 2349 * 42-43 | s16 | IMU ABS_RZ | gyro Y value 2350 * 44-45 | s16 | IMU ABS_RY | gyro Z value 2351 * 46-47 | s16 | -- | * quaternion W value 2352 * 48-49 | s16 | -- | * quaternion X value 2353 * 50-51 | s16 | -- | * quaternion Y value 2354 * 52-53 | s16 | -- | * quaternion Z value 2355 * 2356 * The buttons are: 2357 * Bit | Mapped to | Description 2358 * ------+------------+-------------------------------- 2359 * 2.0 | BTN_A | button A 2360 * 2.1 | BTN_B | button B 2361 * 2.2 | BTN_X | button X 2362 * 2.3 | BTN_Y | button Y 2363 * 2.4 | BTN_BASE | quick access button 2364 * 2.5 | BTN_THUMBR | right joystick clicked 2365 * 2.6 | BTN_START | menu 2366 * 2.7 | BTN_GRIPR | right top grip button 2367 * 3.0 | BTN_GRIPR2 | right bottom grip button 2368 * 3.1 | BTN_TR | right shoulder 2369 * 3.2 | BTN_DPAD_DOWN | left-pad down 2370 * 3.3 | BTN_DPAD_RIGHT | left-pad right 2371 * 3.4 | BTN_DPAD_LEFT | left-pad left 2372 * 3.5 | BTN_DPAD_UP | left-pad up 2373 * 3.6 | BTN_SELECT | view 2374 * 3.7 | BTN_THUMBL | left joystick clicked 2375 * 4.0 | BTN_MODE | steam logo 2376 * 4.1 | BTN_GRIPL | left top grip button 2377 * 4.2 | BTN_GRIPL2 | left bottom grip button 2378 * 4.3 | BTN_TL | left shoulder 2379 * 4.4 | -- | right joystick touched 2380 * 4.5 | -- | right pad touched 2381 * 4.6 | BTN_THUMB2 | right pad pressed 2382 * 4.7 | BTN_TR2 | right trigger fully pressed 2383 * 5.0 | -- | left joystick touched 2384 * 5.1 | -- | left pad touched 2385 * 5.2 | BTN_THUMB | left pad pressed 2386 * 5.3 | BTN_TL2 | left trigger fully pressed 2387 * 5.4 | -- | right grip touch 2388 * 5.5 | -- | left grip touch 2389 * 5.6 | -- | unmapped 2390 * 5.7 | -- | unmapped 2391 */ 2392 2393 static const struct steam_button_mapping steam_ibex_button_mappings[] = { 2394 { BTN_A, 2, 0 }, 2395 { BTN_B, 2, 1 }, 2396 { BTN_X, 2, 2 }, 2397 { BTN_Y, 2, 3 }, 2398 { BTN_BASE, 2, 4 }, 2399 { BTN_THUMBR, 2, 5 }, 2400 { BTN_START, 2, 6 }, 2401 { BTN_GRIPR, 2, 7 }, 2402 { BTN_GRIPR2, 3, 0 }, 2403 { BTN_TR, 3, 1 }, 2404 { BTN_DPAD_DOWN, 3, 2 }, 2405 { BTN_DPAD_RIGHT, 3, 3 }, 2406 { BTN_DPAD_LEFT, 3, 4 }, 2407 { BTN_DPAD_UP, 3, 5 }, 2408 { BTN_SELECT, 3, 6 }, 2409 { BTN_THUMBL, 3, 7 }, 2410 { BTN_MODE, 4, 0 }, 2411 { BTN_GRIPL, 4, 1 }, 2412 { BTN_GRIPL2, 4, 2 }, 2413 { BTN_TL, 4, 3 }, 2414 { BTN_THUMB2, 4, 6 }, 2415 { BTN_TR2, 4, 7 }, 2416 { BTN_THUMB, 5, 2 }, 2417 { BTN_TL2, 5, 3 }, 2418 { /* sentinel */ }, 2419 }; 2420 2421 static const struct steam_axis_mapping steam_ibex_axis_mappings[] = { 2422 { ABS_X, 1, 10 }, 2423 { ABS_Y, -1, 12 }, 2424 { ABS_RX, 1, 14 }, 2425 { ABS_RY, -1, 16 }, 2426 { ABS_HAT2Y, 1, 6 }, 2427 { ABS_HAT2X, 1, 8 }, 2428 { /* sentinel */ }, 2429 }; 2430 2431 static const struct steam_axis_mapping steam_ibex_imu_mappings[] = { 2432 { ABS_X, 1, 34 }, 2433 { ABS_Z, -1, 36 }, 2434 { ABS_Y, 1, 38 }, 2435 { ABS_RX, 1, 40 }, 2436 { ABS_RZ, -1, 42 }, 2437 { ABS_RY, 1, 44 }, 2438 { /* sentinel */ }, 2439 }; 2440 2441 static void steam_do_ibex_input_event(struct steam_device *steam, 2442 struct input_dev *input, const u8 *data) 2443 { 2444 bool start_pressed; 2445 bool lpad_touched, rpad_touched; 2446 2447 start_pressed = data[2] & BIT(6); 2448 2449 if (!start_pressed && steam->did_mode_switch) { 2450 steam->did_mode_switch = false; 2451 cancel_delayed_work(&steam->mode_switch); 2452 } else if (!steam->client_opened && start_pressed && !steam->did_mode_switch) { 2453 steam->did_mode_switch = true; 2454 schedule_delayed_work(&steam->mode_switch, 45 * HZ / 100); 2455 } 2456 2457 if (!steam->gamepad_mode && lizard_mode) 2458 return; 2459 2460 lpad_touched = data[5] & BIT(1); 2461 rpad_touched = data[4] & BIT(5); 2462 2463 if (lpad_touched) { 2464 input_report_abs(input, ABS_HAT0X, steam_le16(data + 18)); 2465 input_report_abs(input, ABS_HAT0Y, steam_le16(data + 20)); 2466 } else { 2467 input_report_abs(input, ABS_HAT0X, 0); 2468 input_report_abs(input, ABS_HAT0Y, 0); 2469 } 2470 2471 if (rpad_touched) { 2472 input_report_abs(input, ABS_HAT1X, steam_le16(data + 24)); 2473 input_report_abs(input, ABS_HAT1Y, steam_le16(data + 26)); 2474 } else { 2475 input_report_abs(input, ABS_HAT1X, 0); 2476 input_report_abs(input, ABS_HAT1Y, 0); 2477 } 2478 steam_map_buttons(input, steam_ibex_button_mappings, data); 2479 steam_map_axes(input, steam_ibex_axis_mappings, data); 2480 2481 input_sync(input); 2482 } 2483 2484 static void steam_do_ibex_sensors_event(struct steam_device *steam, 2485 struct input_dev *sensors, const u8 *data) 2486 { 2487 u32 timestamp; 2488 2489 if (!steam->gamepad_mode && lizard_mode) 2490 return; 2491 2492 timestamp = (u32) get_unaligned_le32((__le32 *)&data[30]); 2493 input_event(sensors, EV_MSC, MSC_TIMESTAMP, timestamp); 2494 steam_map_axes(sensors, steam_ibex_imu_mappings, data); 2495 2496 input_sync(sensors); 2497 } 2498 2499 static void steam_do_ibex_battery_event(struct steam_device *steam, 2500 struct power_supply *battery, 2501 const struct steam_ibex_battery_status *data) 2502 { 2503 /* Creating the battery may have failed */ 2504 guard(rcu)(); 2505 battery = rcu_dereference(steam->battery); 2506 if (!likely(battery)) 2507 return; 2508 2509 steam->battery_voltage = get_unaligned_le16(&data->battery_voltage); 2510 steam->battery_current = get_unaligned_le16(&data->battery_current); 2511 steam->battery_temp = get_unaligned_le16(&data->temperature); 2512 steam->battery_charge = data->battery_level; 2513 switch (data->charge_state) { 2514 case CHARGE_STATE_CHARGING: 2515 steam->battery_status = POWER_SUPPLY_STATUS_CHARGING; 2516 break; 2517 case CHARGE_STATE_DISCHARGING: 2518 steam->battery_status = POWER_SUPPLY_STATUS_DISCHARGING; 2519 break; 2520 case CHARGE_STATE_CHARGING_DONE: 2521 steam->battery_status = POWER_SUPPLY_STATUS_FULL; 2522 break; 2523 default: 2524 steam->battery_status = POWER_SUPPLY_STATUS_UNKNOWN; 2525 break; 2526 } 2527 power_supply_changed(battery); 2528 } 2529 2530 static int steam_raw_event(struct hid_device *hdev, 2531 struct hid_report *report, u8 *data, 2532 int size) 2533 { 2534 struct steam_device *steam = hid_get_drvdata(hdev); 2535 struct input_dev *input; 2536 struct input_dev *sensors; 2537 struct power_supply *battery; 2538 2539 if (!steam) 2540 return 0; 2541 2542 guard(spinlock_irqsave)(&steam->lock); 2543 if (steam->client_opened) 2544 hid_input_report(steam->client_hdev, report->type, data, size, 0); 2545 2546 /* Ibex uses a different report format */ 2547 if (steam->quirks & STEAM_QUIRK_IBEX) { 2548 if (report->type != HID_INPUT_REPORT) 2549 return 0; 2550 2551 switch (report->id) { 2552 case REPORT_ID_INPUT: 2553 if (size != 54) 2554 return 0; 2555 if (steam->client_opened) 2556 return 0; 2557 rcu_read_lock(); 2558 input = rcu_dereference(steam->input); 2559 if (likely(input)) { 2560 steam_do_ibex_input_event(steam, input, data); 2561 } else { 2562 dbg_hid("%s: input data without connect event\n", 2563 __func__); 2564 steam_do_connect_event(steam, true); 2565 } 2566 sensors = rcu_dereference(steam->sensors); 2567 if (likely(sensors)) 2568 steam_do_ibex_sensors_event(steam, sensors, data); 2569 rcu_read_unlock(); 2570 break; 2571 case REPORT_ID_INPUT2: 2572 if (size != 46) 2573 return 0; 2574 if (steam->client_opened) 2575 return 0; 2576 rcu_read_lock(); 2577 input = rcu_dereference(steam->input); 2578 if (likely(input)) { 2579 steam_do_ibex_input_event(steam, input, data); 2580 } else { 2581 dbg_hid("%s: input data without connect event\n", 2582 __func__); 2583 steam_do_connect_event(steam, true); 2584 } 2585 sensors = rcu_dereference(steam->sensors); 2586 if (likely(sensors)) 2587 steam_do_ibex_sensors_event(steam, sensors, data); 2588 rcu_read_unlock(); 2589 break; 2590 case REPORT_ID_BATTERY: 2591 if (size != 15) 2592 return 0; 2593 rcu_read_lock(); 2594 battery = rcu_dereference(steam->battery); 2595 if (likely(battery)) { 2596 steam_do_ibex_battery_event(steam, battery, 2597 (const struct steam_ibex_battery_status *)&data[1]); 2598 } else { 2599 dbg_hid("%s: battery data without connect event\n", 2600 __func__); 2601 steam_do_connect_event(steam, true); 2602 } 2603 rcu_read_unlock(); 2604 break; 2605 case REPORT_ID_WIRELESS_EVENT: 2606 if (size != 2) 2607 return 0; 2608 switch (data[1]) { 2609 case WIRELESS_EVENT_DISCONNECT: 2610 steam_do_connect_event(steam, false); 2611 break; 2612 case WIRELESS_EVENT_CONNECT: 2613 steam_do_connect_event(steam, true); 2614 break; 2615 } 2616 break; 2617 } 2618 2619 return 0; 2620 } 2621 2622 /* 2623 * All messages are size=64, all values little-endian. 2624 * The format is: 2625 * Offset| Meaning 2626 * -------+-------------------------------------------- 2627 * 0-1 | always 0x01, 0x00, maybe protocol version? 2628 * 2 | type of message 2629 * 3 | length of the real payload (not checked) 2630 * 4-n | payload data, depends on the type 2631 * 2632 * There are these known types of message: 2633 * 0x01: input data (60 bytes) 2634 * 0x03: wireless connect/disconnect (1 byte) 2635 * 0x04: battery status (11 bytes) 2636 * 0x09: Steam Deck input data (56 bytes) 2637 */ 2638 2639 if (size != 64 || data[0] != 1 || data[1] != 0) 2640 return 0; 2641 2642 switch (data[2]) { 2643 case ID_CONTROLLER_STATE: 2644 if (steam->client_opened) 2645 return 0; 2646 rcu_read_lock(); 2647 input = rcu_dereference(steam->input); 2648 if (likely(input)) 2649 steam_do_input_event(steam, input, data); 2650 sensors = rcu_dereference(steam->sensors); 2651 if (likely(sensors)) 2652 steam_do_sensors_event(steam, sensors, data); 2653 rcu_read_unlock(); 2654 break; 2655 case ID_CONTROLLER_DECK_STATE: 2656 if (steam->client_opened) 2657 return 0; 2658 rcu_read_lock(); 2659 input = rcu_dereference(steam->input); 2660 if (likely(input)) 2661 steam_do_deck_input_event(steam, input, data); 2662 sensors = rcu_dereference(steam->sensors); 2663 if (likely(sensors)) 2664 steam_do_deck_sensors_event(steam, sensors, data); 2665 rcu_read_unlock(); 2666 break; 2667 case ID_CONTROLLER_WIRELESS: 2668 /* 2669 * The payload of this event is a single byte: 2670 * 0x01: disconnected. 2671 * 0x02: connected. 2672 */ 2673 switch (data[4]) { 2674 case 0x01: 2675 steam_do_connect_event(steam, false); 2676 break; 2677 case 0x02: 2678 steam_do_connect_event(steam, true); 2679 break; 2680 } 2681 break; 2682 case ID_CONTROLLER_STATUS: 2683 if (steam->quirks & STEAM_QUIRK_WIRELESS) { 2684 rcu_read_lock(); 2685 battery = rcu_dereference(steam->battery); 2686 if (likely(battery)) { 2687 steam_do_battery_event(steam, battery, data); 2688 } else { 2689 dbg_hid( 2690 "%s: battery data without connect event\n", 2691 __func__); 2692 steam_do_connect_event(steam, true); 2693 } 2694 rcu_read_unlock(); 2695 } 2696 break; 2697 } 2698 return 0; 2699 } 2700 2701 static int steam_param_set_lizard_mode(const char *val, 2702 const struct kernel_param *kp) 2703 { 2704 struct steam_device *steam; 2705 int ret; 2706 bool client_opened; 2707 unsigned long flags; 2708 2709 ret = param_set_bool(val, kp); 2710 if (ret) 2711 return ret; 2712 2713 mutex_lock(&steam_devices_lock); 2714 list_for_each_entry(steam, &steam_devices, list) { 2715 spin_lock_irqsave(&steam->lock, flags); 2716 client_opened = steam->client_opened; 2717 spin_unlock_irqrestore(&steam->lock, flags); 2718 if (!client_opened) { 2719 guard(mutex)(&steam->report_mutex); 2720 steam_set_lizard_mode(steam, lizard_mode); 2721 } 2722 } 2723 mutex_unlock(&steam_devices_lock); 2724 return 0; 2725 } 2726 2727 static const struct kernel_param_ops steam_lizard_mode_ops = { 2728 .set = steam_param_set_lizard_mode, 2729 .get = param_get_bool, 2730 }; 2731 2732 module_param_cb(lizard_mode, &steam_lizard_mode_ops, &lizard_mode, 0644); 2733 MODULE_PARM_DESC(lizard_mode, 2734 "Enable mouse and keyboard emulation (lizard mode) when the gamepad is not in use"); 2735 2736 static const struct hid_device_id steam_controllers[] = { 2737 { /* Wired Steam Controller (2015) */ 2738 HID_USB_DEVICE(USB_VENDOR_ID_VALVE, 2739 USB_DEVICE_ID_STEAM_CONTROLLER) 2740 }, 2741 { /* Wireless Steam Controller (2015) */ 2742 HID_USB_DEVICE(USB_VENDOR_ID_VALVE, 2743 USB_DEVICE_ID_STEAM_CONTROLLER_WIRELESS), 2744 .driver_data = STEAM_QUIRK_WIRELESS 2745 }, 2746 { /* Steam Deck */ 2747 HID_USB_DEVICE(USB_VENDOR_ID_VALVE, 2748 USB_DEVICE_ID_STEAM_DECK), 2749 .driver_data = STEAM_QUIRK_DECK 2750 }, 2751 { /* Steam Controller (2026) wired */ 2752 HID_USB_DEVICE(USB_VENDOR_ID_VALVE, 2753 USB_DEVICE_ID_STEAM_CONTROLLER_IBEX), 2754 .driver_data = STEAM_QUIRK_IBEX 2755 }, 2756 { /* Steam Controller (2026) BLE */ 2757 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_VALVE, 2758 USB_DEVICE_ID_STEAM_CONTROLLER_IBEX_BLE), 2759 .driver_data = STEAM_QUIRK_IBEX | STEAM_QUIRK_BLE 2760 }, 2761 { /* Steam Controller (2026) Puck */ 2762 HID_USB_DEVICE(USB_VENDOR_ID_VALVE, 2763 USB_DEVICE_ID_STEAM_CONTROLLER_PROTEUS), 2764 .driver_data = STEAM_QUIRK_IBEX | STEAM_QUIRK_WIRELESS 2765 }, 2766 { /* Steam Controller (2026) Steam Machine internal receiver */ 2767 HID_USB_DEVICE(USB_VENDOR_ID_VALVE, 2768 USB_DEVICE_ID_STEAM_CONTROLLER_NEREID), 2769 .driver_data = STEAM_QUIRK_IBEX | STEAM_QUIRK_WIRELESS 2770 }, 2771 {} 2772 }; 2773 2774 MODULE_DEVICE_TABLE(hid, steam_controllers); 2775 2776 static struct hid_driver steam_controller_driver = { 2777 .name = "hid-steam", 2778 .id_table = steam_controllers, 2779 .probe = steam_probe, 2780 .remove = steam_remove, 2781 .raw_event = steam_raw_event, 2782 }; 2783 2784 module_hid_driver(steam_controller_driver); 2785