Skip to contentSkip to conversation

HID: steam: Add 2026 Steam Controller support

PATCH Superseded Vicki Pfau 2 participants 10 messages Aug 5

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Vicki Pfau

author maintainer
#0 lore raw

cover HID: steam: Add 2026 Steam Controller support

This is the second half of a series that modernizes the hid-steam controller. It adds support for the new 2026 Steam Controller, with feature parity compared to the 2015 model and the Steam Deck drivers, as well as some smaller cleanup. The biggest cleanup is cleaning up the somewhat lax locking practices, but a few more minor things exposed to userspace include logging improvements and zeroing inputs when switching from gamepad mode to lizard mode.

Vicki Pfau (5):
HID: steam: Initial 2026 Steam Controller support
HID: steam: Fix wording of connect/disconnect logs
HID: steam: Don't set feature reports when disconnecting
HID: steam: Clean up locking
HID: steam: Zero out inputs when disabling gamepad mode

 drivers/hid/hid-ids.h   |    4 +
 drivers/hid/hid-steam.c | 1005 ++++++++++++++++++++++++++++++++-------
 2 files changed, 827 insertions(+), 182 deletions(-)
Signature · 1 line
2.54.0
author maintainer
#1 lore raw

PATCH 1/5 HID: steam: Initial 2026 Steam Controller support

This brings support for the 2026 Steam Controller, comparably featureful to the existing support for the Steam Deck.

---
 drivers/hid/hid-ids.h   |   4 +
 drivers/hid/hid-steam.c | 847 +++++++++++++++++++++++++++++++++-------
 2 files changed, 712 insertions(+), 139 deletions(-)
drivers/hid/hid-ids.h +4 -0
index 1059922baaac..b3559e70eec8 100644
@@ -1374,6 +1374,10 @@
 #define USB_DEVICE_ID_STEAM_CONTROLLER		0x1102
 #define USB_DEVICE_ID_STEAM_CONTROLLER_WIRELESS	0x1142
 #define USB_DEVICE_ID_STEAM_DECK	0x1205
+#define USB_DEVICE_ID_STEAM_CONTROLLER_IBEX	0x1302
+#define USB_DEVICE_ID_STEAM_CONTROLLER_IBEX_BLE	0x1303
+#define USB_DEVICE_ID_STEAM_CONTROLLER_PROTEUS	0x1304
+#define USB_DEVICE_ID_STEAM_CONTROLLER_NEREID	0x1305
 
 #define USB_VENDOR_ID_STEELSERIES	0x1038
 #define USB_DEVICE_ID_STEELSERIES_SRWS1	0x1410
drivers/hid/hid-steam.c +708 -139

1,172 lines not on this page · raw message

Signature · 1 line
2.54.0
#2 lore raw

Re: [PATCH 1/5] HID: steam: Initial 2026 Steam Controller support

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

New issues:
- [High] Battery registration fails due to thermal zone name length limits if the device serial number is long.
- [Medium] Incorrect initialization of battery temperature results in reporting 0.2 °C.
- [Low] Duplicate error logging block introduced in steam_recv_report_id.

Pre-existing issues:
- [High] Probe failure leaves a registered power supply with dangling devm pointers.
- [High] Unbinding the driver while the device is active can lead to a Use-After-Free in steam_work_connect_cb. --

--- Patch [1]: [PATCH 1/5] HID: steam: Initial 2026 Steam Controller support --- commit 10f5a4093529f8973ab3200723fc61d0c419c6bb
Author: Vicki Pfau <[email protected]>

HID: steam: Initial 2026 Steam Controller support

This brings support for the 2026 Steam Controller, comparably featureful to the existing support for the Steam Deck.

Quoted 4 lines
drivers/hid/hid-steam.c +0 -0
index 6199f67f3c4cd..111e57926d049 100644

[ ... ]

