1 // SPDX-License-Identifier: GPL-2.0+
3 * HID driver for Valve Steam Controller
6 * Copyright (c) 2022 Valve Software
8 * Supports both the wired and wireless interfaces.
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
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.
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.
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
29 * When the client is closed, this input device will be created again.
31 * For additional functions, such as changing the right-pad margin or switching
32 * the led, you can use the user-space tool at:
34 * https://github.com/rodrigorc/steamctrl
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>
48 MODULE_DESCRIPTION("HID driver for Valve Steam Controller");
49 MODULE_LICENSE("GPL");
52 static bool lizard_mode = true;
54 static DEFINE_MUTEX(steam_devices_lock);
55 static LIST_HEAD(steam_devices);
57 #define STEAM_QUIRK_WIRELESS BIT(0)
58 #define STEAM_QUIRK_DECK BIT(1)
60 /* Touch pads are 40 mm in diameter and 65535 units */
61 #define STEAM_PAD_RESOLUTION 1638
62 /* Trigger runs are about 5 mm and 256 units */
63 #define STEAM_TRIGGER_RESOLUTION 51
64 /* Joystick runs are about 5 mm and 256 units */
65 #define STEAM_JOYSTICK_RESOLUTION 51
66 /* Trigger runs are about 6 mm and 32768 units */
67 #define STEAM_DECK_TRIGGER_RESOLUTION 5461
68 /* Joystick runs are about 5 mm and 32768 units */
69 #define STEAM_DECK_JOYSTICK_RESOLUTION 6553
70 /* Accelerometer has 16 bit resolution and a range of +/- 2g */
71 #define STEAM_DECK_ACCEL_RES_PER_G 16384
72 #define STEAM_DECK_ACCEL_RANGE 32768
73 #define STEAM_DECK_ACCEL_FUZZ 32
74 /* Gyroscope has 16 bit resolution and a range of +/- 2000 dps */
75 #define STEAM_DECK_GYRO_RES_PER_DPS 16
76 #define STEAM_DECK_GYRO_RANGE 32768
77 #define STEAM_DECK_GYRO_FUZZ 1
79 #define STEAM_PAD_FUZZ 256
82 * Commands that can be sent in a feature report.
83 * Thanks to Valve and SDL for the names.
86 ID_SET_DIGITAL_MAPPINGS = 0x80,
87 ID_CLEAR_DIGITAL_MAPPINGS = 0x81,
88 ID_GET_DIGITAL_MAPPINGS = 0x82,
89 ID_GET_ATTRIBUTES_VALUES = 0x83,
90 ID_GET_ATTRIBUTE_LABEL = 0x84,
91 ID_SET_DEFAULT_DIGITAL_MAPPINGS = 0x85,
92 ID_FACTORY_RESET = 0x86,
93 ID_SET_SETTINGS_VALUES = 0x87,
94 ID_CLEAR_SETTINGS_VALUES = 0x88,
95 ID_GET_SETTINGS_VALUES = 0x89,
96 ID_GET_SETTING_LABEL = 0x8A,
97 ID_GET_SETTINGS_MAXS = 0x8B,
98 ID_GET_SETTINGS_DEFAULTS = 0x8C,
99 ID_SET_CONTROLLER_MODE = 0x8D,
100 ID_LOAD_DEFAULT_SETTINGS = 0x8E,
101 ID_TRIGGER_HAPTIC_PULSE = 0x8F,
102 ID_TURN_OFF_CONTROLLER = 0x9F,
104 ID_GET_DEVICE_INFO = 0xA1,
106 ID_CALIBRATE_TRACKPADS = 0xA7,
107 ID_RESERVED_0 = 0xA8,
108 ID_SET_SERIAL_NUMBER = 0xA9,
109 ID_GET_TRACKPAD_CALIBRATION = 0xAA,
110 ID_GET_TRACKPAD_FACTORY_CALIBRATION = 0xAB,
111 ID_GET_TRACKPAD_RAW_DATA = 0xAC,
112 ID_ENABLE_PAIRING = 0xAD,
113 ID_GET_STRING_ATTRIBUTE = 0xAE,
114 ID_RADIO_ERASE_RECORDS = 0xAF,
115 ID_RADIO_WRITE_RECORD = 0xB0,
116 ID_SET_DONGLE_SETTING = 0xB1,
117 ID_DONGLE_DISCONNECT_DEVICE = 0xB2,
118 ID_DONGLE_COMMIT_DEVICE = 0xB3,
119 ID_DONGLE_GET_WIRELESS_STATE = 0xB4,
120 ID_CALIBRATE_GYRO = 0xB5,
121 ID_PLAY_AUDIO = 0xB6,
122 ID_AUDIO_UPDATE_START = 0xB7,
123 ID_AUDIO_UPDATE_DATA = 0xB8,
124 ID_AUDIO_UPDATE_COMPLETE = 0xB9,
125 ID_GET_CHIPID = 0xBA,
127 ID_CALIBRATE_JOYSTICK = 0xBF,
128 ID_CALIBRATE_ANALOG_TRIGGERS = 0xC0,
129 ID_SET_AUDIO_MAPPING = 0xC1,
130 ID_CHECK_GYRO_FW_LOAD = 0xC2,
131 ID_CALIBRATE_ANALOG = 0xC3,
132 ID_DONGLE_GET_CONNECTED_SLOTS = 0xC4,
136 ID_TRIGGER_HAPTIC_CMD = 0xEA,
137 ID_TRIGGER_RUMBLE_CMD = 0xEB,
143 SETTING_MOUSE_SENSITIVITY,
144 SETTING_MOUSE_ACCELERATION,
145 SETTING_TRACKBALL_ROTATION_ANGLE,
146 SETTING_HAPTIC_INTENSITY_UNUSED,
147 SETTING_LEFT_GAMEPAD_STICK_ENABLED,
148 SETTING_RIGHT_GAMEPAD_STICK_ENABLED,
149 SETTING_USB_DEBUG_MODE,
150 SETTING_LEFT_TRACKPAD_MODE,
151 SETTING_RIGHT_TRACKPAD_MODE,
152 SETTING_MOUSE_POINTER_ENABLED,
155 SETTING_DPAD_DEADZONE,
156 SETTING_MINIMUM_MOMENTUM_VEL,
157 SETTING_MOMENTUM_DECAY_AMMOUNT,
158 SETTING_TRACKPAD_RELATIVE_MODE_TICKS_PER_PIXEL,
159 SETTING_HAPTIC_INCREMENT,
160 SETTING_DPAD_ANGLE_SIN,
161 SETTING_DPAD_ANGLE_COS,
162 SETTING_MOMENTUM_VERTICAL_DIVISOR,
163 SETTING_MOMENTUM_MAXIMUM_VELOCITY,
164 SETTING_TRACKPAD_Z_ON,
167 SETTING_TRACKPAD_Z_OFF,
168 SETTING_SENSITIVY_SCALE_AMMOUNT,
169 SETTING_LEFT_TRACKPAD_SECONDARY_MODE,
170 SETTING_RIGHT_TRACKPAD_SECONDARY_MODE,
171 SETTING_SMOOTH_ABSOLUTE_MOUSE,
172 SETTING_STEAMBUTTON_POWEROFF_TIME,
174 SETTING_TRACKPAD_OUTER_RADIUS,
175 SETTING_TRACKPAD_Z_ON_LEFT,
176 SETTING_TRACKPAD_Z_OFF_LEFT,
179 SETTING_TRACKPAD_OUTER_SPIN_VEL,
180 SETTING_TRACKPAD_OUTER_SPIN_RADIUS,
181 SETTING_TRACKPAD_OUTER_SPIN_HORIZONTAL_ONLY,
182 SETTING_TRACKPAD_RELATIVE_MODE_DEADZONE,
183 SETTING_TRACKPAD_RELATIVE_MODE_MAX_VEL,
184 SETTING_TRACKPAD_RELATIVE_MODE_INVERT_Y,
185 SETTING_TRACKPAD_DOUBLE_TAP_BEEP_ENABLED,
186 SETTING_TRACKPAD_DOUBLE_TAP_BEEP_PERIOD,
187 SETTING_TRACKPAD_DOUBLE_TAP_BEEP_COUNT,
188 SETTING_TRACKPAD_OUTER_RADIUS_RELEASE_ON_TRANSITION,
191 SETTING_RADIAL_MODE_ANGLE,
192 SETTING_HAPTIC_INTENSITY_MOUSE_MODE,
193 SETTING_LEFT_DPAD_REQUIRES_CLICK,
194 SETTING_RIGHT_DPAD_REQUIRES_CLICK,
195 SETTING_LED_BASELINE_BRIGHTNESS,
196 SETTING_LED_USER_BRIGHTNESS,
197 SETTING_ENABLE_RAW_JOYSTICK,
198 SETTING_ENABLE_FAST_SCAN,
200 SETTING_WIRELESS_PACKET_VERSION,
203 SETTING_SLEEP_INACTIVITY_TIMEOUT,
204 SETTING_TRACKPAD_NOISE_THRESHOLD,
205 SETTING_LEFT_TRACKPAD_CLICK_PRESSURE,
206 SETTING_RIGHT_TRACKPAD_CLICK_PRESSURE,
207 SETTING_LEFT_BUMPER_CLICK_PRESSURE,
208 SETTING_RIGHT_BUMPER_CLICK_PRESSURE,
209 SETTING_LEFT_GRIP_CLICK_PRESSURE,
210 SETTING_RIGHT_GRIP_CLICK_PRESSURE,
211 SETTING_LEFT_GRIP2_CLICK_PRESSURE,
212 SETTING_RIGHT_GRIP2_CLICK_PRESSURE,
215 SETTING_PRESSURE_MODE,
216 SETTING_CONTROLLER_TEST_MODE,
217 SETTING_TRIGGER_MODE,
218 SETTING_TRACKPAD_Z_THRESHOLD,
220 SETTING_TRACKPAD_FILT_CTRL,
221 SETTING_TRACKPAD_CLIP,
222 SETTING_DEBUG_OUTPUT_SELECT,
223 SETTING_TRIGGER_THRESHOLD_PERCENT,
224 SETTING_TRACKPAD_FREQUENCY_HOPPING,
227 SETTING_HAPTICS_ENABLED,
228 SETTING_STEAM_WATCHDOG_ENABLE,
229 SETTING_TIMP_TOUCH_THRESHOLD_ON,
230 SETTING_TIMP_TOUCH_THRESHOLD_OFF,
231 SETTING_FREQ_HOPPING,
232 SETTING_TEST_CONTROL,
233 SETTING_HAPTIC_MASTER_GAIN_DB,
234 SETTING_THUMB_TOUCH_THRESH,
235 SETTING_DEVICE_POWER_STATUS,
236 SETTING_HAPTIC_INTENSITY,
239 SETTING_STABILIZER_ENABLED,
240 SETTING_TIMP_MODE_MTE,
243 /* Input report identifiers */
246 ID_CONTROLLER_STATE = 1,
247 ID_CONTROLLER_DEBUG = 2,
248 ID_CONTROLLER_WIRELESS = 3,
249 ID_CONTROLLER_STATUS = 4,
250 ID_CONTROLLER_DEBUG2 = 5,
251 ID_CONTROLLER_SECONDARY_STATE = 6,
252 ID_CONTROLLER_BLE_STATE = 7,
253 ID_CONTROLLER_DECK_STATE = 9
256 /* String attribute idenitifiers */
258 ATTRIB_STR_BOARD_SERIAL,
259 ATTRIB_STR_UNIT_SERIAL,
262 /* Values for GYRO_MODE (bitmask) */
264 SETTING_GYRO_MODE_OFF = 0,
265 SETTING_GYRO_MODE_STEERING = BIT(0),
266 SETTING_GYRO_MODE_TILT = BIT(1),
267 SETTING_GYRO_MODE_SEND_ORIENTATION = BIT(2),
268 SETTING_GYRO_MODE_SEND_RAW_ACCEL = BIT(3),
269 SETTING_GYRO_MODE_SEND_RAW_GYRO = BIT(4),
274 TRACKPAD_ABSOLUTE_MOUSE,
275 TRACKPAD_RELATIVE_MOUSE,
276 TRACKPAD_DPAD_FOUR_WAY_DISCRETE,
277 TRACKPAD_DPAD_FOUR_WAY_OVERLAP,
278 TRACKPAD_DPAD_EIGHT_WAY,
279 TRACKPAD_RADIAL_MODE,
280 TRACKPAD_ABSOLUTE_DPAD,
282 TRACKPAD_GESTURE_KEYBOARD,
285 /* Pad identifiers for the deck */
286 #define STEAM_PAD_LEFT 0
287 #define STEAM_PAD_RIGHT 1
288 #define STEAM_PAD_BOTH 2
290 /* Other random constants */
291 #define STEAM_SERIAL_LEN 0x15
293 struct steam_device {
294 struct list_head list;
296 struct hid_device *hdev, *client_hdev;
297 struct mutex report_mutex;
298 unsigned long client_opened;
299 struct input_dev __rcu *input;
300 struct input_dev __rcu *sensors;
301 unsigned long quirks;
302 struct work_struct work_connect;
304 char serial_no[STEAM_SERIAL_LEN + 1];
305 struct power_supply_desc battery_desc;
306 struct power_supply __rcu *battery;
309 struct delayed_work mode_switch;
310 bool did_mode_switch;
312 struct work_struct rumble_work;
315 unsigned int sensor_timestamp_us;
318 static int steam_recv_report(struct steam_device *steam,
321 struct hid_report *r;
325 r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0];
327 hid_err(steam->hdev, "No HID_FEATURE_REPORT submitted - nothing to read\n");
331 if (hid_report_len(r) < 64)
334 buf = hid_alloc_report_buf(r, GFP_KERNEL);
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.
344 ret = hid_hw_raw_request(steam->hdev, 0x00,
345 buf, hid_report_len(r) + 1,
346 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
348 memcpy(data, buf + 1, min(size, ret - 1));
353 static int steam_send_report(struct steam_device *steam,
356 struct hid_report *r;
358 unsigned int retries = 50;
361 r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0];
363 hid_err(steam->hdev, "No HID_FEATURE_REPORT submitted - nothing to read\n");
367 if (hid_report_len(r) < 64)
370 buf = hid_alloc_report_buf(r, GFP_KERNEL);
374 /* The report ID is always 0 */
375 memcpy(buf + 1, cmd, size);
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
384 ret = hid_hw_raw_request(steam->hdev, 0,
385 buf, max(size, 64) + 1,
386 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
394 hid_err(steam->hdev, "%s: error %d (%*ph)\n", __func__,
399 static inline int steam_send_report_byte(struct steam_device *steam, u8 cmd)
401 return steam_send_report(steam, &cmd, 1);
404 static int steam_write_settings(struct steam_device *steam,
405 /* u8 reg, u16 val */...)
407 /* Send: 0x87 len (reg valLo valHi)* */
410 u8 cmd[64] = {ID_SET_SETTINGS_VALUES, 0x00};
414 va_start(args, steam);
416 reg = va_arg(args, int);
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;
427 ret = steam_send_report(steam, cmd, 2 + cmd[1]);
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
436 return steam_recv_report(steam, cmd, 2 + cmd[1]);
439 static int steam_get_serial(struct steam_device *steam)
442 * Send: 0xae 0x15 0x01
443 * Recv: 0xae 0x15 0x01 serialnumber
446 u8 cmd[] = {ID_GET_STRING_ATTRIBUTE, sizeof(steam->serial_no), ATTRIB_STR_UNIT_SERIAL};
447 u8 reply[3 + STEAM_SERIAL_LEN + 1];
449 mutex_lock(&steam->report_mutex);
450 ret = steam_send_report(steam, cmd, sizeof(cmd));
453 ret = steam_recv_report(steam, reply, sizeof(reply));
456 if (reply[0] != ID_GET_STRING_ATTRIBUTE || reply[1] < 1 ||
457 reply[1] > sizeof(steam->serial_no) || reply[2] != ATTRIB_STR_UNIT_SERIAL) {
461 reply[3 + STEAM_SERIAL_LEN] = 0;
462 strscpy(steam->serial_no, reply + 3, reply[1]);
464 mutex_unlock(&steam->report_mutex);
469 * This command requests the wireless adaptor to post an event
470 * with the connection status. Useful if this driver is loaded when
471 * the controller is already connected.
473 static inline int steam_request_conn_status(struct steam_device *steam)
476 mutex_lock(&steam->report_mutex);
477 ret = steam_send_report_byte(steam, ID_DONGLE_GET_WIRELESS_STATE);
478 mutex_unlock(&steam->report_mutex);
483 * Send a haptic pulse to the trackpads
484 * Duration and interval are measured in microseconds, count is the number
485 * of pulses to send for duration time with interval microseconds between them
486 * and gain is measured in decibels, ranging from -24 to +6
488 static inline int steam_haptic_pulse(struct steam_device *steam, u8 pad,
489 u16 duration, u16 interval, u16 count, u8 gain)
492 u8 report[10] = {ID_TRIGGER_HAPTIC_PULSE, 8};
494 /* Left and right are swapped on this report for legacy reasons */
495 if (pad < STEAM_PAD_BOTH)
499 report[3] = duration & 0xFF;
500 report[4] = duration >> 8;
501 report[5] = interval & 0xFF;
502 report[6] = interval >> 8;
503 report[7] = count & 0xFF;
504 report[8] = count >> 8;
507 mutex_lock(&steam->report_mutex);
508 ret = steam_send_report(steam, report, sizeof(report));
509 mutex_unlock(&steam->report_mutex);
513 static inline int steam_haptic_rumble(struct steam_device *steam,
514 u16 intensity, u16 left_speed, u16 right_speed,
515 u8 left_gain, u8 right_gain)
518 u8 report[11] = {ID_TRIGGER_RUMBLE_CMD, 9};
520 report[3] = intensity & 0xFF;
521 report[4] = intensity >> 8;
522 report[5] = left_speed & 0xFF;
523 report[6] = left_speed >> 8;
524 report[7] = right_speed & 0xFF;
525 report[8] = right_speed >> 8;
526 report[9] = left_gain;
527 report[10] = right_gain;
529 mutex_lock(&steam->report_mutex);
530 ret = steam_send_report(steam, report, sizeof(report));
531 mutex_unlock(&steam->report_mutex);
535 static void steam_haptic_rumble_cb(struct work_struct *work)
537 struct steam_device *steam = container_of(work, struct steam_device,
539 steam_haptic_rumble(steam, 0, steam->rumble_left,
540 steam->rumble_right, 2, 0);
543 #ifdef CONFIG_STEAM_FF
544 static int steam_play_effect(struct input_dev *dev, void *data,
545 struct ff_effect *effect)
547 struct steam_device *steam = input_get_drvdata(dev);
549 steam->rumble_left = effect->u.rumble.strong_magnitude;
550 steam->rumble_right = effect->u.rumble.weak_magnitude;
552 return schedule_work(&steam->rumble_work);
556 static void steam_set_lizard_mode(struct steam_device *steam, bool enable)
558 if (steam->gamepad_mode)
562 mutex_lock(&steam->report_mutex);
563 /* enable esc, enter, cursors */
564 steam_send_report_byte(steam, ID_SET_DEFAULT_DIGITAL_MAPPINGS);
566 steam_send_report_byte(steam, ID_LOAD_DEFAULT_SETTINGS);
567 mutex_unlock(&steam->report_mutex);
569 mutex_lock(&steam->report_mutex);
570 /* disable esc, enter, cursor */
571 steam_send_report_byte(steam, ID_CLEAR_DIGITAL_MAPPINGS);
573 if (steam->quirks & STEAM_QUIRK_DECK) {
574 steam_write_settings(steam,
575 SETTING_LEFT_TRACKPAD_MODE, TRACKPAD_NONE, /* disable mouse */
576 SETTING_RIGHT_TRACKPAD_MODE, TRACKPAD_NONE, /* disable mouse */
577 SETTING_LEFT_TRACKPAD_CLICK_PRESSURE, 0xFFFF, /* disable haptic click */
578 SETTING_RIGHT_TRACKPAD_CLICK_PRESSURE, 0xFFFF, /* disable haptic click */
579 SETTING_STEAM_WATCHDOG_ENABLE, 0, /* disable watchdog that tests if Steam is active */
581 mutex_unlock(&steam->report_mutex);
583 steam_write_settings(steam,
584 SETTING_LEFT_TRACKPAD_MODE, TRACKPAD_NONE, /* disable mouse */
585 SETTING_RIGHT_TRACKPAD_MODE, TRACKPAD_NONE, /* disable mouse */
587 mutex_unlock(&steam->report_mutex);
592 static int steam_input_open(struct input_dev *dev)
594 struct steam_device *steam = input_get_drvdata(dev);
596 bool set_lizard_mode;
599 * Disabling lizard mode automatically is only done on the Steam
600 * Controller. On the Steam Deck, this is toggled manually by holding
601 * the options button instead, handled by steam_mode_switch_cb.
603 if (!(steam->quirks & STEAM_QUIRK_DECK)) {
604 spin_lock_irqsave(&steam->lock, flags);
605 set_lizard_mode = !steam->client_opened && lizard_mode;
606 spin_unlock_irqrestore(&steam->lock, flags);
608 steam_set_lizard_mode(steam, false);
614 static void steam_input_close(struct input_dev *dev)
616 struct steam_device *steam = input_get_drvdata(dev);
618 bool set_lizard_mode;
620 if (!(steam->quirks & STEAM_QUIRK_DECK)) {
621 spin_lock_irqsave(&steam->lock, flags);
622 set_lizard_mode = !steam->client_opened && lizard_mode;
623 spin_unlock_irqrestore(&steam->lock, flags);
625 steam_set_lizard_mode(steam, true);
629 static enum power_supply_property steam_battery_props[] = {
630 POWER_SUPPLY_PROP_PRESENT,
631 POWER_SUPPLY_PROP_SCOPE,
632 POWER_SUPPLY_PROP_VOLTAGE_NOW,
633 POWER_SUPPLY_PROP_CAPACITY,
636 static int steam_battery_get_property(struct power_supply *psy,
637 enum power_supply_property psp,
638 union power_supply_propval *val)
640 struct steam_device *steam = power_supply_get_drvdata(psy);
646 spin_lock_irqsave(&steam->lock, flags);
647 volts = steam->voltage;
648 batt = steam->battery_charge;
649 spin_unlock_irqrestore(&steam->lock, flags);
652 case POWER_SUPPLY_PROP_PRESENT:
655 case POWER_SUPPLY_PROP_SCOPE:
656 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
658 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
659 val->intval = volts * 1000; /* mV -> uV */
661 case POWER_SUPPLY_PROP_CAPACITY:
671 static int steam_battery_register(struct steam_device *steam)
673 struct power_supply *battery;
674 struct power_supply_config battery_cfg = { .drv_data = steam, };
678 steam->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
679 steam->battery_desc.properties = steam_battery_props;
680 steam->battery_desc.num_properties = ARRAY_SIZE(steam_battery_props);
681 steam->battery_desc.get_property = steam_battery_get_property;
682 steam->battery_desc.name = devm_kasprintf(&steam->hdev->dev,
683 GFP_KERNEL, "steam-controller-%s-battery",
685 if (!steam->battery_desc.name)
688 /* avoid the warning of 0% battery while waiting for the first info */
689 spin_lock_irqsave(&steam->lock, flags);
690 steam->voltage = 3000;
691 steam->battery_charge = 100;
692 spin_unlock_irqrestore(&steam->lock, flags);
694 battery = power_supply_register(&steam->hdev->dev,
695 &steam->battery_desc, &battery_cfg);
696 if (IS_ERR(battery)) {
697 ret = PTR_ERR(battery);
699 "%s:power_supply_register failed with error %d\n",
703 rcu_assign_pointer(steam->battery, battery);
704 power_supply_powers(battery, &steam->hdev->dev);
708 static int steam_input_register(struct steam_device *steam)
710 struct hid_device *hdev = steam->hdev;
711 struct input_dev *input;
715 input = rcu_dereference(steam->input);
718 dbg_hid("%s: already connected\n", __func__);
722 input = input_allocate_device();
726 input_set_drvdata(input, steam);
727 input->dev.parent = &hdev->dev;
728 input->open = steam_input_open;
729 input->close = steam_input_close;
731 input->name = (steam->quirks & STEAM_QUIRK_WIRELESS) ? "Wireless Steam Controller" :
732 (steam->quirks & STEAM_QUIRK_DECK) ? "Steam Deck" :
734 input->phys = hdev->phys;
735 input->uniq = steam->serial_no;
736 input->id.bustype = hdev->bus;
737 input->id.vendor = hdev->vendor;
738 input->id.product = hdev->product;
739 input->id.version = hdev->version;
741 input_set_capability(input, EV_KEY, BTN_TR2);
742 input_set_capability(input, EV_KEY, BTN_TL2);
743 input_set_capability(input, EV_KEY, BTN_TR);
744 input_set_capability(input, EV_KEY, BTN_TL);
745 input_set_capability(input, EV_KEY, BTN_Y);
746 input_set_capability(input, EV_KEY, BTN_B);
747 input_set_capability(input, EV_KEY, BTN_X);
748 input_set_capability(input, EV_KEY, BTN_A);
749 input_set_capability(input, EV_KEY, BTN_DPAD_UP);
750 input_set_capability(input, EV_KEY, BTN_DPAD_RIGHT);
751 input_set_capability(input, EV_KEY, BTN_DPAD_LEFT);
752 input_set_capability(input, EV_KEY, BTN_DPAD_DOWN);
753 input_set_capability(input, EV_KEY, BTN_SELECT);
754 input_set_capability(input, EV_KEY, BTN_MODE);
755 input_set_capability(input, EV_KEY, BTN_START);
756 input_set_capability(input, EV_KEY, BTN_THUMBR);
757 input_set_capability(input, EV_KEY, BTN_THUMBL);
758 input_set_capability(input, EV_KEY, BTN_THUMB);
759 input_set_capability(input, EV_KEY, BTN_THUMB2);
760 if (steam->quirks & STEAM_QUIRK_DECK) {
761 input_set_capability(input, EV_KEY, BTN_BASE);
762 input_set_capability(input, EV_KEY, BTN_TRIGGER_HAPPY1);
763 input_set_capability(input, EV_KEY, BTN_TRIGGER_HAPPY2);
764 input_set_capability(input, EV_KEY, BTN_TRIGGER_HAPPY3);
765 input_set_capability(input, EV_KEY, BTN_TRIGGER_HAPPY4);
767 input_set_capability(input, EV_KEY, BTN_GEAR_DOWN);
768 input_set_capability(input, EV_KEY, BTN_GEAR_UP);
771 input_set_abs_params(input, ABS_X, -32767, 32767, 0, 0);
772 input_set_abs_params(input, ABS_Y, -32767, 32767, 0, 0);
774 input_set_abs_params(input, ABS_HAT0X, -32767, 32767,
776 input_set_abs_params(input, ABS_HAT0Y, -32767, 32767,
779 if (steam->quirks & STEAM_QUIRK_DECK) {
780 input_set_abs_params(input, ABS_HAT2Y, 0, 32767, 0, 0);
781 input_set_abs_params(input, ABS_HAT2X, 0, 32767, 0, 0);
783 input_set_abs_params(input, ABS_RX, -32767, 32767, 0, 0);
784 input_set_abs_params(input, ABS_RY, -32767, 32767, 0, 0);
786 input_set_abs_params(input, ABS_HAT1X, -32767, 32767,
788 input_set_abs_params(input, ABS_HAT1Y, -32767, 32767,
791 input_abs_set_res(input, ABS_X, STEAM_DECK_JOYSTICK_RESOLUTION);
792 input_abs_set_res(input, ABS_Y, STEAM_DECK_JOYSTICK_RESOLUTION);
793 input_abs_set_res(input, ABS_RX, STEAM_DECK_JOYSTICK_RESOLUTION);
794 input_abs_set_res(input, ABS_RY, STEAM_DECK_JOYSTICK_RESOLUTION);
795 input_abs_set_res(input, ABS_HAT1X, STEAM_PAD_RESOLUTION);
796 input_abs_set_res(input, ABS_HAT1Y, STEAM_PAD_RESOLUTION);
797 input_abs_set_res(input, ABS_HAT2Y, STEAM_DECK_TRIGGER_RESOLUTION);
798 input_abs_set_res(input, ABS_HAT2X, STEAM_DECK_TRIGGER_RESOLUTION);
800 input_set_abs_params(input, ABS_HAT2Y, 0, 255, 0, 0);
801 input_set_abs_params(input, ABS_HAT2X, 0, 255, 0, 0);
803 input_set_abs_params(input, ABS_RX, -32767, 32767,
805 input_set_abs_params(input, ABS_RY, -32767, 32767,
808 input_abs_set_res(input, ABS_X, STEAM_JOYSTICK_RESOLUTION);
809 input_abs_set_res(input, ABS_Y, STEAM_JOYSTICK_RESOLUTION);
810 input_abs_set_res(input, ABS_RX, STEAM_PAD_RESOLUTION);
811 input_abs_set_res(input, ABS_RY, STEAM_PAD_RESOLUTION);
812 input_abs_set_res(input, ABS_HAT2Y, STEAM_TRIGGER_RESOLUTION);
813 input_abs_set_res(input, ABS_HAT2X, STEAM_TRIGGER_RESOLUTION);
815 input_abs_set_res(input, ABS_HAT0X, STEAM_PAD_RESOLUTION);
816 input_abs_set_res(input, ABS_HAT0Y, STEAM_PAD_RESOLUTION);
818 #ifdef CONFIG_STEAM_FF
819 if (steam->quirks & STEAM_QUIRK_DECK) {
820 input_set_capability(input, EV_FF, FF_RUMBLE);
821 ret = input_ff_create_memless(input, NULL, steam_play_effect);
823 goto input_register_fail;
827 ret = input_register_device(input);
829 goto input_register_fail;
831 rcu_assign_pointer(steam->input, input);
835 input_free_device(input);
839 static int steam_sensors_register(struct steam_device *steam)
841 struct hid_device *hdev = steam->hdev;
842 struct input_dev *sensors;
845 if (!(steam->quirks & STEAM_QUIRK_DECK))
849 sensors = rcu_dereference(steam->sensors);
852 dbg_hid("%s: already connected\n", __func__);
856 sensors = input_allocate_device();
860 input_set_drvdata(sensors, steam);
861 sensors->dev.parent = &hdev->dev;
863 sensors->name = "Steam Deck Motion Sensors";
864 sensors->phys = hdev->phys;
865 sensors->uniq = steam->serial_no;
866 sensors->id.bustype = hdev->bus;
867 sensors->id.vendor = hdev->vendor;
868 sensors->id.product = hdev->product;
869 sensors->id.version = hdev->version;
871 __set_bit(INPUT_PROP_ACCELEROMETER, sensors->propbit);
872 __set_bit(EV_MSC, sensors->evbit);
873 __set_bit(MSC_TIMESTAMP, sensors->mscbit);
875 input_set_abs_params(sensors, ABS_X, -STEAM_DECK_ACCEL_RANGE,
876 STEAM_DECK_ACCEL_RANGE, STEAM_DECK_ACCEL_FUZZ, 0);
877 input_set_abs_params(sensors, ABS_Y, -STEAM_DECK_ACCEL_RANGE,
878 STEAM_DECK_ACCEL_RANGE, STEAM_DECK_ACCEL_FUZZ, 0);
879 input_set_abs_params(sensors, ABS_Z, -STEAM_DECK_ACCEL_RANGE,
880 STEAM_DECK_ACCEL_RANGE, STEAM_DECK_ACCEL_FUZZ, 0);
881 input_abs_set_res(sensors, ABS_X, STEAM_DECK_ACCEL_RES_PER_G);
882 input_abs_set_res(sensors, ABS_Y, STEAM_DECK_ACCEL_RES_PER_G);
883 input_abs_set_res(sensors, ABS_Z, STEAM_DECK_ACCEL_RES_PER_G);
885 input_set_abs_params(sensors, ABS_RX, -STEAM_DECK_GYRO_RANGE,
886 STEAM_DECK_GYRO_RANGE, STEAM_DECK_GYRO_FUZZ, 0);
887 input_set_abs_params(sensors, ABS_RY, -STEAM_DECK_GYRO_RANGE,
888 STEAM_DECK_GYRO_RANGE, STEAM_DECK_GYRO_FUZZ, 0);
889 input_set_abs_params(sensors, ABS_RZ, -STEAM_DECK_GYRO_RANGE,
890 STEAM_DECK_GYRO_RANGE, STEAM_DECK_GYRO_FUZZ, 0);
891 input_abs_set_res(sensors, ABS_RX, STEAM_DECK_GYRO_RES_PER_DPS);
892 input_abs_set_res(sensors, ABS_RY, STEAM_DECK_GYRO_RES_PER_DPS);
893 input_abs_set_res(sensors, ABS_RZ, STEAM_DECK_GYRO_RES_PER_DPS);
895 ret = input_register_device(sensors);
897 goto sensors_register_fail;
899 rcu_assign_pointer(steam->sensors, sensors);
902 sensors_register_fail:
903 input_free_device(sensors);
907 static void steam_input_unregister(struct steam_device *steam)
909 struct input_dev *input;
911 input = rcu_dereference(steam->input);
915 RCU_INIT_POINTER(steam->input, NULL);
917 input_unregister_device(input);
920 static void steam_sensors_unregister(struct steam_device *steam)
922 struct input_dev *sensors;
924 if (!(steam->quirks & STEAM_QUIRK_DECK))
928 sensors = rcu_dereference(steam->sensors);
933 RCU_INIT_POINTER(steam->sensors, NULL);
935 input_unregister_device(sensors);
938 static void steam_battery_unregister(struct steam_device *steam)
940 struct power_supply *battery;
943 battery = rcu_dereference(steam->battery);
948 RCU_INIT_POINTER(steam->battery, NULL);
950 power_supply_unregister(battery);
953 static int steam_register(struct steam_device *steam)
956 unsigned long client_opened;
960 * This function can be called several times in a row with the
961 * wireless adaptor, without steam_unregister() between them, because
962 * another client send a get_connection_status command, for example.
963 * The battery and serial number are set just once per device.
965 if (!steam->serial_no[0]) {
967 * Unlikely, but getting the serial could fail, and it is not so
968 * important, so make up a serial number and go on.
970 if (steam_get_serial(steam) < 0)
971 strscpy(steam->serial_no, "XXXXXXXXXX",
972 sizeof(steam->serial_no));
974 hid_info(steam->hdev, "Steam Controller '%s' connected",
977 /* ignore battery errors, we can live without it */
978 if (steam->quirks & STEAM_QUIRK_WIRELESS)
979 steam_battery_register(steam);
981 mutex_lock(&steam_devices_lock);
982 if (list_empty(&steam->list))
983 list_add(&steam->list, &steam_devices);
984 mutex_unlock(&steam_devices_lock);
987 spin_lock_irqsave(&steam->lock, flags);
988 client_opened = steam->client_opened;
989 spin_unlock_irqrestore(&steam->lock, flags);
991 if (!client_opened) {
992 steam_set_lizard_mode(steam, lizard_mode);
993 ret = steam_input_register(steam);
995 goto steam_register_input_fail;
996 ret = steam_sensors_register(steam);
998 goto steam_register_sensors_fail;
1002 steam_register_sensors_fail:
1003 steam_input_unregister(steam);
1004 steam_register_input_fail:
1008 static void steam_unregister(struct steam_device *steam)
1010 steam_battery_unregister(steam);
1011 steam_sensors_unregister(steam);
1012 steam_input_unregister(steam);
1013 if (steam->serial_no[0]) {
1014 hid_info(steam->hdev, "Steam Controller '%s' disconnected",
1016 mutex_lock(&steam_devices_lock);
1017 list_del_init(&steam->list);
1018 mutex_unlock(&steam_devices_lock);
1019 steam->serial_no[0] = 0;
1023 static void steam_work_connect_cb(struct work_struct *work)
1025 struct steam_device *steam = container_of(work, struct steam_device,
1027 unsigned long flags;
1031 spin_lock_irqsave(&steam->lock, flags);
1032 connected = steam->connected;
1033 spin_unlock_irqrestore(&steam->lock, flags);
1036 ret = steam_register(steam);
1038 hid_err(steam->hdev,
1039 "%s:steam_register failed with error %d\n",
1043 steam_unregister(steam);
1047 static void steam_mode_switch_cb(struct work_struct *work)
1049 struct steam_device *steam = container_of(to_delayed_work(work),
1050 struct steam_device, mode_switch);
1051 unsigned long flags;
1053 steam->gamepad_mode = !steam->gamepad_mode;
1057 if (steam->gamepad_mode)
1058 steam_set_lizard_mode(steam, false);
1060 spin_lock_irqsave(&steam->lock, flags);
1061 client_opened = steam->client_opened;
1062 spin_unlock_irqrestore(&steam->lock, flags);
1064 steam_set_lizard_mode(steam, lizard_mode);
1067 steam_haptic_pulse(steam, STEAM_PAD_RIGHT, 0x190, 0, 1, 0);
1068 if (steam->gamepad_mode) {
1069 steam_haptic_pulse(steam, STEAM_PAD_LEFT, 0x14D, 0x14D, 0x2D, 0);
1071 steam_haptic_pulse(steam, STEAM_PAD_LEFT, 0x1F4, 0x1F4, 0x1E, 0);
1075 static bool steam_is_valve_interface(struct hid_device *hdev)
1077 struct hid_report_enum *rep_enum;
1080 * The wired device creates 3 interfaces:
1081 * 0: emulated mouse.
1082 * 1: emulated keyboard.
1083 * 2: the real game pad.
1084 * The wireless device creates 5 interfaces:
1085 * 0: emulated keyboard.
1086 * 1-4: slots where up to 4 real game pads will be connected to.
1087 * We know which one is the real gamepad interface because they are the
1088 * only ones with a feature report.
1090 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
1091 return !list_empty(&rep_enum->report_list);
1094 static int steam_client_ll_parse(struct hid_device *hdev)
1096 struct steam_device *steam = hdev->driver_data;
1098 return hid_parse_report(hdev, steam->hdev->dev_rdesc,
1099 steam->hdev->dev_rsize);
1102 static int steam_client_ll_start(struct hid_device *hdev)
1107 static void steam_client_ll_stop(struct hid_device *hdev)
1111 static int steam_client_ll_open(struct hid_device *hdev)
1113 struct steam_device *steam = hdev->driver_data;
1114 unsigned long flags;
1116 spin_lock_irqsave(&steam->lock, flags);
1117 steam->client_opened++;
1118 spin_unlock_irqrestore(&steam->lock, flags);
1120 steam_sensors_unregister(steam);
1121 steam_input_unregister(steam);
1126 static void steam_client_ll_close(struct hid_device *hdev)
1128 struct steam_device *steam = hdev->driver_data;
1130 unsigned long flags;
1133 spin_lock_irqsave(&steam->lock, flags);
1134 steam->client_opened--;
1135 connected = steam->connected && !steam->client_opened;
1136 spin_unlock_irqrestore(&steam->lock, flags);
1139 steam_set_lizard_mode(steam, lizard_mode);
1140 steam_input_register(steam);
1141 steam_sensors_register(steam);
1145 static int steam_client_ll_raw_request(struct hid_device *hdev,
1146 unsigned char reportnum, u8 *buf,
1147 size_t count, unsigned char report_type,
1150 struct steam_device *steam = hdev->driver_data;
1152 return hid_hw_raw_request(steam->hdev, reportnum, buf, count,
1153 report_type, reqtype);
1156 static const struct hid_ll_driver steam_client_ll_driver = {
1157 .parse = steam_client_ll_parse,
1158 .start = steam_client_ll_start,
1159 .stop = steam_client_ll_stop,
1160 .open = steam_client_ll_open,
1161 .close = steam_client_ll_close,
1162 .raw_request = steam_client_ll_raw_request,
1165 static struct hid_device *steam_create_client_hid(struct hid_device *hdev)
1167 struct hid_device *client_hdev;
1169 client_hdev = hid_allocate_device();
1170 if (IS_ERR(client_hdev))
1173 client_hdev->ll_driver = &steam_client_ll_driver;
1174 client_hdev->dev.parent = hdev->dev.parent;
1175 client_hdev->bus = hdev->bus;
1176 client_hdev->vendor = hdev->vendor;
1177 client_hdev->product = hdev->product;
1178 client_hdev->version = hdev->version;
1179 client_hdev->type = hdev->type;
1180 client_hdev->country = hdev->country;
1181 strscpy(client_hdev->name, hdev->name,
1182 sizeof(client_hdev->name));
1183 strscpy(client_hdev->phys, hdev->phys,
1184 sizeof(client_hdev->phys));
1186 * Since we use the same device info than the real interface to
1187 * trick userspace, we will be calling steam_probe recursively.
1188 * We need to recognize the client interface somehow.
1190 client_hdev->group = HID_GROUP_STEAM;
1194 static int steam_probe(struct hid_device *hdev,
1195 const struct hid_device_id *id)
1197 struct steam_device *steam;
1200 ret = hid_parse(hdev);
1203 "%s:parse of hid interface failed\n", __func__);
1208 * The virtual client_dev is only used for hidraw.
1209 * Also avoid the recursive probe.
1211 if (hdev->group == HID_GROUP_STEAM)
1212 return hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1214 * The non-valve interfaces (mouse and keyboard emulation) are
1215 * connected without changes.
1217 if (!steam_is_valve_interface(hdev))
1218 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1220 steam = devm_kzalloc(&hdev->dev, sizeof(*steam), GFP_KERNEL);
1225 hid_set_drvdata(hdev, steam);
1226 spin_lock_init(&steam->lock);
1227 mutex_init(&steam->report_mutex);
1228 steam->quirks = id->driver_data;
1229 INIT_WORK(&steam->work_connect, steam_work_connect_cb);
1230 INIT_DELAYED_WORK(&steam->mode_switch, steam_mode_switch_cb);
1231 INIT_LIST_HEAD(&steam->list);
1232 INIT_WORK(&steam->rumble_work, steam_haptic_rumble_cb);
1233 steam->sensor_timestamp_us = 0;
1236 * With the real steam controller interface, do not connect hidraw.
1237 * Instead, create the client_hid and connect that.
1239 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_HIDRAW);
1241 goto err_cancel_work;
1243 ret = hid_hw_open(hdev);
1251 if (steam->quirks & STEAM_QUIRK_WIRELESS) {
1252 hid_info(hdev, "Steam wireless receiver connected");
1253 /* If using a wireless adaptor ask for connection status */
1254 steam->connected = false;
1255 steam_request_conn_status(steam);
1257 /* A wired connection is always present */
1258 steam->connected = true;
1259 ret = steam_register(steam);
1262 "%s:steam_register failed with error %d\n",
1268 steam->client_hdev = steam_create_client_hid(hdev);
1269 if (IS_ERR(steam->client_hdev)) {
1270 ret = PTR_ERR(steam->client_hdev);
1271 goto err_steam_unregister;
1273 steam->client_hdev->driver_data = steam;
1275 ret = hid_add_device(steam->client_hdev);
1282 hid_destroy_device(steam->client_hdev);
1283 err_steam_unregister:
1284 if (steam->connected)
1285 steam_unregister(steam);
1291 cancel_work_sync(&steam->work_connect);
1292 cancel_delayed_work_sync(&steam->mode_switch);
1293 cancel_work_sync(&steam->rumble_work);
1298 static void steam_remove(struct hid_device *hdev)
1300 struct steam_device *steam = hid_get_drvdata(hdev);
1302 if (!steam || hdev->group == HID_GROUP_STEAM) {
1307 cancel_delayed_work_sync(&steam->mode_switch);
1308 cancel_work_sync(&steam->work_connect);
1309 hid_destroy_device(steam->client_hdev);
1310 steam->client_hdev = NULL;
1311 steam->client_opened = 0;
1312 if (steam->quirks & STEAM_QUIRK_WIRELESS) {
1313 hid_info(hdev, "Steam wireless receiver disconnected");
1317 steam_unregister(steam);
1320 static void steam_do_connect_event(struct steam_device *steam, bool connected)
1322 unsigned long flags;
1325 spin_lock_irqsave(&steam->lock, flags);
1326 changed = steam->connected != connected;
1327 steam->connected = connected;
1328 spin_unlock_irqrestore(&steam->lock, flags);
1330 if (changed && schedule_work(&steam->work_connect) == 0)
1331 dbg_hid("%s: connected=%d event already queued\n",
1332 __func__, connected);
1336 * Some input data in the protocol has the opposite sign.
1337 * Clamp the values to 32767..-32767 so that the range is
1338 * symmetrical and can be negated safely.
1340 static inline s16 steam_le16(u8 *data)
1342 s16 x = (s16) le16_to_cpup((__le16 *)data);
1344 return x == -32768 ? -32767 : x;
1348 * The size for this message payload is 60.
1349 * The known values are:
1350 * (* values are not sent through wireless)
1351 * (* accelerator/gyro is disabled by default)
1352 * Offset| Type | Mapped to |Meaning
1353 * -------+-------+-----------+--------------------------
1354 * 4-7 | u32 | -- | sequence number
1355 * 8-10 | 24bit | see below | buttons
1356 * 11 | u8 | ABS_HAT2Y | left trigger
1357 * 12 | u8 | ABS_HAT2X | right trigger
1358 * 13-15 | -- | -- | always 0
1359 * 16-17 | s16 | ABS_X/ABS_HAT0X | X value
1360 * 18-19 | s16 | ABS_Y/ABS_HAT0Y | Y value
1361 * 20-21 | s16 | ABS_RX | right-pad X value
1362 * 22-23 | s16 | ABS_RY | right-pad Y value
1363 * 24-25 | s16 | -- | * left trigger
1364 * 26-27 | s16 | -- | * right trigger
1365 * 28-29 | s16 | -- | * accelerometer X value
1366 * 30-31 | s16 | -- | * accelerometer Y value
1367 * 32-33 | s16 | -- | * accelerometer Z value
1368 * 34-35 | s16 | -- | gyro X value
1369 * 36-36 | s16 | -- | gyro Y value
1370 * 38-39 | s16 | -- | gyro Z value
1371 * 40-41 | s16 | -- | quaternion W value
1372 * 42-43 | s16 | -- | quaternion X value
1373 * 44-45 | s16 | -- | quaternion Y value
1374 * 46-47 | s16 | -- | quaternion Z value
1375 * 48-49 | -- | -- | always 0
1376 * 50-51 | s16 | -- | * left trigger (uncalibrated)
1377 * 52-53 | s16 | -- | * right trigger (uncalibrated)
1378 * 54-55 | s16 | -- | * joystick X value (uncalibrated)
1379 * 56-57 | s16 | -- | * joystick Y value (uncalibrated)
1380 * 58-59 | s16 | -- | * left-pad X value
1381 * 60-61 | s16 | -- | * left-pad Y value
1382 * 62-63 | u16 | -- | * battery voltage
1385 * Bit | Mapped to | Description
1386 * ------+------------+--------------------------------
1387 * 8.0 | BTN_TR2 | right trigger fully pressed
1388 * 8.1 | BTN_TL2 | left trigger fully pressed
1389 * 8.2 | BTN_TR | right shoulder
1390 * 8.3 | BTN_TL | left shoulder
1391 * 8.4 | BTN_Y | button Y
1392 * 8.5 | BTN_B | button B
1393 * 8.6 | BTN_X | button X
1394 * 8.7 | BTN_A | button A
1395 * 9.0 | BTN_DPAD_UP | left-pad up
1396 * 9.1 | BTN_DPAD_RIGHT | left-pad right
1397 * 9.2 | BTN_DPAD_LEFT | left-pad left
1398 * 9.3 | BTN_DPAD_DOWN | left-pad down
1399 * 9.4 | BTN_SELECT | menu left
1400 * 9.5 | BTN_MODE | steam logo
1401 * 9.6 | BTN_START | menu right
1402 * 9.7 | BTN_GEAR_DOWN | left back lever
1403 * 10.0 | BTN_GEAR_UP | right back lever
1404 * 10.1 | -- | left-pad clicked
1405 * 10.2 | BTN_THUMBR | right-pad clicked
1406 * 10.3 | BTN_THUMB | left-pad touched (but see explanation below)
1407 * 10.4 | BTN_THUMB2 | right-pad touched
1408 * 10.5 | -- | unknown
1409 * 10.6 | BTN_THUMBL | joystick clicked
1410 * 10.7 | -- | lpad_and_joy
1413 static void steam_do_input_event(struct steam_device *steam,
1414 struct input_dev *input, u8 *data)
1416 /* 24 bits of buttons */
1419 bool lpad_touched, lpad_and_joy;
1425 input_report_abs(input, ABS_HAT2Y, data[11]);
1426 input_report_abs(input, ABS_HAT2X, data[12]);
1429 * These two bits tells how to interpret the values X and Y.
1430 * lpad_and_joy tells that the joystick and the lpad are used at the
1432 * lpad_touched tells whether X/Y are to be read as lpad coord or
1434 * (lpad_touched || lpad_and_joy) tells if the lpad is really touched.
1436 lpad_touched = b10 & BIT(3);
1437 lpad_and_joy = b10 & BIT(7);
1438 x = steam_le16(data + 16);
1439 y = -steam_le16(data + 18);
1441 input_report_abs(input, lpad_touched ? ABS_HAT0X : ABS_X, x);
1442 input_report_abs(input, lpad_touched ? ABS_HAT0Y : ABS_Y, y);
1443 /* Check if joystick is centered */
1444 if (lpad_touched && !lpad_and_joy) {
1445 input_report_abs(input, ABS_X, 0);
1446 input_report_abs(input, ABS_Y, 0);
1448 /* Check if lpad is untouched */
1449 if (!(lpad_touched || lpad_and_joy)) {
1450 input_report_abs(input, ABS_HAT0X, 0);
1451 input_report_abs(input, ABS_HAT0Y, 0);
1454 input_report_abs(input, ABS_RX, steam_le16(data + 20));
1455 input_report_abs(input, ABS_RY, -steam_le16(data + 22));
1457 input_event(input, EV_KEY, BTN_TR2, !!(b8 & BIT(0)));
1458 input_event(input, EV_KEY, BTN_TL2, !!(b8 & BIT(1)));
1459 input_event(input, EV_KEY, BTN_TR, !!(b8 & BIT(2)));
1460 input_event(input, EV_KEY, BTN_TL, !!(b8 & BIT(3)));
1461 input_event(input, EV_KEY, BTN_Y, !!(b8 & BIT(4)));
1462 input_event(input, EV_KEY, BTN_B, !!(b8 & BIT(5)));
1463 input_event(input, EV_KEY, BTN_X, !!(b8 & BIT(6)));
1464 input_event(input, EV_KEY, BTN_A, !!(b8 & BIT(7)));
1465 input_event(input, EV_KEY, BTN_SELECT, !!(b9 & BIT(4)));
1466 input_event(input, EV_KEY, BTN_MODE, !!(b9 & BIT(5)));
1467 input_event(input, EV_KEY, BTN_START, !!(b9 & BIT(6)));
1468 input_event(input, EV_KEY, BTN_GEAR_DOWN, !!(b9 & BIT(7)));
1469 input_event(input, EV_KEY, BTN_GEAR_UP, !!(b10 & BIT(0)));
1470 input_event(input, EV_KEY, BTN_THUMBR, !!(b10 & BIT(2)));
1471 input_event(input, EV_KEY, BTN_THUMBL, !!(b10 & BIT(6)));
1472 input_event(input, EV_KEY, BTN_THUMB, lpad_touched || lpad_and_joy);
1473 input_event(input, EV_KEY, BTN_THUMB2, !!(b10 & BIT(4)));
1474 input_event(input, EV_KEY, BTN_DPAD_UP, !!(b9 & BIT(0)));
1475 input_event(input, EV_KEY, BTN_DPAD_RIGHT, !!(b9 & BIT(1)));
1476 input_event(input, EV_KEY, BTN_DPAD_LEFT, !!(b9 & BIT(2)));
1477 input_event(input, EV_KEY, BTN_DPAD_DOWN, !!(b9 & BIT(3)));
1483 * The size for this message payload is 56.
1484 * The known values are:
1485 * Offset| Type | Mapped to |Meaning
1486 * -------+-------+-----------+--------------------------
1487 * 4-7 | u32 | -- | sequence number
1488 * 8-15 | u64 | see below | buttons
1489 * 16-17 | s16 | ABS_HAT0X | left-pad X value
1490 * 18-19 | s16 | ABS_HAT0Y | left-pad Y value
1491 * 20-21 | s16 | ABS_HAT1X | right-pad X value
1492 * 22-23 | s16 | ABS_HAT1Y | right-pad Y value
1493 * 24-25 | s16 | IMU ABS_X | accelerometer X value
1494 * 26-27 | s16 | IMU ABS_Z | accelerometer Y value
1495 * 28-29 | s16 | IMU ABS_Y | accelerometer Z value
1496 * 30-31 | s16 | IMU ABS_RX | gyro X value
1497 * 32-33 | s16 | IMU ABS_RZ | gyro Y value
1498 * 34-35 | s16 | IMU ABS_RY | gyro Z value
1499 * 36-37 | s16 | -- | quaternion W value
1500 * 38-39 | s16 | -- | quaternion X value
1501 * 40-41 | s16 | -- | quaternion Y value
1502 * 42-43 | s16 | -- | quaternion Z value
1503 * 44-45 | u16 | ABS_HAT2Y | left trigger (uncalibrated)
1504 * 46-47 | u16 | ABS_HAT2X | right trigger (uncalibrated)
1505 * 48-49 | s16 | ABS_X | left joystick X
1506 * 50-51 | s16 | ABS_Y | left joystick Y
1507 * 52-53 | s16 | ABS_RX | right joystick X
1508 * 54-55 | s16 | ABS_RY | right joystick Y
1509 * 56-57 | u16 | -- | left pad pressure
1510 * 58-59 | u16 | -- | right pad pressure
1513 * Bit | Mapped to | Description
1514 * ------+------------+--------------------------------
1515 * 8.0 | BTN_TR2 | right trigger fully pressed
1516 * 8.1 | BTN_TL2 | left trigger fully pressed
1517 * 8.2 | BTN_TR | right shoulder
1518 * 8.3 | BTN_TL | left shoulder
1519 * 8.4 | BTN_Y | button Y
1520 * 8.5 | BTN_B | button B
1521 * 8.6 | BTN_X | button X
1522 * 8.7 | BTN_A | button A
1523 * 9.0 | BTN_DPAD_UP | left-pad up
1524 * 9.1 | BTN_DPAD_RIGHT | left-pad right
1525 * 9.2 | BTN_DPAD_LEFT | left-pad left
1526 * 9.3 | BTN_DPAD_DOWN | left-pad down
1527 * 9.4 | BTN_SELECT | menu left
1528 * 9.5 | BTN_MODE | steam logo
1529 * 9.6 | BTN_START | menu right
1530 * 9.7 | BTN_TRIGGER_HAPPY3 | left bottom grip button
1531 * 10.0 | BTN_TRIGGER_HAPPY4 | right bottom grip button
1532 * 10.1 | BTN_THUMB | left pad pressed
1533 * 10.2 | BTN_THUMB2 | right pad pressed
1534 * 10.3 | -- | left pad touched
1535 * 10.4 | -- | right pad touched
1536 * 10.5 | -- | unknown
1537 * 10.6 | BTN_THUMBL | left joystick clicked
1538 * 10.7 | -- | unknown
1539 * 11.0 | -- | unknown
1540 * 11.1 | -- | unknown
1541 * 11.2 | BTN_THUMBR | right joystick clicked
1542 * 11.3 | -- | unknown
1543 * 11.4 | -- | unknown
1544 * 11.5 | -- | unknown
1545 * 11.6 | -- | unknown
1546 * 11.7 | -- | unknown
1547 * 12.0 | -- | unknown
1548 * 12.1 | -- | unknown
1549 * 12.2 | -- | unknown
1550 * 12.3 | -- | unknown
1551 * 12.4 | -- | unknown
1552 * 12.5 | -- | unknown
1553 * 12.6 | -- | unknown
1554 * 12.7 | -- | unknown
1555 * 13.0 | -- | unknown
1556 * 13.1 | BTN_TRIGGER_HAPPY1 | left top grip button
1557 * 13.2 | BTN_TRIGGER_HAPPY2 | right top grip button
1558 * 13.3 | -- | unknown
1559 * 13.4 | -- | unknown
1560 * 13.5 | -- | unknown
1561 * 13.6 | -- | left joystick touched
1562 * 13.7 | -- | right joystick touched
1563 * 14.0 | -- | unknown
1564 * 14.1 | -- | unknown
1565 * 14.2 | BTN_BASE | quick access button
1566 * 14.3 | -- | unknown
1567 * 14.4 | -- | unknown
1568 * 14.5 | -- | unknown
1569 * 14.6 | -- | unknown
1570 * 14.7 | -- | unknown
1571 * 15.0 | -- | unknown
1572 * 15.1 | -- | unknown
1573 * 15.2 | -- | unknown
1574 * 15.3 | -- | unknown
1575 * 15.4 | -- | unknown
1576 * 15.5 | -- | unknown
1577 * 15.6 | -- | unknown
1578 * 15.7 | -- | unknown
1580 static void steam_do_deck_input_event(struct steam_device *steam,
1581 struct input_dev *input, u8 *data)
1583 u8 b8, b9, b10, b11, b13, b14;
1584 bool lpad_touched, rpad_touched;
1593 if (!(b9 & BIT(6)) && steam->did_mode_switch) {
1594 steam->did_mode_switch = false;
1595 cancel_delayed_work_sync(&steam->mode_switch);
1596 } else if (!steam->client_opened && (b9 & BIT(6)) && !steam->did_mode_switch) {
1597 steam->did_mode_switch = true;
1598 schedule_delayed_work(&steam->mode_switch, 45 * HZ / 100);
1601 if (!steam->gamepad_mode)
1604 lpad_touched = b10 & BIT(3);
1605 rpad_touched = b10 & BIT(4);
1608 input_report_abs(input, ABS_HAT0X, steam_le16(data + 16));
1609 input_report_abs(input, ABS_HAT0Y, steam_le16(data + 18));
1611 input_report_abs(input, ABS_HAT0X, 0);
1612 input_report_abs(input, ABS_HAT0Y, 0);
1616 input_report_abs(input, ABS_HAT1X, steam_le16(data + 20));
1617 input_report_abs(input, ABS_HAT1Y, steam_le16(data + 22));
1619 input_report_abs(input, ABS_HAT1X, 0);
1620 input_report_abs(input, ABS_HAT1Y, 0);
1623 input_report_abs(input, ABS_X, steam_le16(data + 48));
1624 input_report_abs(input, ABS_Y, -steam_le16(data + 50));
1625 input_report_abs(input, ABS_RX, steam_le16(data + 52));
1626 input_report_abs(input, ABS_RY, -steam_le16(data + 54));
1628 input_report_abs(input, ABS_HAT2Y, steam_le16(data + 44));
1629 input_report_abs(input, ABS_HAT2X, steam_le16(data + 46));
1631 input_event(input, EV_KEY, BTN_TR2, !!(b8 & BIT(0)));
1632 input_event(input, EV_KEY, BTN_TL2, !!(b8 & BIT(1)));
1633 input_event(input, EV_KEY, BTN_TR, !!(b8 & BIT(2)));
1634 input_event(input, EV_KEY, BTN_TL, !!(b8 & BIT(3)));
1635 input_event(input, EV_KEY, BTN_Y, !!(b8 & BIT(4)));
1636 input_event(input, EV_KEY, BTN_B, !!(b8 & BIT(5)));
1637 input_event(input, EV_KEY, BTN_X, !!(b8 & BIT(6)));
1638 input_event(input, EV_KEY, BTN_A, !!(b8 & BIT(7)));
1639 input_event(input, EV_KEY, BTN_SELECT, !!(b9 & BIT(4)));
1640 input_event(input, EV_KEY, BTN_MODE, !!(b9 & BIT(5)));
1641 input_event(input, EV_KEY, BTN_START, !!(b9 & BIT(6)));
1642 input_event(input, EV_KEY, BTN_TRIGGER_HAPPY3, !!(b9 & BIT(7)));
1643 input_event(input, EV_KEY, BTN_TRIGGER_HAPPY4, !!(b10 & BIT(0)));
1644 input_event(input, EV_KEY, BTN_THUMBL, !!(b10 & BIT(6)));
1645 input_event(input, EV_KEY, BTN_THUMBR, !!(b11 & BIT(2)));
1646 input_event(input, EV_KEY, BTN_DPAD_UP, !!(b9 & BIT(0)));
1647 input_event(input, EV_KEY, BTN_DPAD_RIGHT, !!(b9 & BIT(1)));
1648 input_event(input, EV_KEY, BTN_DPAD_LEFT, !!(b9 & BIT(2)));
1649 input_event(input, EV_KEY, BTN_DPAD_DOWN, !!(b9 & BIT(3)));
1650 input_event(input, EV_KEY, BTN_THUMB, !!(b10 & BIT(1)));
1651 input_event(input, EV_KEY, BTN_THUMB2, !!(b10 & BIT(2)));
1652 input_event(input, EV_KEY, BTN_TRIGGER_HAPPY1, !!(b13 & BIT(1)));
1653 input_event(input, EV_KEY, BTN_TRIGGER_HAPPY2, !!(b13 & BIT(2)));
1654 input_event(input, EV_KEY, BTN_BASE, !!(b14 & BIT(2)));
1659 static void steam_do_deck_sensors_event(struct steam_device *steam,
1660 struct input_dev *sensors, u8 *data)
1663 * The deck input report is received every 4 ms on average,
1664 * with a jitter of +/- 4 ms even though the USB descriptor claims
1665 * that it uses 1 kHz.
1666 * Since the HID report does not include a sensor timestamp,
1667 * use a fixed increment here.
1669 steam->sensor_timestamp_us += 4000;
1671 if (!steam->gamepad_mode)
1674 input_event(sensors, EV_MSC, MSC_TIMESTAMP, steam->sensor_timestamp_us);
1675 input_report_abs(sensors, ABS_X, steam_le16(data + 24));
1676 input_report_abs(sensors, ABS_Z, -steam_le16(data + 26));
1677 input_report_abs(sensors, ABS_Y, steam_le16(data + 28));
1678 input_report_abs(sensors, ABS_RX, steam_le16(data + 30));
1679 input_report_abs(sensors, ABS_RZ, -steam_le16(data + 32));
1680 input_report_abs(sensors, ABS_RY, steam_le16(data + 34));
1682 input_sync(sensors);
1686 * The size for this message payload is 11.
1687 * The known values are:
1688 * Offset| Type | Meaning
1689 * -------+-------+---------------------------
1690 * 4-7 | u32 | sequence number
1691 * 8-11 | -- | always 0
1692 * 12-13 | u16 | voltage (mV)
1693 * 14 | u8 | battery percent
1695 static void steam_do_battery_event(struct steam_device *steam,
1696 struct power_supply *battery, u8 *data)
1698 unsigned long flags;
1700 s16 volts = steam_le16(data + 12);
1703 /* Creating the battery may have failed */
1705 battery = rcu_dereference(steam->battery);
1706 if (likely(battery)) {
1707 spin_lock_irqsave(&steam->lock, flags);
1708 steam->voltage = volts;
1709 steam->battery_charge = batt;
1710 spin_unlock_irqrestore(&steam->lock, flags);
1711 power_supply_changed(battery);
1716 static int steam_raw_event(struct hid_device *hdev,
1717 struct hid_report *report, u8 *data,
1720 struct steam_device *steam = hid_get_drvdata(hdev);
1721 struct input_dev *input;
1722 struct input_dev *sensors;
1723 struct power_supply *battery;
1728 if (steam->client_opened)
1729 hid_input_report(steam->client_hdev, HID_FEATURE_REPORT,
1732 * All messages are size=64, all values little-endian.
1735 * -------+--------------------------------------------
1736 * 0-1 | always 0x01, 0x00, maybe protocol version?
1737 * 2 | type of message
1738 * 3 | length of the real payload (not checked)
1739 * 4-n | payload data, depends on the type
1741 * There are these known types of message:
1742 * 0x01: input data (60 bytes)
1743 * 0x03: wireless connect/disconnect (1 byte)
1744 * 0x04: battery status (11 bytes)
1745 * 0x09: Steam Deck input data (56 bytes)
1748 if (size != 64 || data[0] != 1 || data[1] != 0)
1752 case ID_CONTROLLER_STATE:
1753 if (steam->client_opened)
1756 input = rcu_dereference(steam->input);
1758 steam_do_input_event(steam, input, data);
1761 case ID_CONTROLLER_DECK_STATE:
1762 if (steam->client_opened)
1765 input = rcu_dereference(steam->input);
1767 steam_do_deck_input_event(steam, input, data);
1768 sensors = rcu_dereference(steam->sensors);
1769 if (likely(sensors))
1770 steam_do_deck_sensors_event(steam, sensors, data);
1773 case ID_CONTROLLER_WIRELESS:
1775 * The payload of this event is a single byte:
1776 * 0x01: disconnected.
1781 steam_do_connect_event(steam, false);
1784 steam_do_connect_event(steam, true);
1788 case ID_CONTROLLER_STATUS:
1789 if (steam->quirks & STEAM_QUIRK_WIRELESS) {
1791 battery = rcu_dereference(steam->battery);
1792 if (likely(battery)) {
1793 steam_do_battery_event(steam, battery, data);
1796 "%s: battery data without connect event\n",
1798 steam_do_connect_event(steam, true);
1807 static int steam_param_set_lizard_mode(const char *val,
1808 const struct kernel_param *kp)
1810 struct steam_device *steam;
1813 ret = param_set_bool(val, kp);
1817 mutex_lock(&steam_devices_lock);
1818 list_for_each_entry(steam, &steam_devices, list) {
1819 if (!steam->client_opened)
1820 steam_set_lizard_mode(steam, lizard_mode);
1822 mutex_unlock(&steam_devices_lock);
1826 static const struct kernel_param_ops steam_lizard_mode_ops = {
1827 .set = steam_param_set_lizard_mode,
1828 .get = param_get_bool,
1831 module_param_cb(lizard_mode, &steam_lizard_mode_ops, &lizard_mode, 0644);
1832 MODULE_PARM_DESC(lizard_mode,
1833 "Enable mouse and keyboard emulation (lizard mode) when the gamepad is not in use");
1835 static const struct hid_device_id steam_controllers[] = {
1836 { /* Wired Steam Controller */
1837 HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
1838 USB_DEVICE_ID_STEAM_CONTROLLER)
1840 { /* Wireless Steam Controller */
1841 HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
1842 USB_DEVICE_ID_STEAM_CONTROLLER_WIRELESS),
1843 .driver_data = STEAM_QUIRK_WIRELESS
1846 HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
1847 USB_DEVICE_ID_STEAM_DECK),
1848 .driver_data = STEAM_QUIRK_DECK
1853 MODULE_DEVICE_TABLE(hid, steam_controllers);
1855 static struct hid_driver steam_controller_driver = {
1856 .name = "hid-steam",
1857 .id_table = steam_controllers,
1858 .probe = steam_probe,
1859 .remove = steam_remove,
1860 .raw_event = steam_raw_event,
1863 module_hid_driver(steam_controller_driver);