Skip to contentSkip to conversation

HID: corsair-void: Add Corsair Void headset family driver

PATCH Applied Stuart Hayhurst 3 participants 14 messages Oct 8, 2024 – Feb 11, 2025 HID

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 HID: corsair-void: Add Corsair Void headset family driver

Introduce a driver for the Corsair Void family of headsets, supporting:
- Battery reporting (power_supply)
- Sidetone setting support
- Physical microphone location reporting
- Headset and receiver firmware version reporting
- Built-in alert triggering
- USB wireless_status

Tested with a Void Pro Wireless, Void Elite Wireless and a Void Elite Wired

---

v4 -> v5:
- Changed kernel version in sysfs docs to 6.13
- Use CONFIG_HID_CORSAIR instead of CONFIG_HID_CORSAIR_VOID

v3 -> v4:
- Added missing <linux/device.h> include
- Added missing newline, fix indent
- Use hid_hw_raw_request return code directly for wireless sidetone
- Use battery status enum for remaining values
- Use of devm_kasprintf() to create battery string
- Moved corsair_void_request_status error reporting to callers
- Simplified corsair_void_request_status
- Removed battery_struct_size, use sizeof(*battery_data) instead
- Removed unnecessary curly brackets in send_alert_store

v2 -> v3:
- Use __free(kfree) helper to free allocations
- Combined similar condition checks
- Check power_supply was created successfully before saving it
- Removed explicit initialisation of return value in corsair_void_probe
- Use preferred syntax for drvdata's struct allocation size
- Removed named success exit point from driver probe
- Removed unused driver probe exit point

v1 -> v2:
- Added a module author email address
- Removed unused variable (psy_cfg)
- Corrected status request buffer size from 12 bytes to 2 bytes
- Use enums for battery status
- Use kmalloc instead of kzalloc where applicable
- Use __le16 type for cpu_to_le16
- Use DEVICE_ATTR_RO and DEVICE_ATTR_WO to create attributes
- Simplified corsair_void_set_sidetone_wired
- Cleaned up patch description

 .../ABI/testing/sysfs-driver-hid-corsair-void |  38 +
 drivers/hid/Kconfig                           |   3 +
 drivers/hid/Makefile                          |   2 +-
 drivers/hid/hid-corsair-void.c                | 829 ++++++++++++++++++
 4 files changed, 871 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ABI/testing/sysfs-driver-hid-corsair-void
 create mode 100644 drivers/hid/hid-corsair-void.c
Documentation/ABI/testing/sysfs-driver-hid-corsair-void new file +38 -0
new file mode 100644
index 000000000000..83fa625c0025
@@ -0,0 +1,38 @@
+What:		/sys/bus/hid/drivers/hid-corsair-void/<dev>/fw_version_headset
+Date:		January 2024
+KernelVersion:	6.13
+Contact:	Stuart Hayhurst <[email protected]>
+Description:	(R) The firmware version of the headset
+			* Returns -ENODATA if no version was reported
+
+What:		/sys/bus/hid/drivers/hid-corsair-void/<dev>/fw_version_receiver
+Date:		January 2024
+KernelVersion:	6.13
+Contact:	Stuart Hayhurst <[email protected]>
+Description:	(R) The firmware version of the receiver
+
+What:		/sys/bus/hid/drivers/hid-corsair-void/<dev>/microphone_up
+Date:		July 2023
+KernelVersion:	6.13
+Contact:	Stuart Hayhurst <[email protected]>
+Description:	(R) Get the physical position of the microphone
+			* 1 -> Microphone up
+			* 0 -> Microphone down
+
+What:		/sys/bus/hid/drivers/hid-corsair-void/<dev>/send_alert
+Date:		July 2023
+KernelVersion:	6.13
+Contact:	Stuart Hayhurst <[email protected]>
+Description:	(W) Play a built-in notification from the headset (0 / 1)
+
+What:		/sys/bus/hid/drivers/hid-corsair-void/<dev>/set_sidetone
+Date:		December 2023
+KernelVersion:	6.13
+Contact:	Stuart Hayhurst <[email protected]>
+Description:	(W) Set the sidetone volume (0 - sidetone_max)
+
+What:		/sys/bus/hid/drivers/hid-corsair-void/<dev>/sidetone_max
+Date:		July 2024
+KernelVersion:	6.13
+Contact:	Stuart Hayhurst <[email protected]>
+Description:	(R) Report the maximum sidetone volume
drivers/hid/Kconfig +3 -0
index f8a56d631242..5a90523146f7 100644
@@ -213,13 +213,16 @@ config HID_CHICONY
 config HID_CORSAIR
 	tristate "Corsair devices"
 	depends on USB_HID && LEDS_CLASS
