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 /* Accelerometer has 16 bit resolution and a range of +/- 2g */
70 #define STEAM_DECK_ACCEL_RES_PER_G 16384
71 #define STEAM_DECK_ACCEL_RANGE 32768
72 #define STEAM_DECK_ACCEL_FUZZ 32
73 /* Gyroscope has 16 bit resolution and a range of +/- 2000 dps */
74 #define STEAM_DECK_GYRO_RES_PER_DPS 16
75 #define STEAM_DECK_GYRO_RANGE 32768
76 #define STEAM_DECK_GYRO_FUZZ 1
77
78 #define STEAM_PAD_FUZZ 256
79
80 /*
81 * Commands that can be sent in a feature report.
82 * Thanks to Valve and SDL for the names.
83 */
84 enum {
85 ID_SET_DIGITAL_MAPPINGS = 0x80,
86 ID_CLEAR_DIGITAL_MAPPINGS = 0x81,
87 ID_GET_DIGITAL_MAPPINGS = 0x82,
88 ID_GET_ATTRIBUTES_VALUES = 0x83,
89 ID_GET_ATTRIBUTE_LABEL = 0x84,
90 ID_SET_DEFAULT_DIGITAL_MAPPINGS = 0x85,
91 ID_FACTORY_RESET = 0x86,
92 ID_SET_SETTINGS_VALUES = 0x87,
93 ID_CLEAR_SETTINGS_VALUES = 0x88,
94 ID_GET_SETTINGS_VALUES = 0x89,
95 ID_GET_SETTING_LABEL = 0x8A,
96 ID_GET_SETTINGS_MAXS = 0x8B,
97 ID_GET_SETTINGS_DEFAULTS = 0x8C,
98 ID_SET_CONTROLLER_MODE = 0x8D,
99 ID_LOAD_DEFAULT_SETTINGS = 0x8E,
100 ID_TRIGGER_HAPTIC_PULSE = 0x8F,
101 ID_TURN_OFF_CONTROLLER = 0x9F,
102
103 ID_GET_DEVICE_INFO = 0xA1,
104
105 ID_CALIBRATE_TRACKPADS = 0xA7,
106 ID_RESERVED_0 = 0xA8,
107 ID_SET_SERIAL_NUMBER = 0xA9,
108 ID_GET_TRACKPAD_CALIBRATION = 0xAA,
109 ID_GET_TRACKPAD_FACTORY_CALIBRATION = 0xAB,
110 ID_GET_TRACKPAD_RAW_DATA = 0xAC,
111 ID_ENABLE_PAIRING = 0xAD,
112 ID_GET_STRING_ATTRIBUTE = 0xAE,
113 ID_RADIO_ERASE_RECORDS = 0xAF,
114 ID_RADIO_WRITE_RECORD = 0xB0,
115 ID_SET_DONGLE_SETTING = 0xB1,
116 ID_DONGLE_DISCONNECT_DEVICE = 0xB2,
117 ID_DONGLE_COMMIT_DEVICE = 0xB3,
118 ID_DONGLE_GET_WIRELESS_STATE = 0xB4,
119 ID_CALIBRATE_GYRO = 0xB5,
120 ID_PLAY_AUDIO = 0xB6,
121 ID_AUDIO_UPDATE_START = 0xB7,
122 ID_AUDIO_UPDATE_DATA = 0xB8,
123 ID_AUDIO_UPDATE_COMPLETE = 0xB9,
124 ID_GET_CHIPID = 0xBA,
125
126 ID_CALIBRATE_JOYSTICK = 0xBF,
127 ID_CALIBRATE_ANALOG_TRIGGERS = 0xC0,
128 ID_SET_AUDIO_MAPPING = 0xC1,
129 ID_CHECK_GYRO_FW_LOAD = 0xC2,
130 ID_CALIBRATE_ANALOG = 0xC3,
131 ID_DONGLE_GET_CONNECTED_SLOTS = 0xC4,
132
133 ID_RESET_IMU = 0xCE,
134
135 ID_TRIGGER_HAPTIC_CMD = 0xEA,
136 ID_TRIGGER_RUMBLE_CMD = 0xEB,
137 };
138
139 /* Settings IDs */
140 enum {
141 /* 0 */
142 SETTING_MOUSE_SENSITIVITY,
143 SETTING_MOUSE_ACCELERATION,
144 SETTING_TRACKBALL_ROTATION_ANGLE,
145 SETTING_HAPTIC_INTENSITY_UNUSED,
146 SETTING_LEFT_GAMEPAD_STICK_ENABLED,
147 SETTING_RIGHT_GAMEPAD_STICK_ENABLED,
148 SETTING_USB_DEBUG_MODE,
149 SETTING_LEFT_TRACKPAD_MODE,
150 SETTING_RIGHT_TRACKPAD_MODE,
151 SETTING_MOUSE_POINTER_ENABLED,
152
153 /* 10 */
154 SETTING_DPAD_DEADZONE,
155 SETTING_MINIMUM_MOMENTUM_VEL,
156 SETTING_MOMENTUM_DECAY_AMMOUNT,
157 SETTING_TRACKPAD_RELATIVE_MODE_TICKS_PER_PIXEL,
158 SETTING_HAPTIC_INCREMENT,
159 SETTING_DPAD_ANGLE_SIN,
160 SETTING_DPAD_ANGLE_COS,
161 SETTING_MOMENTUM_VERTICAL_DIVISOR,
162 SETTING_MOMENTUM_MAXIMUM_VELOCITY,
163 SETTING_TRACKPAD_Z_ON,
164
165 /* 20 */
166 SETTING_TRACKPAD_Z_OFF,
167 SETTING_SENSITIVY_SCALE_AMMOUNT,
168 SETTING_LEFT_TRACKPAD_SECONDARY_MODE,
169 SETTING_RIGHT_TRACKPAD_SECONDARY_MODE,
170 SETTING_SMOOTH_ABSOLUTE_MOUSE,
171 SETTING_STEAMBUTTON_POWEROFF_TIME,
172 SETTING_UNUSED_1,
173 SETTING_TRACKPAD_OUTER_RADIUS,
174 SETTING_TRACKPAD_Z_ON_LEFT,
175 SETTING_TRACKPAD_Z_OFF_LEFT,
176
177 /* 30 */
178 SETTING_TRACKPAD_OUTER_SPIN_VEL,
179 SETTING_TRACKPAD_OUTER_SPIN_RADIUS,
180 SETTING_TRACKPAD_OUTER_SPIN_HORIZONTAL_ONLY,
181 SETTING_TRACKPAD_RELATIVE_MODE_DEADZONE,
182 SETTING_TRACKPAD_RELATIVE_MODE_MAX_VEL,
183 SETTING_TRACKPAD_RELATIVE_MODE_INVERT_Y,
184 SETTING_TRACKPAD_DOUBLE_TAP_BEEP_ENABLED,
185 SETTING_TRACKPAD_DOUBLE_TAP_BEEP_PERIOD,
186 SETTING_TRACKPAD_DOUBLE_TAP_BEEP_COUNT,
187 SETTING_TRACKPAD_OUTER_RADIUS_RELEASE_ON_TRANSITION,
188
189 /* 40 */
190 SETTING_RADIAL_MODE_ANGLE,
191 SETTING_HAPTIC_INTENSITY_MOUSE_MODE,
192 SETTING_LEFT_DPAD_REQUIRES_CLICK,
193 SETTING_RIGHT_DPAD_REQUIRES_CLICK,
194 SETTING_LED_BASELINE_BRIGHTNESS,
195 SETTING_LED_USER_BRIGHTNESS,
196 SETTING_ENABLE_RAW_JOYSTICK,
197 SETTING_ENABLE_FAST_SCAN,
198 SETTING_IMU_MODE,
199 SETTING_WIRELESS_PACKET_VERSION,
200
201 /* 50 */
202 SETTING_SLEEP_INACTIVITY_TIMEOUT,
203 SETTING_TRACKPAD_NOISE_THRESHOLD,
204 SETTING_LEFT_TRACKPAD_CLICK_PRESSURE,
205 SETTING_RIGHT_TRACKPAD_CLICK_PRESSURE,
206 SETTING_LEFT_BUMPER_CLICK_PRESSURE,
207 SETTING_RIGHT_BUMPER_CLICK_PRESSURE,
208 SETTING_LEFT_GRIP_CLICK_PRESSURE,
209 SETTING_RIGHT_GRIP_CLICK_PRESSURE,
210 SETTING_LEFT_GRIP2_CLICK_PRESSURE,
211 SETTING_RIGHT_GRIP2_CLICK_PRESSURE,
212
213 /* 60 */
214 SETTING_PRESSURE_MODE,
215 SETTING_CONTROLLER_TEST_MODE,
216 SETTING_TRIGGER_MODE,
217 SETTING_TRACKPAD_Z_THRESHOLD,
218 SETTING_FRAME_RATE,
219 SETTING_TRACKPAD_FILT_CTRL,
220 SETTING_TRACKPAD_CLIP,
221 SETTING_DEBUG_OUTPUT_SELECT,
222 SETTING_TRIGGER_THRESHOLD_PERCENT,
223 SETTING_TRACKPAD_FREQUENCY_HOPPING,
224
225 /* 70 */
226 SETTING_HAPTICS_ENABLED,
227 SETTING_STEAM_WATCHDOG_ENABLE,
228 SETTING_TIMP_TOUCH_THRESHOLD_ON,
229 SETTING_TIMP_TOUCH_THRESHOLD_OFF,
230 SETTING_FREQ_HOPPING,
231 SETTING_TEST_CONTROL,
232 SETTING_HAPTIC_MASTER_GAIN_DB,
233 SETTING_THUMB_TOUCH_THRESH,
234 SETTING_DEVICE_POWER_STATUS,
235 SETTING_HAPTIC_INTENSITY,
236
237 /* 80 */
238 SETTING_STABILIZER_ENABLED,
239 SETTING_TIMP_MODE_MTE,
240 };
241
242 /* Input report identifiers */
243 enum
244 {
245 ID_CONTROLLER_STATE = 1,
246 ID_CONTROLLER_DEBUG = 2,
247 ID_CONTROLLER_WIRELESS = 3,
248 ID_CONTROLLER_STATUS = 4,
249 ID_CONTROLLER_DEBUG2 = 5,
250 ID_CONTROLLER_SECONDARY_STATE = 6,
251 ID_CONTROLLER_BLE_STATE = 7,
252 ID_CONTROLLER_DECK_STATE = 9
253 };
254
255 /* String attribute idenitifiers */
256 enum {
257 ATTRIB_STR_BOARD_SERIAL,
258 ATTRIB_STR_UNIT_SERIAL,
259 };
260
261 /* Values for GYRO_MODE (bitmask) */
262 enum {
263 SETTING_GYRO_MODE_OFF = 0,
264 SETTING_GYRO_MODE_STEERING = BIT(0),
265 SETTING_GYRO_MODE_TILT = BIT(1),
266 SETTING_GYRO_MODE_SEND_ORIENTATION = BIT(2),
267 SETTING_GYRO_MODE_SEND_RAW_ACCEL = BIT(3),
268 SETTING_GYRO_MODE_SEND_RAW_GYRO = BIT(4),
269 };
270
271 /* Trackpad modes */
272 enum {
273 TRACKPAD_ABSOLUTE_MOUSE,
274 TRACKPAD_RELATIVE_MOUSE,
275 TRACKPAD_DPAD_FOUR_WAY_DISCRETE,
276 TRACKPAD_DPAD_FOUR_WAY_OVERLAP,
277 TRACKPAD_DPAD_EIGHT_WAY,
278 TRACKPAD_RADIAL_MODE,
279 TRACKPAD_ABSOLUTE_DPAD,
280 TRACKPAD_NONE,
281 TRACKPAD_GESTURE_KEYBOARD,
282 };
283
284 /* Pad identifiers for the deck */
285 #define STEAM_PAD_LEFT 0
286 #define STEAM_PAD_RIGHT 1
287 #define STEAM_PAD_BOTH 2
288
289 /* Other random constants */
290 #define STEAM_SERIAL_LEN 10
291
292 struct steam_device {
293 struct list_head list;
294 spinlock_t lock;
295 struct hid_device *hdev, *client_hdev;
296 struct mutex report_mutex;
297 bool client_opened;
298 struct input_dev __rcu *input;
299 struct input_dev __rcu *sensors;
300 unsigned long quirks;
301 struct work_struct work_connect;
302 bool connected;
303 char serial_no[STEAM_SERIAL_LEN + 1];
304 struct power_supply_desc battery_desc;
305 struct power_supply __rcu *battery;
306 u8 battery_charge;
307 u16 voltage;
308 struct delayed_work mode_switch;
309 bool did_mode_switch;
310 bool gamepad_mode;
311 struct work_struct rumble_work;
312 u16 rumble_left;
313 u16 rumble_right;
314 unsigned int sensor_timestamp_us;
315 struct work_struct unregister_work;
316 };
317
steam_recv_report(struct steam_device * steam,u8 * data,int size)318 static int steam_recv_report(struct steam_device *steam,
319 u8 *data, int size)
320 {
321 struct hid_report *r;
322 u8 *buf;
323 int ret;
324
325 r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0];
326 if (!r) {
327 hid_err(steam->hdev, "No HID_FEATURE_REPORT submitted - nothing to read\n");
328 return -EINVAL;
329 }
330
331 if (hid_report_len(r) < 64)
332 return -EINVAL;
333
334 buf = hid_alloc_report_buf(r, GFP_KERNEL);
335 if (!buf)
336 return -ENOMEM;
337
338 /*
339 * The report ID is always 0, so strip the first byte from the output.
340 * hid_report_len() is not counting the report ID, so +1 to the length
341 * or else we get a EOVERFLOW. We are safe from a buffer overflow
342 * because hid_alloc_report_buf() allocates +7 bytes.
343 */
344 ret = hid_hw_raw_request(steam->hdev, 0x00,
345 buf, hid_report_len(r) + 1,
346 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
347 if (ret > 0)
348 memcpy(data, buf + 1, min(size, ret - 1));
349 kfree(buf);
350 return ret;
351 }
352
steam_send_report(struct steam_device * steam,u8 * cmd,int size)353 static int steam_send_report(struct steam_device *steam,
354 u8 *cmd, int size)
355 {
356 struct hid_report *r;
357 u8 *buf;
358 unsigned int retries = 50;
359 int ret;
360
361 r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0];
362 if (!r) {
363 hid_err(steam->hdev, "No HID_FEATURE_REPORT submitted - nothing to read\n");
364 return -EINVAL;
365 }
366
367 if (hid_report_len(r) < 64)
368 return -EINVAL;
369
370 buf = hid_alloc_report_buf(r, GFP_KERNEL);
371 if (!buf)
372 return -ENOMEM;
373
374 /* The report ID is always 0 */
375 memcpy(buf + 1, cmd, size);
376
377 /*
378 * Sometimes the wireless controller fails with EPIPE
379 * when sending a feature report.
380 * Doing a HID_REQ_GET_REPORT and waiting for a while
381 * seems to fix that.
382 */
383 do {
384 ret = hid_hw_raw_request(steam->hdev, 0,
385 buf, max(size, 64) + 1,
386 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
387 if (ret != -EPIPE)
388 break;
389 msleep(20);
390 } while (--retries);
391
392 kfree(buf);
393 if (ret < 0)
394 hid_err(steam->hdev, "%s: error %d (%*ph)\n", __func__,
395 ret, size, cmd);
396 return ret;
397 }
398
steam_send_report_byte(struct steam_device * steam,u8 cmd)399 static inline int steam_send_report_byte(struct steam_device *steam, u8 cmd)
400 {
401 return steam_send_report(steam, &cmd, 1);
402 }
403
steam_write_settings(struct steam_device * steam,...)404 static int steam_write_settings(struct steam_device *steam,
405 /* u8 reg, u16 val */...)
406 {
407 /* Send: 0x87 len (reg valLo valHi)* */
408 u8 reg;
409 u16 val;
410 u8 cmd[64] = {ID_SET_SETTINGS_VALUES, 0x00};
411 int ret;
412 va_list args;
413
414 va_start(args, steam);
415 for (;;) {
416 reg = va_arg(args, int);
417 if (reg == 0)
418 break;
419 val = va_arg(args, int);
420 cmd[cmd[1] + 2] = reg;
421 cmd[cmd[1] + 3] = val & 0xff;
422 cmd[cmd[1] + 4] = val >> 8;
423 cmd[1] += 3;
424 }
425 va_end(args);
426
427 ret = steam_send_report(steam, cmd, 2 + cmd[1]);
428 if (ret < 0)
429 return ret;
430
431 /*
432 * Sometimes a lingering report for this command can
433 * get read back instead of the last set report if
434 * this isn't explicitly queried
435 */
436 return steam_recv_report(steam, cmd, 2 + cmd[1]);
437 }
438
steam_get_serial(struct steam_device * steam)439 static int steam_get_serial(struct steam_device *steam)
440 {
441 /*
442 * Send: 0xae 0x15 0x01
443 * Recv: 0xae 0x15 0x01 serialnumber (10 chars)
444 */
445 int ret = 0;
446 u8 cmd[] = {ID_GET_STRING_ATTRIBUTE, 0x15, ATTRIB_STR_UNIT_SERIAL};
447 u8 reply[3 + STEAM_SERIAL_LEN + 1];
448
449 mutex_lock(&steam->report_mutex);
450 ret = steam_send_report(steam, cmd, sizeof(cmd));
451 if (ret < 0)
452 goto out;
453 ret = steam_recv_report(steam, reply, sizeof(reply));
454 if (ret < 0)
455 goto out;
456 if (reply[0] != ID_GET_STRING_ATTRIBUTE || reply[1] != 0x15 || reply[2] != ATTRIB_STR_UNIT_SERIAL) {
457 ret = -EIO;
458 goto out;
459 }
460 reply[3 + STEAM_SERIAL_LEN] = 0;
461 strscpy(steam->serial_no, reply + 3, sizeof(steam->serial_no));
462 out:
463 mutex_unlock(&steam->report_mutex);
464 return ret;
465 }
466
467 /*
468 * This command requests the wireless adaptor to post an event
469 * with the connection status. Useful if this driver is loaded when
470 * the controller is already connected.
471 */
steam_request_conn_status(struct steam_device * steam)472 static inline int steam_request_conn_status(struct steam_device *steam)
473 {
474 int ret;
475 mutex_lock(&steam->report_mutex);
476 ret = steam_send_report_byte(steam, ID_DONGLE_GET_WIRELESS_STATE);
477 mutex_unlock(&steam->report_mutex);
478 return ret;
479 }
480
481 /*
482 * Send a haptic pulse to the trackpads
483 * Duration and interval are measured in microseconds, count is the number
484 * of pulses to send for duration time with interval microseconds between them
485 * and gain is measured in decibels, ranging from -24 to +6
486 */
steam_haptic_pulse(struct steam_device * steam,u8 pad,u16 duration,u16 interval,u16 count,u8 gain)487 static inline int steam_haptic_pulse(struct steam_device *steam, u8 pad,
488 u16 duration, u16 interval, u16 count, u8 gain)
489 {
490 int ret;
491 u8 report[10] = {ID_TRIGGER_HAPTIC_PULSE, 8};
492
493 /* Left and right are swapped on this report for legacy reasons */
494 if (pad < STEAM_PAD_BOTH)
495 pad ^= 1;
496
497 report[2] = pad;
498 report[3] = duration & 0xFF;
499 report[4] = duration >> 8;
500 report[5] = interval & 0xFF;
501 report[6] = interval >> 8;
502 report[7] = count & 0xFF;
503 report[8] = count >> 8;
504 report[9] = gain;
505
506 mutex_lock(&steam->report_mutex);
507 ret = steam_send_report(steam, report, sizeof(report));
508 mutex_unlock(&steam->report_mutex);
509 return ret;
510 }
511
steam_haptic_rumble(struct steam_device * steam,u16 intensity,u16 left_speed,u16 right_speed,u8 left_gain,u8 right_gain)512 static inline int steam_haptic_rumble(struct steam_device *steam,
513 u16 intensity, u16 left_speed, u16 right_speed,
514 u8 left_gain, u8 right_gain)
515 {
516 int ret;
517 u8 report[11] = {ID_TRIGGER_RUMBLE_CMD, 9};
518
519 report[3] = intensity & 0xFF;
520 report[4] = intensity >> 8;
521 report[5] = left_speed & 0xFF;
522 report[6] = left_speed >> 8;
523 report[7] = right_speed & 0xFF;
524 report[8] = right_speed >> 8;
525 report[9] = left_gain;
526 report[10] = right_gain;
527
528 mutex_lock(&steam->report_mutex);
529 ret = steam_send_report(steam, report, sizeof(report));
530 mutex_unlock(&steam->report_mutex);
531 return ret;
532 }
533
steam_haptic_rumble_cb(struct work_struct * work)534 static void steam_haptic_rumble_cb(struct work_struct *work)
535 {
536 struct steam_device *steam = container_of(work, struct steam_device,
537 rumble_work);
538 steam_haptic_rumble(steam, 0, steam->rumble_left,
539 steam->rumble_right, 2, 0);
540 }
541
542 #ifdef CONFIG_STEAM_FF
steam_play_effect(struct input_dev * dev,void * data,struct ff_effect * effect)543 static int steam_play_effect(struct input_dev *dev, void *data,
544 struct ff_effect *effect)
545 {
546 struct steam_device *steam = input_get_drvdata(dev);
547
548 steam->rumble_left = effect->u.rumble.strong_magnitude;
549 steam->rumble_right = effect->u.rumble.weak_magnitude;
550
551 return schedule_work(&steam->rumble_work);
552 }
553 #endif
554
steam_set_lizard_mode(struct steam_device * steam,bool enable)555 static void steam_set_lizard_mode(struct steam_device *steam, bool enable)
556 {
557 if (steam->gamepad_mode)
558 enable = false;
559
560 if (enable) {
561 mutex_lock(&steam->report_mutex);
562 /* enable esc, enter, cursors */
563 steam_send_report_byte(steam, ID_SET_DEFAULT_DIGITAL_MAPPINGS);
564 /* reset settings */
565 steam_send_report_byte(steam, ID_LOAD_DEFAULT_SETTINGS);
566 mutex_unlock(&steam->report_mutex);
567 } else {
568 mutex_lock(&steam->report_mutex);
569 /* disable esc, enter, cursor */
570 steam_send_report_byte(steam, ID_CLEAR_DIGITAL_MAPPINGS);
571
572 if (steam->quirks & STEAM_QUIRK_DECK) {
573 steam_write_settings(steam,
574 SETTING_LEFT_TRACKPAD_MODE, TRACKPAD_NONE, /* disable mouse */
575 SETTING_RIGHT_TRACKPAD_MODE, TRACKPAD_NONE, /* disable mouse */
576 SETTING_LEFT_TRACKPAD_CLICK_PRESSURE, 0xFFFF, /* disable haptic click */
577 SETTING_RIGHT_TRACKPAD_CLICK_PRESSURE, 0xFFFF, /* disable haptic click */
578 SETTING_STEAM_WATCHDOG_ENABLE, 0, /* disable watchdog that tests if Steam is active */
579 0);
580 mutex_unlock(&steam->report_mutex);
581 } else {
582 steam_write_settings(steam,
583 SETTING_LEFT_TRACKPAD_MODE, TRACKPAD_NONE, /* disable mouse */
584 SETTING_RIGHT_TRACKPAD_MODE, TRACKPAD_NONE, /* disable mouse */
585 0);
586 mutex_unlock(&steam->report_mutex);
587 }
588 }
589 }
590
steam_input_open(struct input_dev * dev)591 static int steam_input_open(struct input_dev *dev)
592 {
593 struct steam_device *steam = input_get_drvdata(dev);
594 unsigned long flags;
595 bool set_lizard_mode;
596
597 /*
598 * Disabling lizard mode automatically is only done on the Steam
599 * Controller. On the Steam Deck, this is toggled manually by holding
600 * the options button instead, handled by steam_mode_switch_cb.
601 */
602 if (!(steam->quirks & STEAM_QUIRK_DECK)) {
603 spin_lock_irqsave(&steam->lock, flags);
604 set_lizard_mode = !steam->client_opened && lizard_mode;
605 spin_unlock_irqrestore(&steam->lock, flags);
606 if (set_lizard_mode)
607 steam_set_lizard_mode(steam, false);
608 }
609
610 return 0;
611 }
612
steam_input_close(struct input_dev * dev)613 static void steam_input_close(struct input_dev *dev)
614 {
615 struct steam_device *steam = input_get_drvdata(dev);
616 unsigned long flags;
617 bool set_lizard_mode;
618
619 if (!(steam->quirks & STEAM_QUIRK_DECK)) {
620 spin_lock_irqsave(&steam->lock, flags);
621 set_lizard_mode = !steam->client_opened && lizard_mode;
622 spin_unlock_irqrestore(&steam->lock, flags);
623 if (set_lizard_mode)
624 steam_set_lizard_mode(steam, true);
625 }
626 }
627
628 static enum power_supply_property steam_battery_props[] = {
629 POWER_SUPPLY_PROP_PRESENT,
630 POWER_SUPPLY_PROP_SCOPE,
631 POWER_SUPPLY_PROP_VOLTAGE_NOW,
632 POWER_SUPPLY_PROP_CAPACITY,
633 };
634
steam_battery_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)635 static int steam_battery_get_property(struct power_supply *psy,
636 enum power_supply_property psp,
637 union power_supply_propval *val)
638 {
639 struct steam_device *steam = power_supply_get_drvdata(psy);
640 unsigned long flags;
641 s16 volts;
642 u8 batt;
643 int ret = 0;
644
645 spin_lock_irqsave(&steam->lock, flags);
646 volts = steam->voltage;
647 batt = steam->battery_charge;
648 spin_unlock_irqrestore(&steam->lock, flags);
649
650 switch (psp) {
651 case POWER_SUPPLY_PROP_PRESENT:
652 val->intval = 1;
653 break;
654 case POWER_SUPPLY_PROP_SCOPE:
655 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
656 break;
657 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
658 val->intval = volts * 1000; /* mV -> uV */
659 break;
660 case POWER_SUPPLY_PROP_CAPACITY:
661 val->intval = batt;
662 break;
663 default:
664 ret = -EINVAL;
665 break;
666 }
667 return ret;
668 }
669
steam_battery_register(struct steam_device * steam)670 static int steam_battery_register(struct steam_device *steam)
671 {
672 struct power_supply *battery;
673 struct power_supply_config battery_cfg = { .drv_data = steam, };
674 unsigned long flags;
675 int ret;
676
677 steam->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
678 steam->battery_desc.properties = steam_battery_props;
679 steam->battery_desc.num_properties = ARRAY_SIZE(steam_battery_props);
680 steam->battery_desc.get_property = steam_battery_get_property;
681 steam->battery_desc.name = devm_kasprintf(&steam->hdev->dev,
682 GFP_KERNEL, "steam-controller-%s-battery",
683 steam->serial_no);
684 if (!steam->battery_desc.name)
685 return -ENOMEM;
686
687 /* avoid the warning of 0% battery while waiting for the first info */
688 spin_lock_irqsave(&steam->lock, flags);
689 steam->voltage = 3000;
690 steam->battery_charge = 100;
691 spin_unlock_irqrestore(&steam->lock, flags);
692
693 battery = power_supply_register(&steam->hdev->dev,
694 &steam->battery_desc, &battery_cfg);
695 if (IS_ERR(battery)) {
696 ret = PTR_ERR(battery);
697 hid_err(steam->hdev,
698 "%s:power_supply_register failed with error %d\n",
699 __func__, ret);
700 return ret;
701 }
702 rcu_assign_pointer(steam->battery, battery);
703 power_supply_powers(battery, &steam->hdev->dev);
704 return 0;
705 }
706
steam_input_register(struct steam_device * steam)707 static int steam_input_register(struct steam_device *steam)
708 {
709 struct hid_device *hdev = steam->hdev;
710 struct input_dev *input;
711 int ret;
712
713 rcu_read_lock();
714 input = rcu_dereference(steam->input);
715 rcu_read_unlock();
716 if (input) {
717 dbg_hid("%s: already connected\n", __func__);
718 return 0;
719 }
720
721 input = input_allocate_device();
722 if (!input)
723 return -ENOMEM;
724
725 input_set_drvdata(input, steam);
726 input->dev.parent = &hdev->dev;
727 input->open = steam_input_open;
728 input->close = steam_input_close;
729
730 input->name = (steam->quirks & STEAM_QUIRK_WIRELESS) ? "Wireless Steam Controller" :
731 (steam->quirks & STEAM_QUIRK_DECK) ? "Steam Deck" :
732 "Steam Controller";
733 input->phys = hdev->phys;
734 input->uniq = steam->serial_no;
735 input->id.bustype = hdev->bus;
736 input->id.vendor = hdev->vendor;
737 input->id.product = hdev->product;
738 input->id.version = hdev->version;
739
740 input_set_capability(input, EV_KEY, BTN_TR2);
741 input_set_capability(input, EV_KEY, BTN_TL2);
742 input_set_capability(input, EV_KEY, BTN_TR);
743 input_set_capability(input, EV_KEY, BTN_TL);
744 input_set_capability(input, EV_KEY, BTN_Y);
745 input_set_capability(input, EV_KEY, BTN_B);
746 input_set_capability(input, EV_KEY, BTN_X);
747 input_set_capability(input, EV_KEY, BTN_A);
748 input_set_capability(input, EV_KEY, BTN_DPAD_UP);
749 input_set_capability(input, EV_KEY, BTN_DPAD_RIGHT);
750 input_set_capability(input, EV_KEY, BTN_DPAD_LEFT);
751 input_set_capability(input, EV_KEY, BTN_DPAD_DOWN);
752 input_set_capability(input, EV_KEY, BTN_SELECT);
753 input_set_capability(input, EV_KEY, BTN_MODE);
754 input_set_capability(input, EV_KEY, BTN_START);
755 input_set_capability(input, EV_KEY, BTN_THUMBR);
756 input_set_capability(input, EV_KEY, BTN_THUMBL);
757 input_set_capability(input, EV_KEY, BTN_THUMB);
758 input_set_capability(input, EV_KEY, BTN_THUMB2);
759 if (steam->quirks & STEAM_QUIRK_DECK) {
760 input_set_capability(input, EV_KEY, BTN_BASE);
761 input_set_capability(input, EV_KEY, BTN_TRIGGER_HAPPY1);
762 input_set_capability(input, EV_KEY, BTN_TRIGGER_HAPPY2);
763 input_set_capability(input, EV_KEY, BTN_TRIGGER_HAPPY3);
764 input_set_capability(input, EV_KEY, BTN_TRIGGER_HAPPY4);
765 } else {
766 input_set_capability(input, EV_KEY, BTN_GEAR_DOWN);
767 input_set_capability(input, EV_KEY, BTN_GEAR_UP);
768 }
769
770 input_set_abs_params(input, ABS_X, -32767, 32767, 0, 0);
771 input_set_abs_params(input, ABS_Y, -32767, 32767, 0, 0);
772
773 input_set_abs_params(input, ABS_HAT0X, -32767, 32767,
774 STEAM_PAD_FUZZ, 0);
775 input_set_abs_params(input, ABS_HAT0Y, -32767, 32767,
776 STEAM_PAD_FUZZ, 0);
777
778 if (steam->quirks & STEAM_QUIRK_DECK) {
779 input_set_abs_params(input, ABS_HAT2Y, 0, 32767, 0, 0);
780 input_set_abs_params(input, ABS_HAT2X, 0, 32767, 0, 0);
781
782 input_set_abs_params(input, ABS_RX, -32767, 32767, 0, 0);
783 input_set_abs_params(input, ABS_RY, -32767, 32767, 0, 0);
784
785 input_set_abs_params(input, ABS_HAT1X, -32767, 32767,
786 STEAM_PAD_FUZZ, 0);
787 input_set_abs_params(input, ABS_HAT1Y, -32767, 32767,
788 STEAM_PAD_FUZZ, 0);
789
790 input_abs_set_res(input, ABS_X, STEAM_DECK_JOYSTICK_RESOLUTION);
791 input_abs_set_res(input, ABS_Y, STEAM_DECK_JOYSTICK_RESOLUTION);
792 input_abs_set_res(input, ABS_RX, STEAM_DECK_JOYSTICK_RESOLUTION);
793 input_abs_set_res(input, ABS_RY, STEAM_DECK_JOYSTICK_RESOLUTION);
794 input_abs_set_res(input, ABS_HAT1X, STEAM_PAD_RESOLUTION);
795 input_abs_set_res(input, ABS_HAT1Y, STEAM_PAD_RESOLUTION);
796 input_abs_set_res(input, ABS_HAT2Y, STEAM_DECK_TRIGGER_RESOLUTION);
797 input_abs_set_res(input, ABS_HAT2X, STEAM_DECK_TRIGGER_RESOLUTION);
798 } else {
799 input_set_abs_params(input, ABS_HAT2Y, 0, 255, 0, 0);
800 input_set_abs_params(input, ABS_HAT2X, 0, 255, 0, 0);
801
802 input_set_abs_params(input, ABS_RX, -32767, 32767,
803 STEAM_PAD_FUZZ, 0);
804 input_set_abs_params(input, ABS_RY, -32767, 32767,
805 STEAM_PAD_FUZZ, 0);
806
807 input_abs_set_res(input, ABS_X, STEAM_JOYSTICK_RESOLUTION);
808 input_abs_set_res(input, ABS_Y, STEAM_JOYSTICK_RESOLUTION);
809 input_abs_set_res(input, ABS_RX, STEAM_PAD_RESOLUTION);
810 input_abs_set_res(input, ABS_RY, STEAM_PAD_RESOLUTION);
811 input_abs_set_res(input, ABS_HAT2Y, STEAM_TRIGGER_RESOLUTION);
812 input_abs_set_res(input, ABS_HAT2X, STEAM_TRIGGER_RESOLUTION);
813 }
814 input_abs_set_res(input, ABS_HAT0X, STEAM_PAD_RESOLUTION);
815 input_abs_set_res(input, ABS_HAT0Y, STEAM_PAD_RESOLUTION);
816
817 #ifdef CONFIG_STEAM_FF
818 if (steam->quirks & STEAM_QUIRK_DECK) {
819 input_set_capability(input, EV_FF, FF_RUMBLE);
820 ret = input_ff_create_memless(input, NULL, steam_play_effect);
821 if (ret)
822 goto input_register_fail;
823 }
824 #endif
825
826 ret = input_register_device(input);
827 if (ret)
828 goto input_register_fail;
829
830 rcu_assign_pointer(steam->input, input);
831 return 0;
832
833 input_register_fail:
834 input_free_device(input);
835 return ret;
836 }
837
steam_sensors_register(struct steam_device * steam)838 static int steam_sensors_register(struct steam_device *steam)
839 {
840 struct hid_device *hdev = steam->hdev;
841 struct input_dev *sensors;
842 int ret;
843
844 if (!(steam->quirks & STEAM_QUIRK_DECK))
845 return 0;
846
847 rcu_read_lock();
848 sensors = rcu_dereference(steam->sensors);
849 rcu_read_unlock();
850 if (sensors) {
851 dbg_hid("%s: already connected\n", __func__);
852 return 0;
853 }
854
855 sensors = input_allocate_device();
856 if (!sensors)
857 return -ENOMEM;
858
859 input_set_drvdata(sensors, steam);
860 sensors->dev.parent = &hdev->dev;
861
862 sensors->name = "Steam Deck Motion Sensors";
863 sensors->phys = hdev->phys;
864 sensors->uniq = steam->serial_no;
865 sensors->id.bustype = hdev->bus;
866 sensors->id.vendor = hdev->vendor;
867 sensors->id.product = hdev->product;
868 sensors->id.version = hdev->version;
869
870 __set_bit(INPUT_PROP_ACCELEROMETER, sensors->propbit);
871 __set_bit(EV_MSC, sensors->evbit);
872 __set_bit(MSC_TIMESTAMP, sensors->mscbit);
873
874 input_set_abs_params(sensors, ABS_X, -STEAM_DECK_ACCEL_RANGE,
875 STEAM_DECK_ACCEL_RANGE, STEAM_DECK_ACCEL_FUZZ, 0);
876 input_set_abs_params(sensors, ABS_Y, -STEAM_DECK_ACCEL_RANGE,
877 STEAM_DECK_ACCEL_RANGE, STEAM_DECK_ACCEL_FUZZ, 0);
878 input_set_abs_params(sensors, ABS_Z, -STEAM_DECK_ACCEL_RANGE,
879 STEAM_DECK_ACCEL_RANGE, STEAM_DECK_ACCEL_FUZZ, 0);
880 input_abs_set_res(sensors, ABS_X, STEAM_DECK_ACCEL_RES_PER_G);
881 input_abs_set_res(sensors, ABS_Y, STEAM_DECK_ACCEL_RES_PER_G);
882 input_abs_set_res(sensors, ABS_Z, STEAM_DECK_ACCEL_RES_PER_G);
883
884 input_set_abs_params(sensors, ABS_RX, -STEAM_DECK_GYRO_RANGE,
885 STEAM_DECK_GYRO_RANGE, STEAM_DECK_GYRO_FUZZ, 0);
886 input_set_abs_params(sensors, ABS_RY, -STEAM_DECK_GYRO_RANGE,
887 STEAM_DECK_GYRO_RANGE, STEAM_DECK_GYRO_FUZZ, 0);
888 input_set_abs_params(sensors, ABS_RZ, -STEAM_DECK_GYRO_RANGE,
889 STEAM_DECK_GYRO_RANGE, STEAM_DECK_GYRO_FUZZ, 0);
890 input_abs_set_res(sensors, ABS_RX, STEAM_DECK_GYRO_RES_PER_DPS);
891 input_abs_set_res(sensors, ABS_RY, STEAM_DECK_GYRO_RES_PER_DPS);
892 input_abs_set_res(sensors, ABS_RZ, STEAM_DECK_GYRO_RES_PER_DPS);
893
894 ret = input_register_device(sensors);
895 if (ret)
896 goto sensors_register_fail;
897
898 rcu_assign_pointer(steam->sensors, sensors);
899 return 0;
900
901 sensors_register_fail:
902 input_free_device(sensors);
903 return ret;
904 }
905
steam_input_unregister(struct steam_device * steam)906 static void steam_input_unregister(struct steam_device *steam)
907 {
908 struct input_dev *input;
909 rcu_read_lock();
910 input = rcu_dereference(steam->input);
911 rcu_read_unlock();
912 if (!input)
913 return;
914 RCU_INIT_POINTER(steam->input, NULL);
915 synchronize_rcu();
916 input_unregister_device(input);
917 }
918
steam_sensors_unregister(struct steam_device * steam)919 static void steam_sensors_unregister(struct steam_device *steam)
920 {
921 struct input_dev *sensors;
922
923 if (!(steam->quirks & STEAM_QUIRK_DECK))
924 return;
925
926 rcu_read_lock();
927 sensors = rcu_dereference(steam->sensors);
928 rcu_read_unlock();
929
930 if (!sensors)
931 return;
932 RCU_INIT_POINTER(steam->sensors, NULL);
933 synchronize_rcu();
934 input_unregister_device(sensors);
935 }
936
steam_battery_unregister(struct steam_device * steam)937 static void steam_battery_unregister(struct steam_device *steam)
938 {
939 struct power_supply *battery;
940
941 rcu_read_lock();
942 battery = rcu_dereference(steam->battery);
943 rcu_read_unlock();
944
945 if (!battery)
946 return;
947 RCU_INIT_POINTER(steam->battery, NULL);
948 synchronize_rcu();
949 power_supply_unregister(battery);
950 }
951
steam_register(struct steam_device * steam)952 static int steam_register(struct steam_device *steam)
953 {
954 int ret;
955 bool client_opened;
956 unsigned long flags;
957
958 /*
959 * This function can be called several times in a row with the
960 * wireless adaptor, without steam_unregister() between them, because
961 * another client send a get_connection_status command, for example.
962 * The battery and serial number are set just once per device.
963 */
964 if (!steam->serial_no[0]) {
965 /*
966 * Unlikely, but getting the serial could fail, and it is not so
967 * important, so make up a serial number and go on.
968 */
969 if (steam_get_serial(steam) < 0)
970 strscpy(steam->serial_no, "XXXXXXXXXX",
971 sizeof(steam->serial_no));
972
973 hid_info(steam->hdev, "Steam Controller '%s' connected",
974 steam->serial_no);
975
976 /* ignore battery errors, we can live without it */
977 if (steam->quirks & STEAM_QUIRK_WIRELESS)
978 steam_battery_register(steam);
979
980 mutex_lock(&steam_devices_lock);
981 if (list_empty(&steam->list))
982 list_add(&steam->list, &steam_devices);
983 mutex_unlock(&steam_devices_lock);
984 }
985
986 spin_lock_irqsave(&steam->lock, flags);
987 client_opened = steam->client_opened;
988 spin_unlock_irqrestore(&steam->lock, flags);
989
990 if (!client_opened) {
991 steam_set_lizard_mode(steam, lizard_mode);
992 ret = steam_input_register(steam);
993 if (ret != 0)
994 goto steam_register_input_fail;
995 ret = steam_sensors_register(steam);
996 if (ret != 0)
997 goto steam_register_sensors_fail;
998 }
999 return 0;
1000
1001 steam_register_sensors_fail:
1002 steam_input_unregister(steam);
1003 steam_register_input_fail:
1004 return ret;
1005 }
1006
steam_unregister(struct steam_device * steam)1007 static void steam_unregister(struct steam_device *steam)
1008 {
1009 steam_battery_unregister(steam);
1010 steam_sensors_unregister(steam);
1011 steam_input_unregister(steam);
1012 if (steam->serial_no[0]) {
1013 hid_info(steam->hdev, "Steam Controller '%s' disconnected",
1014 steam->serial_no);
1015 mutex_lock(&steam_devices_lock);
1016 list_del_init(&steam->list);
1017 mutex_unlock(&steam_devices_lock);
1018 steam->serial_no[0] = 0;
1019 }
1020 }
1021
steam_work_connect_cb(struct work_struct * work)1022 static void steam_work_connect_cb(struct work_struct *work)
1023 {
1024 struct steam_device *steam = container_of(work, struct steam_device,
1025 work_connect);
1026 unsigned long flags;
1027 bool connected;
1028 int ret;
1029
1030 spin_lock_irqsave(&steam->lock, flags);
1031 connected = steam->connected;
1032 spin_unlock_irqrestore(&steam->lock, flags);
1033
1034 if (connected) {
1035 ret = steam_register(steam);
1036 if (ret) {
1037 hid_err(steam->hdev,
1038 "%s:steam_register failed with error %d\n",
1039 __func__, ret);
1040 }
1041 } else {
1042 steam_unregister(steam);
1043 }
1044 }
1045
steam_mode_switch_cb(struct work_struct * work)1046 static void steam_mode_switch_cb(struct work_struct *work)
1047 {
1048 struct steam_device *steam = container_of(to_delayed_work(work),
1049 struct steam_device, mode_switch);
1050 unsigned long flags;
1051 bool client_opened;
1052 steam->gamepad_mode = !steam->gamepad_mode;
1053 if (!lizard_mode)
1054 return;
1055
1056 if (steam->gamepad_mode)
1057 steam_set_lizard_mode(steam, false);
1058 else {
1059 spin_lock_irqsave(&steam->lock, flags);
1060 client_opened = steam->client_opened;
1061 spin_unlock_irqrestore(&steam->lock, flags);
1062 if (!client_opened)
1063 steam_set_lizard_mode(steam, lizard_mode);
1064 }
1065
1066 steam_haptic_pulse(steam, STEAM_PAD_RIGHT, 0x190, 0, 1, 0);
1067 if (steam->gamepad_mode) {
1068 steam_haptic_pulse(steam, STEAM_PAD_LEFT, 0x14D, 0x14D, 0x2D, 0);
1069 } else {
1070 steam_haptic_pulse(steam, STEAM_PAD_LEFT, 0x1F4, 0x1F4, 0x1E, 0);
1071 }
1072 }
1073
steam_work_unregister_cb(struct work_struct * work)1074 static void steam_work_unregister_cb(struct work_struct *work)
1075 {
1076 struct steam_device *steam = container_of(work, struct steam_device,
1077 unregister_work);
1078 unsigned long flags;
1079 bool connected;
1080 bool opened;
1081
1082 spin_lock_irqsave(&steam->lock, flags);
1083 opened = steam->client_opened;
1084 connected = steam->connected;
1085 spin_unlock_irqrestore(&steam->lock, flags);
1086
1087 if (connected) {
1088 if (opened) {
1089 steam_sensors_unregister(steam);
1090 steam_input_unregister(steam);
1091 } else {
1092 steam_set_lizard_mode(steam, lizard_mode);
1093 steam_input_register(steam);
1094 steam_sensors_register(steam);
1095 }
1096 }
1097 }
1098
steam_is_valve_interface(struct hid_device * hdev)1099 static bool steam_is_valve_interface(struct hid_device *hdev)
1100 {
1101 struct hid_report_enum *rep_enum;
1102
1103 /*
1104 * The wired device creates 3 interfaces:
1105 * 0: emulated mouse.
1106 * 1: emulated keyboard.
1107 * 2: the real game pad.
1108 * The wireless device creates 5 interfaces:
1109 * 0: emulated keyboard.
1110 * 1-4: slots where up to 4 real game pads will be connected to.
1111 * We know which one is the real gamepad interface because they are the
1112 * only ones with a feature report.
1113 */
1114 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
1115 return !list_empty(&rep_enum->report_list);
1116 }
1117
steam_client_ll_parse(struct hid_device * hdev)1118 static int steam_client_ll_parse(struct hid_device *hdev)
1119 {
1120 struct steam_device *steam = hdev->driver_data;
1121
1122 return hid_parse_report(hdev, steam->hdev->dev_rdesc,
1123 steam->hdev->dev_rsize);
1124 }
1125
steam_client_ll_start(struct hid_device * hdev)1126 static int steam_client_ll_start(struct hid_device *hdev)
1127 {
1128 return 0;
1129 }
1130
steam_client_ll_stop(struct hid_device * hdev)1131 static void steam_client_ll_stop(struct hid_device *hdev)
1132 {
1133 }
1134
steam_client_ll_open(struct hid_device * hdev)1135 static int steam_client_ll_open(struct hid_device *hdev)
1136 {
1137 struct steam_device *steam = hdev->driver_data;
1138 unsigned long flags;
1139
1140 spin_lock_irqsave(&steam->lock, flags);
1141 steam->client_opened = true;
1142 spin_unlock_irqrestore(&steam->lock, flags);
1143
1144 schedule_work(&steam->unregister_work);
1145
1146 return 0;
1147 }
1148
steam_client_ll_close(struct hid_device * hdev)1149 static void steam_client_ll_close(struct hid_device *hdev)
1150 {
1151 struct steam_device *steam = hdev->driver_data;
1152
1153 unsigned long flags;
1154 bool connected;
1155
1156 spin_lock_irqsave(&steam->lock, flags);
1157 steam->client_opened = false;
1158 connected = steam->connected && !steam->client_opened;
1159 spin_unlock_irqrestore(&steam->lock, flags);
1160
1161 schedule_work(&steam->unregister_work);
1162 }
1163
steam_client_ll_raw_request(struct hid_device * hdev,unsigned char reportnum,u8 * buf,size_t count,unsigned char report_type,int reqtype)1164 static int steam_client_ll_raw_request(struct hid_device *hdev,
1165 unsigned char reportnum, u8 *buf,
1166 size_t count, unsigned char report_type,
1167 int reqtype)
1168 {
1169 struct steam_device *steam = hdev->driver_data;
1170
1171 return hid_hw_raw_request(steam->hdev, reportnum, buf, count,
1172 report_type, reqtype);
1173 }
1174
1175 static const struct hid_ll_driver steam_client_ll_driver = {
1176 .parse = steam_client_ll_parse,
1177 .start = steam_client_ll_start,
1178 .stop = steam_client_ll_stop,
1179 .open = steam_client_ll_open,
1180 .close = steam_client_ll_close,
1181 .raw_request = steam_client_ll_raw_request,
1182 };
1183
steam_create_client_hid(struct hid_device * hdev)1184 static struct hid_device *steam_create_client_hid(struct hid_device *hdev)
1185 {
1186 struct hid_device *client_hdev;
1187
1188 client_hdev = hid_allocate_device();
1189 if (IS_ERR(client_hdev))
1190 return client_hdev;
1191
1192 client_hdev->ll_driver = &steam_client_ll_driver;
1193 client_hdev->dev.parent = hdev->dev.parent;
1194 client_hdev->bus = hdev->bus;
1195 client_hdev->vendor = hdev->vendor;
1196 client_hdev->product = hdev->product;
1197 client_hdev->version = hdev->version;
1198 client_hdev->type = hdev->type;
1199 client_hdev->country = hdev->country;
1200 strscpy(client_hdev->name, hdev->name,
1201 sizeof(client_hdev->name));
1202 strscpy(client_hdev->phys, hdev->phys,
1203 sizeof(client_hdev->phys));
1204 /*
1205 * Since we use the same device info than the real interface to
1206 * trick userspace, we will be calling steam_probe recursively.
1207 * We need to recognize the client interface somehow.
1208 */
1209 client_hdev->group = HID_GROUP_STEAM;
1210 return client_hdev;
1211 }
1212
steam_probe(struct hid_device * hdev,const struct hid_device_id * id)1213 static int steam_probe(struct hid_device *hdev,
1214 const struct hid_device_id *id)
1215 {
1216 struct steam_device *steam;
1217 int ret;
1218
1219 ret = hid_parse(hdev);
1220 if (ret) {
1221 hid_err(hdev,
1222 "%s:parse of hid interface failed\n", __func__);
1223 return ret;
1224 }
1225
1226 /*
1227 * The virtual client_dev is only used for hidraw.
1228 * Also avoid the recursive probe.
1229 */
1230 if (hdev->group == HID_GROUP_STEAM)
1231 return hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1232 /*
1233 * The non-valve interfaces (mouse and keyboard emulation) are
1234 * connected without changes.
1235 */
1236 if (!steam_is_valve_interface(hdev))
1237 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1238
1239 steam = devm_kzalloc(&hdev->dev, sizeof(*steam), GFP_KERNEL);
1240 if (!steam)
1241 return -ENOMEM;
1242
1243 steam->hdev = hdev;
1244 hid_set_drvdata(hdev, steam);
1245 spin_lock_init(&steam->lock);
1246 mutex_init(&steam->report_mutex);
1247 steam->quirks = id->driver_data;
1248 INIT_WORK(&steam->work_connect, steam_work_connect_cb);
1249 INIT_DELAYED_WORK(&steam->mode_switch, steam_mode_switch_cb);
1250 INIT_LIST_HEAD(&steam->list);
1251 INIT_WORK(&steam->rumble_work, steam_haptic_rumble_cb);
1252 steam->sensor_timestamp_us = 0;
1253 INIT_WORK(&steam->unregister_work, steam_work_unregister_cb);
1254
1255 /*
1256 * With the real steam controller interface, do not connect hidraw.
1257 * Instead, create the client_hid and connect that.
1258 */
1259 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_HIDRAW);
1260 if (ret)
1261 goto err_cancel_work;
1262
1263 ret = hid_hw_open(hdev);
1264 if (ret) {
1265 hid_err(hdev,
1266 "%s:hid_hw_open\n",
1267 __func__);
1268 goto err_hw_stop;
1269 }
1270
1271 if (steam->quirks & STEAM_QUIRK_WIRELESS) {
1272 hid_info(hdev, "Steam wireless receiver connected");
1273 /* If using a wireless adaptor ask for connection status */
1274 steam->connected = false;
1275 steam_request_conn_status(steam);
1276 } else {
1277 /* A wired connection is always present */
1278 steam->connected = true;
1279 ret = steam_register(steam);
1280 if (ret) {
1281 hid_err(hdev,
1282 "%s:steam_register failed with error %d\n",
1283 __func__, ret);
1284 goto err_hw_close;
1285 }
1286 }
1287
1288 steam->client_hdev = steam_create_client_hid(hdev);
1289 if (IS_ERR(steam->client_hdev)) {
1290 ret = PTR_ERR(steam->client_hdev);
1291 goto err_stream_unregister;
1292 }
1293 steam->client_hdev->driver_data = steam;
1294
1295 ret = hid_add_device(steam->client_hdev);
1296 if (ret)
1297 goto err_destroy;
1298
1299 return 0;
1300
1301 err_destroy:
1302 hid_destroy_device(steam->client_hdev);
1303 err_stream_unregister:
1304 if (steam->connected)
1305 steam_unregister(steam);
1306 err_hw_close:
1307 hid_hw_close(hdev);
1308 err_hw_stop:
1309 hid_hw_stop(hdev);
1310 err_cancel_work:
1311 cancel_work_sync(&steam->work_connect);
1312 cancel_delayed_work_sync(&steam->mode_switch);
1313 cancel_work_sync(&steam->rumble_work);
1314 cancel_work_sync(&steam->unregister_work);
1315
1316 return ret;
1317 }
1318
steam_remove(struct hid_device * hdev)1319 static void steam_remove(struct hid_device *hdev)
1320 {
1321 struct steam_device *steam = hid_get_drvdata(hdev);
1322
1323 if (!steam || hdev->group == HID_GROUP_STEAM) {
1324 hid_hw_stop(hdev);
1325 return;
1326 }
1327
1328 cancel_delayed_work_sync(&steam->mode_switch);
1329 cancel_work_sync(&steam->work_connect);
1330 cancel_work_sync(&steam->rumble_work);
1331 cancel_work_sync(&steam->unregister_work);
1332 hid_destroy_device(steam->client_hdev);
1333 steam->client_hdev = NULL;
1334 steam->client_opened = false;
1335 if (steam->quirks & STEAM_QUIRK_WIRELESS) {
1336 hid_info(hdev, "Steam wireless receiver disconnected");
1337 }
1338 hid_hw_close(hdev);
1339 hid_hw_stop(hdev);
1340 steam_unregister(steam);
1341 }
1342
steam_do_connect_event(struct steam_device * steam,bool connected)1343 static void steam_do_connect_event(struct steam_device *steam, bool connected)
1344 {
1345 unsigned long flags;
1346 bool changed;
1347
1348 spin_lock_irqsave(&steam->lock, flags);
1349 changed = steam->connected != connected;
1350 steam->connected = connected;
1351 spin_unlock_irqrestore(&steam->lock, flags);
1352
1353 if (changed && schedule_work(&steam->work_connect) == 0)
1354 dbg_hid("%s: connected=%d event already queued\n",
1355 __func__, connected);
1356 }
1357
1358 /*
1359 * Some input data in the protocol has the opposite sign.
1360 * Clamp the values to 32767..-32767 so that the range is
1361 * symmetrical and can be negated safely.
1362 */
steam_le16(u8 * data)1363 static inline s16 steam_le16(u8 *data)
1364 {
1365 s16 x = (s16) le16_to_cpup((__le16 *)data);
1366
1367 return x == -32768 ? -32767 : x;
1368 }
1369
1370 /*
1371 * The size for this message payload is 60.
1372 * The known values are:
1373 * (* values are not sent through wireless)
1374 * (* accelerator/gyro is disabled by default)
1375 * Offset| Type | Mapped to |Meaning
1376 * -------+-------+-----------+--------------------------
1377 * 4-7 | u32 | -- | sequence number
1378 * 8-10 | 24bit | see below | buttons
1379 * 11 | u8 | ABS_HAT2Y | left trigger
1380 * 12 | u8 | ABS_HAT2X | right trigger
1381 * 13-15 | -- | -- | always 0
1382 * 16-17 | s16 | ABS_X/ABS_HAT0X | X value
1383 * 18-19 | s16 | ABS_Y/ABS_HAT0Y | Y value
1384 * 20-21 | s16 | ABS_RX | right-pad X value
1385 * 22-23 | s16 | ABS_RY | right-pad Y value
1386 * 24-25 | s16 | -- | * left trigger
1387 * 26-27 | s16 | -- | * right trigger
1388 * 28-29 | s16 | -- | * accelerometer X value
1389 * 30-31 | s16 | -- | * accelerometer Y value
1390 * 32-33 | s16 | -- | * accelerometer Z value
1391 * 34-35 | s16 | -- | gyro X value
1392 * 36-36 | s16 | -- | gyro Y value
1393 * 38-39 | s16 | -- | gyro Z value
1394 * 40-41 | s16 | -- | quaternion W value
1395 * 42-43 | s16 | -- | quaternion X value
1396 * 44-45 | s16 | -- | quaternion Y value
1397 * 46-47 | s16 | -- | quaternion Z value
1398 * 48-49 | -- | -- | always 0
1399 * 50-51 | s16 | -- | * left trigger (uncalibrated)
1400 * 52-53 | s16 | -- | * right trigger (uncalibrated)
1401 * 54-55 | s16 | -- | * joystick X value (uncalibrated)
1402 * 56-57 | s16 | -- | * joystick Y value (uncalibrated)
1403 * 58-59 | s16 | -- | * left-pad X value
1404 * 60-61 | s16 | -- | * left-pad Y value
1405 * 62-63 | u16 | -- | * battery voltage
1406 *
1407 * The buttons are:
1408 * Bit | Mapped to | Description
1409 * ------+------------+--------------------------------
1410 * 8.0 | BTN_TR2 | right trigger fully pressed
1411 * 8.1 | BTN_TL2 | left trigger fully pressed
1412 * 8.2 | BTN_TR | right shoulder
1413 * 8.3 | BTN_TL | left shoulder
1414 * 8.4 | BTN_Y | button Y
1415 * 8.5 | BTN_B | button B
1416 * 8.6 | BTN_X | button X
1417 * 8.7 | BTN_A | button A
1418 * 9.0 | BTN_DPAD_UP | left-pad up
1419 * 9.1 | BTN_DPAD_RIGHT | left-pad right
1420 * 9.2 | BTN_DPAD_LEFT | left-pad left
1421 * 9.3 | BTN_DPAD_DOWN | left-pad down
1422 * 9.4 | BTN_SELECT | menu left
1423 * 9.5 | BTN_MODE | steam logo
1424 * 9.6 | BTN_START | menu right
1425 * 9.7 | BTN_GEAR_DOWN | left back lever
1426 * 10.0 | BTN_GEAR_UP | right back lever
1427 * 10.1 | -- | left-pad clicked
1428 * 10.2 | BTN_THUMBR | right-pad clicked
1429 * 10.3 | BTN_THUMB | left-pad touched (but see explanation below)
1430 * 10.4 | BTN_THUMB2 | right-pad touched
1431 * 10.5 | -- | unknown
1432 * 10.6 | BTN_THUMBL | joystick clicked
1433 * 10.7 | -- | lpad_and_joy
1434 */
1435
steam_do_input_event(struct steam_device * steam,struct input_dev * input,u8 * data)1436 static void steam_do_input_event(struct steam_device *steam,
1437 struct input_dev *input, u8 *data)
1438 {
1439 /* 24 bits of buttons */
1440 u8 b8, b9, b10;
1441 s16 x, y;
1442 bool lpad_touched, lpad_and_joy;
1443
1444 b8 = data[8];
1445 b9 = data[9];
1446 b10 = data[10];
1447
1448 input_report_abs(input, ABS_HAT2Y, data[11]);
1449 input_report_abs(input, ABS_HAT2X, data[12]);
1450
1451 /*
1452 * These two bits tells how to interpret the values X and Y.
1453 * lpad_and_joy tells that the joystick and the lpad are used at the
1454 * same time.
1455 * lpad_touched tells whether X/Y are to be read as lpad coord or
1456 * joystick values.
1457 * (lpad_touched || lpad_and_joy) tells if the lpad is really touched.
1458 */
1459 lpad_touched = b10 & BIT(3);
1460 lpad_and_joy = b10 & BIT(7);
1461 x = steam_le16(data + 16);
1462 y = -steam_le16(data + 18);
1463
1464 input_report_abs(input, lpad_touched ? ABS_HAT0X : ABS_X, x);
1465 input_report_abs(input, lpad_touched ? ABS_HAT0Y : ABS_Y, y);
1466 /* Check if joystick is centered */
1467 if (lpad_touched && !lpad_and_joy) {
1468 input_report_abs(input, ABS_X, 0);
1469 input_report_abs(input, ABS_Y, 0);
1470 }
1471 /* Check if lpad is untouched */
1472 if (!(lpad_touched || lpad_and_joy)) {
1473 input_report_abs(input, ABS_HAT0X, 0);
1474 input_report_abs(input, ABS_HAT0Y, 0);
1475 }
1476
1477 input_report_abs(input, ABS_RX, steam_le16(data + 20));
1478 input_report_abs(input, ABS_RY, -steam_le16(data + 22));
1479
1480 input_event(input, EV_KEY, BTN_TR2, !!(b8 & BIT(0)));
1481 input_event(input, EV_KEY, BTN_TL2, !!(b8 & BIT(1)));
1482 input_event(input, EV_KEY, BTN_TR, !!(b8 & BIT(2)));
1483 input_event(input, EV_KEY, BTN_TL, !!(b8 & BIT(3)));
1484 input_event(input, EV_KEY, BTN_Y, !!(b8 & BIT(4)));
1485 input_event(input, EV_KEY, BTN_B, !!(b8 & BIT(5)));
1486 input_event(input, EV_KEY, BTN_X, !!(b8 & BIT(6)));
1487 input_event(input, EV_KEY, BTN_A, !!(b8 & BIT(7)));
1488 input_event(input, EV_KEY, BTN_SELECT, !!(b9 & BIT(4)));
1489 input_event(input, EV_KEY, BTN_MODE, !!(b9 & BIT(5)));
1490 input_event(input, EV_KEY, BTN_START, !!(b9 & BIT(6)));
1491 input_event(input, EV_KEY, BTN_GEAR_DOWN, !!(b9 & BIT(7)));
1492 input_event(input, EV_KEY, BTN_GEAR_UP, !!(b10 & BIT(0)));
1493 input_event(input, EV_KEY, BTN_THUMBR, !!(b10 & BIT(2)));
1494 input_event(input, EV_KEY, BTN_THUMBL, !!(b10 & BIT(6)));
1495 input_event(input, EV_KEY, BTN_THUMB, lpad_touched || lpad_and_joy);
1496 input_event(input, EV_KEY, BTN_THUMB2, !!(b10 & BIT(4)));
1497 input_event(input, EV_KEY, BTN_DPAD_UP, !!(b9 & BIT(0)));
1498 input_event(input, EV_KEY, BTN_DPAD_RIGHT, !!(b9 & BIT(1)));
1499 input_event(input, EV_KEY, BTN_DPAD_LEFT, !!(b9 & BIT(2)));
1500 input_event(input, EV_KEY, BTN_DPAD_DOWN, !!(b9 & BIT(3)));
1501
1502 input_sync(input);
1503 }
1504
1505 /*
1506 * The size for this message payload is 56.
1507 * The known values are:
1508 * Offset| Type | Mapped to |Meaning
1509 * -------+-------+-----------+--------------------------
1510 * 4-7 | u32 | -- | sequence number
1511 * 8-15 | u64 | see below | buttons
1512 * 16-17 | s16 | ABS_HAT0X | left-pad X value
1513 * 18-19 | s16 | ABS_HAT0Y | left-pad Y value
1514 * 20-21 | s16 | ABS_HAT1X | right-pad X value
1515 * 22-23 | s16 | ABS_HAT1Y | right-pad Y value
1516 * 24-25 | s16 | IMU ABS_X | accelerometer X value
1517 * 26-27 | s16 | IMU ABS_Z | accelerometer Y value
1518 * 28-29 | s16 | IMU ABS_Y | accelerometer Z value
1519 * 30-31 | s16 | IMU ABS_RX | gyro X value
1520 * 32-33 | s16 | IMU ABS_RZ | gyro Y value
1521 * 34-35 | s16 | IMU ABS_RY | gyro Z value
1522 * 36-37 | s16 | -- | quaternion W value
1523 * 38-39 | s16 | -- | quaternion X value
1524 * 40-41 | s16 | -- | quaternion Y value
1525 * 42-43 | s16 | -- | quaternion Z value
1526 * 44-45 | u16 | ABS_HAT2Y | left trigger (uncalibrated)
1527 * 46-47 | u16 | ABS_HAT2X | right trigger (uncalibrated)
1528 * 48-49 | s16 | ABS_X | left joystick X
1529 * 50-51 | s16 | ABS_Y | left joystick Y
1530 * 52-53 | s16 | ABS_RX | right joystick X
1531 * 54-55 | s16 | ABS_RY | right joystick Y
1532 * 56-57 | u16 | -- | left pad pressure
1533 * 58-59 | u16 | -- | right pad pressure
1534 *
1535 * The buttons are:
1536 * Bit | Mapped to | Description
1537 * ------+------------+--------------------------------
1538 * 8.0 | BTN_TR2 | right trigger fully pressed
1539 * 8.1 | BTN_TL2 | left trigger fully pressed
1540 * 8.2 | BTN_TR | right shoulder
1541 * 8.3 | BTN_TL | left shoulder
1542 * 8.4 | BTN_Y | button Y
1543 * 8.5 | BTN_B | button B
1544 * 8.6 | BTN_X | button X
1545 * 8.7 | BTN_A | button A
1546 * 9.0 | BTN_DPAD_UP | left-pad up
1547 * 9.1 | BTN_DPAD_RIGHT | left-pad right
1548 * 9.2 | BTN_DPAD_LEFT | left-pad left
1549 * 9.3 | BTN_DPAD_DOWN | left-pad down
1550 * 9.4 | BTN_SELECT | menu left
1551 * 9.5 | BTN_MODE | steam logo
1552 * 9.6 | BTN_START | menu right
1553 * 9.7 | BTN_TRIGGER_HAPPY3 | left bottom grip button
1554 * 10.0 | BTN_TRIGGER_HAPPY4 | right bottom grip button
1555 * 10.1 | BTN_THUMB | left pad pressed
1556 * 10.2 | BTN_THUMB2 | right pad pressed
1557 * 10.3 | -- | left pad touched
1558 * 10.4 | -- | right pad touched
1559 * 10.5 | -- | unknown
1560 * 10.6 | BTN_THUMBL | left joystick clicked
1561 * 10.7 | -- | unknown
1562 * 11.0 | -- | unknown
1563 * 11.1 | -- | unknown
1564 * 11.2 | BTN_THUMBR | right joystick clicked
1565 * 11.3 | -- | unknown
1566 * 11.4 | -- | unknown
1567 * 11.5 | -- | unknown
1568 * 11.6 | -- | unknown
1569 * 11.7 | -- | unknown
1570 * 12.0 | -- | unknown
1571 * 12.1 | -- | unknown
1572 * 12.2 | -- | unknown
1573 * 12.3 | -- | unknown
1574 * 12.4 | -- | unknown
1575 * 12.5 | -- | unknown
1576 * 12.6 | -- | unknown
1577 * 12.7 | -- | unknown
1578 * 13.0 | -- | unknown
1579 * 13.1 | BTN_TRIGGER_HAPPY1 | left top grip button
1580 * 13.2 | BTN_TRIGGER_HAPPY2 | right top grip button
1581 * 13.3 | -- | unknown
1582 * 13.4 | -- | unknown
1583 * 13.5 | -- | unknown
1584 * 13.6 | -- | left joystick touched
1585 * 13.7 | -- | right joystick touched
1586 * 14.0 | -- | unknown
1587 * 14.1 | -- | unknown
1588 * 14.2 | BTN_BASE | quick access button
1589 * 14.3 | -- | unknown
1590 * 14.4 | -- | unknown
1591 * 14.5 | -- | unknown
1592 * 14.6 | -- | unknown
1593 * 14.7 | -- | unknown
1594 * 15.0 | -- | unknown
1595 * 15.1 | -- | unknown
1596 * 15.2 | -- | unknown
1597 * 15.3 | -- | unknown
1598 * 15.4 | -- | unknown
1599 * 15.5 | -- | unknown
1600 * 15.6 | -- | unknown
1601 * 15.7 | -- | unknown
1602 */
steam_do_deck_input_event(struct steam_device * steam,struct input_dev * input,u8 * data)1603 static void steam_do_deck_input_event(struct steam_device *steam,
1604 struct input_dev *input, u8 *data)
1605 {
1606 u8 b8, b9, b10, b11, b13, b14;
1607 bool lpad_touched, rpad_touched;
1608
1609 b8 = data[8];
1610 b9 = data[9];
1611 b10 = data[10];
1612 b11 = data[11];
1613 b13 = data[13];
1614 b14 = data[14];
1615
1616 if (!(b9 & BIT(6)) && steam->did_mode_switch) {
1617 steam->did_mode_switch = false;
1618 cancel_delayed_work(&steam->mode_switch);
1619 } else if (!steam->client_opened && (b9 & BIT(6)) && !steam->did_mode_switch) {
1620 steam->did_mode_switch = true;
1621 schedule_delayed_work(&steam->mode_switch, 45 * HZ / 100);
1622 }
1623
1624 if (!steam->gamepad_mode)
1625 return;
1626
1627 lpad_touched = b10 & BIT(3);
1628 rpad_touched = b10 & BIT(4);
1629
1630 if (lpad_touched) {
1631 input_report_abs(input, ABS_HAT0X, steam_le16(data + 16));
1632 input_report_abs(input, ABS_HAT0Y, steam_le16(data + 18));
1633 } else {
1634 input_report_abs(input, ABS_HAT0X, 0);
1635 input_report_abs(input, ABS_HAT0Y, 0);
1636 }
1637
1638 if (rpad_touched) {
1639 input_report_abs(input, ABS_HAT1X, steam_le16(data + 20));
1640 input_report_abs(input, ABS_HAT1Y, steam_le16(data + 22));
1641 } else {
1642 input_report_abs(input, ABS_HAT1X, 0);
1643 input_report_abs(input, ABS_HAT1Y, 0);
1644 }
1645
1646 input_report_abs(input, ABS_X, steam_le16(data + 48));
1647 input_report_abs(input, ABS_Y, -steam_le16(data + 50));
1648 input_report_abs(input, ABS_RX, steam_le16(data + 52));
1649 input_report_abs(input, ABS_RY, -steam_le16(data + 54));
1650
1651 input_report_abs(input, ABS_HAT2Y, steam_le16(data + 44));
1652 input_report_abs(input, ABS_HAT2X, steam_le16(data + 46));
1653
1654 input_event(input, EV_KEY, BTN_TR2, !!(b8 & BIT(0)));
1655 input_event(input, EV_KEY, BTN_TL2, !!(b8 & BIT(1)));
1656 input_event(input, EV_KEY, BTN_TR, !!(b8 & BIT(2)));
1657 input_event(input, EV_KEY, BTN_TL, !!(b8 & BIT(3)));
1658 input_event(input, EV_KEY, BTN_Y, !!(b8 & BIT(4)));
1659 input_event(input, EV_KEY, BTN_B, !!(b8 & BIT(5)));
1660 input_event(input, EV_KEY, BTN_X, !!(b8 & BIT(6)));
1661 input_event(input, EV_KEY, BTN_A, !!(b8 & BIT(7)));
1662 input_event(input, EV_KEY, BTN_SELECT, !!(b9 & BIT(4)));
1663 input_event(input, EV_KEY, BTN_MODE, !!(b9 & BIT(5)));
1664 input_event(input, EV_KEY, BTN_START, !!(b9 & BIT(6)));
1665 input_event(input, EV_KEY, BTN_TRIGGER_HAPPY3, !!(b9 & BIT(7)));
1666 input_event(input, EV_KEY, BTN_TRIGGER_HAPPY4, !!(b10 & BIT(0)));
1667 input_event(input, EV_KEY, BTN_THUMBL, !!(b10 & BIT(6)));
1668 input_event(input, EV_KEY, BTN_THUMBR, !!(b11 & BIT(2)));
1669 input_event(input, EV_KEY, BTN_DPAD_UP, !!(b9 & BIT(0)));
1670 input_event(input, EV_KEY, BTN_DPAD_RIGHT, !!(b9 & BIT(1)));
1671 input_event(input, EV_KEY, BTN_DPAD_LEFT, !!(b9 & BIT(2)));
1672 input_event(input, EV_KEY, BTN_DPAD_DOWN, !!(b9 & BIT(3)));
1673 input_event(input, EV_KEY, BTN_THUMB, !!(b10 & BIT(1)));
1674 input_event(input, EV_KEY, BTN_THUMB2, !!(b10 & BIT(2)));
1675 input_event(input, EV_KEY, BTN_TRIGGER_HAPPY1, !!(b13 & BIT(1)));
1676 input_event(input, EV_KEY, BTN_TRIGGER_HAPPY2, !!(b13 & BIT(2)));
1677 input_event(input, EV_KEY, BTN_BASE, !!(b14 & BIT(2)));
1678
1679 input_sync(input);
1680 }
1681
steam_do_deck_sensors_event(struct steam_device * steam,struct input_dev * sensors,u8 * data)1682 static void steam_do_deck_sensors_event(struct steam_device *steam,
1683 struct input_dev *sensors, u8 *data)
1684 {
1685 /*
1686 * The deck input report is received every 4 ms on average,
1687 * with a jitter of +/- 4 ms even though the USB descriptor claims
1688 * that it uses 1 kHz.
1689 * Since the HID report does not include a sensor timestamp,
1690 * use a fixed increment here.
1691 */
1692 steam->sensor_timestamp_us += 4000;
1693
1694 if (!steam->gamepad_mode)
1695 return;
1696
1697 input_event(sensors, EV_MSC, MSC_TIMESTAMP, steam->sensor_timestamp_us);
1698 input_report_abs(sensors, ABS_X, steam_le16(data + 24));
1699 input_report_abs(sensors, ABS_Z, -steam_le16(data + 26));
1700 input_report_abs(sensors, ABS_Y, steam_le16(data + 28));
1701 input_report_abs(sensors, ABS_RX, steam_le16(data + 30));
1702 input_report_abs(sensors, ABS_RZ, -steam_le16(data + 32));
1703 input_report_abs(sensors, ABS_RY, steam_le16(data + 34));
1704
1705 input_sync(sensors);
1706 }
1707
1708 /*
1709 * The size for this message payload is 11.
1710 * The known values are:
1711 * Offset| Type | Meaning
1712 * -------+-------+---------------------------
1713 * 4-7 | u32 | sequence number
1714 * 8-11 | -- | always 0
1715 * 12-13 | u16 | voltage (mV)
1716 * 14 | u8 | battery percent
1717 */
steam_do_battery_event(struct steam_device * steam,struct power_supply * battery,u8 * data)1718 static void steam_do_battery_event(struct steam_device *steam,
1719 struct power_supply *battery, u8 *data)
1720 {
1721 unsigned long flags;
1722
1723 s16 volts = steam_le16(data + 12);
1724 u8 batt = data[14];
1725
1726 /* Creating the battery may have failed */
1727 rcu_read_lock();
1728 battery = rcu_dereference(steam->battery);
1729 if (likely(battery)) {
1730 spin_lock_irqsave(&steam->lock, flags);
1731 steam->voltage = volts;
1732 steam->battery_charge = batt;
1733 spin_unlock_irqrestore(&steam->lock, flags);
1734 power_supply_changed(battery);
1735 }
1736 rcu_read_unlock();
1737 }
1738
steam_raw_event(struct hid_device * hdev,struct hid_report * report,u8 * data,int size)1739 static int steam_raw_event(struct hid_device *hdev,
1740 struct hid_report *report, u8 *data,
1741 int size)
1742 {
1743 struct steam_device *steam = hid_get_drvdata(hdev);
1744 struct input_dev *input;
1745 struct input_dev *sensors;
1746 struct power_supply *battery;
1747
1748 if (!steam)
1749 return 0;
1750
1751 if (steam->client_opened)
1752 hid_input_report(steam->client_hdev, HID_FEATURE_REPORT,
1753 data, size, 0);
1754 /*
1755 * All messages are size=64, all values little-endian.
1756 * The format is:
1757 * Offset| Meaning
1758 * -------+--------------------------------------------
1759 * 0-1 | always 0x01, 0x00, maybe protocol version?
1760 * 2 | type of message
1761 * 3 | length of the real payload (not checked)
1762 * 4-n | payload data, depends on the type
1763 *
1764 * There are these known types of message:
1765 * 0x01: input data (60 bytes)
1766 * 0x03: wireless connect/disconnect (1 byte)
1767 * 0x04: battery status (11 bytes)
1768 * 0x09: Steam Deck input data (56 bytes)
1769 */
1770
1771 if (size != 64 || data[0] != 1 || data[1] != 0)
1772 return 0;
1773
1774 switch (data[2]) {
1775 case ID_CONTROLLER_STATE:
1776 if (steam->client_opened)
1777 return 0;
1778 rcu_read_lock();
1779 input = rcu_dereference(steam->input);
1780 if (likely(input))
1781 steam_do_input_event(steam, input, data);
1782 rcu_read_unlock();
1783 break;
1784 case ID_CONTROLLER_DECK_STATE:
1785 if (steam->client_opened)
1786 return 0;
1787 rcu_read_lock();
1788 input = rcu_dereference(steam->input);
1789 if (likely(input))
1790 steam_do_deck_input_event(steam, input, data);
1791 sensors = rcu_dereference(steam->sensors);
1792 if (likely(sensors))
1793 steam_do_deck_sensors_event(steam, sensors, data);
1794 rcu_read_unlock();
1795 break;
1796 case ID_CONTROLLER_WIRELESS:
1797 /*
1798 * The payload of this event is a single byte:
1799 * 0x01: disconnected.
1800 * 0x02: connected.
1801 */
1802 switch (data[4]) {
1803 case 0x01:
1804 steam_do_connect_event(steam, false);
1805 break;
1806 case 0x02:
1807 steam_do_connect_event(steam, true);
1808 break;
1809 }
1810 break;
1811 case ID_CONTROLLER_STATUS:
1812 if (steam->quirks & STEAM_QUIRK_WIRELESS) {
1813 rcu_read_lock();
1814 battery = rcu_dereference(steam->battery);
1815 if (likely(battery)) {
1816 steam_do_battery_event(steam, battery, data);
1817 } else {
1818 dbg_hid(
1819 "%s: battery data without connect event\n",
1820 __func__);
1821 steam_do_connect_event(steam, true);
1822 }
1823 rcu_read_unlock();
1824 }
1825 break;
1826 }
1827 return 0;
1828 }
1829
steam_param_set_lizard_mode(const char * val,const struct kernel_param * kp)1830 static int steam_param_set_lizard_mode(const char *val,
1831 const struct kernel_param *kp)
1832 {
1833 struct steam_device *steam;
1834 int ret;
1835
1836 ret = param_set_bool(val, kp);
1837 if (ret)
1838 return ret;
1839
1840 mutex_lock(&steam_devices_lock);
1841 list_for_each_entry(steam, &steam_devices, list) {
1842 if (!steam->client_opened)
1843 steam_set_lizard_mode(steam, lizard_mode);
1844 }
1845 mutex_unlock(&steam_devices_lock);
1846 return 0;
1847 }
1848
1849 static const struct kernel_param_ops steam_lizard_mode_ops = {
1850 .set = steam_param_set_lizard_mode,
1851 .get = param_get_bool,
1852 };
1853
1854 module_param_cb(lizard_mode, &steam_lizard_mode_ops, &lizard_mode, 0644);
1855 MODULE_PARM_DESC(lizard_mode,
1856 "Enable mouse and keyboard emulation (lizard mode) when the gamepad is not in use");
1857
1858 static const struct hid_device_id steam_controllers[] = {
1859 { /* Wired Steam Controller */
1860 HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
1861 USB_DEVICE_ID_STEAM_CONTROLLER)
1862 },
1863 { /* Wireless Steam Controller */
1864 HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
1865 USB_DEVICE_ID_STEAM_CONTROLLER_WIRELESS),
1866 .driver_data = STEAM_QUIRK_WIRELESS
1867 },
1868 { /* Steam Deck */
1869 HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
1870 USB_DEVICE_ID_STEAM_DECK),
1871 .driver_data = STEAM_QUIRK_DECK
1872 },
1873 {}
1874 };
1875
1876 MODULE_DEVICE_TABLE(hid, steam_controllers);
1877
1878 static struct hid_driver steam_controller_driver = {
1879 .name = "hid-steam",
1880 .id_table = steam_controllers,
1881 .probe = steam_probe,
1882 .remove = steam_remove,
1883 .raw_event = steam_raw_event,
1884 };
1885
1886 module_hid_driver(steam_controller_driver);
1887