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