Quoted 33 lines
drivers/hid/hid-steam.c +18 -4
@@ -378,21 +459,38 @@ static int steam_recv_report(struct steam_device *steam,
 		return -ENOMEM;
 
 	/*
-	 * The report ID is always 0, so strip the first byte from the output.
+	 * The report ID is consistent, so strip the first byte from the output.
 	 * hid_report_len() is not counting the report ID, so +1 to the length
 	 * or else we get a EOVERFLOW. We are safe from a buffer overflow
 	 * because hid_alloc_report_buf() allocates +7 bytes.
 	 */
-	ret = hid_hw_raw_request(steam->hdev, 0x00,
-			buf, hid_report_len(r) + 1,
-			HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
+	if (!(steam->quirks & STEAM_QUIRK_IBEX))
+		len += 1;
+
+	/*
+	 * Sometimes the wireless controller fails with EPIPE
+	 * when sending a feature report.
+	 * Doing a HID_REQ_GET_REPORT and waiting for a while
+	 * seems to fix that.
+	 */
+	do {
+		ret = hid_hw_raw_request(steam->hdev, report_id,
+				buf, len,
+				HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
+		if (ret != -EPIPE)
+			break;
+		msleep(20);
+	} while (--retries);
 	if (ret > 0) {
 		/* Remove the report ID from the return buffer */
 		ret--;

drivers/hid/hid-steam.c static int steam_recv_report(struct steam_device *steam, · line 496

Quoted 10 lines
drivers/hid/hid-steam.c +3 -0
 		size = min(size, ret);
 		memcpy(data, buf + 1, size);
 	}
+
 	kfree(buf);
+	if (ret < 0)
+		hid_err(steam->hdev, "%s: error %d\n", __func__, ret);
 
 	if (ret < 0)
 		hid_err(steam->hdev, "%s: error %d\n", __func__, ret);

[Severity: Low]
Is this error logging block accidentally duplicated? There are two identical checks and hid_err() calls sequentially in steam_recv_report_id(), which will cause the exact same error message to be printed twice upon a single hid_hw_raw_request() failure.

[ ... ]

Quoted 7 lines
drivers/hid/hid-steam.c +1 -2
@@ -854,19 +1051,35 @@ static int steam_battery_register(struct steam_device *steam)
 	int ret;
 
 	steam->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
-	steam->battery_desc.properties = steam_battery_props;
-	steam->battery_desc.num_properties = ARRAY_SIZE(steam_battery_props);
+	if (steam->quirks & STEAM_QUIRK_IBEX) {

drivers/hid/hid-steam.c static int steam_battery_register(struct steam_device *steam) · line 1064

Quoted 10 lines
drivers/hid/hid-steam.c +10 -0
+		steam->battery_desc.properties = steam_ibex_battery_props;
+		steam->battery_desc.num_properties = ARRAY_SIZE(steam_ibex_battery_props);
+		/*
+		 * Ibex needs a shorter name as it has a temperature and the
+		 * thermal zone name length limit is 20 characters. It's more
+		 * ambiguous sounding, so let's only use it when needed.
+		 */
+		steam->battery_desc.name = devm_kasprintf(&steam->hdev->dev,
+				GFP_KERNEL, "steam-%s",
+				steam->serial_no);

[Severity: High]
Will this name exceed the 20-character maximum permitted for a thermal zone device type if the serial number is long?

Because STEAM_SERIAL_LEN is 21, a serial number of 14 characters or longer combined with the "steam-" prefix will exceed 20 characters. This causes thermal_zone_device_register_with_trips() to return -EINVAL, resulting in power_supply_register() and steam_battery_register() failing entirely.

Quoted 11 lines
drivers/hid/hid-steam.c +7 -3
+	} else {
+		steam->battery_desc.properties = steam_battery_props;
+		steam->battery_desc.num_properties = ARRAY_SIZE(steam_battery_props);
+		steam->battery_desc.name = devm_kasprintf(&steam->hdev->dev,
+				GFP_KERNEL, "steam-controller-%s-battery",
+				steam->serial_no);
+	}
 	steam->battery_desc.get_property = steam_battery_get_property;
-	steam->battery_desc.name = devm_kasprintf(&steam->hdev->dev,
-			GFP_KERNEL, "steam-controller-%s-battery",
-			steam->serial_no);

drivers/hid/hid-steam.c static int steam_battery_register(struct steam_device *steam) · line 1081

Quoted 10 lines
drivers/hid/hid-steam.c +3 -1
 	if (!steam->battery_desc.name)
 		return -ENOMEM;
 
 	/* avoid the warning of 0% battery while waiting for the first info */
 	spin_lock_irqsave(&steam->lock, flags);
-	steam->voltage = 3000;
+	steam->battery_voltage = 3000;
 	steam->battery_charge = 100;
+	steam->battery_current = 0;
+	steam->battery_temp = 200;

[Severity: Medium]
Does this initialize the default temperature to 0.2 degrees Celsius?

In steam_battery_get_property(), this value is divided by 100:
val->intval = temp / 100;

Since the power supply subsystem expects POWER_SUPPLY_PROP_TEMP in tenths of a degree Celsius, returning 2 represents 0.2 C. If the intended default temperature was 20.0 C, should this be initialized to 20000?

Quoted 4 lines
drivers/hid/hid-steam.c +1 -0
+	steam->battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
 	spin_unlock_irqrestore(&steam->lock, flags);
 
 	battery = power_supply_register(&steam->hdev->dev,

[ ... ]

Quoted 14 lines
drivers/hid/hid-steam.c +3 -9
@@ -1149,51 +1380,42 @@ static int steam_register(struct steam_device *steam)
-	spin_lock_irqsave(&steam->lock, flags);
-	client_opened = steam->client_opened;
-	spin_unlock_irqrestore(&steam->lock, flags);
+	/* ignore battery errors, we can live without it */
+	if (steam->quirks & (STEAM_QUIRK_WIRELESS | STEAM_QUIRK_IBEX))
+		steam_battery_register(steam);
 
-	if (!client_opened) {
-		steam_set_lizard_mode(steam, lizard_mode);
-		ret = steam_input_register(steam);
-		if (ret != 0)
-			goto steam_register_input_fail;
-		ret = steam_sensors_register(steam);

drivers/hid/hid-steam.c static int steam_register(struct steam_device *steam) · line 1390

Quoted 10 lines
drivers/hid/hid-steam.c +7 -3
-		if (ret != 0)
-			goto steam_register_sensors_fail;
-	}
+	steam_set_lizard_mode(steam, lizard_mode);
+	ret = steam_input_register(steam);
+	if (ret != 0)
+		goto steam_register_input_fail;
+	ret = steam_sensors_register(steam);
+	if (ret != 0)
+		goto steam_register_sensors_fail;

[Severity: High]
This is a pre-existing issue, but does this error path leave the battery registered if steam_input_register() or steam_sensors_register() fails?

If steam_register() fails, steam_probe() jumps to the err_hw_close label, which bypasses steam_unregister(). Because steam_probe() returns an error, devres will free the steam_device structure.

Since the battery was not unregistered, it remains active in sysfs, and any subsequent userspace read of the battery's properties will dereference the freed steam_device pointer, causing a kernel use-after-free.

[ ... ]

Quoted 16 lines
drivers/hid/hid-steam.c +10 -2
@@ -1989,8 +2462,84 @@ static int steam_raw_event(struct hid_device *hdev,
 		return 0;
 
 	if (steam->client_opened)
-		hid_input_report(steam->client_hdev, HID_FEATURE_REPORT,
-				data, size, 0);
+		hid_input_report(steam->client_hdev, report->type, data, size, 0);
+
+	/* Ibex uses a different report format */
+	if (steam->quirks & STEAM_QUIRK_IBEX) {
+		if (report->type != HID_INPUT_REPORT)
+			return 0;
+
+		switch (report->id) {
+		case REPORT_ID_INPUT:
+			if (size != 54)

[ ... ]

drivers/hid/hid-steam.c static int steam_raw_event(struct hid_device *hdev, · line 2537

Quoted 12 lines
drivers/hid/hid-steam.c +12 -0
+		case REPORT_ID_WIRELESS_EVENT:
+			if (size != 2)
+				return 0;
+			switch (data[1]) {
+			case WIRELESS_EVENT_DISCONNECT:
+				steam_do_connect_event(steam, false);
+				break;
+			case WIRELESS_EVENT_CONNECT:
+				steam_do_connect_event(steam, true);
+				break;
+			}
+			break;

[Severity: High]
This is a pre-existing issue, but can this lead to a use-after-free during driver unbind?

When the driver is unbound, steam_remove() cancels the steam->work_connect work item and calls steam_unregister(), but hid_hw_stop() is not called until the very end.

In this window, the device can still stream data and trigger steam_raw_event(), which can call steam_do_connect_event(). This reschedules steam->work_connect back onto the workqueue. Once steam_remove() returns, devres frees the steam_device structure, causing the scheduled work item to execute steam_work_connect_cb() on freed memory.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

PATCH 2/5 HID: steam: Fix wording of connect/disconnect logs

In reply to Vicki Pfau

It always said Controller, even on Deck. Since we special-case other instances of Controller vs. Deck in strings, let's be consistent here too.

---
 drivers/hid/hid-steam.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/hid/hid-steam.c +4 -2
index 111e57926d04..fdf32722c8b3 100644
@@ -1397,7 +1397,8 @@ static int steam_register(struct steam_device *steam)
 			"%s:steam_get_attributes failed with error %d\n",
 			__func__, ret);
 
-	hid_info(steam->hdev, "Steam Controller '%s' connected",
+	hid_info(steam->hdev, "Steam %s '%s' connected",
+			steam->quirks & STEAM_QUIRK_DECK ? "Deck" : "Controller",
 			steam->serial_no);
 
 	/* ignore battery errors, we can live without it */
@@ -1429,7 +1430,8 @@ static void steam_unregister(struct steam_device *steam)
 	if (!steam->serial_no[0])
 		return;
 
-	hid_info(steam->hdev, "Steam Controller '%s' disconnected",
+	hid_info(steam->hdev, "Steam %s '%s' disconnected",
+			steam->quirks & STEAM_QUIRK_DECK ? "Deck" : "Controller",
 			steam->serial_no);
 	steam_battery_unregister(steam);
 	steam_sensors_unregister(steam);
Signature · 1 line
2.54.0
#4 lore raw

Re: [PATCH 2/5] HID: steam: Fix wording of connect/disconnect logs

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The error path in steam_register() fails to unregister the battery, leading to a leaked power supply and subsequent use-after-free if probe fails. --

--- Patch [2]: [PATCH 2/5] HID: steam: Fix wording of connect/disconnect logs --- commit 86b8e9ab8ffc7b90075c933cefda13a663141100
Author: Vicki Pfau <[email protected]>

HID: steam: Fix wording of connect/disconnect logs

It always said Controller, even on Deck. Since we special-case other instances of Controller vs. Deck in strings, let's be consistent here too.

Quoted 5 lines
drivers/hid/hid-steam.c +0 -0
index 111e57926d049..fdf32722c8b31 100644

drivers/hid/hid-steam.c static int steam_register(struct steam_device *steam) · line 1404

Quoted 9 lines
drivers/hid/hid-steam.c +2 -1
@@ -1397,7 +1397,8 @@ static int steam_register(struct steam_device *steam)
 			"%s:steam_get_attributes failed with error %d\n",
 			__func__, ret);
 
-	hid_info(steam->hdev, "Steam Controller '%s' connected",
+	hid_info(steam->hdev, "Steam %s '%s' connected",
+			steam->quirks & STEAM_QUIRK_DECK ? "Deck" : "Controller",
 			steam->serial_no);
 
 	/* ignore battery errors, we can live without it */

[Severity: High]
This is a pre-existing issue, but I noticed a potential use-after-free issue regarding the battery registration near this log message.

If steam_battery_register() is called, but a subsequent registration fails:

drivers/hid/hid-steam.c:steam_register() {
    ...
	if (steam->quirks & (STEAM_QUIRK_WIRELESS | STEAM_QUIRK_IBEX))
		steam_battery_register(steam);
    ...
	ret = steam_input_register(steam);
	if (ret != 0)
		goto steam_register_input_fail;
	ret = steam_sensors_register(steam);
	if (ret != 0)
		goto steam_register_sensors_fail;
    ...
steam_register_sensors_fail:
	steam_input_unregister(steam);
steam_register_input_fail:
	return ret;
}

Could this error path leave the power supply registered when steam_register() returns an error?

If probe fails, the driver core will release devres allocations, freeing the steam_device struct. A user querying sysfs for battery properties could then trigger a use-after-free since the power supply subsystem would still have the freed pointer.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author maintainer
#5 lore raw

PATCH 3/5 HID: steam: Don't set feature reports when disconnecting

In reply to Vicki Pfau

When an input device is closed, we set a feature report to reset lizard mode and IMU mode. However, if the input device is closed because it was removed, then we will necessarily error out when sending this, resulting in logged errors. Since an error here is expected, we should just fail silently.

---
 drivers/hid/hid-steam.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
drivers/hid/hid-steam.c +11 -6
index fdf32722c8b3..2d60b79441b6 100644
@@ -489,12 +489,13 @@ static int steam_recv_report_id(struct steam_device *steam,
 	}
 
 	kfree(buf);
-	if (ret < 0)
-		hid_err(steam->hdev, "%s: error %d\n", __func__, ret);
-
-	if (ret < 0)
+	/*
+	 * Don't log if the failure is -ENODEV, as this
+	 * can happen normally on disconnect.
+	 */
+	if (ret < 0 && ret != -ENODEV)
 		hid_err(steam->hdev, "%s: error %d\n", __func__, ret);
-	else
+	else if (ret > 0)
 		hid_dbg(steam->hdev, "Received report %*ph\n", size, data);
 	if (ret < 0)
 		return ret;
@@ -572,7 +573,11 @@ static int steam_send_report_id(struct steam_device *steam,
 	} while (--retries);
 
 	kfree(buf);
-	if (ret < 0)
+	/*
+	 * Don't log if the failure is -ENODEV, as this
+	 * can happen normally on disconnect.
+	 */
+	if (ret < 0 && ret != -ENODEV)
 		hid_err(steam->hdev, "%s: error %d (%*ph)\n", __func__,
 				ret, size, cmd);
 	return ret;
Signature · 1 line
2.54.0
author maintainer
#6 lore raw

PATCH 4/5 HID: steam: Clean up locking

In reply to Vicki Pfau

This cleans up several issues with locking behavior, including RCU accesses not being guarded behind a lock.

---
 drivers/hid/hid-steam.c | 86 ++++++++++++++++++++---------------------
 1 file changed, 43 insertions(+), 43 deletions(-)
drivers/hid/hid-steam.c +43 -43
index 2d60b79441b6..87af4bee9f5f 100644
@@ -632,7 +632,6 @@ static int steam_exchange_report(struct steam_device *steam, u8 *cmd, int csize,
 	unsigned int retries = 5;
 	int ret;
 
-	guard(mutex)(&steam->report_mutex);
 	do {
 		ret = steam_send_report(steam, cmd, csize);
 		if (ret < 0)
@@ -728,7 +727,6 @@ static int steam_get_conn_status(struct steam_device *steam)
 	else
 		report_id = 0;
 
-	guard(mutex)(&steam->report_mutex);
 	ret = steam_send_report_id(steam, cmd, sizeof(cmd), report_id);
 	if (ret < 0)
 		return ret;
@@ -786,7 +784,6 @@ static inline int steam_haptic_pulse(struct steam_device *steam, u8 pad,
 		report[8] = count >> 8;
 		report[9] = gain;
 
-		guard(mutex)(&steam->report_mutex);
 		ret = steam_send_report(steam, report, 10);
 	}
 
@@ -828,7 +825,6 @@ static inline int steam_haptic_rumble(struct steam_device *steam,
 		report[9] = left_gain;
 		report[10] = right_gain;
 
-		guard(mutex)(&steam->report_mutex);
 		ret = steam_send_report(steam, report, sizeof(report));
 	}
 	return ret;
@@ -839,8 +835,10 @@ static void steam_haptic_rumble_cb(struct work_struct *work)
 	struct steam_device *steam = container_of(work, struct steam_device,
 							rumble_work);
 
+	mutex_lock(&steam->report_mutex);
 	steam_haptic_rumble(steam, 0, steam->rumble_left,
 		steam->rumble_right, 2, 0);
+	mutex_unlock(&steam->report_mutex);
 }
 
 static void steam_coalesce_rumble_cb(struct work_struct *work)
@@ -849,8 +847,10 @@ static void steam_coalesce_rumble_cb(struct work_struct *work)
 							struct steam_device,
 							coalesce_rumble_work);
 
+	mutex_lock(&steam->report_mutex);
 	steam_haptic_rumble(steam, 0, steam->rumble_left,
 		steam->rumble_right, 2, 0);
+	mutex_unlock(&steam->report_mutex);
 
 	if (steam->rumble_left || steam->rumble_right)
 		schedule_delayed_work(&steam->coalesce_rumble_work, HZ / 20);
@@ -882,7 +882,6 @@ static void steam_set_lizard_mode(struct steam_device *steam, bool enable)
 	if (steam->gamepad_mode)
 		enable = false;
 
-	mutex_lock(&steam->report_mutex);
 	if (enable) {
 		/* enable esc, enter, cursors */
 		steam_send_report_byte(steam, ID_SET_DEFAULT_DIGITAL_MAPPINGS);
@@ -905,14 +904,13 @@ static void steam_set_lizard_mode(struct steam_device *steam, bool enable)
 				SETTING_RIGHT_TRACKPAD_MODE, TRACKPAD_NONE, /* disable mouse */
 				0);
 	}
-	mutex_unlock(&steam->report_mutex);
 }
 
 static int steam_input_open(struct input_dev *dev)
 {
 	struct steam_device *steam = input_get_drvdata(dev);
 	unsigned long flags;
-	bool set_lizard_mode;
+	bool client_opened;
 
 	/*
 	 * Disabling lizard mode automatically is only done on the Steam
@@ -921,9 +919,10 @@ static int steam_input_open(struct input_dev *dev)
 	 */
 	if (!(steam->quirks & (STEAM_QUIRK_DECK | STEAM_QUIRK_IBEX))) {
 		spin_lock_irqsave(&steam->lock, flags);
-		set_lizard_mode = !steam->client_opened && lizard_mode;
+		client_opened = steam->client_opened;
 		spin_unlock_irqrestore(&steam->lock, flags);
-		if (set_lizard_mode)
+		guard(mutex)(&steam->report_mutex);
+		if (!client_opened && lizard_mode)
 			steam_set_lizard_mode(steam, false);
 	}
 
@@ -934,13 +933,14 @@ static void steam_input_close(struct input_dev *dev)
 {
 	struct steam_device *steam = input_get_drvdata(dev);
 	unsigned long flags;
-	bool set_lizard_mode;
+	bool client_opened;
 
 	if (!(steam->quirks & (STEAM_QUIRK_DECK | STEAM_QUIRK_IBEX))) {
 		spin_lock_irqsave(&steam->lock, flags);
-		set_lizard_mode = !steam->client_opened && lizard_mode;
+		client_opened = steam->client_opened;
 		spin_unlock_irqrestore(&steam->lock, flags);
-		if (set_lizard_mode)
+		guard(mutex)(&steam->report_mutex);
+		if (!client_opened && lizard_mode)
 			steam_set_lizard_mode(steam, true);
 	}
 }
@@ -948,14 +948,11 @@ static void steam_input_close(struct input_dev *dev)
 static int steam_sensor_open(struct input_dev *dev)
 {
 	struct steam_device *steam = input_get_drvdata(dev);
-	unsigned long flags;
-	bool client_opened;
 
-	spin_lock_irqsave(&steam->lock, flags);
-	client_opened = steam->client_opened;
-	spin_unlock_irqrestore(&steam->lock, flags);
-	if (client_opened)
-		return 0;
+	scoped_guard(spinlock_irqsave, &steam->lock) {
+		if (steam->client_opened)
+			return 0;
+	}
 
 	guard(mutex)(&steam->report_mutex);
 	steam_write_settings(steam, SETTING_IMU_MODE,
@@ -968,14 +965,11 @@ static int steam_sensor_open(struct input_dev *dev)
 static void steam_sensor_close(struct input_dev *dev)
 {
 	struct steam_device *steam = input_get_drvdata(dev);
-	unsigned long flags;
-	bool client_opened;
 
-	spin_lock_irqsave(&steam->lock, flags);
-	client_opened = steam->client_opened;
-	spin_unlock_irqrestore(&steam->lock, flags);
-	if (client_opened)
-		return;
+	scoped_guard(spinlock_irqsave, &steam->lock) {
+		if (steam->client_opened)
+			return;
+	}
 
 	guard(mutex)(&steam->report_mutex);
 	steam_write_settings(steam, SETTING_IMU_MODE, 0, 0);
@@ -1460,6 +1454,7 @@ static void steam_work_connect_cb(struct work_struct *work)
 	bool opened;
 	int ret;
 
+	guard(mutex)(&steam->report_mutex);
 	spin_lock_irqsave(&steam->lock, flags);
 	opened = steam->client_opened;
 	connected = steam->connected;
@@ -1482,23 +1477,28 @@ static void steam_mode_switch_cb(struct work_struct *work)
 							struct steam_device, mode_switch);
 	unsigned long flags;
 	bool client_opened;
+	bool gamepad_mode;
+
 	if (!lizard_mode)
 		return;
 
+	spin_lock_irqsave(&steam->lock, flags);
 	steam->gamepad_mode = !steam->gamepad_mode;
+	gamepad_mode = steam->gamepad_mode;
+	client_opened = steam->client_opened;
+	spin_unlock_irqrestore(&steam->lock, flags);
+
+	guard(mutex)(&steam->report_mutex);
 	hid_dbg(steam->hdev, "%s: switching gamepad mode to %i\n", __func__, steam->gamepad_mode);
-	if (steam->gamepad_mode)
+	if (gamepad_mode)
 		steam_set_lizard_mode(steam, false);
 	else {
-		spin_lock_irqsave(&steam->lock, flags);
-		client_opened = steam->client_opened;
-		spin_unlock_irqrestore(&steam->lock, flags);
 		if (!client_opened)
 			steam_set_lizard_mode(steam, lizard_mode);
 	}
 
 	steam_haptic_pulse(steam, STEAM_PAD_RIGHT, 0x190, 0, 1, 0);
-	if (steam->gamepad_mode) {
+	if (gamepad_mode) {
 		steam_haptic_pulse(steam, STEAM_PAD_LEFT, 0x14D, 0x14D, 0x2D, 0);
 	} else {
 		steam_haptic_pulse(steam, STEAM_PAD_LEFT, 0x1F4, 0x1F4, 0x1E, 0);
@@ -1728,7 +1728,9 @@ static int steam_probe(struct hid_device *hdev,
 			steam->connected = true;
 	}
 	if (steam->connected) {
+		mutex_lock(&steam->report_mutex);
 		ret = steam_register(steam);
+		mutex_unlock(&steam->report_mutex);
 		if (ret) {
 			hid_err(hdev,
 				"%s:steam_register failed with error %d\n",
@@ -1787,19 +1789,17 @@ static void steam_remove(struct hid_device *hdev)
 	if (steam->quirks & STEAM_QUIRK_WIRELESS) {
 		hid_info(hdev, "Steam wireless receiver disconnected");
 	}
+	guard(mutex)(&steam->report_mutex);
 	steam_unregister(steam);
 	hid_hw_stop(hdev);
 }
 
 static void steam_do_connect_event(struct steam_device *steam, bool connected)
 {
-	unsigned long flags;
 	bool changed;
 
-	spin_lock_irqsave(&steam->lock, flags);
 	changed = steam->connected != connected;
 	steam->connected = connected;
-	spin_unlock_irqrestore(&steam->lock, flags);
 
 	if (changed && schedule_work(&steam->work_connect) == 0)
 		dbg_hid("%s: connected=%d event already queued\n",
@@ -2228,8 +2228,6 @@ static void steam_do_deck_sensors_event(struct steam_device *steam,
 static void steam_do_battery_event(struct steam_device *steam,
 		struct power_supply *battery, u8 *data)
 {
-	unsigned long flags;
-
 	s16 volts = steam_le16(data + 12);
 	u8 batt = data[14];
 
@@ -2237,10 +2235,8 @@ static void steam_do_battery_event(struct steam_device *steam,
 	rcu_read_lock();
 	battery = rcu_dereference(steam->battery);
 	if (likely(battery)) {
-		spin_lock_irqsave(&steam->lock, flags);
 		steam->battery_voltage = volts;
 		steam->battery_charge = batt;
-		spin_unlock_irqrestore(&steam->lock, flags);
 		power_supply_changed(battery);
 	}
 	rcu_read_unlock();
@@ -2425,15 +2421,12 @@ static void steam_do_ibex_battery_event(struct steam_device *steam,
 		struct power_supply *battery,
 		const struct steam_ibex_battery_status *data)
 {
-	unsigned long flags;
-
 	/* Creating the battery may have failed */
 	guard(rcu)();
 	battery = rcu_dereference(steam->battery);
 	if (!likely(battery))
 		return;
 
-	spin_lock_irqsave(&steam->lock, flags);
 	steam->battery_voltage = get_unaligned_le16(&data->battery_voltage);
 	steam->battery_current = get_unaligned_le16(&data->battery_current);
 	steam->battery_temp = get_unaligned_le16(&data->temperature);
@@ -2452,7 +2445,6 @@ static void steam_do_ibex_battery_event(struct steam_device *steam,
 		steam->battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
 		break;
 	}
-	spin_unlock_irqrestore(&steam->lock, flags);
 	power_supply_changed(battery);
 }
 
@@ -2468,6 +2460,7 @@ static int steam_raw_event(struct hid_device *hdev,
 	if (!steam)
 		return 0;
 
+	guard(spinlock_irqsave)(&steam->lock);
 	if (steam->client_opened)
 		hid_input_report(steam->client_hdev, report->type, data, size, 0);
 
@@ -2631,6 +2624,8 @@ static int steam_param_set_lizard_mode(const char *val,
 {
 	struct steam_device *steam;
 	int ret;
+	bool client_opened;
+	unsigned long flags;
 
 	ret = param_set_bool(val, kp);
 	if (ret)
@@ -2638,8 +2633,13 @@ static int steam_param_set_lizard_mode(const char *val,
 
 	mutex_lock(&steam_devices_lock);
 	list_for_each_entry(steam, &steam_devices, list) {
-		if (!steam->client_opened)
+		spin_lock_irqsave(&steam->lock, flags);
+		client_opened = steam->client_opened;
+		spin_unlock_irqrestore(&steam->lock, flags);
+		if (client_opened) {
+			guard(mutex)(&steam->report_mutex);
 			steam_set_lizard_mode(steam, lizard_mode);
+		}
 	}
 	mutex_unlock(&steam_devices_lock);
 	return 0;
Signature · 1 line
2.54.0
#7 lore raw

Re: [PATCH 4/5] HID: steam: Clean up locking

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:

New issues:
- [High] Self-deadlock in steam_unregister due to report_mutex acquisition.
- [High] AB-BA lock inversion between steam_devices_lock and steam->report_mutex.
- [Medium] Inverted lizard mode logic in steam_param_set_lizard_mode breaks the parameter.
- [Medium] Missing report_mutex in steam_get_conn_status creates a race condition with concurrent hardware access.

Pre-existing issues:
- [High] Use-after-free caused by cancelling workqueues before stopping the hardware.
- [High] Resource leak and use-after-free of power_supply object on probe failure. --

--- Patch [4]: [PATCH 4/5] HID: steam: Clean up locking --- commit 4a13ea9d8650f6b804ba3ebe1f98c65bb697f0c4
Author: Vicki Pfau <[email protected]>

HID: steam: Clean up locking

This cleans up several issues with locking behavior, including RCU accesses not being guarded behind a lock.

Quoted 4 lines
drivers/hid/hid-steam.c +0 -0
index 2d60b79441b69..87af4bee9f5ff 100644

[ ... ]

drivers/hid/hid-steam.c static int steam_get_conn_status(struct steam_device *steam) · line 730

Quoted 6 lines
drivers/hid/hid-steam.c +0 -1
@@ -728,7 +727,6 @@ static int steam_get_conn_status(struct steam_device *steam)
 	else
 		report_id = 0;
 
-	guard(mutex)(&steam->report_mutex);
 	ret = steam_send_report_id(steam, cmd, sizeof(cmd), report_id);

[Severity: Medium]
Will this removal create a race condition with concurrent hardware access? During steam_probe() in hid-steam.c, the hardware is opened and interrupts are enabled before steam_get_conn_status() is called. If the device immediately sends a connection event, the interrupt handler schedules steam_work_connect_cb(), which acquires report_mutex and sends feature reports. Without the mutex here, can the control transfers from steam_get_conn_status() interleave with the worker's transfers and confuse the controller firmware?

[ ... ]

drivers/hid/hid-steam.c

Quoted 12 lines
drivers/hid/hid-steam.c +2 -0
@@ -1728,7 +1728,9 @@ static int steam_probe(struct hid_device *hdev,
 			steam->connected = true;
 	}
 	if (steam->connected) {
+		mutex_lock(&steam->report_mutex);
 		ret = steam_register(steam);
+		mutex_unlock(&steam->report_mutex);
 		if (ret) {
 			hid_err(hdev,
 				"%s:steam_register failed with error %d\n",
drivers/hid/hid-steam.c +0 -0
 				__func__, ret);
 			goto err_hw_close;

[Severity: High]
This isn't a bug introduced by this patch, but does steam_probe() leak the power_supply object if steam_register() fails? If steam_input_register() or steam_sensors_register() fails inside steam_register(), it jumps to an error label and returns an error without unregistering the battery created via steam_battery_register(). The error propagates here, skipping steam_unregister() and jumping to err_hw_close. Doesn't this leave the power_supply device registered while devm frees the steam_device, leading to a use-after-free when userspace interacts with the stale power_supply?

[ ... ]

drivers/hid/hid-steam.c static void steam_remove(struct hid_device *hdev) · line 1795

Quoted 8 lines
drivers/hid/hid-steam.c +1 -0
@@ -1787,19 +1789,17 @@ static void steam_remove(struct hid_device *hdev)
 	if (steam->quirks & STEAM_QUIRK_WIRELESS) {
 		hid_info(hdev, "Steam wireless receiver disconnected");
 	}
+	guard(mutex)(&steam->report_mutex);
 	steam_unregister(steam);
 	hid_hw_stop(hdev);
 }

[Severity: High]
Can holding report_mutex across steam_unregister() cause a self-deadlock? When a device is removed, steam_remove() acquires report_mutex here and calls steam_unregister(). steam_unregister() calls cancel_work_sync() on rumble_work, and steam_input_unregister(). If the rumble work is running in steam_haptic_rumble_cb(), it will attempt to acquire the same report_mutex and block forever. Additionally, unregistering the input device triggers the steam_input_close() callback, which also attempts to acquire this non-recursive report_mutex.

[Severity: High]
This is a pre-existing issue, but is there a use-after-free window here in steam_remove() due to the order of teardown? cancel_work_sync() for work_connect is called just before the quoted code, but hid_hw_stop() is called at the end. If an interrupt arrives between the cancellation and hid_hw_stop(), steam_raw_event() can process it and re-queue the work. When steam_remove() returns, devm frees the steam_device, but the queued work could later execute steam_work_connect_cb() on the freed memory.

[ ... ]

Quoted 14 lines
drivers/hid/hid-steam.c +2 -1
@@ -2631,6 +2624,8 @@ static int steam_param_set_lizard_mode(const char *val,
 {
 	struct steam_device *steam;
 	int ret;
+	bool client_opened;
+	unsigned long flags;
 
 	ret = param_set_bool(val, kp);
 	if (ret)
@@ -2638,8 +2633,13 @@ static int steam_param_set_lizard_mode(const char *val,
 
 	mutex_lock(&steam_devices_lock);
 	list_for_each_entry(steam, &steam_devices, list) {
-		if (!steam->client_opened)

drivers/hid/hid-steam.c static int steam_param_set_lizard_mode(const char *val, · line 2645

Quoted 10 lines
drivers/hid/hid-steam.c +6 -0
+		spin_lock_irqsave(&steam->lock, flags);
+		client_opened = steam->client_opened;
+		spin_unlock_irqrestore(&steam->lock, flags);
+		if (client_opened) {
+			guard(mutex)(&steam->report_mutex);
 			steam_set_lizard_mode(steam, lizard_mode);
+		}
 	}
 	mutex_unlock(&steam_devices_lock);
 	return 0;

[Severity: High]
Does acquiring report_mutex while holding steam_devices_lock introduce an lock inversion? Here in steam_param_set_lizard_mode(), steam_devices_lock is acquired first, followed by report_mutex. However, concurrently in steam_probe() or steam_work_connect_cb(), report_mutex is acquired first, and then steam_register() is called, which attempts to acquire steam_devices_lock to add the device to the list. Could this lead to an indefinite deadlock?

[Severity: Medium]
Was the condition intentionally changed from !steam->client_opened to if (client_opened)? It seems like this applies the lizard mode settings to devices actively controlled by the Steam client, and ignores devices that actually need the fallback mappings.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author maintainer
#8 lore raw

PATCH 5/5 HID: steam: Zero out inputs when disabling gamepad mode

In reply to Vicki Pfau

When gamepad mode is disabled the gamepad input devices will stop receiving updates. However, in the case where there are buttons still pressed this will appear as an indefinitely-held button. Instead we should zero out the inputs to make it look like things are all released. We do the same thing for gyroscope inputs to make sure it doesn't look like it's endlessly rotating, but we freeze the accelerometer input since zero isn't a neutral input on the surface of the Earth.

---
 drivers/hid/hid-steam.c | 69 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 67 insertions(+), 2 deletions(-)
drivers/hid/hid-steam.c +67 -2
index 87af4bee9f5f..93a6f4e17fb7 100644
@@ -1490,11 +1490,76 @@ static void steam_mode_switch_cb(struct work_struct *work)
 
 	guard(mutex)(&steam->report_mutex);
 	hid_dbg(steam->hdev, "%s: switching gamepad mode to %i\n", __func__, steam->gamepad_mode);
-	if (gamepad_mode)
+	if (gamepad_mode) {
 		steam_set_lizard_mode(steam, false);
-	else {
+	} else {
+		struct input_dev *input;
+		struct input_dev *sensors;
+
 		if (!client_opened)
 			steam_set_lizard_mode(steam, lizard_mode);
+
+		/*
+		 * Zero out inputs so it doesn't look like we're holding
+		 * anything indefinitely.
+		 */
+		guard(spinlock_irqsave)(&steam->lock);
+		rcu_read_lock();
+		input = rcu_dereference(steam->input);
+		if (likely(input)) {
+			input_report_key(input, BTN_TR2, 0);
+			input_report_key(input, BTN_TL2, 0);
+			input_report_key(input, BTN_TR, 0);
+			input_report_key(input, BTN_TL, 0);
+			input_report_key(input, BTN_Y, 0);
+			input_report_key(input, BTN_B, 0);
+			input_report_key(input, BTN_X, 0);
+			input_report_key(input, BTN_A, 0);
+			input_report_key(input, BTN_DPAD_UP, 0);
+			input_report_key(input, BTN_DPAD_RIGHT, 0);
+			input_report_key(input, BTN_DPAD_LEFT, 0);
+			input_report_key(input, BTN_DPAD_DOWN, 0);
+			input_report_key(input, BTN_SELECT, 0);
+			input_report_key(input, BTN_MODE, 0);
+			input_report_key(input, BTN_START, 0);
+			input_report_key(input, BTN_THUMBR, 0);
+			input_report_key(input, BTN_THUMBL, 0);
+			input_report_key(input, BTN_THUMB, 0);
+			input_report_key(input, BTN_THUMB2, 0);
+			input_report_key(input, BTN_GRIPL, 0);
+			input_report_key(input, BTN_GRIPR, 0);
+
+			input_report_abs(input, ABS_X, 0);
+			input_report_abs(input, ABS_Y, 0);
+			input_report_abs(input, ABS_RX, 0);
+			input_report_abs(input, ABS_RY, 0);
+			input_report_abs(input, ABS_HAT0X, 0);
+			input_report_abs(input, ABS_HAT0Y, 0);
+			input_report_abs(input, ABS_HAT2Y, 0);
+			input_report_abs(input, ABS_HAT2X, 0);
+
+			if (steam->quirks & (STEAM_QUIRK_DECK | STEAM_QUIRK_IBEX)) {
+				input_report_key(input, BTN_BASE, 0);
+				input_report_key(input, BTN_GRIPL2, 0);
+				input_report_key(input, BTN_GRIPR2, 0);
+
+				input_report_abs(input, ABS_HAT1X, 0);
+				input_report_abs(input, ABS_HAT1Y, 0);
+			}
+
+			input_sync(input);
+		}
+		sensors = rcu_dereference(steam->sensors);
+		if (likely(sensors)) {
+			/* Skip accelerometers since 0 isn't a neutral input */
+
+			input_report_abs(sensors, ABS_RX, 0);
+			input_report_abs(sensors, ABS_RY, 0);
+			input_report_abs(sensors, ABS_RZ, 0);
+
+			input_sync(sensors);
+		}
+		rcu_read_unlock();
 	}
 
 	steam_haptic_pulse(steam, STEAM_PAD_RIGHT, 0x190, 0, 1, 0);
Signature · 1 line
2.54.0
#9 lore raw

Re: [PATCH 5/5] HID: steam: Zero out inputs when disabling gamepad mode

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [High] AB-BA deadlock between report_mutex and workqueues during device unregistration.

Pre-existing issues:
- [High] Concurrent unregistration in steam_probe error path leads to a double free of input devices. --

--- Patch [5]: [PATCH 5/5] HID: steam: Zero out inputs when disabling gamepad mode --- commit 7a64f604e0a34949b1434a03544f0ca71ef9305f
Author: Vicki Pfau <[email protected]>

HID: steam: Zero out inputs when disabling gamepad mode

When gamepad mode is disabled the gamepad input devices will stop receiving updates. However, in the case where there are buttons still pressed this will appear as an indefinitely-held button. Instead we should zero out the inputs to make it look like things are all released.

drivers/hid/hid-steam.c static void steam_mode_switch_cb(struct work_struct *work) · line 1491

Quoted 7 lines
drivers/hid/hid-steam.c +0 -0
index 87af4bee9f5ff..93a6f4e17fb75 100644
@@ -1490,11 +1490,76 @@ static void steam_mode_switch_cb(struct work_struct *work)
 
 	guard(mutex)(&steam->report_mutex);

[Severity: High]
Is there a risk of an AB-BA deadlock here during device unregistration?

If steam_remove() is called and acquires steam->report_mutex, it then calls steam_unregister(). If steam_unregister() then calls cancel_delayed_work_sync(&steam->mode_switch) while the mutex is held:

steam_remove()
guard(mutex)(&steam->report_mutex);

  steam_unregister(steam);
    cancel_delayed_work_sync(&steam->mode_switch);

This workqueue callback would block here attempting to acquire the same steam->report_mutex, while steam_remove() blocks waiting for this work to finish. Since the wait condition is permanent, this could hang the USB/HID hotplug subsystem.

[ ... ]

[Severity: High]
This is a pre-existing issue, but could the error path in steam_probe() race with disconnect events and cause a double-free of the input devices?

If steam_probe() fails in hid_add_device(), it jumps to err_steam_unregister and calls steam_unregister() if steam->connected is true (checked locklessly):

steam_probe():
err_steam_unregister:

	if (steam->connected)
		steam_unregister(steam);

Because hid_device_io_start() was already called, a concurrent disconnect event can queue steam_work_connect_cb(). If that worker runs, sees connected == false, and also calls steam_unregister(steam), both threads can concurrently enter steam_input_unregister() since the error path does not hold report_mutex.

Both threads would then read steam->input, find it valid, set it to NULL locklessly, and call input_unregister_device() on the same pointer:

steam_input_unregister():

	input = rcu_dereference(steam->input);
	...
	RCU_INIT_POINTER(steam->input, NULL);
	...
	input_unregister_device(input);

Can this lead to a double-free of the input_dev structure?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5