1 // SPDX-License-Identifier: GPL-2.0+
3 * HID driver for Valve Steam Controller
5 * Copyright (c) 2018 Rodrigo Rivas Costa <rodrigorivascosta@gmail.com>
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_LICENSE("GPL");
49 MODULE_AUTHOR("Rodrigo Rivas Costa <rodrigorivascosta@gmail.com>");
51 static bool lizard_mode = true;
53 static DEFINE_MUTEX(steam_devices_lock);
54 static LIST_HEAD(steam_devices);
56 #define STEAM_QUIRK_WIRELESS BIT(0)
57 #define STEAM_QUIRK_DECK BIT(1)
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
70 #define STEAM_PAD_FUZZ 256
73 * Commands that can be sent in a feature report.
74 * Thanks to Valve for some valuable hints.
76 #define STEAM_CMD_SET_MAPPINGS 0x80
77 #define STEAM_CMD_CLEAR_MAPPINGS 0x81
78 #define STEAM_CMD_GET_MAPPINGS 0x82
79 #define STEAM_CMD_GET_ATTRIB 0x83
80 #define STEAM_CMD_GET_ATTRIB_LABEL 0x84
81 #define STEAM_CMD_DEFAULT_MAPPINGS 0x85
82 #define STEAM_CMD_FACTORY_RESET 0x86
83 #define STEAM_CMD_WRITE_REGISTER 0x87
84 #define STEAM_CMD_CLEAR_REGISTER 0x88
85 #define STEAM_CMD_READ_REGISTER 0x89
86 #define STEAM_CMD_GET_REGISTER_LABEL 0x8a
87 #define STEAM_CMD_GET_REGISTER_MAX 0x8b
88 #define STEAM_CMD_GET_REGISTER_DEFAULT 0x8c
89 #define STEAM_CMD_SET_MODE 0x8d
90 #define STEAM_CMD_DEFAULT_MOUSE 0x8e
91 #define STEAM_CMD_FORCEFEEDBAK 0x8f
92 #define STEAM_CMD_REQUEST_COMM_STATUS 0xb4
93 #define STEAM_CMD_GET_SERIAL 0xae
94 #define STEAM_CMD_HAPTIC_RUMBLE 0xeb
96 /* Some useful register ids */
97 #define STEAM_REG_LPAD_MODE 0x07
98 #define STEAM_REG_RPAD_MODE 0x08
99 #define STEAM_REG_RPAD_MARGIN 0x18
100 #define STEAM_REG_LED 0x2d
101 #define STEAM_REG_GYRO_MODE 0x30
102 #define STEAM_REG_LPAD_CLICK_PRESSURE 0x34
103 #define STEAM_REG_RPAD_CLICK_PRESSURE 0x35
105 /* Raw event identifiers */
106 #define STEAM_EV_INPUT_DATA 0x01
107 #define STEAM_EV_CONNECT 0x03
108 #define STEAM_EV_BATTERY 0x04
109 #define STEAM_EV_DECK_INPUT_DATA 0x09
111 /* Values for GYRO_MODE (bitmask) */
112 #define STEAM_GYRO_MODE_OFF 0x0000
113 #define STEAM_GYRO_MODE_STEERING 0x0001
114 #define STEAM_GYRO_MODE_TILT 0x0002
115 #define STEAM_GYRO_MODE_SEND_ORIENTATION 0x0004
116 #define STEAM_GYRO_MODE_SEND_RAW_ACCEL 0x0008
117 #define STEAM_GYRO_MODE_SEND_RAW_GYRO 0x0010
119 /* Other random constants */
120 #define STEAM_SERIAL_LEN 10
122 struct steam_device {
123 struct list_head list;
125 struct hid_device *hdev, *client_hdev;
128 struct input_dev __rcu *input;
129 unsigned long quirks;
130 struct work_struct work_connect;
132 char serial_no[STEAM_SERIAL_LEN + 1];
133 struct power_supply_desc battery_desc;
134 struct power_supply __rcu *battery;
137 struct delayed_work heartbeat;
138 struct work_struct rumble_work;
143 static int steam_recv_report(struct steam_device *steam,
146 struct hid_report *r;
150 r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0];
152 hid_err(steam->hdev, "No HID_FEATURE_REPORT submitted - nothing to read\n");
156 if (hid_report_len(r) < 64)
159 buf = hid_alloc_report_buf(r, GFP_KERNEL);
164 * The report ID is always 0, so strip the first byte from the output.
165 * hid_report_len() is not counting the report ID, so +1 to the length
166 * or else we get a EOVERFLOW. We are safe from a buffer overflow
167 * because hid_alloc_report_buf() allocates +7 bytes.
169 ret = hid_hw_raw_request(steam->hdev, 0x00,
170 buf, hid_report_len(r) + 1,
171 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
173 memcpy(data, buf + 1, min(size, ret - 1));
178 static int steam_send_report(struct steam_device *steam,
181 struct hid_report *r;
183 unsigned int retries = 50;
186 r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0];
188 hid_err(steam->hdev, "No HID_FEATURE_REPORT submitted - nothing to read\n");
192 if (hid_report_len(r) < 64)
195 buf = hid_alloc_report_buf(r, GFP_KERNEL);
199 /* The report ID is always 0 */
200 memcpy(buf + 1, cmd, size);
203 * Sometimes the wireless controller fails with EPIPE
204 * when sending a feature report.
205 * Doing a HID_REQ_GET_REPORT and waiting for a while
209 ret = hid_hw_raw_request(steam->hdev, 0,
210 buf, max(size, 64) + 1,
211 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
219 hid_err(steam->hdev, "%s: error %d (%*ph)\n", __func__,
224 static inline int steam_send_report_byte(struct steam_device *steam, u8 cmd)
226 return steam_send_report(steam, &cmd, 1);
229 static int steam_write_registers(struct steam_device *steam,
230 /* u8 reg, u16 val */...)
232 /* Send: 0x87 len (reg valLo valHi)* */
235 u8 cmd[64] = {STEAM_CMD_WRITE_REGISTER, 0x00};
239 va_start(args, steam);
241 reg = va_arg(args, int);
244 val = va_arg(args, int);
245 cmd[cmd[1] + 2] = reg;
246 cmd[cmd[1] + 3] = val & 0xff;
247 cmd[cmd[1] + 4] = val >> 8;
252 ret = steam_send_report(steam, cmd, 2 + cmd[1]);
257 * Sometimes a lingering report for this command can
258 * get read back instead of the last set report if
259 * this isn't explicitly queried
261 return steam_recv_report(steam, cmd, 2 + cmd[1]);
264 static int steam_get_serial(struct steam_device *steam)
267 * Send: 0xae 0x15 0x01
268 * Recv: 0xae 0x15 0x01 serialnumber (10 chars)
271 u8 cmd[] = {STEAM_CMD_GET_SERIAL, 0x15, 0x01};
272 u8 reply[3 + STEAM_SERIAL_LEN + 1];
274 ret = steam_send_report(steam, cmd, sizeof(cmd));
277 ret = steam_recv_report(steam, reply, sizeof(reply));
280 if (reply[0] != 0xae || reply[1] != 0x15 || reply[2] != 0x01)
282 reply[3 + STEAM_SERIAL_LEN] = 0;
283 strscpy(steam->serial_no, reply + 3, sizeof(steam->serial_no));
288 * This command requests the wireless adaptor to post an event
289 * with the connection status. Useful if this driver is loaded when
290 * the controller is already connected.
292 static inline int steam_request_conn_status(struct steam_device *steam)
294 return steam_send_report_byte(steam, STEAM_CMD_REQUEST_COMM_STATUS);
297 static inline int steam_haptic_rumble(struct steam_device *steam,
298 u16 intensity, u16 left_speed, u16 right_speed,
299 u8 left_gain, u8 right_gain)
301 u8 report[11] = {STEAM_CMD_HAPTIC_RUMBLE, 9};
303 report[3] = intensity & 0xFF;
304 report[4] = intensity >> 8;
305 report[5] = left_speed & 0xFF;
306 report[6] = left_speed >> 8;
307 report[7] = right_speed & 0xFF;
308 report[8] = right_speed >> 8;
309 report[9] = left_gain;
310 report[10] = right_gain;
312 return steam_send_report(steam, report, sizeof(report));
315 static void steam_haptic_rumble_cb(struct work_struct *work)
317 struct steam_device *steam = container_of(work, struct steam_device,
319 steam_haptic_rumble(steam, 0, steam->rumble_left,
320 steam->rumble_right, 2, 0);
323 #ifdef CONFIG_STEAM_FF
324 static int steam_play_effect(struct input_dev *dev, void *data,
325 struct ff_effect *effect)
327 struct steam_device *steam = input_get_drvdata(dev);
329 steam->rumble_left = effect->u.rumble.strong_magnitude;
330 steam->rumble_right = effect->u.rumble.weak_magnitude;
332 return schedule_work(&steam->rumble_work);
336 static void steam_set_lizard_mode(struct steam_device *steam, bool enable)
339 /* enable esc, enter, cursors */
340 steam_send_report_byte(steam, STEAM_CMD_DEFAULT_MAPPINGS);
342 steam_send_report_byte(steam, STEAM_CMD_DEFAULT_MOUSE);
343 steam_write_registers(steam,
344 STEAM_REG_RPAD_MARGIN, 0x01, /* enable margin */
347 cancel_delayed_work_sync(&steam->heartbeat);
349 /* disable esc, enter, cursor */
350 steam_send_report_byte(steam, STEAM_CMD_CLEAR_MAPPINGS);
352 if (steam->quirks & STEAM_QUIRK_DECK) {
353 steam_write_registers(steam,
354 STEAM_REG_RPAD_MARGIN, 0x00, /* disable margin */
355 STEAM_REG_LPAD_MODE, 0x07, /* disable mouse */
356 STEAM_REG_RPAD_MODE, 0x07, /* disable mouse */
357 STEAM_REG_LPAD_CLICK_PRESSURE, 0xFFFF, /* disable clicky pad */
358 STEAM_REG_RPAD_CLICK_PRESSURE, 0xFFFF, /* disable clicky pad */
361 * The Steam Deck has a watchdog that automatically enables
362 * lizard mode if it doesn't see any traffic for too long
364 if (!work_busy(&steam->heartbeat.work))
365 schedule_delayed_work(&steam->heartbeat, 5 * HZ);
367 steam_write_registers(steam,
368 STEAM_REG_RPAD_MARGIN, 0x00, /* disable margin */
369 STEAM_REG_LPAD_MODE, 0x07, /* disable mouse */
370 STEAM_REG_RPAD_MODE, 0x07, /* disable mouse */
376 static int steam_input_open(struct input_dev *dev)
378 struct steam_device *steam = input_get_drvdata(dev);
380 mutex_lock(&steam->mutex);
381 if (!steam->client_opened && lizard_mode)
382 steam_set_lizard_mode(steam, false);
383 mutex_unlock(&steam->mutex);
387 static void steam_input_close(struct input_dev *dev)
389 struct steam_device *steam = input_get_drvdata(dev);
391 mutex_lock(&steam->mutex);
392 if (!steam->client_opened && lizard_mode)
393 steam_set_lizard_mode(steam, true);
394 mutex_unlock(&steam->mutex);
397 static enum power_supply_property steam_battery_props[] = {
398 POWER_SUPPLY_PROP_PRESENT,
399 POWER_SUPPLY_PROP_SCOPE,
400 POWER_SUPPLY_PROP_VOLTAGE_NOW,
401 POWER_SUPPLY_PROP_CAPACITY,
404 static int steam_battery_get_property(struct power_supply *psy,
405 enum power_supply_property psp,
406 union power_supply_propval *val)
408 struct steam_device *steam = power_supply_get_drvdata(psy);
414 spin_lock_irqsave(&steam->lock, flags);
415 volts = steam->voltage;
416 batt = steam->battery_charge;
417 spin_unlock_irqrestore(&steam->lock, flags);
420 case POWER_SUPPLY_PROP_PRESENT:
423 case POWER_SUPPLY_PROP_SCOPE:
424 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
426 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
427 val->intval = volts * 1000; /* mV -> uV */
429 case POWER_SUPPLY_PROP_CAPACITY:
439 static int steam_battery_register(struct steam_device *steam)
441 struct power_supply *battery;
442 struct power_supply_config battery_cfg = { .drv_data = steam, };
446 steam->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
447 steam->battery_desc.properties = steam_battery_props;
448 steam->battery_desc.num_properties = ARRAY_SIZE(steam_battery_props);
449 steam->battery_desc.get_property = steam_battery_get_property;
450 steam->battery_desc.name = devm_kasprintf(&steam->hdev->dev,
451 GFP_KERNEL, "steam-controller-%s-battery",
453 if (!steam->battery_desc.name)
456 /* avoid the warning of 0% battery while waiting for the first info */
457 spin_lock_irqsave(&steam->lock, flags);
458 steam->voltage = 3000;
459 steam->battery_charge = 100;
460 spin_unlock_irqrestore(&steam->lock, flags);
462 battery = power_supply_register(&steam->hdev->dev,
463 &steam->battery_desc, &battery_cfg);
464 if (IS_ERR(battery)) {
465 ret = PTR_ERR(battery);
467 "%s:power_supply_register failed with error %d\n",
471 rcu_assign_pointer(steam->battery, battery);
472 power_supply_powers(battery, &steam->hdev->dev);
476 static int steam_input_register(struct steam_device *steam)
478 struct hid_device *hdev = steam->hdev;
479 struct input_dev *input;
483 input = rcu_dereference(steam->input);
486 dbg_hid("%s: already connected\n", __func__);
490 input = input_allocate_device();
494 input_set_drvdata(input, steam);
495 input->dev.parent = &hdev->dev;
496 input->open = steam_input_open;
497 input->close = steam_input_close;
499 input->name = (steam->quirks & STEAM_QUIRK_WIRELESS) ? "Wireless Steam Controller" :
500 (steam->quirks & STEAM_QUIRK_DECK) ? "Steam Deck" :
502 input->phys = hdev->phys;
503 input->uniq = steam->serial_no;
504 input->id.bustype = hdev->bus;
505 input->id.vendor = hdev->vendor;
506 input->id.product = hdev->product;
507 input->id.version = hdev->version;
509 input_set_capability(input, EV_KEY, BTN_TR2);
510 input_set_capability(input, EV_KEY, BTN_TL2);
511 input_set_capability(input, EV_KEY, BTN_TR);
512 input_set_capability(input, EV_KEY, BTN_TL);
513 input_set_capability(input, EV_KEY, BTN_Y);
514 input_set_capability(input, EV_KEY, BTN_B);
515 input_set_capability(input, EV_KEY, BTN_X);
516 input_set_capability(input, EV_KEY, BTN_A);
517 input_set_capability(input, EV_KEY, BTN_DPAD_UP);
518 input_set_capability(input, EV_KEY, BTN_DPAD_RIGHT);
519 input_set_capability(input, EV_KEY, BTN_DPAD_LEFT);
520 input_set_capability(input, EV_KEY, BTN_DPAD_DOWN);
521 input_set_capability(input, EV_KEY, BTN_SELECT);
522 input_set_capability(input, EV_KEY, BTN_MODE);
523 input_set_capability(input, EV_KEY, BTN_START);
524 input_set_capability(input, EV_KEY, BTN_THUMBR);
525 input_set_capability(input, EV_KEY, BTN_THUMBL);
526 input_set_capability(input, EV_KEY, BTN_THUMB);
527 input_set_capability(input, EV_KEY, BTN_THUMB2);
528 if (steam->quirks & STEAM_QUIRK_DECK) {
529 input_set_capability(input, EV_KEY, BTN_BASE);
530 input_set_capability(input, EV_KEY, BTN_TRIGGER_HAPPY1);
531 input_set_capability(input, EV_KEY, BTN_TRIGGER_HAPPY2);
532 input_set_capability(input, EV_KEY, BTN_TRIGGER_HAPPY3);
533 input_set_capability(input, EV_KEY, BTN_TRIGGER_HAPPY4);
535 input_set_capability(input, EV_KEY, BTN_GEAR_DOWN);
536 input_set_capability(input, EV_KEY, BTN_GEAR_UP);
539 input_set_abs_params(input, ABS_X, -32767, 32767, 0, 0);
540 input_set_abs_params(input, ABS_Y, -32767, 32767, 0, 0);
542 input_set_abs_params(input, ABS_HAT0X, -32767, 32767,
544 input_set_abs_params(input, ABS_HAT0Y, -32767, 32767,
547 if (steam->quirks & STEAM_QUIRK_DECK) {
548 input_set_abs_params(input, ABS_HAT2Y, 0, 32767, 0, 0);
549 input_set_abs_params(input, ABS_HAT2X, 0, 32767, 0, 0);
551 input_set_abs_params(input, ABS_RX, -32767, 32767, 0, 0);
552 input_set_abs_params(input, ABS_RY, -32767, 32767, 0, 0);
554 input_set_abs_params(input, ABS_HAT1X, -32767, 32767,
556 input_set_abs_params(input, ABS_HAT1Y, -32767, 32767,
559 input_abs_set_res(input, ABS_X, STEAM_DECK_JOYSTICK_RESOLUTION);
560 input_abs_set_res(input, ABS_Y, STEAM_DECK_JOYSTICK_RESOLUTION);
561 input_abs_set_res(input, ABS_RX, STEAM_DECK_JOYSTICK_RESOLUTION);
562 input_abs_set_res(input, ABS_RY, STEAM_DECK_JOYSTICK_RESOLUTION);
563 input_abs_set_res(input, ABS_HAT1X, STEAM_PAD_RESOLUTION);
564 input_abs_set_res(input, ABS_HAT1Y, STEAM_PAD_RESOLUTION);
565 input_abs_set_res(input, ABS_HAT2Y, STEAM_DECK_TRIGGER_RESOLUTION);
566 input_abs_set_res(input, ABS_HAT2X, STEAM_DECK_TRIGGER_RESOLUTION);
568 input_set_abs_params(input, ABS_HAT2Y, 0, 255, 0, 0);
569 input_set_abs_params(input, ABS_HAT2X, 0, 255, 0, 0);
571 input_set_abs_params(input, ABS_RX, -32767, 32767,
573 input_set_abs_params(input, ABS_RY, -32767, 32767,
576 input_abs_set_res(input, ABS_X, STEAM_JOYSTICK_RESOLUTION);
577 input_abs_set_res(input, ABS_Y, STEAM_JOYSTICK_RESOLUTION);
578 input_abs_set_res(input, ABS_RX, STEAM_PAD_RESOLUTION);
579 input_abs_set_res(input, ABS_RY, STEAM_PAD_RESOLUTION);
580 input_abs_set_res(input, ABS_HAT2Y, STEAM_TRIGGER_RESOLUTION);
581 input_abs_set_res(input, ABS_HAT2X, STEAM_TRIGGER_RESOLUTION);
583 input_abs_set_res(input, ABS_HAT0X, STEAM_PAD_RESOLUTION);
584 input_abs_set_res(input, ABS_HAT0Y, STEAM_PAD_RESOLUTION);
586 #ifdef CONFIG_STEAM_FF
587 if (steam->quirks & STEAM_QUIRK_DECK) {
588 input_set_capability(input, EV_FF, FF_RUMBLE);
589 ret = input_ff_create_memless(input, NULL, steam_play_effect);
591 goto input_register_fail;
595 ret = input_register_device(input);
597 goto input_register_fail;
599 rcu_assign_pointer(steam->input, input);
603 input_free_device(input);
607 static void steam_input_unregister(struct steam_device *steam)
609 struct input_dev *input;
611 input = rcu_dereference(steam->input);
615 RCU_INIT_POINTER(steam->input, NULL);
617 input_unregister_device(input);
620 static void steam_battery_unregister(struct steam_device *steam)
622 struct power_supply *battery;
625 battery = rcu_dereference(steam->battery);
630 RCU_INIT_POINTER(steam->battery, NULL);
632 power_supply_unregister(battery);
635 static int steam_register(struct steam_device *steam)
641 * This function can be called several times in a row with the
642 * wireless adaptor, without steam_unregister() between them, because
643 * another client send a get_connection_status command, for example.
644 * The battery and serial number are set just once per device.
646 if (!steam->serial_no[0]) {
648 * Unlikely, but getting the serial could fail, and it is not so
649 * important, so make up a serial number and go on.
651 mutex_lock(&steam->mutex);
652 if (steam_get_serial(steam) < 0)
653 strscpy(steam->serial_no, "XXXXXXXXXX",
654 sizeof(steam->serial_no));
655 mutex_unlock(&steam->mutex);
657 hid_info(steam->hdev, "Steam Controller '%s' connected",
660 /* ignore battery errors, we can live without it */
661 if (steam->quirks & STEAM_QUIRK_WIRELESS)
662 steam_battery_register(steam);
664 mutex_lock(&steam_devices_lock);
665 if (list_empty(&steam->list))
666 list_add(&steam->list, &steam_devices);
667 mutex_unlock(&steam_devices_lock);
670 mutex_lock(&steam->mutex);
671 client_opened = steam->client_opened;
673 steam_set_lizard_mode(steam, lizard_mode);
674 mutex_unlock(&steam->mutex);
677 ret = steam_input_register(steam);
684 static void steam_unregister(struct steam_device *steam)
686 steam_battery_unregister(steam);
687 steam_input_unregister(steam);
688 if (steam->serial_no[0]) {
689 hid_info(steam->hdev, "Steam Controller '%s' disconnected",
691 mutex_lock(&steam_devices_lock);
692 list_del_init(&steam->list);
693 mutex_unlock(&steam_devices_lock);
694 steam->serial_no[0] = 0;
698 static void steam_work_connect_cb(struct work_struct *work)
700 struct steam_device *steam = container_of(work, struct steam_device,
706 spin_lock_irqsave(&steam->lock, flags);
707 connected = steam->connected;
708 spin_unlock_irqrestore(&steam->lock, flags);
711 ret = steam_register(steam);
714 "%s:steam_register failed with error %d\n",
718 steam_unregister(steam);
722 static bool steam_is_valve_interface(struct hid_device *hdev)
724 struct hid_report_enum *rep_enum;
727 * The wired device creates 3 interfaces:
729 * 1: emulated keyboard.
730 * 2: the real game pad.
731 * The wireless device creates 5 interfaces:
732 * 0: emulated keyboard.
733 * 1-4: slots where up to 4 real game pads will be connected to.
734 * We know which one is the real gamepad interface because they are the
735 * only ones with a feature report.
737 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
738 return !list_empty(&rep_enum->report_list);
741 static void steam_lizard_mode_heartbeat(struct work_struct *work)
743 struct steam_device *steam = container_of(work, struct steam_device,
746 mutex_lock(&steam->mutex);
747 if (!steam->client_opened && steam->client_hdev) {
748 steam_send_report_byte(steam, STEAM_CMD_CLEAR_MAPPINGS);
749 steam_write_registers(steam,
750 STEAM_REG_RPAD_MODE, 0x07, /* disable mouse */
752 schedule_delayed_work(&steam->heartbeat, 5 * HZ);
754 mutex_unlock(&steam->mutex);
757 static int steam_client_ll_parse(struct hid_device *hdev)
759 struct steam_device *steam = hdev->driver_data;
761 return hid_parse_report(hdev, steam->hdev->dev_rdesc,
762 steam->hdev->dev_rsize);
765 static int steam_client_ll_start(struct hid_device *hdev)
770 static void steam_client_ll_stop(struct hid_device *hdev)
774 static int steam_client_ll_open(struct hid_device *hdev)
776 struct steam_device *steam = hdev->driver_data;
778 mutex_lock(&steam->mutex);
779 steam->client_opened = true;
780 mutex_unlock(&steam->mutex);
782 steam_input_unregister(steam);
787 static void steam_client_ll_close(struct hid_device *hdev)
789 struct steam_device *steam = hdev->driver_data;
794 spin_lock_irqsave(&steam->lock, flags);
795 connected = steam->connected;
796 spin_unlock_irqrestore(&steam->lock, flags);
798 mutex_lock(&steam->mutex);
799 steam->client_opened = false;
801 steam_set_lizard_mode(steam, lizard_mode);
802 mutex_unlock(&steam->mutex);
805 steam_input_register(steam);
808 static int steam_client_ll_raw_request(struct hid_device *hdev,
809 unsigned char reportnum, u8 *buf,
810 size_t count, unsigned char report_type,
813 struct steam_device *steam = hdev->driver_data;
815 return hid_hw_raw_request(steam->hdev, reportnum, buf, count,
816 report_type, reqtype);
819 static const struct hid_ll_driver steam_client_ll_driver = {
820 .parse = steam_client_ll_parse,
821 .start = steam_client_ll_start,
822 .stop = steam_client_ll_stop,
823 .open = steam_client_ll_open,
824 .close = steam_client_ll_close,
825 .raw_request = steam_client_ll_raw_request,
828 static struct hid_device *steam_create_client_hid(struct hid_device *hdev)
830 struct hid_device *client_hdev;
832 client_hdev = hid_allocate_device();
833 if (IS_ERR(client_hdev))
836 client_hdev->ll_driver = &steam_client_ll_driver;
837 client_hdev->dev.parent = hdev->dev.parent;
838 client_hdev->bus = hdev->bus;
839 client_hdev->vendor = hdev->vendor;
840 client_hdev->product = hdev->product;
841 client_hdev->version = hdev->version;
842 client_hdev->type = hdev->type;
843 client_hdev->country = hdev->country;
844 strscpy(client_hdev->name, hdev->name,
845 sizeof(client_hdev->name));
846 strscpy(client_hdev->phys, hdev->phys,
847 sizeof(client_hdev->phys));
849 * Since we use the same device info than the real interface to
850 * trick userspace, we will be calling steam_probe recursively.
851 * We need to recognize the client interface somehow.
853 client_hdev->group = HID_GROUP_STEAM;
857 static int steam_probe(struct hid_device *hdev,
858 const struct hid_device_id *id)
860 struct steam_device *steam;
863 ret = hid_parse(hdev);
866 "%s:parse of hid interface failed\n", __func__);
871 * The virtual client_dev is only used for hidraw.
872 * Also avoid the recursive probe.
874 if (hdev->group == HID_GROUP_STEAM)
875 return hid_hw_start(hdev, HID_CONNECT_HIDRAW);
877 * The non-valve interfaces (mouse and keyboard emulation) are
878 * connected without changes.
880 if (!steam_is_valve_interface(hdev))
881 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
883 steam = devm_kzalloc(&hdev->dev, sizeof(*steam), GFP_KERNEL);
886 goto steam_alloc_fail;
889 hid_set_drvdata(hdev, steam);
890 spin_lock_init(&steam->lock);
891 mutex_init(&steam->mutex);
892 steam->quirks = id->driver_data;
893 INIT_WORK(&steam->work_connect, steam_work_connect_cb);
894 INIT_LIST_HEAD(&steam->list);
895 INIT_DEFERRABLE_WORK(&steam->heartbeat, steam_lizard_mode_heartbeat);
896 INIT_WORK(&steam->rumble_work, steam_haptic_rumble_cb);
898 steam->client_hdev = steam_create_client_hid(hdev);
899 if (IS_ERR(steam->client_hdev)) {
900 ret = PTR_ERR(steam->client_hdev);
901 goto client_hdev_fail;
903 steam->client_hdev->driver_data = steam;
906 * With the real steam controller interface, do not connect hidraw.
907 * Instead, create the client_hid and connect that.
909 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_HIDRAW);
911 goto hid_hw_start_fail;
913 ret = hid_add_device(steam->client_hdev);
915 goto client_hdev_add_fail;
917 ret = hid_hw_open(hdev);
922 goto hid_hw_open_fail;
925 if (steam->quirks & STEAM_QUIRK_WIRELESS) {
926 hid_info(hdev, "Steam wireless receiver connected");
927 /* If using a wireless adaptor ask for connection status */
928 steam->connected = false;
929 steam_request_conn_status(steam);
931 /* A wired connection is always present */
932 steam->connected = true;
933 ret = steam_register(steam);
936 "%s:steam_register failed with error %d\n",
938 goto input_register_fail;
946 client_hdev_add_fail:
949 hid_destroy_device(steam->client_hdev);
951 cancel_work_sync(&steam->work_connect);
952 cancel_delayed_work_sync(&steam->heartbeat);
953 cancel_work_sync(&steam->rumble_work);
955 hid_err(hdev, "%s: failed with error %d\n",
960 static void steam_remove(struct hid_device *hdev)
962 struct steam_device *steam = hid_get_drvdata(hdev);
964 if (!steam || hdev->group == HID_GROUP_STEAM) {
969 hid_destroy_device(steam->client_hdev);
970 mutex_lock(&steam->mutex);
971 steam->client_hdev = NULL;
972 steam->client_opened = false;
973 cancel_delayed_work_sync(&steam->heartbeat);
974 mutex_unlock(&steam->mutex);
975 cancel_work_sync(&steam->work_connect);
976 if (steam->quirks & STEAM_QUIRK_WIRELESS) {
977 hid_info(hdev, "Steam wireless receiver disconnected");
981 steam_unregister(steam);
984 static void steam_do_connect_event(struct steam_device *steam, bool connected)
989 spin_lock_irqsave(&steam->lock, flags);
990 changed = steam->connected != connected;
991 steam->connected = connected;
992 spin_unlock_irqrestore(&steam->lock, flags);
994 if (changed && schedule_work(&steam->work_connect) == 0)
995 dbg_hid("%s: connected=%d event already queued\n",
996 __func__, connected);
1000 * Some input data in the protocol has the opposite sign.
1001 * Clamp the values to 32767..-32767 so that the range is
1002 * symmetrical and can be negated safely.
1004 static inline s16 steam_le16(u8 *data)
1006 s16 x = (s16) le16_to_cpup((__le16 *)data);
1008 return x == -32768 ? -32767 : x;
1012 * The size for this message payload is 60.
1013 * The known values are:
1014 * (* values are not sent through wireless)
1015 * (* accelerator/gyro is disabled by default)
1016 * Offset| Type | Mapped to |Meaning
1017 * -------+-------+-----------+--------------------------
1018 * 4-7 | u32 | -- | sequence number
1019 * 8-10 | 24bit | see below | buttons
1020 * 11 | u8 | ABS_HAT2Y | left trigger
1021 * 12 | u8 | ABS_HAT2X | right trigger
1022 * 13-15 | -- | -- | always 0
1023 * 16-17 | s16 | ABS_X/ABS_HAT0X | X value
1024 * 18-19 | s16 | ABS_Y/ABS_HAT0Y | Y value
1025 * 20-21 | s16 | ABS_RX | right-pad X value
1026 * 22-23 | s16 | ABS_RY | right-pad Y value
1027 * 24-25 | s16 | -- | * left trigger
1028 * 26-27 | s16 | -- | * right trigger
1029 * 28-29 | s16 | -- | * accelerometer X value
1030 * 30-31 | s16 | -- | * accelerometer Y value
1031 * 32-33 | s16 | -- | * accelerometer Z value
1032 * 34-35 | s16 | -- | gyro X value
1033 * 36-36 | s16 | -- | gyro Y value
1034 * 38-39 | s16 | -- | gyro Z value
1035 * 40-41 | s16 | -- | quaternion W value
1036 * 42-43 | s16 | -- | quaternion X value
1037 * 44-45 | s16 | -- | quaternion Y value
1038 * 46-47 | s16 | -- | quaternion Z value
1039 * 48-49 | -- | -- | always 0
1040 * 50-51 | s16 | -- | * left trigger (uncalibrated)
1041 * 52-53 | s16 | -- | * right trigger (uncalibrated)
1042 * 54-55 | s16 | -- | * joystick X value (uncalibrated)
1043 * 56-57 | s16 | -- | * joystick Y value (uncalibrated)
1044 * 58-59 | s16 | -- | * left-pad X value
1045 * 60-61 | s16 | -- | * left-pad Y value
1046 * 62-63 | u16 | -- | * battery voltage
1049 * Bit | Mapped to | Description
1050 * ------+------------+--------------------------------
1051 * 8.0 | BTN_TR2 | right trigger fully pressed
1052 * 8.1 | BTN_TL2 | left trigger fully pressed
1053 * 8.2 | BTN_TR | right shoulder
1054 * 8.3 | BTN_TL | left shoulder
1055 * 8.4 | BTN_Y | button Y
1056 * 8.5 | BTN_B | button B
1057 * 8.6 | BTN_X | button X
1058 * 8.7 | BTN_A | button A
1059 * 9.0 | BTN_DPAD_UP | left-pad up
1060 * 9.1 | BTN_DPAD_RIGHT | left-pad right
1061 * 9.2 | BTN_DPAD_LEFT | left-pad left
1062 * 9.3 | BTN_DPAD_DOWN | left-pad down
1063 * 9.4 | BTN_SELECT | menu left
1064 * 9.5 | BTN_MODE | steam logo
1065 * 9.6 | BTN_START | menu right
1066 * 9.7 | BTN_GEAR_DOWN | left back lever
1067 * 10.0 | BTN_GEAR_UP | right back lever
1068 * 10.1 | -- | left-pad clicked
1069 * 10.2 | BTN_THUMBR | right-pad clicked
1070 * 10.3 | BTN_THUMB | left-pad touched (but see explanation below)
1071 * 10.4 | BTN_THUMB2 | right-pad touched
1072 * 10.5 | -- | unknown
1073 * 10.6 | BTN_THUMBL | joystick clicked
1074 * 10.7 | -- | lpad_and_joy
1077 static void steam_do_input_event(struct steam_device *steam,
1078 struct input_dev *input, u8 *data)
1080 /* 24 bits of buttons */
1083 bool lpad_touched, lpad_and_joy;
1089 input_report_abs(input, ABS_HAT2Y, data[11]);
1090 input_report_abs(input, ABS_HAT2X, data[12]);
1093 * These two bits tells how to interpret the values X and Y.
1094 * lpad_and_joy tells that the joystick and the lpad are used at the
1096 * lpad_touched tells whether X/Y are to be read as lpad coord or
1098 * (lpad_touched || lpad_and_joy) tells if the lpad is really touched.
1100 lpad_touched = b10 & BIT(3);
1101 lpad_and_joy = b10 & BIT(7);
1102 x = steam_le16(data + 16);
1103 y = -steam_le16(data + 18);
1105 input_report_abs(input, lpad_touched ? ABS_HAT0X : ABS_X, x);
1106 input_report_abs(input, lpad_touched ? ABS_HAT0Y : ABS_Y, y);
1107 /* Check if joystick is centered */
1108 if (lpad_touched && !lpad_and_joy) {
1109 input_report_abs(input, ABS_X, 0);
1110 input_report_abs(input, ABS_Y, 0);
1112 /* Check if lpad is untouched */
1113 if (!(lpad_touched || lpad_and_joy)) {
1114 input_report_abs(input, ABS_HAT0X, 0);
1115 input_report_abs(input, ABS_HAT0Y, 0);
1118 input_report_abs(input, ABS_RX, steam_le16(data + 20));
1119 input_report_abs(input, ABS_RY, -steam_le16(data + 22));
1121 input_event(input, EV_KEY, BTN_TR2, !!(b8 & BIT(0)));
1122 input_event(input, EV_KEY, BTN_TL2, !!(b8 & BIT(1)));
1123 input_event(input, EV_KEY, BTN_TR, !!(b8 & BIT(2)));
1124 input_event(input, EV_KEY, BTN_TL, !!(b8 & BIT(3)));
1125 input_event(input, EV_KEY, BTN_Y, !!(b8 & BIT(4)));
1126 input_event(input, EV_KEY, BTN_B, !!(b8 & BIT(5)));
1127 input_event(input, EV_KEY, BTN_X, !!(b8 & BIT(6)));
1128 input_event(input, EV_KEY, BTN_A, !!(b8 & BIT(7)));
1129 input_event(input, EV_KEY, BTN_SELECT, !!(b9 & BIT(4)));
1130 input_event(input, EV_KEY, BTN_MODE, !!(b9 & BIT(5)));
1131 input_event(input, EV_KEY, BTN_START, !!(b9 & BIT(6)));
1132 input_event(input, EV_KEY, BTN_GEAR_DOWN, !!(b9 & BIT(7)));
1133 input_event(input, EV_KEY, BTN_GEAR_UP, !!(b10 & BIT(0)));
1134 input_event(input, EV_KEY, BTN_THUMBR, !!(b10 & BIT(2)));
1135 input_event(input, EV_KEY, BTN_THUMBL, !!(b10 & BIT(6)));
1136 input_event(input, EV_KEY, BTN_THUMB, lpad_touched || lpad_and_joy);
1137 input_event(input, EV_KEY, BTN_THUMB2, !!(b10 & BIT(4)));
1138 input_event(input, EV_KEY, BTN_DPAD_UP, !!(b9 & BIT(0)));
1139 input_event(input, EV_KEY, BTN_DPAD_RIGHT, !!(b9 & BIT(1)));
1140 input_event(input, EV_KEY, BTN_DPAD_LEFT, !!(b9 & BIT(2)));
1141 input_event(input, EV_KEY, BTN_DPAD_DOWN, !!(b9 & BIT(3)));
1147 * The size for this message payload is 56.
1148 * The known values are:
1149 * Offset| Type | Mapped to |Meaning
1150 * -------+-------+-----------+--------------------------
1151 * 4-7 | u32 | -- | sequence number
1152 * 8-15 | u64 | see below | buttons
1153 * 16-17 | s16 | ABS_HAT0X | left-pad X value
1154 * 18-19 | s16 | ABS_HAT0Y | left-pad Y value
1155 * 20-21 | s16 | ABS_HAT1X | right-pad X value
1156 * 22-23 | s16 | ABS_HAT1Y | right-pad Y value
1157 * 24-25 | s16 | -- | accelerometer X value
1158 * 26-27 | s16 | -- | accelerometer Y value
1159 * 28-29 | s16 | -- | accelerometer Z value
1160 * 30-31 | s16 | -- | gyro X value
1161 * 32-33 | s16 | -- | gyro Y value
1162 * 34-35 | s16 | -- | gyro Z value
1163 * 36-37 | s16 | -- | quaternion W value
1164 * 38-39 | s16 | -- | quaternion X value
1165 * 40-41 | s16 | -- | quaternion Y value
1166 * 42-43 | s16 | -- | quaternion Z value
1167 * 44-45 | u16 | ABS_HAT2Y | left trigger (uncalibrated)
1168 * 46-47 | u16 | ABS_HAT2X | right trigger (uncalibrated)
1169 * 48-49 | s16 | ABS_X | left joystick X
1170 * 50-51 | s16 | ABS_Y | left joystick Y
1171 * 52-53 | s16 | ABS_RX | right joystick X
1172 * 54-55 | s16 | ABS_RY | right joystick Y
1173 * 56-57 | u16 | -- | left pad pressure
1174 * 58-59 | u16 | -- | right pad pressure
1177 * Bit | Mapped to | Description
1178 * ------+------------+--------------------------------
1179 * 8.0 | BTN_TR2 | right trigger fully pressed
1180 * 8.1 | BTN_TL2 | left trigger fully pressed
1181 * 8.2 | BTN_TR | right shoulder
1182 * 8.3 | BTN_TL | left shoulder
1183 * 8.4 | BTN_Y | button Y
1184 * 8.5 | BTN_B | button B
1185 * 8.6 | BTN_X | button X
1186 * 8.7 | BTN_A | button A
1187 * 9.0 | BTN_DPAD_UP | left-pad up
1188 * 9.1 | BTN_DPAD_RIGHT | left-pad right
1189 * 9.2 | BTN_DPAD_LEFT | left-pad left
1190 * 9.3 | BTN_DPAD_DOWN | left-pad down
1191 * 9.4 | BTN_SELECT | menu left
1192 * 9.5 | BTN_MODE | steam logo
1193 * 9.6 | BTN_START | menu right
1194 * 9.7 | BTN_TRIGGER_HAPPY3 | left bottom grip button
1195 * 10.0 | BTN_TRIGGER_HAPPY4 | right bottom grip button
1196 * 10.1 | BTN_THUMB | left pad pressed
1197 * 10.2 | BTN_THUMB2 | right pad pressed
1198 * 10.3 | -- | left pad touched
1199 * 10.4 | -- | right pad touched
1200 * 10.5 | -- | unknown
1201 * 10.6 | BTN_THUMBL | left joystick clicked
1202 * 10.7 | -- | unknown
1203 * 11.0 | -- | unknown
1204 * 11.1 | -- | unknown
1205 * 11.2 | BTN_THUMBR | right joystick clicked
1206 * 11.3 | -- | unknown
1207 * 11.4 | -- | unknown
1208 * 11.5 | -- | unknown
1209 * 11.6 | -- | unknown
1210 * 11.7 | -- | unknown
1211 * 12.0 | -- | unknown
1212 * 12.1 | -- | unknown
1213 * 12.2 | -- | unknown
1214 * 12.3 | -- | unknown
1215 * 12.4 | -- | unknown
1216 * 12.5 | -- | unknown
1217 * 12.6 | -- | unknown
1218 * 12.7 | -- | unknown
1219 * 13.0 | -- | unknown
1220 * 13.1 | BTN_TRIGGER_HAPPY1 | left top grip button
1221 * 13.2 | BTN_TRIGGER_HAPPY2 | right top grip button
1222 * 13.3 | -- | unknown
1223 * 13.4 | -- | unknown
1224 * 13.5 | -- | unknown
1225 * 13.6 | -- | left joystick touched
1226 * 13.7 | -- | right joystick touched
1227 * 14.0 | -- | unknown
1228 * 14.1 | -- | unknown
1229 * 14.2 | BTN_BASE | quick access button
1230 * 14.3 | -- | unknown
1231 * 14.4 | -- | unknown
1232 * 14.5 | -- | unknown
1233 * 14.6 | -- | unknown
1234 * 14.7 | -- | unknown
1235 * 15.0 | -- | unknown
1236 * 15.1 | -- | unknown
1237 * 15.2 | -- | unknown
1238 * 15.3 | -- | unknown
1239 * 15.4 | -- | unknown
1240 * 15.5 | -- | unknown
1241 * 15.6 | -- | unknown
1242 * 15.7 | -- | unknown
1244 static void steam_do_deck_input_event(struct steam_device *steam,
1245 struct input_dev *input, u8 *data)
1247 u8 b8, b9, b10, b11, b13, b14;
1248 bool lpad_touched, rpad_touched;
1257 lpad_touched = b10 & BIT(3);
1258 rpad_touched = b10 & BIT(4);
1261 input_report_abs(input, ABS_HAT0X, steam_le16(data + 16));
1262 input_report_abs(input, ABS_HAT0Y, steam_le16(data + 18));
1264 input_report_abs(input, ABS_HAT0X, 0);
1265 input_report_abs(input, ABS_HAT0Y, 0);
1269 input_report_abs(input, ABS_HAT1X, steam_le16(data + 20));
1270 input_report_abs(input, ABS_HAT1Y, steam_le16(data + 22));
1272 input_report_abs(input, ABS_HAT1X, 0);
1273 input_report_abs(input, ABS_HAT1Y, 0);
1276 input_report_abs(input, ABS_X, steam_le16(data + 48));
1277 input_report_abs(input, ABS_Y, -steam_le16(data + 50));
1278 input_report_abs(input, ABS_RX, steam_le16(data + 52));
1279 input_report_abs(input, ABS_RY, -steam_le16(data + 54));
1281 input_report_abs(input, ABS_HAT2Y, steam_le16(data + 44));
1282 input_report_abs(input, ABS_HAT2X, steam_le16(data + 46));
1284 input_event(input, EV_KEY, BTN_TR2, !!(b8 & BIT(0)));
1285 input_event(input, EV_KEY, BTN_TL2, !!(b8 & BIT(1)));
1286 input_event(input, EV_KEY, BTN_TR, !!(b8 & BIT(2)));
1287 input_event(input, EV_KEY, BTN_TL, !!(b8 & BIT(3)));
1288 input_event(input, EV_KEY, BTN_Y, !!(b8 & BIT(4)));
1289 input_event(input, EV_KEY, BTN_B, !!(b8 & BIT(5)));
1290 input_event(input, EV_KEY, BTN_X, !!(b8 & BIT(6)));
1291 input_event(input, EV_KEY, BTN_A, !!(b8 & BIT(7)));
1292 input_event(input, EV_KEY, BTN_SELECT, !!(b9 & BIT(4)));
1293 input_event(input, EV_KEY, BTN_MODE, !!(b9 & BIT(5)));
1294 input_event(input, EV_KEY, BTN_START, !!(b9 & BIT(6)));
1295 input_event(input, EV_KEY, BTN_TRIGGER_HAPPY3, !!(b9 & BIT(7)));
1296 input_event(input, EV_KEY, BTN_TRIGGER_HAPPY4, !!(b10 & BIT(0)));
1297 input_event(input, EV_KEY, BTN_THUMBL, !!(b10 & BIT(6)));
1298 input_event(input, EV_KEY, BTN_THUMBR, !!(b11 & BIT(2)));
1299 input_event(input, EV_KEY, BTN_DPAD_UP, !!(b9 & BIT(0)));
1300 input_event(input, EV_KEY, BTN_DPAD_RIGHT, !!(b9 & BIT(1)));
1301 input_event(input, EV_KEY, BTN_DPAD_LEFT, !!(b9 & BIT(2)));
1302 input_event(input, EV_KEY, BTN_DPAD_DOWN, !!(b9 & BIT(3)));
1303 input_event(input, EV_KEY, BTN_THUMB, !!(b10 & BIT(1)));
1304 input_event(input, EV_KEY, BTN_THUMB2, !!(b10 & BIT(2)));
1305 input_event(input, EV_KEY, BTN_TRIGGER_HAPPY1, !!(b13 & BIT(1)));
1306 input_event(input, EV_KEY, BTN_TRIGGER_HAPPY2, !!(b13 & BIT(2)));
1307 input_event(input, EV_KEY, BTN_BASE, !!(b14 & BIT(2)));
1313 * The size for this message payload is 11.
1314 * The known values are:
1315 * Offset| Type | Meaning
1316 * -------+-------+---------------------------
1317 * 4-7 | u32 | sequence number
1318 * 8-11 | -- | always 0
1319 * 12-13 | u16 | voltage (mV)
1320 * 14 | u8 | battery percent
1322 static void steam_do_battery_event(struct steam_device *steam,
1323 struct power_supply *battery, u8 *data)
1325 unsigned long flags;
1327 s16 volts = steam_le16(data + 12);
1330 /* Creating the battery may have failed */
1332 battery = rcu_dereference(steam->battery);
1333 if (likely(battery)) {
1334 spin_lock_irqsave(&steam->lock, flags);
1335 steam->voltage = volts;
1336 steam->battery_charge = batt;
1337 spin_unlock_irqrestore(&steam->lock, flags);
1338 power_supply_changed(battery);
1343 static int steam_raw_event(struct hid_device *hdev,
1344 struct hid_report *report, u8 *data,
1347 struct steam_device *steam = hid_get_drvdata(hdev);
1348 struct input_dev *input;
1349 struct power_supply *battery;
1354 if (steam->client_opened)
1355 hid_input_report(steam->client_hdev, HID_FEATURE_REPORT,
1358 * All messages are size=64, all values little-endian.
1361 * -------+--------------------------------------------
1362 * 0-1 | always 0x01, 0x00, maybe protocol version?
1363 * 2 | type of message
1364 * 3 | length of the real payload (not checked)
1365 * 4-n | payload data, depends on the type
1367 * There are these known types of message:
1368 * 0x01: input data (60 bytes)
1369 * 0x03: wireless connect/disconnect (1 byte)
1370 * 0x04: battery status (11 bytes)
1371 * 0x09: Steam Deck input data (56 bytes)
1374 if (size != 64 || data[0] != 1 || data[1] != 0)
1378 case STEAM_EV_INPUT_DATA:
1379 if (steam->client_opened)
1382 input = rcu_dereference(steam->input);
1384 steam_do_input_event(steam, input, data);
1387 case STEAM_EV_DECK_INPUT_DATA:
1388 if (steam->client_opened)
1391 input = rcu_dereference(steam->input);
1393 steam_do_deck_input_event(steam, input, data);
1396 case STEAM_EV_CONNECT:
1398 * The payload of this event is a single byte:
1399 * 0x01: disconnected.
1404 steam_do_connect_event(steam, false);
1407 steam_do_connect_event(steam, true);
1411 case STEAM_EV_BATTERY:
1412 if (steam->quirks & STEAM_QUIRK_WIRELESS) {
1414 battery = rcu_dereference(steam->battery);
1415 if (likely(battery)) {
1416 steam_do_battery_event(steam, battery, data);
1419 "%s: battery data without connect event\n",
1421 steam_do_connect_event(steam, true);
1430 static int steam_param_set_lizard_mode(const char *val,
1431 const struct kernel_param *kp)
1433 struct steam_device *steam;
1436 ret = param_set_bool(val, kp);
1440 mutex_lock(&steam_devices_lock);
1441 list_for_each_entry(steam, &steam_devices, list) {
1442 mutex_lock(&steam->mutex);
1443 if (!steam->client_opened)
1444 steam_set_lizard_mode(steam, lizard_mode);
1445 mutex_unlock(&steam->mutex);
1447 mutex_unlock(&steam_devices_lock);
1451 static const struct kernel_param_ops steam_lizard_mode_ops = {
1452 .set = steam_param_set_lizard_mode,
1453 .get = param_get_bool,
1456 module_param_cb(lizard_mode, &steam_lizard_mode_ops, &lizard_mode, 0644);
1457 MODULE_PARM_DESC(lizard_mode,
1458 "Enable mouse and keyboard emulation (lizard mode) when the gamepad is not in use");
1460 static const struct hid_device_id steam_controllers[] = {
1461 { /* Wired Steam Controller */
1462 HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
1463 USB_DEVICE_ID_STEAM_CONTROLLER)
1465 { /* Wireless Steam Controller */
1466 HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
1467 USB_DEVICE_ID_STEAM_CONTROLLER_WIRELESS),
1468 .driver_data = STEAM_QUIRK_WIRELESS
1471 HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
1472 USB_DEVICE_ID_STEAM_DECK),
1473 .driver_data = STEAM_QUIRK_DECK
1478 MODULE_DEVICE_TABLE(hid, steam_controllers);
1480 static struct hid_driver steam_controller_driver = {
1481 .name = "hid-steam",
1482 .id_table = steam_controllers,
1483 .probe = steam_probe,
1484 .remove = steam_remove,
1485 .raw_event = steam_raw_event,
1488 module_hid_driver(steam_controller_driver);