+	select POWER_SUPPLY
 	help
 	Support for Corsair devices that are not fully compliant with the
 	HID standard.
+	Support for Corsair Void headsets.
 
 	Supported devices:
 	- Vengeance K90
 	- Scimitar PRO RGB
+	- Corsair Void headsets
 
 config HID_COUGAR
 	tristate "Cougar devices"
drivers/hid/Makefile +1 -1
index 496dab54c73a..ea2ed38d7e31 100644
@@ -38,7 +38,7 @@ obj-$(CONFIG_HID_BIGBEN_FF)	+= hid-bigbenff.o
 obj-$(CONFIG_HID_CHERRY)	+= hid-cherry.o
 obj-$(CONFIG_HID_CHICONY)	+= hid-chicony.o
 obj-$(CONFIG_HID_CMEDIA)	+= hid-cmedia.o
-obj-$(CONFIG_HID_CORSAIR)	+= hid-corsair.o
+obj-$(CONFIG_HID_CORSAIR)	+= hid-corsair.o hid-corsair-void.o
 obj-$(CONFIG_HID_COUGAR)	+= hid-cougar.o
 obj-$(CONFIG_HID_CP2112)	+= hid-cp2112.o
 obj-$(CONFIG_HID_CYPRESS)	+= hid-cypress.o
drivers/hid/hid-corsair-void.c new file +829 -0

829 lines not on this page · raw message

Signature · 1 line
2.45.2
maintainer Applied
#1 lore raw
Quoted 48 lines · Stuart Hayhurst

Introduce a driver for the Corsair Void family of headsets, supporting:
- Battery reporting (power_supply)
- Sidetone setting support
- Physical microphone location reporting
- Headset and receiver firmware version reporting
- Built-in alert triggering
- USB wireless_status

Tested with a Void Pro Wireless, Void Elite Wireless and a Void Elite Wired

---

v4 -> v5:
- Changed kernel version in sysfs docs to 6.13
- Use CONFIG_HID_CORSAIR instead of CONFIG_HID_CORSAIR_VOID

v3 -> v4:
- Added missing <linux/device.h> include
- Added missing newline, fix indent
- Use hid_hw_raw_request return code directly for wireless sidetone
- Use battery status enum for remaining values
- Use of devm_kasprintf() to create battery string
- Moved corsair_void_request_status error reporting to callers
- Simplified corsair_void_request_status
- Removed battery_struct_size, use sizeof(*battery_data) instead
- Removed unnecessary curly brackets in send_alert_store

v2 -> v3:
- Use __free(kfree) helper to free allocations
- Combined similar condition checks
- Check power_supply was created successfully before saving it
- Removed explicit initialisation of return value in corsair_void_probe
- Use preferred syntax for drvdata's struct allocation size
- Removed named success exit point from driver probe
- Removed unused driver probe exit point

v1 -> v2:
- Added a module author email address
- Removed unused variable (psy_cfg)
- Corrected status request buffer size from 12 bytes to 2 bytes
- Use enums for battery status
- Use kmalloc instead of kzalloc where applicable
- Use __le16 type for cpu_to_le16
- Use DEVICE_ATTR_RO and DEVICE_ATTR_WO to create attributes
- Simplified corsair_void_set_sidetone_wired
- Cleaned up patch description

Quoted 7 lines
 .../ABI/testing/sysfs-driver-hid-corsair-void |  38 +
 drivers/hid/Kconfig                           |   3 +
 drivers/hid/Makefile                          |   2 +-
 drivers/hid/hid-corsair-void.c                | 829 ++++++++++++++++++
 4 files changed, 871 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ABI/testing/sysfs-driver-hid-corsair-void
 create mode 100644 drivers/hid/hid-corsair-void.c

Applied to hid.git#for-6.13/corsair. Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs
#2 lore raw
In reply to Stuart Hayhurst
Quoted 9 lines · Stuart Hayhurst

Introduce a driver for the Corsair Void family of headsets, supporting:
- Battery reporting (power_supply)
- Sidetone setting support
- Physical microphone location reporting
- Headset and receiver firmware version reporting
- Built-in alert triggering
- USB wireless_status

Tested with a Void Pro Wireless, Void Elite Wireless and a Void Elite Wired

...

Quoted 3 lines
drivers/hid/hid-corsair-void.c new file +0 -0
@@ -0,0 +1,829 @@

...

