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