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 * 7 * Supports both the wired and wireless interfaces. 8 * 9 * This controller has a builtin emulation of mouse and keyboard: the right pad 10 * can be used as a mouse, the shoulder buttons are mouse buttons, A and B 11 * buttons are ENTER and ESCAPE, and so on. This is implemented as additional 12 * HID interfaces. 13 * 14 * This is known as the "lizard mode", because apparently lizards like to use 15 * the computer from the coach, without a proper mouse and keyboard. 16 * 17 * This driver will disable the lizard mode when the input device is opened 18 * and re-enable it when the input device is closed, so as not to break user 19 * mode behaviour. The lizard_mode parameter can be used to change that. 20 * 21 * There are a few user space applications (notably Steam Client) that use 22 * the hidraw interface directly to create input devices (XTest, uinput...). 23 * In order to avoid breaking them this driver creates a layered hidraw device, 24 * so it can detect when the client is running and then: 25 * - it will not send any command to the controller. 26 * - this input device will be removed, to avoid double input of the same 27 * user action. 28 * When the client is closed, this input device will be created again. 29 * 30 * For additional functions, such as changing the right-pad margin or switching 31 * the led, you can use the user-space tool at: 32 * 33 * https://github.com/rodrigorc/steamctrl 34 */ 35 36 #include <linux/device.h> 37 #include <linux/input.h> 38 #include <linux/hid.h> 39 #include <linux/module.h> 40 #include <linux/workqueue.h> 41 #include <linux/mutex.h> 42 #include <linux/rcupdate.h> 43 #include <linux/delay.h> 44 #include <linux/power_supply.h> 45 #include "hid-ids.h" 46 47 MODULE_LICENSE("GPL"); 48 MODULE_AUTHOR("Rodrigo Rivas Costa <rodrigorivascosta@gmail.com>"); 49 50 static bool lizard_mode = true; 51 52 static DEFINE_MUTEX(steam_devices_lock); 53 static LIST_HEAD(steam_devices); 54 55 #define STEAM_QUIRK_WIRELESS BIT(0) 56 57 /* Touch pads are 40 mm in diameter and 65535 units */ 58 #define STEAM_PAD_RESOLUTION 1638 59 /* Trigger runs are about 5 mm and 256 units */ 60 #define STEAM_TRIGGER_RESOLUTION 51 61 /* Joystick runs are about 5 mm and 256 units */ 62 #define STEAM_JOYSTICK_RESOLUTION 51 63 64 #define STEAM_PAD_FUZZ 256 65 66 /* 67 * Commands that can be sent in a feature report. 68 * Thanks to Valve for some valuable hints. 69 */ 70 #define STEAM_CMD_SET_MAPPINGS 0x80 71 #define STEAM_CMD_CLEAR_MAPPINGS 0x81 72 #define STEAM_CMD_GET_MAPPINGS 0x82 73 #define STEAM_CMD_GET_ATTRIB 0x83 74 #define STEAM_CMD_GET_ATTRIB_LABEL 0x84 75 #define STEAM_CMD_DEFAULT_MAPPINGS 0x85 76 #define STEAM_CMD_FACTORY_RESET 0x86 77 #define STEAM_CMD_WRITE_REGISTER 0x87 78 #define STEAM_CMD_CLEAR_REGISTER 0x88 79 #define STEAM_CMD_READ_REGISTER 0x89 80 #define STEAM_CMD_GET_REGISTER_LABEL 0x8a 81 #define STEAM_CMD_GET_REGISTER_MAX 0x8b 82 #define STEAM_CMD_GET_REGISTER_DEFAULT 0x8c 83 #define STEAM_CMD_SET_MODE 0x8d 84 #define STEAM_CMD_DEFAULT_MOUSE 0x8e 85 #define STEAM_CMD_FORCEFEEDBAK 0x8f 86 #define STEAM_CMD_REQUEST_COMM_STATUS 0xb4 87 #define STEAM_CMD_GET_SERIAL 0xae 88 89 /* Some useful register ids */ 90 #define STEAM_REG_LPAD_MODE 0x07 91 #define STEAM_REG_RPAD_MODE 0x08 92 #define STEAM_REG_RPAD_MARGIN 0x18 93 #define STEAM_REG_LED 0x2d 94 #define STEAM_REG_GYRO_MODE 0x30 95 96 /* Raw event identifiers */ 97 #define STEAM_EV_INPUT_DATA 0x01 98 #define STEAM_EV_CONNECT 0x03 99 #define STEAM_EV_BATTERY 0x04 100 101 /* Values for GYRO_MODE (bitmask) */ 102 #define STEAM_GYRO_MODE_OFF 0x0000 103 #define STEAM_GYRO_MODE_STEERING 0x0001 104 #define STEAM_GYRO_MODE_TILT 0x0002 105 #define STEAM_GYRO_MODE_SEND_ORIENTATION 0x0004 106 #define STEAM_GYRO_MODE_SEND_RAW_ACCEL 0x0008 107 #define STEAM_GYRO_MODE_SEND_RAW_GYRO 0x0010 108 109 /* Other random constants */ 110 #define STEAM_SERIAL_LEN 10 111 112 struct steam_device { 113 struct list_head list; 114 spinlock_t lock; 115 struct hid_device *hdev, *client_hdev; 116 struct mutex mutex; 117 bool client_opened; 118 struct input_dev __rcu *input; 119 unsigned long quirks; 120 struct work_struct work_connect; 121 bool connected; 122 char serial_no[STEAM_SERIAL_LEN + 1]; 123 struct power_supply_desc battery_desc; 124 struct power_supply __rcu *battery; 125 u8 battery_charge; 126 u16 voltage; 127 }; 128 129 static int steam_recv_report(struct steam_device *steam, 130 u8 *data, int size) 131 { 132 struct hid_report *r; 133 u8 *buf; 134 int ret; 135 136 r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0]; 137 if (hid_report_len(r) < 64) 138 return -EINVAL; 139 140 buf = hid_alloc_report_buf(r, GFP_KERNEL); 141 if (!buf) 142 return -ENOMEM; 143 144 /* 145 * The report ID is always 0, so strip the first byte from the output. 146 * hid_report_len() is not counting the report ID, so +1 to the length 147 * or else we get a EOVERFLOW. We are safe from a buffer overflow 148 * because hid_alloc_report_buf() allocates +7 bytes. 149 */ 150 ret = hid_hw_raw_request(steam->hdev, 0x00, 151 buf, hid_report_len(r) + 1, 152 HID_FEATURE_REPORT, HID_REQ_GET_REPORT); 153 if (ret > 0) 154 memcpy(data, buf + 1, min(size, ret - 1)); 155 kfree(buf); 156 return ret; 157 } 158 159 static int steam_send_report(struct steam_device *steam, 160 u8 *cmd, int size) 161 { 162 struct hid_report *r; 163 u8 *buf; 164 unsigned int retries = 50; 165 int ret; 166 167 r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0]; 168 if (hid_report_len(r) < 64) 169 return -EINVAL; 170 171 buf = hid_alloc_report_buf(r, GFP_KERNEL); 172 if (!buf) 173 return -ENOMEM; 174 175 /* The report ID is always 0 */ 176 memcpy(buf + 1, cmd, size); 177 178 /* 179 * Sometimes the wireless controller fails with EPIPE 180 * when sending a feature report. 181 * Doing a HID_REQ_GET_REPORT and waiting for a while 182 * seems to fix that. 183 */ 184 do { 185 ret = hid_hw_raw_request(steam->hdev, 0, 186 buf, size + 1, 187 HID_FEATURE_REPORT, HID_REQ_SET_REPORT); 188 if (ret != -EPIPE) 189 break; 190 msleep(20); 191 } while (--retries); 192 193 kfree(buf); 194 if (ret < 0) 195 hid_err(steam->hdev, "%s: error %d (%*ph)\n", __func__, 196 ret, size, cmd); 197 return ret; 198 } 199 200 static inline int steam_send_report_byte(struct steam_device *steam, u8 cmd) 201 { 202 return steam_send_report(steam, &cmd, 1); 203 } 204 205 static int steam_write_registers(struct steam_device *steam, 206 /* u8 reg, u16 val */...) 207 { 208 /* Send: 0x87 len (reg valLo valHi)* */ 209 u8 reg; 210 u16 val; 211 u8 cmd[64] = {STEAM_CMD_WRITE_REGISTER, 0x00}; 212 va_list args; 213 214 va_start(args, steam); 215 for (;;) { 216 reg = va_arg(args, int); 217 if (reg == 0) 218 break; 219 val = va_arg(args, int); 220 cmd[cmd[1] + 2] = reg; 221 cmd[cmd[1] + 3] = val & 0xff; 222 cmd[cmd[1] + 4] = val >> 8; 223 cmd[1] += 3; 224 } 225 va_end(args); 226 227 return steam_send_report(steam, cmd, 2 + cmd[1]); 228 } 229 230 static int steam_get_serial(struct steam_device *steam) 231 { 232 /* 233 * Send: 0xae 0x15 0x01 234 * Recv: 0xae 0x15 0x01 serialnumber (10 chars) 235 */ 236 int ret; 237 u8 cmd[] = {STEAM_CMD_GET_SERIAL, 0x15, 0x01}; 238 u8 reply[3 + STEAM_SERIAL_LEN + 1]; 239 240 ret = steam_send_report(steam, cmd, sizeof(cmd)); 241 if (ret < 0) 242 return ret; 243 ret = steam_recv_report(steam, reply, sizeof(reply)); 244 if (ret < 0) 245 return ret; 246 if (reply[0] != 0xae || reply[1] != 0x15 || reply[2] != 0x01) 247 return -EIO; 248 reply[3 + STEAM_SERIAL_LEN] = 0; 249 strlcpy(steam->serial_no, reply + 3, sizeof(steam->serial_no)); 250 return 0; 251 } 252 253 /* 254 * This command requests the wireless adaptor to post an event 255 * with the connection status. Useful if this driver is loaded when 256 * the controller is already connected. 257 */ 258 static inline int steam_request_conn_status(struct steam_device *steam) 259 { 260 return steam_send_report_byte(steam, STEAM_CMD_REQUEST_COMM_STATUS); 261 } 262 263 static void steam_set_lizard_mode(struct steam_device *steam, bool enable) 264 { 265 if (enable) { 266 /* enable esc, enter, cursors */ 267 steam_send_report_byte(steam, STEAM_CMD_DEFAULT_MAPPINGS); 268 /* enable mouse */ 269 steam_send_report_byte(steam, STEAM_CMD_DEFAULT_MOUSE); 270 steam_write_registers(steam, 271 STEAM_REG_RPAD_MARGIN, 0x01, /* enable margin */ 272 0); 273 } else { 274 /* disable esc, enter, cursor */ 275 steam_send_report_byte(steam, STEAM_CMD_CLEAR_MAPPINGS); 276 steam_write_registers(steam, 277 STEAM_REG_RPAD_MODE, 0x07, /* disable mouse */ 278 STEAM_REG_RPAD_MARGIN, 0x00, /* disable margin */ 279 0); 280 } 281 } 282 283 static int steam_input_open(struct input_dev *dev) 284 { 285 struct steam_device *steam = input_get_drvdata(dev); 286 int ret; 287 288 ret = hid_hw_open(steam->hdev); 289 if (ret) 290 return ret; 291 292 mutex_lock(&steam->mutex); 293 if (!steam->client_opened && lizard_mode) 294 steam_set_lizard_mode(steam, false); 295 mutex_unlock(&steam->mutex); 296 return 0; 297 } 298 299 static void steam_input_close(struct input_dev *dev) 300 { 301 struct steam_device *steam = input_get_drvdata(dev); 302 303 mutex_lock(&steam->mutex); 304 if (!steam->client_opened && lizard_mode) 305 steam_set_lizard_mode(steam, true); 306 mutex_unlock(&steam->mutex); 307 308 hid_hw_close(steam->hdev); 309 } 310 311 static enum power_supply_property steam_battery_props[] = { 312 POWER_SUPPLY_PROP_PRESENT, 313 POWER_SUPPLY_PROP_SCOPE, 314 POWER_SUPPLY_PROP_VOLTAGE_NOW, 315 POWER_SUPPLY_PROP_CAPACITY, 316 }; 317 318 static int steam_battery_get_property(struct power_supply *psy, 319 enum power_supply_property psp, 320 union power_supply_propval *val) 321 { 322 struct steam_device *steam = power_supply_get_drvdata(psy); 323 unsigned long flags; 324 s16 volts; 325 u8 batt; 326 int ret = 0; 327 328 spin_lock_irqsave(&steam->lock, flags); 329 volts = steam->voltage; 330 batt = steam->battery_charge; 331 spin_unlock_irqrestore(&steam->lock, flags); 332 333 switch (psp) { 334 case POWER_SUPPLY_PROP_PRESENT: 335 val->intval = 1; 336 break; 337 case POWER_SUPPLY_PROP_SCOPE: 338 val->intval = POWER_SUPPLY_SCOPE_DEVICE; 339 break; 340 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 341 val->intval = volts * 1000; /* mV -> uV */ 342 break; 343 case POWER_SUPPLY_PROP_CAPACITY: 344 val->intval = batt; 345 break; 346 default: 347 ret = -EINVAL; 348 break; 349 } 350 return ret; 351 } 352 353 static int steam_battery_register(struct steam_device *steam) 354 { 355 struct power_supply *battery; 356 struct power_supply_config battery_cfg = { .drv_data = steam, }; 357 unsigned long flags; 358 int ret; 359 360 steam->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY; 361 steam->battery_desc.properties = steam_battery_props; 362 steam->battery_desc.num_properties = ARRAY_SIZE(steam_battery_props); 363 steam->battery_desc.get_property = steam_battery_get_property; 364 steam->battery_desc.name = devm_kasprintf(&steam->hdev->dev, 365 GFP_KERNEL, "steam-controller-%s-battery", 366 steam->serial_no); 367 if (!steam->battery_desc.name) 368 return -ENOMEM; 369 370 /* avoid the warning of 0% battery while waiting for the first info */ 371 spin_lock_irqsave(&steam->lock, flags); 372 steam->voltage = 3000; 373 steam->battery_charge = 100; 374 spin_unlock_irqrestore(&steam->lock, flags); 375 376 battery = power_supply_register(&steam->hdev->dev, 377 &steam->battery_desc, &battery_cfg); 378 if (IS_ERR(battery)) { 379 ret = PTR_ERR(battery); 380 hid_err(steam->hdev, 381 "%s:power_supply_register failed with error %d\n", 382 __func__, ret); 383 return ret; 384 } 385 rcu_assign_pointer(steam->battery, battery); 386 power_supply_powers(battery, &steam->hdev->dev); 387 return 0; 388 } 389 390 static int steam_input_register(struct steam_device *steam) 391 { 392 struct hid_device *hdev = steam->hdev; 393 struct input_dev *input; 394 int ret; 395 396 rcu_read_lock(); 397 input = rcu_dereference(steam->input); 398 rcu_read_unlock(); 399 if (input) { 400 dbg_hid("%s: already connected\n", __func__); 401 return 0; 402 } 403 404 input = input_allocate_device(); 405 if (!input) 406 return -ENOMEM; 407 408 input_set_drvdata(input, steam); 409 input->dev.parent = &hdev->dev; 410 input->open = steam_input_open; 411 input->close = steam_input_close; 412 413 input->name = (steam->quirks & STEAM_QUIRK_WIRELESS) ? 414 "Wireless Steam Controller" : 415 "Steam Controller"; 416 input->phys = hdev->phys; 417 input->uniq = steam->serial_no; 418 input->id.bustype = hdev->bus; 419 input->id.vendor = hdev->vendor; 420 input->id.product = hdev->product; 421 input->id.version = hdev->version; 422 423 input_set_capability(input, EV_KEY, BTN_TR2); 424 input_set_capability(input, EV_KEY, BTN_TL2); 425 input_set_capability(input, EV_KEY, BTN_TR); 426 input_set_capability(input, EV_KEY, BTN_TL); 427 input_set_capability(input, EV_KEY, BTN_Y); 428 input_set_capability(input, EV_KEY, BTN_B); 429 input_set_capability(input, EV_KEY, BTN_X); 430 input_set_capability(input, EV_KEY, BTN_A); 431 input_set_capability(input, EV_KEY, BTN_DPAD_UP); 432 input_set_capability(input, EV_KEY, BTN_DPAD_RIGHT); 433 input_set_capability(input, EV_KEY, BTN_DPAD_LEFT); 434 input_set_capability(input, EV_KEY, BTN_DPAD_DOWN); 435 input_set_capability(input, EV_KEY, BTN_SELECT); 436 input_set_capability(input, EV_KEY, BTN_MODE); 437 input_set_capability(input, EV_KEY, BTN_START); 438 input_set_capability(input, EV_KEY, BTN_GEAR_DOWN); 439 input_set_capability(input, EV_KEY, BTN_GEAR_UP); 440 input_set_capability(input, EV_KEY, BTN_THUMBR); 441 input_set_capability(input, EV_KEY, BTN_THUMBL); 442 input_set_capability(input, EV_KEY, BTN_THUMB); 443 input_set_capability(input, EV_KEY, BTN_THUMB2); 444 445 input_set_abs_params(input, ABS_HAT2Y, 0, 255, 0, 0); 446 input_set_abs_params(input, ABS_HAT2X, 0, 255, 0, 0); 447 input_set_abs_params(input, ABS_X, -32767, 32767, 0, 0); 448 input_set_abs_params(input, ABS_Y, -32767, 32767, 0, 0); 449 input_set_abs_params(input, ABS_RX, -32767, 32767, 450 STEAM_PAD_FUZZ, 0); 451 input_set_abs_params(input, ABS_RY, -32767, 32767, 452 STEAM_PAD_FUZZ, 0); 453 input_set_abs_params(input, ABS_HAT0X, -32767, 32767, 454 STEAM_PAD_FUZZ, 0); 455 input_set_abs_params(input, ABS_HAT0Y, -32767, 32767, 456 STEAM_PAD_FUZZ, 0); 457 input_abs_set_res(input, ABS_X, STEAM_JOYSTICK_RESOLUTION); 458 input_abs_set_res(input, ABS_Y, STEAM_JOYSTICK_RESOLUTION); 459 input_abs_set_res(input, ABS_RX, STEAM_PAD_RESOLUTION); 460 input_abs_set_res(input, ABS_RY, STEAM_PAD_RESOLUTION); 461 input_abs_set_res(input, ABS_HAT0X, STEAM_PAD_RESOLUTION); 462 input_abs_set_res(input, ABS_HAT0Y, STEAM_PAD_RESOLUTION); 463 input_abs_set_res(input, ABS_HAT2Y, STEAM_TRIGGER_RESOLUTION); 464 input_abs_set_res(input, ABS_HAT2X, STEAM_TRIGGER_RESOLUTION); 465 466 ret = input_register_device(input); 467 if (ret) 468 goto input_register_fail; 469 470 rcu_assign_pointer(steam->input, input); 471 return 0; 472 473 input_register_fail: 474 input_free_device(input); 475 return ret; 476 } 477 478 static void steam_input_unregister(struct steam_device *steam) 479 { 480 struct input_dev *input; 481 rcu_read_lock(); 482 input = rcu_dereference(steam->input); 483 rcu_read_unlock(); 484 if (!input) 485 return; 486 RCU_INIT_POINTER(steam->input, NULL); 487 synchronize_rcu(); 488 input_unregister_device(input); 489 } 490 491 static void steam_battery_unregister(struct steam_device *steam) 492 { 493 struct power_supply *battery; 494 495 rcu_read_lock(); 496 battery = rcu_dereference(steam->battery); 497 rcu_read_unlock(); 498 499 if (!battery) 500 return; 501 RCU_INIT_POINTER(steam->battery, NULL); 502 synchronize_rcu(); 503 power_supply_unregister(battery); 504 } 505 506 static int steam_register(struct steam_device *steam) 507 { 508 int ret; 509 510 /* 511 * This function can be called several times in a row with the 512 * wireless adaptor, without steam_unregister() between them, because 513 * another client send a get_connection_status command, for example. 514 * The battery and serial number are set just once per device. 515 */ 516 if (!steam->serial_no[0]) { 517 /* 518 * Unlikely, but getting the serial could fail, and it is not so 519 * important, so make up a serial number and go on. 520 */ 521 if (steam_get_serial(steam) < 0) 522 strlcpy(steam->serial_no, "XXXXXXXXXX", 523 sizeof(steam->serial_no)); 524 525 hid_info(steam->hdev, "Steam Controller '%s' connected", 526 steam->serial_no); 527 528 /* ignore battery errors, we can live without it */ 529 if (steam->quirks & STEAM_QUIRK_WIRELESS) 530 steam_battery_register(steam); 531 532 mutex_lock(&steam_devices_lock); 533 list_add(&steam->list, &steam_devices); 534 mutex_unlock(&steam_devices_lock); 535 } 536 537 mutex_lock(&steam->mutex); 538 if (!steam->client_opened) { 539 steam_set_lizard_mode(steam, lizard_mode); 540 ret = steam_input_register(steam); 541 } else { 542 ret = 0; 543 } 544 mutex_unlock(&steam->mutex); 545 546 return ret; 547 } 548 549 static void steam_unregister(struct steam_device *steam) 550 { 551 steam_battery_unregister(steam); 552 steam_input_unregister(steam); 553 if (steam->serial_no[0]) { 554 hid_info(steam->hdev, "Steam Controller '%s' disconnected", 555 steam->serial_no); 556 mutex_lock(&steam_devices_lock); 557 list_del(&steam->list); 558 mutex_unlock(&steam_devices_lock); 559 steam->serial_no[0] = 0; 560 } 561 } 562 563 static void steam_work_connect_cb(struct work_struct *work) 564 { 565 struct steam_device *steam = container_of(work, struct steam_device, 566 work_connect); 567 unsigned long flags; 568 bool connected; 569 int ret; 570 571 spin_lock_irqsave(&steam->lock, flags); 572 connected = steam->connected; 573 spin_unlock_irqrestore(&steam->lock, flags); 574 575 if (connected) { 576 ret = steam_register(steam); 577 if (ret) { 578 hid_err(steam->hdev, 579 "%s:steam_register failed with error %d\n", 580 __func__, ret); 581 } 582 } else { 583 steam_unregister(steam); 584 } 585 } 586 587 static bool steam_is_valve_interface(struct hid_device *hdev) 588 { 589 struct hid_report_enum *rep_enum; 590 591 /* 592 * The wired device creates 3 interfaces: 593 * 0: emulated mouse. 594 * 1: emulated keyboard. 595 * 2: the real game pad. 596 * The wireless device creates 5 interfaces: 597 * 0: emulated keyboard. 598 * 1-4: slots where up to 4 real game pads will be connected to. 599 * We know which one is the real gamepad interface because they are the 600 * only ones with a feature report. 601 */ 602 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT]; 603 return !list_empty(&rep_enum->report_list); 604 } 605 606 static int steam_client_ll_parse(struct hid_device *hdev) 607 { 608 struct steam_device *steam = hdev->driver_data; 609 610 return hid_parse_report(hdev, steam->hdev->dev_rdesc, 611 steam->hdev->dev_rsize); 612 } 613 614 static int steam_client_ll_start(struct hid_device *hdev) 615 { 616 return 0; 617 } 618 619 static void steam_client_ll_stop(struct hid_device *hdev) 620 { 621 } 622 623 static int steam_client_ll_open(struct hid_device *hdev) 624 { 625 struct steam_device *steam = hdev->driver_data; 626 int ret; 627 628 ret = hid_hw_open(steam->hdev); 629 if (ret) 630 return ret; 631 632 mutex_lock(&steam->mutex); 633 steam->client_opened = true; 634 mutex_unlock(&steam->mutex); 635 636 steam_input_unregister(steam); 637 638 return ret; 639 } 640 641 static void steam_client_ll_close(struct hid_device *hdev) 642 { 643 struct steam_device *steam = hdev->driver_data; 644 645 mutex_lock(&steam->mutex); 646 steam->client_opened = false; 647 mutex_unlock(&steam->mutex); 648 649 hid_hw_close(steam->hdev); 650 if (steam->connected) { 651 steam_set_lizard_mode(steam, lizard_mode); 652 steam_input_register(steam); 653 } 654 } 655 656 static int steam_client_ll_raw_request(struct hid_device *hdev, 657 unsigned char reportnum, u8 *buf, 658 size_t count, unsigned char report_type, 659 int reqtype) 660 { 661 struct steam_device *steam = hdev->driver_data; 662 663 return hid_hw_raw_request(steam->hdev, reportnum, buf, count, 664 report_type, reqtype); 665 } 666 667 static struct hid_ll_driver steam_client_ll_driver = { 668 .parse = steam_client_ll_parse, 669 .start = steam_client_ll_start, 670 .stop = steam_client_ll_stop, 671 .open = steam_client_ll_open, 672 .close = steam_client_ll_close, 673 .raw_request = steam_client_ll_raw_request, 674 }; 675 676 static struct hid_device *steam_create_client_hid(struct hid_device *hdev) 677 { 678 struct hid_device *client_hdev; 679 680 client_hdev = hid_allocate_device(); 681 if (IS_ERR(client_hdev)) 682 return client_hdev; 683 684 client_hdev->ll_driver = &steam_client_ll_driver; 685 client_hdev->dev.parent = hdev->dev.parent; 686 client_hdev->bus = hdev->bus; 687 client_hdev->vendor = hdev->vendor; 688 client_hdev->product = hdev->product; 689 client_hdev->version = hdev->version; 690 client_hdev->type = hdev->type; 691 client_hdev->country = hdev->country; 692 strlcpy(client_hdev->name, hdev->name, 693 sizeof(client_hdev->name)); 694 strlcpy(client_hdev->phys, hdev->phys, 695 sizeof(client_hdev->phys)); 696 /* 697 * Since we use the same device info than the real interface to 698 * trick userspace, we will be calling steam_probe recursively. 699 * We need to recognize the client interface somehow. 700 */ 701 client_hdev->group = HID_GROUP_STEAM; 702 return client_hdev; 703 } 704 705 static int steam_probe(struct hid_device *hdev, 706 const struct hid_device_id *id) 707 { 708 struct steam_device *steam; 709 int ret; 710 711 ret = hid_parse(hdev); 712 if (ret) { 713 hid_err(hdev, 714 "%s:parse of hid interface failed\n", __func__); 715 return ret; 716 } 717 718 /* 719 * The virtual client_dev is only used for hidraw. 720 * Also avoid the recursive probe. 721 */ 722 if (hdev->group == HID_GROUP_STEAM) 723 return hid_hw_start(hdev, HID_CONNECT_HIDRAW); 724 /* 725 * The non-valve interfaces (mouse and keyboard emulation) are 726 * connected without changes. 727 */ 728 if (!steam_is_valve_interface(hdev)) 729 return hid_hw_start(hdev, HID_CONNECT_DEFAULT); 730 731 steam = devm_kzalloc(&hdev->dev, sizeof(*steam), GFP_KERNEL); 732 if (!steam) { 733 ret = -ENOMEM; 734 goto steam_alloc_fail; 735 } 736 steam->hdev = hdev; 737 hid_set_drvdata(hdev, steam); 738 spin_lock_init(&steam->lock); 739 mutex_init(&steam->mutex); 740 steam->quirks = id->driver_data; 741 INIT_WORK(&steam->work_connect, steam_work_connect_cb); 742 743 steam->client_hdev = steam_create_client_hid(hdev); 744 if (IS_ERR(steam->client_hdev)) { 745 ret = PTR_ERR(steam->client_hdev); 746 goto client_hdev_fail; 747 } 748 steam->client_hdev->driver_data = steam; 749 750 /* 751 * With the real steam controller interface, do not connect hidraw. 752 * Instead, create the client_hid and connect that. 753 */ 754 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_HIDRAW); 755 if (ret) 756 goto hid_hw_start_fail; 757 758 ret = hid_add_device(steam->client_hdev); 759 if (ret) 760 goto client_hdev_add_fail; 761 762 if (steam->quirks & STEAM_QUIRK_WIRELESS) { 763 ret = hid_hw_open(hdev); 764 if (ret) { 765 hid_err(hdev, 766 "%s:hid_hw_open for wireless\n", 767 __func__); 768 goto hid_hw_open_fail; 769 } 770 hid_info(hdev, "Steam wireless receiver connected"); 771 steam_request_conn_status(steam); 772 } else { 773 ret = steam_register(steam); 774 if (ret) { 775 hid_err(hdev, 776 "%s:steam_register failed with error %d\n", 777 __func__, ret); 778 goto input_register_fail; 779 } 780 } 781 782 return 0; 783 784 hid_hw_open_fail: 785 input_register_fail: 786 client_hdev_add_fail: 787 hid_hw_stop(hdev); 788 hid_hw_start_fail: 789 hid_destroy_device(steam->client_hdev); 790 client_hdev_fail: 791 cancel_work_sync(&steam->work_connect); 792 steam_alloc_fail: 793 hid_err(hdev, "%s: failed with error %d\n", 794 __func__, ret); 795 return ret; 796 } 797 798 static void steam_remove(struct hid_device *hdev) 799 { 800 struct steam_device *steam = hid_get_drvdata(hdev); 801 802 if (!steam || hdev->group == HID_GROUP_STEAM) { 803 hid_hw_stop(hdev); 804 return; 805 } 806 807 hid_destroy_device(steam->client_hdev); 808 steam->client_opened = false; 809 cancel_work_sync(&steam->work_connect); 810 if (steam->quirks & STEAM_QUIRK_WIRELESS) { 811 hid_info(hdev, "Steam wireless receiver disconnected"); 812 hid_hw_close(hdev); 813 } 814 hid_hw_stop(hdev); 815 steam_unregister(steam); 816 } 817 818 static void steam_do_connect_event(struct steam_device *steam, bool connected) 819 { 820 unsigned long flags; 821 bool changed; 822 823 spin_lock_irqsave(&steam->lock, flags); 824 changed = steam->connected != connected; 825 steam->connected = connected; 826 spin_unlock_irqrestore(&steam->lock, flags); 827 828 if (changed && schedule_work(&steam->work_connect) == 0) 829 dbg_hid("%s: connected=%d event already queued\n", 830 __func__, connected); 831 } 832 833 /* 834 * Some input data in the protocol has the opposite sign. 835 * Clamp the values to 32767..-32767 so that the range is 836 * symmetrical and can be negated safely. 837 */ 838 static inline s16 steam_le16(u8 *data) 839 { 840 s16 x = (s16) le16_to_cpup((__le16 *)data); 841 842 return x == -32768 ? -32767 : x; 843 } 844 845 /* 846 * The size for this message payload is 60. 847 * The known values are: 848 * (* values are not sent through wireless) 849 * (* accelerator/gyro is disabled by default) 850 * Offset| Type | Mapped to |Meaning 851 * -------+-------+-----------+-------------------------- 852 * 4-7 | u32 | -- | sequence number 853 * 8-10 | 24bit | see below | buttons 854 * 11 | u8 | ABS_HAT2Y | left trigger 855 * 12 | u8 | ABS_HAT2X | right trigger 856 * 13-15 | -- | -- | always 0 857 * 16-17 | s16 | ABS_X/ABS_HAT0X | X value 858 * 18-19 | s16 | ABS_Y/ABS_HAT0Y | Y value 859 * 20-21 | s16 | ABS_RX | right-pad X value 860 * 22-23 | s16 | ABS_RY | right-pad Y value 861 * 24-25 | s16 | -- | * left trigger 862 * 26-27 | s16 | -- | * right trigger 863 * 28-29 | s16 | -- | * accelerometer X value 864 * 30-31 | s16 | -- | * accelerometer Y value 865 * 32-33 | s16 | -- | * accelerometer Z value 866 * 34-35 | s16 | -- | gyro X value 867 * 36-36 | s16 | -- | gyro Y value 868 * 38-39 | s16 | -- | gyro Z value 869 * 40-41 | s16 | -- | quaternion W value 870 * 42-43 | s16 | -- | quaternion X value 871 * 44-45 | s16 | -- | quaternion Y value 872 * 46-47 | s16 | -- | quaternion Z value 873 * 48-49 | -- | -- | always 0 874 * 50-51 | s16 | -- | * left trigger (uncalibrated) 875 * 52-53 | s16 | -- | * right trigger (uncalibrated) 876 * 54-55 | s16 | -- | * joystick X value (uncalibrated) 877 * 56-57 | s16 | -- | * joystick Y value (uncalibrated) 878 * 58-59 | s16 | -- | * left-pad X value 879 * 60-61 | s16 | -- | * left-pad Y value 880 * 62-63 | u16 | -- | * battery voltage 881 * 882 * The buttons are: 883 * Bit | Mapped to | Description 884 * ------+------------+-------------------------------- 885 * 8.0 | BTN_TR2 | right trigger fully pressed 886 * 8.1 | BTN_TL2 | left trigger fully pressed 887 * 8.2 | BTN_TR | right shoulder 888 * 8.3 | BTN_TL | left shoulder 889 * 8.4 | BTN_Y | button Y 890 * 8.5 | BTN_B | button B 891 * 8.6 | BTN_X | button X 892 * 8.7 | BTN_A | button A 893 * 9.0 | BTN_DPAD_UP | lef-pad up 894 * 9.1 | BTN_DPAD_RIGHT | lef-pad right 895 * 9.2 | BTN_DPAD_LEFT | lef-pad left 896 * 9.3 | BTN_DPAD_DOWN | lef-pad down 897 * 9.4 | BTN_SELECT | menu left 898 * 9.5 | BTN_MODE | steam logo 899 * 9.6 | BTN_START | menu right 900 * 9.7 | BTN_GEAR_DOWN | left back lever 901 * 10.0 | BTN_GEAR_UP | right back lever 902 * 10.1 | -- | left-pad clicked 903 * 10.2 | BTN_THUMBR | right-pad clicked 904 * 10.3 | BTN_THUMB | left-pad touched (but see explanation below) 905 * 10.4 | BTN_THUMB2 | right-pad touched 906 * 10.5 | -- | unknown 907 * 10.6 | BTN_THUMBL | joystick clicked 908 * 10.7 | -- | lpad_and_joy 909 */ 910 911 static void steam_do_input_event(struct steam_device *steam, 912 struct input_dev *input, u8 *data) 913 { 914 /* 24 bits of buttons */ 915 u8 b8, b9, b10; 916 s16 x, y; 917 bool lpad_touched, lpad_and_joy; 918 919 b8 = data[8]; 920 b9 = data[9]; 921 b10 = data[10]; 922 923 input_report_abs(input, ABS_HAT2Y, data[11]); 924 input_report_abs(input, ABS_HAT2X, data[12]); 925 926 /* 927 * These two bits tells how to interpret the values X and Y. 928 * lpad_and_joy tells that the joystick and the lpad are used at the 929 * same time. 930 * lpad_touched tells whether X/Y are to be read as lpad coord or 931 * joystick values. 932 * (lpad_touched || lpad_and_joy) tells if the lpad is really touched. 933 */ 934 lpad_touched = b10 & BIT(3); 935 lpad_and_joy = b10 & BIT(7); 936 x = steam_le16(data + 16); 937 y = -steam_le16(data + 18); 938 939 input_report_abs(input, lpad_touched ? ABS_HAT0X : ABS_X, x); 940 input_report_abs(input, lpad_touched ? ABS_HAT0Y : ABS_Y, y); 941 /* Check if joystick is centered */ 942 if (lpad_touched && !lpad_and_joy) { 943 input_report_abs(input, ABS_X, 0); 944 input_report_abs(input, ABS_Y, 0); 945 } 946 /* Check if lpad is untouched */ 947 if (!(lpad_touched || lpad_and_joy)) { 948 input_report_abs(input, ABS_HAT0X, 0); 949 input_report_abs(input, ABS_HAT0Y, 0); 950 } 951 952 input_report_abs(input, ABS_RX, steam_le16(data + 20)); 953 input_report_abs(input, ABS_RY, -steam_le16(data + 22)); 954 955 input_event(input, EV_KEY, BTN_TR2, !!(b8 & BIT(0))); 956 input_event(input, EV_KEY, BTN_TL2, !!(b8 & BIT(1))); 957 input_event(input, EV_KEY, BTN_TR, !!(b8 & BIT(2))); 958 input_event(input, EV_KEY, BTN_TL, !!(b8 & BIT(3))); 959 input_event(input, EV_KEY, BTN_Y, !!(b8 & BIT(4))); 960 input_event(input, EV_KEY, BTN_B, !!(b8 & BIT(5))); 961 input_event(input, EV_KEY, BTN_X, !!(b8 & BIT(6))); 962 input_event(input, EV_KEY, BTN_A, !!(b8 & BIT(7))); 963 input_event(input, EV_KEY, BTN_SELECT, !!(b9 & BIT(4))); 964 input_event(input, EV_KEY, BTN_MODE, !!(b9 & BIT(5))); 965 input_event(input, EV_KEY, BTN_START, !!(b9 & BIT(6))); 966 input_event(input, EV_KEY, BTN_GEAR_DOWN, !!(b9 & BIT(7))); 967 input_event(input, EV_KEY, BTN_GEAR_UP, !!(b10 & BIT(0))); 968 input_event(input, EV_KEY, BTN_THUMBR, !!(b10 & BIT(2))); 969 input_event(input, EV_KEY, BTN_THUMBL, !!(b10 & BIT(6))); 970 input_event(input, EV_KEY, BTN_THUMB, lpad_touched || lpad_and_joy); 971 input_event(input, EV_KEY, BTN_THUMB2, !!(b10 & BIT(4))); 972 input_event(input, EV_KEY, BTN_DPAD_UP, !!(b9 & BIT(0))); 973 input_event(input, EV_KEY, BTN_DPAD_RIGHT, !!(b9 & BIT(1))); 974 input_event(input, EV_KEY, BTN_DPAD_LEFT, !!(b9 & BIT(2))); 975 input_event(input, EV_KEY, BTN_DPAD_DOWN, !!(b9 & BIT(3))); 976 977 input_sync(input); 978 } 979 980 /* 981 * The size for this message payload is 11. 982 * The known values are: 983 * Offset| Type | Meaning 984 * -------+-------+--------------------------- 985 * 4-7 | u32 | sequence number 986 * 8-11 | -- | always 0 987 * 12-13 | u16 | voltage (mV) 988 * 14 | u8 | battery percent 989 */ 990 static void steam_do_battery_event(struct steam_device *steam, 991 struct power_supply *battery, u8 *data) 992 { 993 unsigned long flags; 994 995 s16 volts = steam_le16(data + 12); 996 u8 batt = data[14]; 997 998 /* Creating the battery may have failed */ 999 rcu_read_lock(); 1000 battery = rcu_dereference(steam->battery); 1001 if (likely(battery)) { 1002 spin_lock_irqsave(&steam->lock, flags); 1003 steam->voltage = volts; 1004 steam->battery_charge = batt; 1005 spin_unlock_irqrestore(&steam->lock, flags); 1006 power_supply_changed(battery); 1007 } 1008 rcu_read_unlock(); 1009 } 1010 1011 static int steam_raw_event(struct hid_device *hdev, 1012 struct hid_report *report, u8 *data, 1013 int size) 1014 { 1015 struct steam_device *steam = hid_get_drvdata(hdev); 1016 struct input_dev *input; 1017 struct power_supply *battery; 1018 1019 if (!steam) 1020 return 0; 1021 1022 if (steam->client_opened) 1023 hid_input_report(steam->client_hdev, HID_FEATURE_REPORT, 1024 data, size, 0); 1025 /* 1026 * All messages are size=64, all values little-endian. 1027 * The format is: 1028 * Offset| Meaning 1029 * -------+-------------------------------------------- 1030 * 0-1 | always 0x01, 0x00, maybe protocol version? 1031 * 2 | type of message 1032 * 3 | length of the real payload (not checked) 1033 * 4-n | payload data, depends on the type 1034 * 1035 * There are these known types of message: 1036 * 0x01: input data (60 bytes) 1037 * 0x03: wireless connect/disconnect (1 byte) 1038 * 0x04: battery status (11 bytes) 1039 */ 1040 1041 if (size != 64 || data[0] != 1 || data[1] != 0) 1042 return 0; 1043 1044 switch (data[2]) { 1045 case STEAM_EV_INPUT_DATA: 1046 if (steam->client_opened) 1047 return 0; 1048 rcu_read_lock(); 1049 input = rcu_dereference(steam->input); 1050 if (likely(input)) 1051 steam_do_input_event(steam, input, data); 1052 rcu_read_unlock(); 1053 break; 1054 case STEAM_EV_CONNECT: 1055 /* 1056 * The payload of this event is a single byte: 1057 * 0x01: disconnected. 1058 * 0x02: connected. 1059 */ 1060 switch (data[4]) { 1061 case 0x01: 1062 steam_do_connect_event(steam, false); 1063 break; 1064 case 0x02: 1065 steam_do_connect_event(steam, true); 1066 break; 1067 } 1068 break; 1069 case STEAM_EV_BATTERY: 1070 if (steam->quirks & STEAM_QUIRK_WIRELESS) { 1071 rcu_read_lock(); 1072 battery = rcu_dereference(steam->battery); 1073 if (likely(battery)) { 1074 steam_do_battery_event(steam, battery, data); 1075 } else { 1076 dbg_hid( 1077 "%s: battery data without connect event\n", 1078 __func__); 1079 steam_do_connect_event(steam, true); 1080 } 1081 rcu_read_unlock(); 1082 } 1083 break; 1084 } 1085 return 0; 1086 } 1087 1088 static int steam_param_set_lizard_mode(const char *val, 1089 const struct kernel_param *kp) 1090 { 1091 struct steam_device *steam; 1092 int ret; 1093 1094 ret = param_set_bool(val, kp); 1095 if (ret) 1096 return ret; 1097 1098 mutex_lock(&steam_devices_lock); 1099 list_for_each_entry(steam, &steam_devices, list) { 1100 mutex_lock(&steam->mutex); 1101 if (!steam->client_opened) 1102 steam_set_lizard_mode(steam, lizard_mode); 1103 mutex_unlock(&steam->mutex); 1104 } 1105 mutex_unlock(&steam_devices_lock); 1106 return 0; 1107 } 1108 1109 static const struct kernel_param_ops steam_lizard_mode_ops = { 1110 .set = steam_param_set_lizard_mode, 1111 .get = param_get_bool, 1112 }; 1113 1114 module_param_cb(lizard_mode, &steam_lizard_mode_ops, &lizard_mode, 0644); 1115 MODULE_PARM_DESC(lizard_mode, 1116 "Enable mouse and keyboard emulation (lizard mode) when the gamepad is not in use"); 1117 1118 static const struct hid_device_id steam_controllers[] = { 1119 { /* Wired Steam Controller */ 1120 HID_USB_DEVICE(USB_VENDOR_ID_VALVE, 1121 USB_DEVICE_ID_STEAM_CONTROLLER) 1122 }, 1123 { /* Wireless Steam Controller */ 1124 HID_USB_DEVICE(USB_VENDOR_ID_VALVE, 1125 USB_DEVICE_ID_STEAM_CONTROLLER_WIRELESS), 1126 .driver_data = STEAM_QUIRK_WIRELESS 1127 }, 1128 {} 1129 }; 1130 1131 MODULE_DEVICE_TABLE(hid, steam_controllers); 1132 1133 static struct hid_driver steam_controller_driver = { 1134 .name = "hid-steam", 1135 .id_table = steam_controllers, 1136 .probe = steam_probe, 1137 .remove = steam_remove, 1138 .raw_event = steam_raw_event, 1139 }; 1140 1141 module_hid_driver(steam_controller_driver); 1142