Quoted 49 lines
drivers/hid/hid-corsair-void.c new file +49 -0
+static void corsair_void_process_receiver(struct corsair_void_drvdata *drvdata,
+					  int raw_battery_capacity,
+					  int raw_connection_status,
+					  int raw_battery_status)
+{
+	struct corsair_void_battery_data *battery_data = &drvdata->battery_data;
+	struct corsair_void_battery_data orig_battery_data;
+
+	/* Save initial battery data, to compare later */
+	orig_battery_data = *battery_data;
+
+	/* Headset not connected, or it's wired */
+	if (raw_connection_status != CORSAIR_VOID_WIRELESS_CONNECTED)
+		goto unknown_battery;
+
+	/* Battery information unavailable */
+	if (raw_battery_status == 0)
+		goto unknown_battery;
+
+	/* Battery must be connected then */
+	battery_data->present = true;
+	battery_data->capacity_level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
+
+	/* Set battery status */
+	switch (raw_battery_status) {
+	case CORSAIR_VOID_BATTERY_NORMAL:
+	case CORSAIR_VOID_BATTERY_LOW:
+	case CORSAIR_VOID_BATTERY_CRITICAL:
+		battery_data->status = POWER_SUPPLY_STATUS_DISCHARGING;
+		if (raw_battery_status == CORSAIR_VOID_BATTERY_LOW)
+			battery_data->capacity_level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
+		else if (raw_battery_status == CORSAIR_VOID_BATTERY_CRITICAL)
+			battery_data->capacity_level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
+
+		break;
+	case CORSAIR_VOID_BATTERY_CHARGED:
+		battery_data->status = POWER_SUPPLY_STATUS_FULL;
+		break;
+	case CORSAIR_VOID_BATTERY_CHARGING:
+		battery_data->status = POWER_SUPPLY_STATUS_CHARGING;
+		break;
+	default:
+		hid_warn(drvdata->hid_dev, "unknown battery status '%d'",
+			 raw_battery_status);
+		goto unknown_battery;
+		break;
+	}
+
+	battery_data->capacity = raw_battery_capacity;

drivers/hid/hid-corsair-void.c line 263

Quoted 10 lines
drivers/hid/hid-corsair-void.c new file +10 -0
+	corsair_void_set_wireless_status(drvdata);
+
+	goto success;
+unknown_battery:
+	corsair_void_set_unknown_batt(drvdata);
+success:
+
+	/* Inform power supply if battery values changed */
+	if (memcmp(&orig_battery_data, battery_data, sizeof(*battery_data))) {
+		scoped_guard(mutex, &drvdata->battery_mutex) {

This effectively kills the system. We came here via:
corsair_void_raw_event (hid_driver::raw_event)
-> corsair_void_process_receiver

    -> scoped_guard(mutex, &drvdata->battery_mutex)

And hid_driver::raw_event can be called from the interrupt context. This happened at:
https://bugzilla.suse.com/show_bug.cgi?id=1236843

In particular, from BH (USB URB BH: usb_giveback_urb_bh()), see the backtrace at:
https://bugzilla.suse.com/attachment.cgi?id=880175

Perhaps you need a separate _spin_ lock for drvdata->battery and use it here for getting and on "= NULL" and "=new_supply" for setting? Or schedule another work for power_supply_changed()? Or perhaps you unify the add/remove/update work into one?

drivers/hid/hid-corsair-void.c line 266

Quoted 3 lines
drivers/hid/hid-corsair-void.c new file +3 -0
+			if (drvdata->battery) {
+				power_supply_changed(drvdata->battery);
+			}

...

thanks,

Signature · 2 lines
js
suse labs
#3 lore raw
Quoted 1 line · Jiri Slaby

Or perhaps you unify the add/remove/update work into one?

That way, you seem you'd need no lock at all.

Quoted 1 line

thanks,

Signature · 2 lines
js
suse labs
author
#4 lore raw
Quoted 7 lines

This effectively kills the system. We came here via:
corsair_void_raw_event (hid_driver::raw_event)
-> corsair_void_process_receiver

     -> scoped_guard(mutex, &drvdata->battery_mutex)

And hid_driver::raw_event can be called from the interrupt context. This happened at:

Damn it, I was suspicious that it was too easy. It's been running for a year without an issue on my system, I'll clearly have to read a bit more.

I'm not sure what the best solution is for this. 'corsair_void_process_receiver()' shouldn't be called frequently, and the locks shouldn't be waiting on each other very often, so I suppose swapping the mutex for a spinlock is probably the best option, instead of increasing code complexity?

Happy to put a patch in for whatever seems best :)

Thanks,
Stuart

#5 lore raw
Quoted 8 lines · Stuart
Quoted 7 lines

This effectively kills the system. We came here via:
corsair_void_raw_event (hid_driver::raw_event)

    -> corsair_void_process_receiver
      -> scoped_guard(mutex, &drvdata->battery_mutex)

And hid_driver::raw_event can be called from the interrupt context. This happened at:

Quoted 8 lines

Damn it, I was suspicious that it was too easy. It's been running for a year without an issue on my system, I'll clearly have to read a bit more.

I'm not sure what the best solution is for this. 'corsair_void_process_receiver()' shouldn't be called frequently, and the locks shouldn't be waiting on each other very often, so I suppose swapping the mutex for a spinlock is probably the best option, instead of increasing code complexity?

Not that easy, as AFAIR power unregister cannot sleep.

Signature · 2 lines
js
suse labs
author
#6 lore raw
Quoted 1 line

Not that easy, as AFAIR power unregister cannot sleep.

Sorry, I'm not sure I follow, why would power_supply_unregister sleep? I thought the issue was about locking mutexes in an interrupt context, why wouldn't using spinlock_irqsave work instead?

Stuart

#7 lore raw
Quoted 5 lines · Stuart
Quoted 1 line

Not that easy, as AFAIR power unregister cannot sleep.

Sorry, I'm not sure I follow, why would power_supply_unregister sleep? I thought the issue was about locking mutexes in an interrupt context, why wouldn't using spinlock_irqsave work instead?

There sure, but you want to protect the ->battery, right? So you'd need to spin lock *also* power unregister.

Anyway, maybe I misunderstand, please send a patch and we will see ;).

Signature · 2 lines
js
suse labs
author
#8 lore raw
Quoted 1 line

Anyway, maybe I misunderstand, please send a patch and we will see ;)

