Skip to contentSkip to conversation

HID: hp: Add support for HyperX headset devices

PATCH Superseded Sofia Schneider 2 participants 2 messages Oct 21 HID

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 HID: hp: Add support for HyperX headset devices

Introduce a HID driver for HyperX Cloud III Wireless headsets, supporting battery reporting and connection status.

Tested with a HyperX Cloud III Wireless only, for lack of other testable devices.

---
v4 -> v5:
- Rebase onto current hid/for-next to resolve merge conflicts in MAINTAINERS, Kconfig, and Makefile.
- Call hid_hw_open() in probe() and hid_hw_close() in remove() to ensure raw events are received.
- Cancel poll_work before stopping hardware in remove().
- Read drvdata->battery under spinlock in hp_headset_battery_work().
- Explicitly include all required headers.
- Minor style fixes.

v3 -> v4:
- Rename driver to hid-hp.

v2 -> v3:
- Unregister power supply on hid_hw_start() failure in probe() to prevent UAF/resource leak.

v1 -> v2:
- Call hid_hw_stop() before cancelling work queues in remove() to prevent UAF.
- Safe unregistration of power supply using the spinlock in remove() to prevent race conditions.
- Cancel battery_work in the hid_hw_start() error path in probe() to prevent UAF.
- Dynamically allocate power supply names to avoid sysfs naming collisions.

v1: https://lore.kernel.org/linux-input/[email protected]/T/
v2: https://lore.kernel.org/linux-input/[email protected]/T/
v3: https://lore.kernel.org/linux-input/[email protected]/T/
v4: https://lore.kernel.org/linux-input/[email protected]/

 MAINTAINERS           |   6 +
 drivers/hid/Kconfig   |  11 ++
 drivers/hid/Makefile  |   1 +
 drivers/hid/hid-hp.c  | 398 ++++++++++++++++++++++++++++++++++++++++++
 drivers/hid/hid-ids.h |   1 +
 5 files changed, 417 insertions(+)
 create mode 100644 drivers/hid/hid-hp.c
MAINTAINERS +6 -0
index c7ccc417cf43..4cd885e61731 100644
@@ -11610,6 +11610,12 @@ F:	include/uapi/linux/hid*
 F:	samples/hid/
 F:	tools/testing/selftests/hid/
 
+HID HP DRIVER
+M:	Sofia Schneider <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/hid/hid-hp.c
+
 HID HYPERX DRIVER
 M:	Benjamin Blume <[email protected]>
 L:	[email protected]
drivers/hid/Kconfig +11 -0
index 2481bc01c435..6c0f37f23b0f 100644
@@ -1252,6 +1252,17 @@ config GREENASIA_FF
 	(like MANTA Warrior MM816 and SpeedLink Strike2 SL-6635) or adapter
 	and want to enable force feedback support for it.
 
+config HID_HP
+	tristate "HP HID devices"
+	depends on USB_HID
+	select POWER_SUPPLY
+	help
+	  Support for HP HID devices, including HyperX headsets.
+
+	  Say Y here if you would like to enable support for HP HID devices.
+	  To compile this driver as a module, choose M here: the module will be called
+	  hid-hp.
+
 config HID_HYPERV_MOUSE
 	tristate "Microsoft Hyper-V mouse driver"
 	depends on HYPERV_VMBUS
drivers/hid/Makefile +1 -0
index 43a8eb0f389b..10e6542303e9 100644
@@ -66,6 +66,7 @@ obj-$(CONFIG_HID_GYRATION)	+= hid-gyration.o
 obj-$(CONFIG_HID_HOLTEK)	+= hid-holtek-kbd.o
 obj-$(CONFIG_HID_HOLTEK)	+= hid-holtek-mouse.o
 obj-$(CONFIG_HID_HOLTEK)	+= hid-holtekff.o
