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 "hid-ids.h"
47
48 MODULE_LICENSE("GPL");
49 MODULE_AUTHOR("Rodrigo Rivas Costa <rodrigorivascosta@gmail.com>");
50
51 static bool lizard_mode = true;
52
53 static DEFINE_MUTEX(steam_devices_lock);
54 static LIST_HEAD(steam_devices);
55
56 #define STEAM_QUIRK_WIRELESS BIT(0)
57 #define STEAM_QUIRK_DECK BIT(1)
58
59 /* Touch pads are 40 mm in diameter and 65535 units */
60 #define STEAM_PAD_RESOLUTION 1638
61 /* Trigger runs are about 5 mm and 256 units */
62 #define STEAM_TRIGGER_RESOLUTION 51
63 /* Joystick runs are about 5 mm and 256 units */
64 #define STEAM_JOYSTICK_RESOLUTION 51
65 /* Trigger runs are about 6 mm and 32768 units */
66 #define STEAM_DECK_TRIGGER_RESOLUTION 5461
67 /* Joystick runs are about 5 mm and 32768 units */
68 #define STEAM_DECK_JOYSTICK_RESOLUTION 6553
69
70 #define STEAM_PAD_FUZZ 256
71
72 /*
73 * Commands that can be sent in a feature report.
74 * Thanks to Valve for some valuable hints.
75 */
76 #define STEAM_CMD_SET_MAPPINGS 0x80
77 #define STEAM_CMD_CLEAR_MAPPINGS 0x81
78 #define STEAM_CMD_GET_MAPPINGS 0x82
79 #define STEAM_CMD_GET_ATTRIB 0x83
80 #define STEAM_CMD_GET_ATTRIB_LABEL 0x84
81 #define STEAM_CMD_DEFAULT_MAPPINGS 0x85
82 #define STEAM_CMD_FACTORY_RESET 0x86
83 #define STEAM_CMD_WRITE_REGISTER 0x87
84 #define STEAM_CMD_CLEAR_REGISTER 0x88
85 #define STEAM_CMD_READ_REGISTER 0x89
86 #define STEAM_CMD_GET_REGISTER_LABEL 0x8a
87 #define STEAM_CMD_GET_REGISTER_MAX 0x8b
88 #define STEAM_CMD_GET_REGISTER_DEFAULT 0x8c
89 #define STEAM_CMD_SET_MODE 0x8d
90 #define STEAM_CMD_DEFAULT_MOUSE 0x8e
91 #define STEAM_CMD_FORCEFEEDBAK 0x8f
92 #define STEAM_CMD_REQUEST_COMM_STATUS 0xb4
93 #define STEAM_CMD_GET_SERIAL 0xae
94 #define STEAM_CMD_HAPTIC_RUMBLE 0xeb
95
96 /* Some useful register ids */
97 #define STEAM_REG_LPAD_MODE 0x07
98 #define STEAM_REG_RPAD_MODE 0x08
99 #define STEAM_REG_RPAD_MARGIN 0x18
100 #define STEAM_REG_LED 0x2d
101 #define STEAM_REG_GYRO_MODE 0x30
102 #define STEAM_REG_LPAD_CLICK_PRESSURE 0x34
103 #define STEAM_REG_RPAD_CLICK_PRESSURE 0x35
104
105 /* Raw event identifiers */
106 #define STEAM_EV_INPUT_DATA 0x01
107 #define STEAM_EV_CONNECT 0x03
108 #define STEAM_EV_BATTERY 0x04
109 #define STEAM_EV_DECK_INPUT_DATA 0x09
110
111 /* Values for GYRO_MODE (bitmask) */
112 #define STEAM_GYRO_MODE_OFF 0x0000
113 #define STEAM_GYRO_MODE_STEERING 0x0001
114 #define STEAM_GYRO_MODE_TILT 0x0002
115 #define STEAM_GYRO_MODE_SEND_ORIENTATION 0x0004
116 #define STEAM_GYRO_MODE_SEND_RAW_ACCEL 0x0008
117 #define STEAM_GYRO_MODE_SEND_RAW_GYRO 0x0010
118
119 /* Other random constants */
120 #define STEAM_SERIAL_LEN 10
121
122 struct steam_device {
123 struct list_head list;
124 spinlock_t lock;
125 struct hid_device *hdev, *client_hdev;
126 struct mutex mutex;
127 bool client_opened;
128 struct input_dev __rcu *input;
129 unsigned long quirks;
130 struct work_struct work_connect;
131 bool connected;
132 char serial_no[STEAM_SERIAL_LEN + 1];
133 struct power_supply_desc battery_desc;
134 struct power_supply __rcu *battery;
135 u8 battery_charge;
136 u16 voltage;
137 struct delayed_work heartbeat;
138 struct work_struct rumble_work;
139 u16 rumble_left;
140 u16 rumble_right;
141 };
142
steam_recv_report(struct steam_device * steam,u8 * data,int size)143 static int steam_recv_report(struct steam_device *steam,
144 u8 *data, int size)
145 {
146 struct hid_report *r;
147 u8 *buf;
148 int ret;
149
150 r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0];
151 if (!r) {
152 hid_err(steam->hdev, "No HID_FEATURE_REPORT submitted - nothing to read\n");
153 return -EINVAL;
154 }
155
156 if (hid_report_len(r) < 64)
157 return -EINVAL;
158
159 buf = hid_alloc_report_buf(r, GFP_KERNEL);
160 if (!buf)
161 return -ENOMEM;
162
163 /*
164 * The report ID is always 0, so strip the first byte from the output.
165 * hid_report_len() is not counting the report ID, so +1 to the length
166 * or else we get a EOVERFLOW. We are safe from a buffer overflow
167 * because hid_alloc_report_buf() allocates +7 bytes.
168 */
169 ret = hid_hw_raw_request(steam->hdev, 0x00,
170 buf, hid_report_len(r) + 1,
171 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
172 if (ret > 0)
173 memcpy(data, buf + 1, min(size, ret - 1));
174 kfree(buf);
175 return ret;
176 }
177
steam_send_report(struct steam_device * steam,u8 * cmd,int size)178 static int steam_send_report(struct steam_device *steam,
179 u8 *cmd, int size)
180 {
181 struct hid_report *r;
182 u8 *buf;
183 unsigned int retries = 50;
184 int ret;
185
186 r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0];
187 if (!r) {
188 hid_err(steam->hdev, "No HID_FEATURE_REPORT submitted - nothing to read\n");
189 return -EINVAL;
190 }
191
192 if (hid_report_len(r) < 64)
193 return -EINVAL;
194
195 buf = hid_alloc_report_buf(r, GFP_KERNEL);
196 if (!buf)
197 return -ENOMEM;
198
199 /* The report ID is always 0 */
200 memcpy(buf + 1, cmd, size);
201
202 /*
203 * Sometimes the wireless controller fails with EPIPE
204 * when sending a feature report.
205 * Doing a HID_REQ_GET_REPORT and waiting for a while
206 * seems to fix that.
207 */
208 do {
209 ret = hid_hw_raw_request(steam->hdev, 0,
210 buf, max(size, 64) + 1,
211 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
212 if (ret != -EPIPE)
213 break;
214 msleep(20);
215 } while (--retries);
216
217 kfree(buf);
218 if (ret < 0)
219 hid_err(steam->hdev, "%s: error %d (%*ph)\n", __func__,
220 ret, size, cmd);
221 return ret;
222 }
223
steam_send_report_byte(struct steam_device * steam,u8 cmd)224 static inline int steam_send_report_byte(struct steam_device *steam, u8 cmd)
225 {
226 return steam_send_report(steam, &cmd, 1);
227 }
228
steam_write_registers(struct steam_device * steam,...)229 static int steam_write_registers(struct steam_device *steam,
230 /* u8 reg, u16 val */...)
231 {
232 /* Send: 0x87 len (reg valLo valHi)* */
233 u8 reg;
234 u16 val;
235 u8 cmd[64] = {STEAM_CMD_WRITE_REGISTER, 0x00};
236 int ret;
237 va_list args;
238
239 va_start(args, steam);
240 for (;;) {
241 reg = va_arg(args, int);
242 if (reg == 0)
243 break;
244 val = va_arg(args, int);
245 cmd[cmd[1] + 2] = reg;
246 cmd[cmd[1] + 3] = val & 0xff;
247 cmd[cmd[1] + 4] = val >> 8;
248 cmd[1] += 3;
249 }
250 va_end(args);
251
252 ret = steam_send_report(steam, cmd, 2 + cmd[1]);
253 if (ret < 0)
254 return ret;
255
256 /*
257 * Sometimes a lingering report for this command can
258 * get read back instead of the last set report if
259 * this isn't explicitly queried
260 */
261 return steam_recv_report(steam, cmd, 2 + cmd[1]);
262 }
263
steam_get_serial(struct steam_device * steam)264 static int steam_get_serial(struct steam_device *steam)
265 {
266 /*
267 * Send: 0xae 0x15 0x01
268 * Recv: 0xae 0x15 0x01 serialnumber (10 chars)
269 */
270 int ret;
271 u8 cmd[] = {STEAM_CMD_GET_SERIAL, 0x15, 0x01};
272 u8 reply[3 + STEAM_SERIAL_LEN + 1];
273
274 ret = steam_send_report(steam, cmd, sizeof(cmd));
275 if (ret < 0)
276 return ret;
277 ret = steam_recv_report(steam, reply, sizeof(reply));
278 if (ret < 0)
279 return ret;
280 if (reply[0] != 0xae || reply[1] != 0x15 || reply[2] != 0x01)
281 return -EIO;
282 reply[3 + STEAM_SERIAL_LEN] = 0;
283 strscpy(steam->serial_no, reply + 3, sizeof(steam->serial_no));
284 return 0;
285 }
286
287 /*
288 * This command requests the wireless adaptor to post an event
289 * with the connection status. Useful if this driver is loaded when
290 * the controller is already connected.
291 */
steam_request_conn_status(struct steam_device * steam)292 static inline int steam_request_conn_status(struct steam_device *steam)
293 {
294 return steam_send_report_byte(steam, STEAM_CMD_REQUEST_COMM_STATUS);
295 }
296
steam_haptic_rumble(struct steam_device * steam,u16 intensity,u16 left_speed,u16 right_speed,u8 left_gain,u8 right_gain)297 static inline int steam_haptic_rumble(struct steam_device *steam,
298 u16 intensity, u16 left_speed, u16 right_speed,
299 u8 left_gain, u8 right_gain)
300 {
301 u8 report[11] = {STEAM_CMD_HAPTIC_RUMBLE, 9};
302
303 report[3] = intensity & 0xFF;
304 report[4] = intensity >> 8;
305 report[5] = left_speed & 0xFF;
306 report[6] = left_speed >> 8;
307 report[7] = right_speed & 0xFF;
308 report[8] = right_speed >> 8;
309 report[9] = left_gain;
310 report[10] = right_gain;
311
312 return steam_send_report(steam, report, sizeof(report));
313 }
314
steam_haptic_rumble_cb(struct work_struct * work)315 static void steam_haptic_rumble_cb(struct work_struct *work)
316 {
317 struct steam_device *steam = container_of(work, struct steam_device,
318 rumble_work);
319 steam_haptic_rumble(steam, 0, steam->rumble_left,
320 steam->rumble_right, 2, 0);
321 }
322
323 #ifdef CONFIG_STEAM_FF
steam_play_effect(struct input_dev * dev,void * data,struct ff_effect * effect)324 static int steam_play_effect(struct input_dev *dev, void *data,
325 struct ff_effect *effect)
326 {
327 struct steam_device *steam = input_get_drvdata(dev);
328
329 steam->rumble_left = effect->u.rumble.strong_magnitude;
330 steam->rumble_right = effect->u.rumble.weak_magnitude;
331
332 return schedule_work(&steam->rumble_work);
333 }
334 #endif
335
steam_set_lizard_mode(struct steam_device * steam,bool enable)336 static void steam_set_lizard_mode(struct steam_device *steam, bool enable)
337 {
338 if (enable) {
339 /* enable esc, enter, cursors */
340 steam_send_report_byte(steam, STEAM_CMD_DEFAULT_MAPPINGS);
341 /* enable mouse */
342 steam_send_report_byte(steam, STEAM_CMD_DEFAULT_MOUSE);
343 steam_write_registers(steam,
344 STEAM_REG_RPAD_MARGIN, 0x01, /* enable margin */
345 0);
346
347 cancel_delayed_work_sync(&steam->heartbeat);
348 } else {
349 /* disable esc, enter, cursor */
350 steam_send_report_byte(steam, STEAM_CMD_CLEAR_MAPPINGS);
351
352 if (steam->quirks & STEAM_QUIRK_DECK) {
353 steam_write_registers(steam,
354 STEAM_REG_RPAD_MARGIN, 0x00, /* disable margin */
355 STEAM_REG_LPAD_MODE, 0x07, /* disable mouse */
356 STEAM_REG_RPAD_MODE, 0x07, /* disable mouse */
357 STEAM_REG_LPAD_CLICK_PRESSURE, 0xFFFF, /* disable clicky pad */
358 STEAM_REG_RPAD_CLICK_PRESSURE, 0xFFFF, /* disable clicky pad */
359 0);
360 /*
361 * The Steam Deck has a watchdog that automatically enables
362 * lizard mode if it doesn't see any traffic for too long
363 */
364 if (!work_busy(&steam->heartbeat.work))
365 schedule_delayed_work(&steam->heartbeat, 5 * HZ);
366 } else {
367 steam_write_registers(steam,
368 STEAM_REG_RPAD_MARGIN, 0x00, /* disable margin */
369 STEAM_REG_LPAD_MODE, 0x07, /* disable mouse */
370 STEAM_REG_RPAD_MODE, 0x07, /* disable mouse */
371 0);
372 }
373 }
374 }
375
steam_input_open(struct input_dev * dev)376 static int steam_input_open(struct input_dev *dev)
377 {
378 struct steam_device *steam = input_get_drvdata(dev);
379
380 mutex_lock(&steam->mutex);
381 if (!steam->client_opened && lizard_mode)
382 steam_set_lizard_mode(steam, false);
383 mutex_unlock(&steam->mutex);
384 return 0;
385 }
386
steam_input_close(struct input_dev * dev)387 static void steam_input_close(struct input_dev *dev)
388 {
389 struct steam_device *steam = input_get_drvdata(dev);
390
391 mutex_lock(&steam->mutex);
392 if (!steam->client_opened && lizard_mode)
393 steam_set_lizard_mode(steam, true);
394 mutex_unlock(&steam->mutex);
395 }
396
397 static enum power_supply_property steam_battery_props[] = {
398 POWER_SUPPLY_PROP_PRESENT,
399 POWER_SUPPLY_PROP_SCOPE,
400 POWER_SUPPLY_PROP_VOLTAGE_NOW,
401 POWER_SUPPLY_PROP_CAPACITY,
402 };
403
steam_battery_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)404 static int steam_battery_get_property(struct power_supply *psy,
405 enum power_supply_property psp,
406 union power_supply_propval *val)
407 {
408 struct steam_device *steam = power_supply_get_drvdata(psy);
409 unsigned long flags;
410 s16 volts;
411 u8 batt;
412 int ret = 0;
413
414 spin_lock_irqsave(&steam->lock, flags);
415 volts = steam->voltage;
416 batt = steam->battery_charge;
417 spin_unlock_irqrestore(&steam->lock, flags);
418
419 switch (psp) {
420 case POWER_SUPPLY_PROP_PRESENT:
421 val->intval = 1;
422 break;
423 case POWER_SUPPLY_PROP_SCOPE:
424 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
425 break;
426 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
427 val->intval = volts * 1000; /* mV -> uV */
428 break;
429 case POWER_SUPPLY_PROP_CAPACITY:
430 val->intval = batt;
431 break;
432 default:
433 ret = -EINVAL;
434 break;
435 }
436 return ret;
437 }
438
steam_battery_register(struct steam_device * steam)439 static int steam_battery_register(struct steam_device *steam)
440 {
441 struct power_supply *battery;
442 struct power_supply_config battery_cfg = { .drv_data = steam, };
443 unsigned long flags;
444 int ret;
445
446 steam->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
447 steam->battery_desc.properties = steam_battery_props;
448 steam->battery_desc.num_properties = ARRAY_SIZE(steam_battery_props);
449 steam->battery_desc.get_property = steam_battery_get_property;
450 steam->battery_desc.name = devm_kasprintf(&steam->hdev->dev,
451 GFP_KERNEL, "steam-controller-%s-battery",
452 steam->serial_no);
453 if (!steam->battery_desc.name)
454 return -ENOMEM;
455
456 /* avoid the warning of 0% battery while waiting for the first info */
457 spin_lock_irqsave(&steam->lock, flags);
458 steam->voltage = 3000;
459 steam->battery_charge = 100;
460 spin_unlock_irqrestore(&steam->lock, flags);
461
462 battery = power_supply_register(&steam->hdev->dev,
463 &steam->battery_desc, &battery_cfg);
464 if (IS_ERR(battery)) {
465 ret = PTR_ERR(battery);
466 hid_err(steam->hdev,
467 "%s:power_supply_register failed with error %d\n",
468 __func__, ret);
469 return ret;
470 }
471 rcu_assign_pointer(steam->battery, battery);
472 power_supply_powers(battery, &steam->hdev->dev);
473 return 0;
474 }
475
steam_input_register(struct steam_device * steam)476 static int steam_input_register(struct steam_device *steam)
477 {
478 struct hid_device *hdev = steam->hdev;
479 struct input_dev *input;
480 int ret;
481
482 rcu_read_lock();
483 input = rcu_dereference(steam->input);
484 rcu_read_unlock();
485 if (input) {
486 dbg_hid("%s: already connected\n", __func__);
487 return 0;
488 }
489
490 input = input_allocate_device();
491 if (!input)
492 return -ENOMEM;
493
494 input_set_drvdata(input, steam);
495 input->dev.parent = &hdev->dev;
496 input->open = steam_input_open;
497 input->close = steam_input_close;
498
499 input->name = (steam->quirks & STEAM_QUIRK_WIRELESS) ? "Wireless Steam Controller" :
500 (steam->quirks & STEAM_QUIRK_DECK) ? "Steam Deck" :
501 "Steam Controller";
502 input->phys = hdev->phys;
503 input->uniq = steam->serial_no;
504 input->id.bustype = hdev->bus;
505 input->id.vendor = hdev->vendor;
506 input->id.product = hdev->product;
507 input->id.version = hdev->version;
508
509 input_set_capability(input, EV_KEY, BTN_TR2);
510 input_set_capability(input, EV_KEY, BTN_TL2);
511 input_set_capability(input, EV_KEY, BTN_TR);
512 input_set_capability(input, EV_KEY, BTN_TL);
513 input_set_capability(input, EV_KEY, BTN_Y);
514 input_set_capability(input, EV_KEY, BTN_B);
515 input_set_capability(input, EV_KEY, BTN_X);
516 input_set_capability(input, EV_KEY, BTN_A);
517 input_set_capability(input, EV_KEY, BTN_DPAD_UP);
518 input_set_capability(input, EV_KEY, BTN_DPAD_RIGHT);
519 input_set_capability(input, EV_KEY, BTN_DPAD_LEFT);
520 input_set_capability(input, EV_KEY, BTN_DPAD_DOWN);
521 input_set_capability(input, EV_KEY, BTN_SELECT);
522 input_set_capability(input, EV_KEY, BTN_MODE);
523 input_set_capability(input, EV_KEY, BTN_START);
524 input_set_capability(input, EV_KEY, BTN_THUMBR);
525 input_set_capability(input, EV_KEY, BTN_THUMBL);
526 input_set_capability(input, EV_KEY, BTN_THUMB);
527 input_set_capability(input, EV_KEY, BTN_THUMB2);
528 if (steam->quirks & STEAM_QUIRK_DECK) {
529 input_set_capability(input, EV_KEY, BTN_BASE);
530 input_set_capability(input, EV_KEY, BTN_TRIGGER_HAPPY1);
531 input_set_capability(input, EV_KEY, BTN_TRIGGER_HAPPY2);
532 input_set_capability(input, EV_KEY, BTN_TRIGGER_HAPPY3);
533 input_set_capability(input, EV_KEY, BTN_TRIGGER_HAPPY4);
534 } else {
535 input_set_capability(input, EV_KEY, BTN_GEAR_DOWN);
536 input_set_capability(input, EV_KEY, BTN_GEAR_UP);
537 }
538
539 input_set_abs_params(input, ABS_X, -32767, 32767, 0, 0);
540 input_set_abs_params(input, ABS_Y, -32767, 32767, 0, 0);
541
542 input_set_abs_params(input, ABS_HAT0X, -32767, 32767,
543 STEAM_PAD_FUZZ, 0);
544 input_set_abs_params(input, ABS_HAT0Y, -32767, 32767,
545 STEAM_PAD_FUZZ, 0);
546
547 if (steam->quirks & STEAM_QUIRK_DECK) {
548 input_set_abs_params(input, ABS_HAT2Y, 0, 32767, 0, 0);
549 input_set_abs_params(input, ABS_HAT2X, 0, 32767, 0, 0);
550
551 input_set_abs_params(input, ABS_RX, -32767, 32767, 0, 0);
552 input_set_abs_params(input, ABS_RY, -32767, 32767, 0, 0);
553
554 input_set_abs_params(input, ABS_HAT1X, -32767, 32767,
555 STEAM_PAD_FUZZ, 0);
556 input_set_abs_params(input, ABS_HAT1Y, -32767, 32767,
557 STEAM_PAD_FUZZ, 0);
558
559 input_abs_set_res(input, ABS_X, STEAM_DECK_JOYSTICK_RESOLUTION);
560 input_abs_set_res(input, ABS_Y, STEAM_DECK_JOYSTICK_RESOLUTION);
561 input_abs_set_res(input, ABS_RX, STEAM_DECK_JOYSTICK_RESOLUTION);
562 input_abs_set_res(input, ABS_RY, STEAM_DECK_JOYSTICK_RESOLUTION);
563 input_abs_set_res(input, ABS_HAT1X, STEAM_PAD_RESOLUTION);
564 input_abs_set_res(input, ABS_HAT1Y, STEAM_PAD_RESOLUTION);
565 input_abs_set_res(input, ABS_HAT2Y, STEAM_DECK_TRIGGER_RESOLUTION);
566 input_abs_set_res(input, ABS_HAT2X, STEAM_DECK_TRIGGER_RESOLUTION);
567 } else {
568 input_set_abs_params(input, ABS_HAT2Y, 0, 255, 0, 0);
569 input_set_abs_params(input, ABS_HAT2X, 0, 255, 0, 0);
570
571 input_set_abs_params(input, ABS_RX, -32767, 32767,
572 STEAM_PAD_FUZZ, 0);
573 input_set_abs_params(input, ABS_RY, -32767, 32767,
574 STEAM_PAD_FUZZ, 0);
575
576 input_abs_set_res(input, ABS_X, STEAM_JOYSTICK_RESOLUTION);
577 input_abs_set_res(input, ABS_Y, STEAM_JOYSTICK_RESOLUTION);
578 input_abs_set_res(input, ABS_RX, STEAM_PAD_RESOLUTION);
579 input_abs_set_res(input, ABS_RY, STEAM_PAD_RESOLUTION);
580 input_abs_set_res(input, ABS_HAT2Y, STEAM_TRIGGER_RESOLUTION);
581 input_abs_set_res(input, ABS_HAT2X, STEAM_TRIGGER_RESOLUTION);
582 }
583 input_abs_set_res(input, ABS_HAT0X, STEAM_PAD_RESOLUTION);
584 input_abs_set_res(input, ABS_HAT0Y, STEAM_PAD_RESOLUTION);
585
586 #ifdef CONFIG_STEAM_FF
587 if (steam->quirks & STEAM_QUIRK_DECK) {
588 input_set_capability(input, EV_FF, FF_RUMBLE);
589 ret = input_ff_create_memless(input, NULL, steam_play_effect);
590 if (ret)
591 goto input_register_fail;
592 }
593 #endif
594
595 ret = input_register_device(input);
596 if (ret)
597 goto input_register_fail;
598
599 rcu_assign_pointer(steam->input, input);
600 return 0;
601
602 input_register_fail:
603 input_free_device(input);
604 return ret;
605 }
606
steam_input_unregister(struct steam_device * steam)607 static void steam_input_unregister(struct steam_device *steam)
608 {
609 struct input_dev *input;
610 rcu_read_lock();
611 input = rcu_dereference(steam->input);
612 rcu_read_unlock();
613 if (!input)
614 return;
615 RCU_INIT_POINTER(steam->input, NULL);
616 synchronize_rcu();
617 input_unregister_device(input);
618 }
619
steam_battery_unregister(struct steam_device * steam)620 static void steam_battery_unregister(struct steam_device *steam)
621 {
622 struct power_supply *battery;
623
624 rcu_read_lock();
625 battery = rcu_dereference(steam->battery);
626 rcu_read_unlock();
627
628 if (!battery)
629 return;
630 RCU_INIT_POINTER(steam->battery, NULL);
631 synchronize_rcu();
632 power_supply_unregister(battery);
633 }
634
steam_register(struct steam_device * steam)635 static int steam_register(struct steam_device *steam)
636 {
637 int ret;
638 bool client_opened;
639
640 /*
641 * This function can be called several times in a row with the
642 * wireless adaptor, without steam_unregister() between them, because
643 * another client send a get_connection_status command, for example.
644 * The battery and serial number are set just once per device.
645 */
646 if (!steam->serial_no[0]) {
647 /*
648 * Unlikely, but getting the serial could fail, and it is not so
649 * important, so make up a serial number and go on.
650 */
651 mutex_lock(&steam->mutex);
652 if (steam_get_serial(steam) < 0)
653 strscpy(steam->serial_no, "XXXXXXXXXX",
654 sizeof(steam->serial_no));
655 mutex_unlock(&steam->mutex);
656
657 hid_info(steam->hdev, "Steam Controller '%s' connected",
658 steam->serial_no);
659
660 /* ignore battery errors, we can live without it */
661 if (steam->quirks & STEAM_QUIRK_WIRELESS)
662 steam_battery_register(steam);
663
664 mutex_lock(&steam_devices_lock);
665 if (list_empty(&steam->list))
666 list_add(&steam->list, &steam_devices);
667 mutex_unlock(&steam_devices_lock);
668 }
669
670 mutex_lock(&steam->mutex);
671 client_opened = steam->client_opened;
672 if (!client_opened)
673 steam_set_lizard_mode(steam, lizard_mode);
674 mutex_unlock(&steam->mutex);
675
676 if (!client_opened)
677 ret = steam_input_register(steam);
678 else
679 ret = 0;
680
681 return ret;
682 }
683
steam_unregister(struct steam_device * steam)684 static void steam_unregister(struct steam_device *steam)
685 {
686 steam_battery_unregister(steam);
687 steam_input_unregister(steam);
688 if (steam->serial_no[0]) {
689 hid_info(steam->hdev, "Steam Controller '%s' disconnected",
690 steam->serial_no);
691 mutex_lock(&steam_devices_lock);
692 list_del_init(&steam->list);
693 mutex_unlock(&steam_devices_lock);
694 steam->serial_no[0] = 0;
695 }
696 }
697
steam_work_connect_cb(struct work_struct * work)698 static void steam_work_connect_cb(struct work_struct *work)
699 {
700 struct steam_device *steam = container_of(work, struct steam_device,
701 work_connect);
702 unsigned long flags;
703 bool connected;
704 int ret;
705
706 spin_lock_irqsave(&steam->lock, flags);
707 connected = steam->connected;
708 spin_unlock_irqrestore(&steam->lock, flags);
709
710 if (connected) {
711 ret = steam_register(steam);
712 if (ret) {
713 hid_err(steam->hdev,
714 "%s:steam_register failed with error %d\n",
715 __func__, ret);
716 }
717 } else {
718 steam_unregister(steam);
719 }
720 }
721
steam_is_valve_interface(struct hid_device * hdev)722 static bool steam_is_valve_interface(struct hid_device *hdev)
723 {
724 struct hid_report_enum *rep_enum;
725
726 /*
727 * The wired device creates 3 interfaces:
728 * 0: emulated mouse.
729 * 1: emulated keyboard.
730 * 2: the real game pad.
731 * The wireless device creates 5 interfaces:
732 * 0: emulated keyboard.
733 * 1-4: slots where up to 4 real game pads will be connected to.
734 * We know which one is the real gamepad interface because they are the
735 * only ones with a feature report.
736 */
737 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
738 return !list_empty(&rep_enum->report_list);
739 }
740
steam_lizard_mode_heartbeat(struct work_struct * work)741 static void steam_lizard_mode_heartbeat(struct work_struct *work)
742 {
743 struct steam_device *steam = container_of(work, struct steam_device,
744 heartbeat.work);
745
746 mutex_lock(&steam->mutex);
747 if (!steam->client_opened && steam->client_hdev) {
748 steam_send_report_byte(steam, STEAM_CMD_CLEAR_MAPPINGS);
749 steam_write_registers(steam,
750 STEAM_REG_RPAD_MODE, 0x07, /* disable mouse */
751 0);
752 schedule_delayed_work(&steam->heartbeat, 5 * HZ);
753 }
754 mutex_unlock(&steam->mutex);
755 }
756
steam_client_ll_parse(struct hid_device * hdev)757 static int steam_client_ll_parse(struct hid_device *hdev)
758 {
759 struct steam_device *steam = hdev->driver_data;
760
761 return hid_parse_report(hdev, steam->hdev->dev_rdesc,
762 steam->hdev->dev_rsize);
763 }
764
steam_client_ll_start(struct hid_device * hdev)765 static int steam_client_ll_start(struct hid_device *hdev)
766 {
767 return 0;
768 }
769
steam_client_ll_stop(struct hid_device * hdev)770 static void steam_client_ll_stop(struct hid_device *hdev)
771 {
772 }
773
steam_client_ll_open(struct hid_device * hdev)774 static int steam_client_ll_open(struct hid_device *hdev)
775 {
776 struct steam_device *steam = hdev->driver_data;
777
778 mutex_lock(&steam->mutex);
779 steam->client_opened = true;
780 mutex_unlock(&steam->mutex);
781
782 steam_input_unregister(steam);
783
784 return 0;
785 }
786
steam_client_ll_close(struct hid_device * hdev)787 static void steam_client_ll_close(struct hid_device *hdev)
788 {
789 struct steam_device *steam = hdev->driver_data;
790
791 unsigned long flags;
792 bool connected;
793
794 spin_lock_irqsave(&steam->lock, flags);
795 connected = steam->connected;
796 spin_unlock_irqrestore(&steam->lock, flags);
797
798 mutex_lock(&steam->mutex);
799 steam->client_opened = false;
800 if (connected)
801 steam_set_lizard_mode(steam, lizard_mode);
802 mutex_unlock(&steam->mutex);
803
804 if (connected)
805 steam_input_register(steam);
806 }
807
steam_client_ll_raw_request(struct hid_device * hdev,unsigned char reportnum,u8 * buf,size_t count,unsigned char report_type,int reqtype)808 static int steam_client_ll_raw_request(struct hid_device *hdev,
809 unsigned char reportnum, u8 *buf,
810 size_t count, unsigned char report_type,
811 int reqtype)
812 {
813 struct steam_device *steam = hdev->driver_data;
814
815 return hid_hw_raw_request(steam->hdev, reportnum, buf, count,
816 report_type, reqtype);
817 }
818
819 static const struct hid_ll_driver steam_client_ll_driver = {
820 .parse = steam_client_ll_parse,
821 .start = steam_client_ll_start,
822 .stop = steam_client_ll_stop,
823 .open = steam_client_ll_open,
824 .close = steam_client_ll_close,
825 .raw_request = steam_client_ll_raw_request,
826 };
827
steam_create_client_hid(struct hid_device * hdev)828 static struct hid_device *steam_create_client_hid(struct hid_device *hdev)
829 {
830 struct hid_device *client_hdev;
831
832 client_hdev = hid_allocate_device();
833 if (IS_ERR(client_hdev))
834 return client_hdev;
835
836 client_hdev->ll_driver = &steam_client_ll_driver;
837 client_hdev->dev.parent = hdev->dev.parent;
838 client_hdev->bus = hdev->bus;
839 client_hdev->vendor = hdev->vendor;
840 client_hdev->product = hdev->product;
841 client_hdev->version = hdev->version;
842 client_hdev->type = hdev->type;
843 client_hdev->country = hdev->country;
844 strscpy(client_hdev->name, hdev->name,
845 sizeof(client_hdev->name));
846 strscpy(client_hdev->phys, hdev->phys,
847 sizeof(client_hdev->phys));
848 /*
849 * Since we use the same device info than the real interface to
850 * trick userspace, we will be calling steam_probe recursively.
851 * We need to recognize the client interface somehow.
852 */
853 client_hdev->group = HID_GROUP_STEAM;
854 return client_hdev;
855 }
856
steam_probe(struct hid_device * hdev,const struct hid_device_id * id)857 static int steam_probe(struct hid_device *hdev,
858 const struct hid_device_id *id)
859 {
860 struct steam_device *steam;
861 int ret;
862
863 ret = hid_parse(hdev);
864 if (ret) {
865 hid_err(hdev,
866 "%s:parse of hid interface failed\n", __func__);
867 return ret;
868 }
869
870 /*
871 * The virtual client_dev is only used for hidraw.
872 * Also avoid the recursive probe.
873 */
874 if (hdev->group == HID_GROUP_STEAM)
875 return hid_hw_start(hdev, HID_CONNECT_HIDRAW);
876 /*
877 * The non-valve interfaces (mouse and keyboard emulation) are
878 * connected without changes.
879 */
880 if (!steam_is_valve_interface(hdev))
881 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
882
883 steam = devm_kzalloc(&hdev->dev, sizeof(*steam), GFP_KERNEL);
884 if (!steam) {
885 ret = -ENOMEM;
886 goto steam_alloc_fail;
887 }
888 steam->hdev = hdev;
889 hid_set_drvdata(hdev, steam);
890 spin_lock_init(&steam->lock);
891 mutex_init(&steam->mutex);
892 steam->quirks = id->driver_data;
893 INIT_WORK(&steam->work_connect, steam_work_connect_cb);
894 INIT_LIST_HEAD(&steam->list);
895 INIT_DEFERRABLE_WORK(&steam->heartbeat, steam_lizard_mode_heartbeat);
896 INIT_WORK(&steam->rumble_work, steam_haptic_rumble_cb);
897
898 steam->client_hdev = steam_create_client_hid(hdev);
899 if (IS_ERR(steam->client_hdev)) {
900 ret = PTR_ERR(steam->client_hdev);
901 goto client_hdev_fail;
902 }
903 steam->client_hdev->driver_data = steam;
904
905 /*
906 * With the real steam controller interface, do not connect hidraw.
907 * Instead, create the client_hid and connect that.
908 */
909 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_HIDRAW);
910 if (ret)
911 goto hid_hw_start_fail;
912
913 ret = hid_add_device(steam->client_hdev);
914 if (ret)
915 goto client_hdev_add_fail;
916
917 ret = hid_hw_open(hdev);
918 if (ret) {
919 hid_err(hdev,
920 "%s:hid_hw_open\n",
921 __func__);
922 goto hid_hw_open_fail;
923 }
924
925 if (steam->quirks & STEAM_QUIRK_WIRELESS) {
926 hid_info(hdev, "Steam wireless receiver connected");
927 /* If using a wireless adaptor ask for connection status */
928 steam->connected = false;
929 steam_request_conn_status(steam);
930 } else {
931 /* A wired connection is always present */
932 steam->connected = true;
933 ret = steam_register(steam);
934 if (ret) {
935 hid_err(hdev,
936 "%s:steam_register failed with error %d\n",
937 __func__, ret);
938 goto input_register_fail;
939 }
940 }
941
942 return 0;
943
944 input_register_fail:
945 hid_hw_open_fail:
946 client_hdev_add_fail:
947 hid_hw_stop(hdev);
948 hid_hw_start_fail:
949 hid_destroy_device(steam->client_hdev);
950 client_hdev_fail:
951 cancel_work_sync(&steam->work_connect);
952 cancel_delayed_work_sync(&steam->heartbeat);
953 cancel_work_sync(&steam->rumble_work);
954 steam_alloc_fail:
955 hid_err(hdev, "%s: failed with error %d\n",
956 __func__, ret);
957 return ret;
958 }
959
steam_remove(struct hid_device * hdev)960 static void steam_remove(struct hid_device *hdev)
961 {
962 struct steam_device *steam = hid_get_drvdata(hdev);
963
964 if (!steam || hdev->group == HID_GROUP_STEAM) {
965 hid_hw_stop(hdev);
966 return;
967 }
968
969 hid_destroy_device(steam->client_hdev);
970 mutex_lock(&steam->mutex);
971 steam->client_hdev = NULL;
972 steam->client_opened = false;
973 cancel_delayed_work_sync(&steam->heartbeat);
974 mutex_unlock(&steam->mutex);
975 cancel_work_sync(&steam->work_connect);
976 if (steam->quirks & STEAM_QUIRK_WIRELESS) {
977 hid_info(hdev, "Steam wireless receiver disconnected");
978 }
979 hid_hw_close(hdev);
980 hid_hw_stop(hdev);
981 steam_unregister(steam);
982 }
983
steam_do_connect_event(struct steam_device * steam,bool connected)984 static void steam_do_connect_event(struct steam_device *steam, bool connected)
985 {
986 unsigned long flags;
987 bool changed;
988
989 spin_lock_irqsave(&steam->lock, flags);
990 changed = steam->connected != connected;
991 steam->connected = connected;
992 spin_unlock_irqrestore(&steam->lock, flags);
993
994 if (changed && schedule_work(&steam->work_connect) == 0)
995 dbg_hid("%s: connected=%d event already queued\n",
996 __func__, connected);
997 }
998
999 /*
1000 * Some input data in the protocol has the opposite sign.
1001 * Clamp the values to 32767..-32767 so that the range is
1002 * symmetrical and can be negated safely.
1003 */
steam_le16(u8 * data)1004 static inline s16 steam_le16(u8 *data)
1005 {
1006 s16 x = (s16) le16_to_cpup((__le16 *)data);
1007
1008 return x == -32768 ? -32767 : x;
1009 }
1010
1011 /*
1012 * The size for this message payload is 60.
1013 * The known values are:
1014 * (* values are not sent through wireless)
1015 * (* accelerator/gyro is disabled by default)
1016 * Offset| Type | Mapped to |Meaning
1017 * -------+-------+-----------+--------------------------
1018 * 4-7 | u32 | -- | sequence number
1019 * 8-10 | 24bit | see below | buttons
1020 * 11 | u8 | ABS_HAT2Y | left trigger
1021 * 12 | u8 | ABS_HAT2X | right trigger
1022 * 13-15 | -- | -- | always 0
1023 * 16-17 | s16 | ABS_X/ABS_HAT0X | X value
1024 * 18-19 | s16 | ABS_Y/ABS_HAT0Y | Y value
1025 * 20-21 | s16 | ABS_RX | right-pad X value
1026 * 22-23 | s16 | ABS_RY | right-pad Y value
1027 * 24-25 | s16 | -- | * left trigger
1028 * 26-27 | s16 | -- | * right trigger
1029 * 28-29 | s16 | -- | * accelerometer X value
1030 * 30-31 | s16 | -- | * accelerometer Y value
1031 * 32-33 | s16 | -- | * accelerometer Z value
1032 * 34-35 | s16 | -- | gyro X value
1033 * 36-36 | s16 | -- | gyro Y value
1034 * 38-39 | s16 | -- | gyro Z value
1035 * 40-41 | s16 | -- | quaternion W value
1036 * 42-43 | s16 | -- | quaternion X value
1037 * 44-45 | s16 | -- | quaternion Y value
1038 * 46-47 | s16 | -- | quaternion Z value
1039 * 48-49 | -- | -- | always 0
1040 * 50-51 | s16 | -- | * left trigger (uncalibrated)
1041 * 52-53 | s16 | -- | * right trigger (uncalibrated)
1042 * 54-55 | s16 | -- | * joystick X value (uncalibrated)
1043 * 56-57 | s16 | -- | * joystick Y value (uncalibrated)
1044 * 58-59 | s16 | -- | * left-pad X value
1045 * 60-61 | s16 | -- | * left-pad Y value
1046 * 62-63 | u16 | -- | * battery voltage
1047 *
1048 * The buttons are:
1049 * Bit | Mapped to | Description
1050 * ------+------------+--------------------------------
1051 * 8.0 | BTN_TR2 | right trigger fully pressed
1052 * 8.1 | BTN_TL2 | left trigger fully pressed
1053 * 8.2 | BTN_TR | right shoulder
1054 * 8.3 | BTN_TL | left shoulder
1055 * 8.4 | BTN_Y | button Y
1056 * 8.5 | BTN_B | button B
1057 * 8.6 | BTN_X | button X
1058 * 8.7 | BTN_A | button A
1059 * 9.0 | BTN_DPAD_UP | left-pad up
1060 * 9.1 | BTN_DPAD_RIGHT | left-pad right
1061 * 9.2 | BTN_DPAD_LEFT | left-pad left
1062 * 9.3 | BTN_DPAD_DOWN | left-pad down
1063 * 9.4 | BTN_SELECT | menu left
1064 * 9.5 | BTN_MODE | steam logo
1065 * 9.6 | BTN_START | menu right
1066 * 9.7 | BTN_GEAR_DOWN | left back lever
1067 * 10.0 | BTN_GEAR_UP | right back lever
1068 * 10.1 | -- | left-pad clicked
1069 * 10.2 | BTN_THUMBR | right-pad clicked
1070 * 10.3 | BTN_THUMB | left-pad touched (but see explanation below)
1071 * 10.4 | BTN_THUMB2 | right-pad touched
1072 * 10.5 | -- | unknown
1073 * 10.6 | BTN_THUMBL | joystick clicked
1074 * 10.7 | -- | lpad_and_joy
1075 */
1076
steam_do_input_event(struct steam_device * steam,struct input_dev * input,u8 * data)1077 static void steam_do_input_event(struct steam_device *steam,
1078 struct input_dev *input, u8 *data)
1079 {
1080 /* 24 bits of buttons */
1081 u8 b8, b9, b10;
1082 s16 x, y;
1083 bool lpad_touched, lpad_and_joy;
1084
1085 b8 = data[8];
1086 b9 = data[9];
1087 b10 = data[10];
1088
1089 input_report_abs(input, ABS_HAT2Y, data[11]);
1090 input_report_abs(input, ABS_HAT2X, data[12]);
1091
1092 /*
1093 * These two bits tells how to interpret the values X and Y.
1094 * lpad_and_joy tells that the joystick and the lpad are used at the
1095 * same time.
1096 * lpad_touched tells whether X/Y are to be read as lpad coord or
1097 * joystick values.
1098 * (lpad_touched || lpad_and_joy) tells if the lpad is really touched.
1099 */
1100 lpad_touched = b10 & BIT(3);
1101 lpad_and_joy = b10 & BIT(7);
1102 x = steam_le16(data + 16);
1103 y = -steam_le16(data + 18);
1104
1105 input_report_abs(input, lpad_touched ? ABS_HAT0X : ABS_X, x);
1106 input_report_abs(input, lpad_touched ? ABS_HAT0Y : ABS_Y, y);
1107 /* Check if joystick is centered */
1108 if (lpad_touched && !lpad_and_joy) {
1109 input_report_abs(input, ABS_X, 0);
1110 input_report_abs(input, ABS_Y, 0);
1111 }
1112 /* Check if lpad is untouched */
1113 if (!(lpad_touched || lpad_and_joy)) {
1114 input_report_abs(input, ABS_HAT0X, 0);
1115 input_report_abs(input, ABS_HAT0Y, 0);
1116 }
1117
1118 input_report_abs(input, ABS_RX, steam_le16(data + 20));
1119 input_report_abs(input, ABS_RY, -steam_le16(data + 22));
1120
1121 input_event(input, EV_KEY, BTN_TR2, !!(b8 & BIT(0)));
1122 input_event(input, EV_KEY, BTN_TL2, !!(b8 & BIT(1)));
1123 input_event(input, EV_KEY, BTN_TR, !!(b8 & BIT(2)));
1124 input_event(input, EV_KEY, BTN_TL, !!(b8 & BIT(3)));
1125 input_event(input, EV_KEY, BTN_Y, !!(b8 & BIT(4)));
1126 input_event(input, EV_KEY, BTN_B, !!(b8 & BIT(5)));
1127 input_event(input, EV_KEY, BTN_X, !!(b8 & BIT(6)));
1128 input_event(input, EV_KEY, BTN_A, !!(b8 & BIT(7)));
1129 input_event(input, EV_KEY, BTN_SELECT, !!(b9 & BIT(4)));
1130 input_event(input, EV_KEY, BTN_MODE, !!(b9 & BIT(5)));
1131 input_event(input, EV_KEY, BTN_START, !!(b9 & BIT(6)));
1132 input_event(input, EV_KEY, BTN_GEAR_DOWN, !!(b9 & BIT(7)));
1133 input_event(input, EV_KEY, BTN_GEAR_UP, !!(b10 & BIT(0)));
1134 input_event(input, EV_KEY, BTN_THUMBR, !!(b10 & BIT(2)));
1135 input_event(input, EV_KEY, BTN_THUMBL, !!(b10 & BIT(6)));
1136 input_event(input, EV_KEY, BTN_THUMB, lpad_touched || lpad_and_joy);
1137 input_event(input, EV_KEY, BTN_THUMB2, !!(b10 & BIT(4)));
1138 input_event(input, EV_KEY, BTN_DPAD_UP, !!(b9 & BIT(0)));
1139 input_event(input, EV_KEY, BTN_DPAD_RIGHT, !!(b9 & BIT(1)));
1140 input_event(input, EV_KEY, BTN_DPAD_LEFT, !!(b9 & BIT(2)));
1141 input_event(input, EV_KEY, BTN_DPAD_DOWN, !!(b9 & BIT(3)));
1142
1143 input_sync(input);
1144 }
1145
1146 /*
1147 * The size for this message payload is 56.
1148 * The known values are:
1149 * Offset| Type | Mapped to |Meaning
1150 * -------+-------+-----------+--------------------------
1151 * 4-7 | u32 | -- | sequence number
1152 * 8-15 | u64 | see below | buttons
1153 * 16-17 | s16 | ABS_HAT0X | left-pad X value
1154 * 18-19 | s16 | ABS_HAT0Y | left-pad Y value
1155 * 20-21 | s16 | ABS_HAT1X | right-pad X value
1156 * 22-23 | s16 | ABS_HAT1Y | right-pad Y value
1157 * 24-25 | s16 | -- | accelerometer X value
1158 * 26-27 | s16 | -- | accelerometer Y value
1159 * 28-29 | s16 | -- | accelerometer Z value
1160 * 30-31 | s16 | -- | gyro X value
1161 * 32-33 | s16 | -- | gyro Y value
1162 * 34-35 | s16 | -- | gyro Z value
1163 * 36-37 | s16 | -- | quaternion W value
1164 * 38-39 | s16 | -- | quaternion X value
1165 * 40-41 | s16 | -- | quaternion Y value
1166 * 42-43 | s16 | -- | quaternion Z value
1167 * 44-45 | u16 | ABS_HAT2Y | left trigger (uncalibrated)
1168 * 46-47 | u16 | ABS_HAT2X | right trigger (uncalibrated)
1169 * 48-49 | s16 | ABS_X | left joystick X
1170 * 50-51 | s16 | ABS_Y | left joystick Y
1171 * 52-53 | s16 | ABS_RX | right joystick X
1172 * 54-55 | s16 | ABS_RY | right joystick Y
1173 * 56-57 | u16 | -- | left pad pressure
1174 * 58-59 | u16 | -- | right pad pressure
1175 *
1176 * The buttons are:
1177 * Bit | Mapped to | Description
1178 * ------+------------+--------------------------------
1179 * 8.0 | BTN_TR2 | right trigger fully pressed
1180 * 8.1 | BTN_TL2 | left trigger fully pressed
1181 * 8.2 | BTN_TR | right shoulder
1182 * 8.3 | BTN_TL | left shoulder
1183 * 8.4 | BTN_Y | button Y
1184 * 8.5 | BTN_B | button B
1185 * 8.6 | BTN_X | button X
1186 * 8.7 | BTN_A | button A
1187 * 9.0 | BTN_DPAD_UP | left-pad up
1188 * 9.1 | BTN_DPAD_RIGHT | left-pad right
1189 * 9.2 | BTN_DPAD_LEFT | left-pad left
1190 * 9.3 | BTN_DPAD_DOWN | left-pad down
1191 * 9.4 | BTN_SELECT | menu left
1192 * 9.5 | BTN_MODE | steam logo
1193 * 9.6 | BTN_START | menu right
1194 * 9.7 | BTN_TRIGGER_HAPPY3 | left bottom grip button
1195 * 10.0 | BTN_TRIGGER_HAPPY4 | right bottom grip button
1196 * 10.1 | BTN_THUMB | left pad pressed
1197 * 10.2 | BTN_THUMB2 | right pad pressed
1198 * 10.3 | -- | left pad touched
1199 * 10.4 | -- | right pad touched
1200 * 10.5 | -- | unknown
1201 * 10.6 | BTN_THUMBL | left joystick clicked
1202 * 10.7 | -- | unknown
1203 * 11.0 | -- | unknown
1204 * 11.1 | -- | unknown
1205 * 11.2 | BTN_THUMBR | right joystick clicked
1206 * 11.3 | -- | unknown
1207 * 11.4 | -- | unknown
1208 * 11.5 | -- | unknown
1209 * 11.6 | -- | unknown
1210 * 11.7 | -- | unknown
1211 * 12.0 | -- | unknown
1212 * 12.1 | -- | unknown
1213 * 12.2 | -- | unknown
1214 * 12.3 | -- | unknown
1215 * 12.4 | -- | unknown
1216 * 12.5 | -- | unknown
1217 * 12.6 | -- | unknown
1218 * 12.7 | -- | unknown
1219 * 13.0 | -- | unknown
1220 * 13.1 | BTN_TRIGGER_HAPPY1 | left top grip button
1221 * 13.2 | BTN_TRIGGER_HAPPY2 | right top grip button
1222 * 13.3 | -- | unknown
1223 * 13.4 | -- | unknown
1224 * 13.5 | -- | unknown
1225 * 13.6 | -- | left joystick touched
1226 * 13.7 | -- | right joystick touched
1227 * 14.0 | -- | unknown
1228 * 14.1 | -- | unknown
1229 * 14.2 | BTN_BASE | quick access button
1230 * 14.3 | -- | unknown
1231 * 14.4 | -- | unknown
1232 * 14.5 | -- | unknown
1233 * 14.6 | -- | unknown
1234 * 14.7 | -- | unknown
1235 * 15.0 | -- | unknown
1236 * 15.1 | -- | unknown
1237 * 15.2 | -- | unknown
1238 * 15.3 | -- | unknown
1239 * 15.4 | -- | unknown
1240 * 15.5 | -- | unknown
1241 * 15.6 | -- | unknown
1242 * 15.7 | -- | unknown
1243 */
steam_do_deck_input_event(struct steam_device * steam,struct input_dev * input,u8 * data)1244 static void steam_do_deck_input_event(struct steam_device *steam,
1245 struct input_dev *input, u8 *data)
1246 {
1247 u8 b8, b9, b10, b11, b13, b14;
1248 bool lpad_touched, rpad_touched;
1249
1250 b8 = data[8];
1251 b9 = data[9];
1252 b10 = data[10];
1253 b11 = data[11];
1254 b13 = data[13];
1255 b14 = data[14];
1256
1257 lpad_touched = b10 & BIT(3);
1258 rpad_touched = b10 & BIT(4);
1259
1260 if (lpad_touched) {
1261 input_report_abs(input, ABS_HAT0X, steam_le16(data + 16));
1262 input_report_abs(input, ABS_HAT0Y, steam_le16(data + 18));
1263 } else {
1264 input_report_abs(input, ABS_HAT0X, 0);
1265 input_report_abs(input, ABS_HAT0Y, 0);
1266 }
1267
1268 if (rpad_touched) {
1269 input_report_abs(input, ABS_HAT1X, steam_le16(data + 20));
1270 input_report_abs(input, ABS_HAT1Y, steam_le16(data + 22));
1271 } else {
1272 input_report_abs(input, ABS_HAT1X, 0);
1273 input_report_abs(input, ABS_HAT1Y, 0);
1274 }
1275
1276 input_report_abs(input, ABS_X, steam_le16(data + 48));
1277 input_report_abs(input, ABS_Y, -steam_le16(data + 50));
1278 input_report_abs(input, ABS_RX, steam_le16(data + 52));
1279 input_report_abs(input, ABS_RY, -steam_le16(data + 54));
1280
1281 input_report_abs(input, ABS_HAT2Y, steam_le16(data + 44));
1282 input_report_abs(input, ABS_HAT2X, steam_le16(data + 46));
1283
1284 input_event(input, EV_KEY, BTN_TR2, !!(b8 & BIT(0)));
1285 input_event(input, EV_KEY, BTN_TL2, !!(b8 & BIT(1)));
1286 input_event(input, EV_KEY, BTN_TR, !!(b8 & BIT(2)));
1287 input_event(input, EV_KEY, BTN_TL, !!(b8 & BIT(3)));
1288 input_event(input, EV_KEY, BTN_Y, !!(b8 & BIT(4)));
1289 input_event(input, EV_KEY, BTN_B, !!(b8 & BIT(5)));
1290 input_event(input, EV_KEY, BTN_X, !!(b8 & BIT(6)));
1291 input_event(input, EV_KEY, BTN_A, !!(b8 & BIT(7)));
1292 input_event(input, EV_KEY, BTN_SELECT, !!(b9 & BIT(4)));
1293 input_event(input, EV_KEY, BTN_MODE, !!(b9 & BIT(5)));
1294 input_event(input, EV_KEY, BTN_START, !!(b9 & BIT(6)));
1295 input_event(input, EV_KEY, BTN_TRIGGER_HAPPY3, !!(b9 & BIT(7)));
1296 input_event(input, EV_KEY, BTN_TRIGGER_HAPPY4, !!(b10 & BIT(0)));
1297 input_event(input, EV_KEY, BTN_THUMBL, !!(b10 & BIT(6)));
1298 input_event(input, EV_KEY, BTN_THUMBR, !!(b11 & BIT(2)));
1299 input_event(input, EV_KEY, BTN_DPAD_UP, !!(b9 & BIT(0)));
1300 input_event(input, EV_KEY, BTN_DPAD_RIGHT, !!(b9 & BIT(1)));
1301 input_event(input, EV_KEY, BTN_DPAD_LEFT, !!(b9 & BIT(2)));
1302 input_event(input, EV_KEY, BTN_DPAD_DOWN, !!(b9 & BIT(3)));
1303 input_event(input, EV_KEY, BTN_THUMB, !!(b10 & BIT(1)));
1304 input_event(input, EV_KEY, BTN_THUMB2, !!(b10 & BIT(2)));
1305 input_event(input, EV_KEY, BTN_TRIGGER_HAPPY1, !!(b13 & BIT(1)));
1306 input_event(input, EV_KEY, BTN_TRIGGER_HAPPY2, !!(b13 & BIT(2)));
1307 input_event(input, EV_KEY, BTN_BASE, !!(b14 & BIT(2)));
1308
1309 input_sync(input);
1310 }
1311
1312 /*
1313 * The size for this message payload is 11.
1314 * The known values are:
1315 * Offset| Type | Meaning
1316 * -------+-------+---------------------------
1317 * 4-7 | u32 | sequence number
1318 * 8-11 | -- | always 0
1319 * 12-13 | u16 | voltage (mV)
1320 * 14 | u8 | battery percent
1321 */
steam_do_battery_event(struct steam_device * steam,struct power_supply * battery,u8 * data)1322 static void steam_do_battery_event(struct steam_device *steam,
1323 struct power_supply *battery, u8 *data)
1324 {
1325 unsigned long flags;
1326
1327 s16 volts = steam_le16(data + 12);
1328 u8 batt = data[14];
1329
1330 /* Creating the battery may have failed */
1331 rcu_read_lock();
1332 battery = rcu_dereference(steam->battery);
1333 if (likely(battery)) {
1334 spin_lock_irqsave(&steam->lock, flags);
1335 steam->voltage = volts;
1336 steam->battery_charge = batt;
1337 spin_unlock_irqrestore(&steam->lock, flags);
1338 power_supply_changed(battery);
1339 }
1340 rcu_read_unlock();
1341 }
1342
steam_raw_event(struct hid_device * hdev,struct hid_report * report,u8 * data,int size)1343 static int steam_raw_event(struct hid_device *hdev,
1344 struct hid_report *report, u8 *data,
1345 int size)
1346 {
1347 struct steam_device *steam = hid_get_drvdata(hdev);
1348 struct input_dev *input;
1349 struct power_supply *battery;
1350
1351 if (!steam)
1352 return 0;
1353
1354 if (steam->client_opened)
1355 hid_input_report(steam->client_hdev, HID_FEATURE_REPORT,
1356 data, size, 0);
1357 /*
1358 * All messages are size=64, all values little-endian.
1359 * The format is:
1360 * Offset| Meaning
1361 * -------+--------------------------------------------
1362 * 0-1 | always 0x01, 0x00, maybe protocol version?
1363 * 2 | type of message
1364 * 3 | length of the real payload (not checked)
1365 * 4-n | payload data, depends on the type
1366 *
1367 * There are these known types of message:
1368 * 0x01: input data (60 bytes)
1369 * 0x03: wireless connect/disconnect (1 byte)
1370 * 0x04: battery status (11 bytes)
1371 * 0x09: Steam Deck input data (56 bytes)
1372 */
1373
1374 if (size != 64 || data[0] != 1 || data[1] != 0)
1375 return 0;
1376
1377 switch (data[2]) {
1378 case STEAM_EV_INPUT_DATA:
1379 if (steam->client_opened)
1380 return 0;
1381 rcu_read_lock();
1382 input = rcu_dereference(steam->input);
1383 if (likely(input))
1384 steam_do_input_event(steam, input, data);
1385 rcu_read_unlock();
1386 break;
1387 case STEAM_EV_DECK_INPUT_DATA:
1388 if (steam->client_opened)
1389 return 0;
1390 rcu_read_lock();
1391 input = rcu_dereference(steam->input);
1392 if (likely(input))
1393 steam_do_deck_input_event(steam, input, data);
1394 rcu_read_unlock();
1395 break;
1396 case STEAM_EV_CONNECT:
1397 /*
1398 * The payload of this event is a single byte:
1399 * 0x01: disconnected.
1400 * 0x02: connected.
1401 */
1402 switch (data[4]) {
1403 case 0x01:
1404 steam_do_connect_event(steam, false);
1405 break;
1406 case 0x02:
1407 steam_do_connect_event(steam, true);
1408 break;
1409 }
1410 break;
1411 case STEAM_EV_BATTERY:
1412 if (steam->quirks & STEAM_QUIRK_WIRELESS) {
1413 rcu_read_lock();
1414 battery = rcu_dereference(steam->battery);
1415 if (likely(battery)) {
1416 steam_do_battery_event(steam, battery, data);
1417 } else {
1418 dbg_hid(
1419 "%s: battery data without connect event\n",
1420 __func__);
1421 steam_do_connect_event(steam, true);
1422 }
1423 rcu_read_unlock();
1424 }
1425 break;
1426 }
1427 return 0;
1428 }
1429
steam_param_set_lizard_mode(const char * val,const struct kernel_param * kp)1430 static int steam_param_set_lizard_mode(const char *val,
1431 const struct kernel_param *kp)
1432 {
1433 struct steam_device *steam;
1434 int ret;
1435
1436 ret = param_set_bool(val, kp);
1437 if (ret)
1438 return ret;
1439
1440 mutex_lock(&steam_devices_lock);
1441 list_for_each_entry(steam, &steam_devices, list) {
1442 mutex_lock(&steam->mutex);
1443 if (!steam->client_opened)
1444 steam_set_lizard_mode(steam, lizard_mode);
1445 mutex_unlock(&steam->mutex);
1446 }
1447 mutex_unlock(&steam_devices_lock);
1448 return 0;
1449 }
1450
1451 static const struct kernel_param_ops steam_lizard_mode_ops = {
1452 .set = steam_param_set_lizard_mode,
1453 .get = param_get_bool,
1454 };
1455
1456 module_param_cb(lizard_mode, &steam_lizard_mode_ops, &lizard_mode, 0644);
1457 MODULE_PARM_DESC(lizard_mode,
1458 "Enable mouse and keyboard emulation (lizard mode) when the gamepad is not in use");
1459
1460 static const struct hid_device_id steam_controllers[] = {
1461 { /* Wired Steam Controller */
1462 HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
1463 USB_DEVICE_ID_STEAM_CONTROLLER)
1464 },
1465 { /* Wireless Steam Controller */
1466 HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
1467 USB_DEVICE_ID_STEAM_CONTROLLER_WIRELESS),
1468 .driver_data = STEAM_QUIRK_WIRELESS
1469 },
1470 { /* Steam Deck */
1471 HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
1472 USB_DEVICE_ID_STEAM_DECK),
1473 .driver_data = STEAM_QUIRK_DECK
1474 },
1475 {}
1476 };
1477
1478 MODULE_DEVICE_TABLE(hid, steam_controllers);
1479
1480 static struct hid_driver steam_controller_driver = {
1481 .name = "hid-steam",
1482 .id_table = steam_controllers,
1483 .probe = steam_probe,
1484 .remove = steam_remove,
1485 .raw_event = steam_raw_event,
1486 };
1487
1488 module_hid_driver(steam_controller_driver);
1489