Nope, you're right, I gave the spinlocks a test and it did not like that at all

In that case, I can just move the power_supply_changed call into its own work and then schedule that from corsair_void_process_receiver()?

#9 lore raw
Quoted 6 lines · Stuart
Quoted 1 line

Anyway, maybe I misunderstand, please send a patch and we will see ;)

Nope, you're right, I gave the spinlocks a test and it did not like that at all

In that case, I can just move the power_supply_changed call into its own work and then schedule that from corsair_void_process_receiver()?

And what about having one work for all three (reg, up, unreg) as I suggested. As I wrote, IMO, you would need no lock then. Only flags to see what to do there.

thanks,

Signature · 2 lines
js
suse labs
author
#10 lore raw
Quoted 3 lines

And what about having one work for all three (reg, up, unreg) as I suggested. As I wrote, IMO, you would need no lock then. Only flags to see what to do there.

I wasn't super keen on that because it felt like forcing three otherwise separate things into one, but it makes sense if I look at it as corsair_void's work function.

Is there a guarantee that the same work item won't run twice at the same time? Do I need to make a work queue for that? I'm also not sure how to pass arguments to the work function. I'm sure all of that is answered by some documentation somewhere, but I couldn't find it myself when I looked previously.

Thanks,
Stuart

#11 lore raw
Quoted 9 lines · Stuart
Quoted 3 lines

And what about having one work for all three (reg, up, unreg) as I suggested. As I wrote, IMO, you would need no lock then. Only flags to see what to do there.

I wasn't super keen on that because it felt like forcing three otherwise separate things into one, but it makes sense if I look at it as corsair_void's work function.

Is there a guarantee that the same work item won't run twice at the same time?

It's a single work, it cannot run twice.

Quoted 3 lines

Do I need to make a work queue for that? I'm also not sure how to pass arguments to the work function. I'm sure all of that is answered by some documentation somewhere, but I couldn't find it myself when I looked previously.

E.g. set_bit() (one bit per OP) to something like 'unsigned long flags' in 'struct corsair_void_drvdata'. But it depends if you want to preserve order of actions... (Which was not guaranteed with separate queues anyway, so I assume not?)

thanks,

Signature · 2 lines
js
suse labs
author
#12 lore raw
Quoted 1 line

It's a single work, it cannot run twice.

Ok, thanks

Quoted 4 lines

E.g. set_bit() (one bit per OP) to something like 'unsigned long flags' in 'struct corsair_void_drvdata'. But it depends if you want to preserve order of actions... (Which was not guaranteed with separate queues anyway, so I assume not?)

Wouldn't using one variable for the flags risk overwriting each other, unless it was atomic? I might be misunderstanding, I haven't played with the kernel's work much.

Yeah I wasn't worried about the order, since add and remove can't fire that close to each other anyway.

#13 lore raw
Quoted 8 lines · Stuart
Quoted 4 lines

E.g. set_bit() (one bit per OP) to something like 'unsigned long flags' in 'struct corsair_void_drvdata'. But it depends if you want to preserve order of actions... (Which was not guaranteed with separate queues anyway, so I assume not?)

Wouldn't using one variable for the flags risk overwriting each other, unless it was atomic? I might be misunderstanding, I haven't played with the kernel's work much.

set/clear/test/test_and_clear/..._bit() are guaranteed to be atomic.

Signature · 2 lines
js
suse labs