+obj-$(CONFIG_HID_HP)		+= hid-hp.o
 obj-$(CONFIG_HID_HYPERV_MOUSE)	+= hid-hyperv.o
 obj-$(CONFIG_HID_HYPERX)	+= hid-hyperx.o
 obj-$(CONFIG_HID_ICADE)		+= hid-icade.o
drivers/hid/hid-hp.c new file +398 -0

398 lines not on this page · raw message

drivers/hid/hid-ids.h +1 -0
index 91319aa6516d..1f6782b3edc6 100644
@@ -710,6 +710,7 @@
 #define USB_PRODUCT_ID_HP_PIXART_OEM_USB_OPTICAL_MOUSE_0941	0x0941
 #define USB_PRODUCT_ID_HP_PIXART_OEM_USB_OPTICAL_MOUSE_0641	0x0641
 #define USB_PRODUCT_ID_HP_PIXART_OEM_USB_OPTICAL_MOUSE_1f4a	0x1f4a
+#define USB_DEVICE_ID_HP_HYPERX_CLOUD_III_WIRELESS              0x05b7
 
 #define USB_VENDOR_ID_HUION		0x256c
 #define USB_DEVICE_ID_HUION_TABLET	0x006e
Signature · 1 line
2.56.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] HID: hp: wireless_status not initialized when headset is off in hp_probe() - [Medium] HID: hp…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] HID: hp: wireless_status not initialized when headset is off in hp_probe()
- [Medium] HID: hp: battery state not polled on connect in hp_raw_event() --

commit eff0223529a8e019e0454a1c8760eff7536e4ec0
Author: Sofia Schneider <[email protected]>

HID: hp: Add support for HyperX headset devices

This commit introduces the hid-hp driver for HyperX Cloud III Wireless headsets, polling the device to report battery capacity, charging state, and USB wireless connection status.

Quoted 6 lines
drivers/hid/hid-hp.c new file +0 -0
new file mode 100644
index 0000000000000..32164b3c8e04a
@@ -0,0 +1,398 @@

[ ... ]

Quoted 38 lines
drivers/hid/hid-hp.c new file +38 -0
+static void hp_headset_battery_work(struct work_struct *work)
+{
+	struct hp_headset_device *drvdata =
+		container_of(work, struct hp_headset_device, battery_work);
+	struct power_supply_config battery_cfg = { .drv_data = drvdata };
+	struct power_supply *ps;
+	unsigned long flags;
+	bool connected;
+
+	spin_lock_irqsave(&drvdata->lock, flags);
+	connected = drvdata->is_connected;
+	ps = drvdata->battery;
+	spin_unlock_irqrestore(&drvdata->lock, flags);
+
+	hp_headset_set_wireless_status(drvdata->hdev, connected);
+
+	if (connected && !ps) {
+		ps = power_supply_register(&drvdata->hdev->dev,
+					   &drvdata->battery_desc,
+					   &battery_cfg);
+		if (IS_ERR(ps)) {
+			hid_err(drvdata->hdev, "power_supply_register failed\n");
+			return;
+		}
+
+		power_supply_powers(ps, &drvdata->hdev->dev);
+
+		spin_lock_irqsave(&drvdata->lock, flags);
+		drvdata->battery = ps;
+		spin_unlock_irqrestore(&drvdata->lock, flags);
+	} else if (!connected && ps) {
+		spin_lock_irqsave(&drvdata->lock, flags);
+		drvdata->battery = NULL;
+		spin_unlock_irqrestore(&drvdata->lock, flags);
+
+		power_supply_unregister(ps);
+	}
+}

[ ... ]

Quoted 9 lines
drivers/hid/hid-hp.c new file +9 -0
+static void
+hp_headset_parse_connected_event(struct hp_headset_device *drvdata, u8 *data)
+{
+	unsigned long flags;
+	bool state_changed = false;
+	bool connected = (data[2] == 1);
+
+	spin_lock_irqsave(&drvdata->lock, flags);
+

drivers/hid/hid-hp.c line 258

Quoted 10 lines
drivers/hid/hid-hp.c new file +10 -0
+	if (drvdata->is_connected != connected) {
+		drvdata->is_connected = connected;
+		state_changed = true;
+	}
+
+	spin_unlock_irqrestore(&drvdata->lock, flags);
+
+	if (state_changed)
+		schedule_work(&drvdata->battery_work);
+}

[Severity: Medium]
When the headset is turned on after hp_probe() has already run, the device sends a connection report handled by:

hp_raw_event()

    -> hp_headset_parse_connected_event()
      -> schedule_work(&drvdata->battery_work)

In that path, hp_headset_battery_work() registers drvdata->battery without sending HYPERX_CMD_GET_BATTERY or HYPERX_CMD_GET_CHARGING and without rescheduling drvdata->poll_work. Because hp_probe() initializes drvdata->battery_level to 100 and drvdata->is_charging to false (and disconnected battery replies with state1 == 0 && state2 == 0 are ignored in hp_headset_parse_battery_event()), the newly registered power supply will report dummy (100% / POWER_SUPPLY_STATUS_FULL) or stale values until the next 2-minute poll interval expires.

Also, during hp_headset_poll_work(), HYPERX_CMD_GET_CONNECTED is sent immediately before HYPERX_CMD_GET_BATTERY and HYPERX_CMD_GET_CHARGING. If those battery and charging replies arrive in hp_raw_event() while hp_headset_battery_work() is still inside power_supply_register() before drvdata->battery = ps is assigned, hp_headset_parse_battery_event() and hp_headset_parse_charging_event() see drvdata->battery as NULL and skip power_supply_changed().

Should connecting the headset trigger an immediate battery and charging query (for example via mod_delayed_work() on drvdata->poll_work or after power_supply_register() completes in hp_headset_battery_work())?

Quoted 17 lines
drivers/hid/hid-hp.c new file +17 -0
+
+static int hp_probe(struct hid_device *hdev, const struct hid_device_id *id)
+{
+	struct hp_headset_device *drvdata;
+	struct power_supply *ps;
+	unsigned long flags;
+	int ret;
+
+	drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
+	if (!drvdata)
+		return -ENOMEM;
+	drvdata->hdev = hdev;
+	drvdata->is_connected = false;
+	drvdata->is_charging = false;
+	drvdata->battery_level = 100;
+	spin_lock_init(&drvdata->lock);
+	hid_set_drvdata(hdev, drvdata);

[ ... ]

drivers/hid/hid-hp.c line 311

Quoted 9 lines
drivers/hid/hid-hp.c new file +9 -0
+	ret = hid_hw_open(hdev);
+	if (ret) {
+		hid_err(hdev, "hw_open failed\n");
+		goto err_stop_hw;
+	}
+
+	schedule_delayed_work(&drvdata->poll_work, 0);
+
+	return 0;

[Severity: Medium]
What happens to the USB interface wireless_status sysfs attribute if the USB dongle is probed while the headset is powered off?

In hp_probe(), drvdata->is_connected is initialized to false, and hp_headset_set_wireless_status() is only called from hp_headset_battery_work(). When the initial hp_headset_poll_work() queries HYPERX_CMD_GET_CONNECTED and the receiver reports data[2] == 0 (connected == false), hp_headset_parse_connected_event() sees:

	if (drvdata->is_connected != connected) {
		drvdata->is_connected = connected;
		state_changed = true;
	}

Since drvdata->is_connected is already false, state_changed remains false and drvdata->battery_work is never scheduled. As a result, usb_set_wireless_status(intf, USB_WIRELESS_STATUS_DISCONNECTED) is never called, leaving intf->wireless_status at USB_WIRELESS_STATUS_NA and keeping the wireless_status sysfs attribute hidden until the headset is turned on and then off.

Would it make sense to call hp_headset_set_wireless_status(hdev, false) during hp_probe()?

[ ... ]

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