HID: corsair-void: Add Corsair Void headset family driver
PATCH v3 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
---
Just a couple questions about the sysfs attributes:
- Should the sysfs documentation use the implementation date (current), or the date it was submitted?
- Are the attribute names correct? I guessed reasonable names, but if there's a convention I'll correct them
- For cases where the wireless headset is disconnected, microphone_up returns -ENODEV
- Should it be left like that, or deregister the attributes when the headset disconnects
v2 -> v3:
- Use __free(kfree) helper to free allocations
- Combine similar condition checks
- Check power_supply was created successfully before saving it
- Remove 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 | 10 + drivers/hid/Makefile | 1 + drivers/hid/hid-corsair-void.c | 842 ++++++++++++++++++ 4 files changed, 891 insertions(+) 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
@@ -0,0 +1,38 @@ +What: /sys/bus/hid/drivers/hid-corsair-void/<dev>/fw_version_headset +Date: January 2024 +KernelVersion: 6.12 +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.12 +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.12 +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.12 +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.12 +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.12 +Contact: Stuart Hayhurst <[email protected]> +Description: (R) Report the maximum sidetone volume
drivers/hid/Kconfig +10 -0
@@ -221,6 +221,16 @@ config HID_CORSAIR - Vengeance K90 - Scimitar PRO RGB +config HID_CORSAIR_VOID + tristate "Corsair Void headsets" + depends on USB_HID + select POWER_SUPPLY + help + Support for Corsair Void headsets. + + Supported devices: + - Corsair Void headsets + config HID_COUGAR tristate "Cougar devices" help
drivers/hid/Makefile +1 -0
@@ -39,6 +39,7 @@ 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_VOID) += 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 +842 -0
842 lines not on this page · raw message
Signature · 1 line
2.45.2
…
Quoted 2 lines
+++
b/drivers/hid/hid-corsair-void.c(quoted diff) +0
-0@@ -0,0 +1,842 @@
…
Quoted 2 lines
(quoted diff) +2
-0+enum { + CORSAIR_VOID_BATTERY_NORMAL = 1,
…
Review
Quoted 2 lines
(quoted diff) +2
-0+ CORSAIR_VOID_BATTERY_CHARGING = 5, +};
Would you like to choose a corresponding name for such an enumeration?
Can any other data type be reused for this purpose?
…
Quoted 4 lines
drivers/hid/hid-corsair-void.c new file +4
-0+static ssize_t send_alert_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{
…
drivers/hid/hid-corsair-void.c line 397
Quoted 6 lines
drivers/hid/hid-corsair-void.c new file +6
-0+if (ret <0 ) { +hid_warn (hid_dev,"failed to send alert request (reason: %d)" , + ret); + }else { + ret = count; + }
Would you like to omit curly brackets here?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.11-rc4#n197
Regards,
Markus
Quoted 1 line
Would you like to choose a corresponding name for such an enumeration?
I'm not entirely convinced it needs one, as the value names are fairly descriptive
Quoted 1 line
Can any other data type be reused for this purpose?
I'm not sure what you're asking
Quoted 2 lines
Would you like to omit curly brackets here?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.11-rc4#n197
Done, thanks
Stuart
Quoted 7 lines
Quoted 1 line
Would you like to choose a corresponding name for such an enumeration?
I'm not entirely convinced it needs one, as the value names are fairly descriptive
Quoted 1 line
Can any other data type be reused for this purpose?
I'm not sure what you're asking
I imagine that more advanced programming interfaces can be applied for the management of batteries together with some devices.
Regards,
Markus
…
Quoted 2 lines
+++
b/drivers/hid/hid-corsair-void.c(quoted diff) +0
-0@@ -0,0 +1,842 @@
…
Quoted 3 lines
drivers/hid/hid-corsair-void.c new file +3
-0+static int corsair_void_set_sidetone_wireless (struct device *dev,const char *buf, +unsigned char sidetone) +{
…
Quoted 1 line
drivers/hid/hid-corsair-void.c new file +1
-0+int ret =0 ;
…
drivers/hid/hid-corsair-void.c line 451
Quoted 6 lines
drivers/hid/hid-corsair-void.c new file +6
-0+ ret =hid_hw_raw_request (hid_dev,CORSAIR_VOID_SIDETONE_REQUEST_ID , + send_buf,12 ,HID_FEATURE_REPORT , +HID_REQ_SET_REPORT ); + +return ret; +}
Would you like to return the value directly (without an intermediate local variable)?
Regards,
Markus
Quoted 1 line
Would you like to return the value directly (without an intermediate local variable)?
Done, thanks
Stuart
Quoted 22 lines · Markus Elfring
…
Quoted 2 lines
+++
b/drivers/hid/hid-corsair-void.c(quoted diff) +0
-0@@ -0,0 +1,842 @@…
Quoted 3 lines
drivers/hid/hid-corsair-void.c new file +3
-0+static int corsair_void_set_sidetone_wireless (struct device *dev,const char *buf, +unsigned char sidetone) +{…
Quoted 1 line
+ int ret = 0;
…
drivers/hid/hid-corsair-void.c line 451
Quoted 6 lines
drivers/hid/hid-corsair-void.c new file +6
-0+ ret =hid_hw_raw_request (hid_dev,CORSAIR_VOID_SIDETONE_REQUEST_ID , + send_buf,12 ,HID_FEATURE_REPORT , +HID_REQ_SET_REPORT ); + +return ret; +}Would you like to return the value directly (without an intermediate local variable)?
Regards,
Markus
Le 20/08/2024 à 02:22, Stuart Hayhurst a écrit :
Quoted 8 lines
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
- USBwireless_status
Quoted 4 lines
Tested with a Void Pro Wireless, Void Elite Wireless and a Void Elite Wired
Signed-off-by Stuart Hayhurst <[email protected]>---
Hi,
...
drivers/hid/hid-corsair-void.c line 144
Quoted 6 lines
drivers/hid/hid-corsair-void.c new file +6
-0+struct corsair_void_drvdata { +struct hid_device *hid_dev; +struct device *dev; + +char *name; +bool is_wired;
Maybe is_wired could be close to other bools below, to improve avoid holes in the structure?
Quoted 19 lines
drivers/hid/hid-corsair-void.c new file +19
-0+unsigned int sidetone_max; + +struct corsair_void_battery_data battery_data; +bool mic_up; +bool connected; +int fw_receiver_major; +int fw_receiver_minor; +int fw_headset_major; +int fw_headset_minor; + +struct power_supply *battery; +struct power_supply_desc battery_desc; +struct mutex battery_mutex; + +struct delayed_work delayed_status_work; +struct delayed_work delayed_firmware_work; +struct work_struct battery_remove_work; +struct work_struct battery_add_work; +};
...
drivers/hid/hid-corsair-void.c line 355
Quoted 5 lines
drivers/hid/hid-corsair-void.c new file +5
-0+static ssize_t sidetone_max_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +struct corsair_void_drvdata *drvdata =dev_get_drvdata (dev);
Missing newline.
Quoted 2 lines
(quoted diff) +2
-0+ return sysfs_emit(buf, "%d\n", drvdata->sidetone_max); +}
...
Quoted 26 lines
drivers/hid/hid-corsair-void.c new file +26
-0+static ssize_t send_alert_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct corsair_void_drvdata *drvdata =dev_get_drvdata (dev); +struct hid_device *hid_dev = drvdata->hid_dev; +unsigned char alert_id; + unsigned char *send_buf __free(kfree) = NULL; +int ret; + +if (!drvdata->connected || drvdata->is_wired) +return -ENODEV ; + +/* Only accept 0 or 1 for alert ID */ +if (kstrtou8 (buf,10 , &alert_id) || alert_id >=2 ) +return -EINVAL ; + + send_buf =kmalloc (3 ,GFP_KERNEL ); +if (!send_buf) +return -ENOMEM ; + +/* Packet format to send alert with ID alert_id */ + send_buf[0 ] =CORSAIR_VOID_NOTIF_REQUEST_ID ; + send_buf[1 ] =0x02 ; + send_buf[2 ] = alert_id; +
drivers/hid/hid-corsair-void.c line 398
Quoted 10 lines
drivers/hid/hid-corsair-void.c new file +10
-0+ ret =hid_hw_raw_request (hid_dev,CORSAIR_VOID_NOTIF_REQUEST_ID , + send_buf,3 ,HID_OUTPUT_REPORT , +HID_REQ_SET_REPORT ); +if (ret <0 ) { +hid_warn (hid_dev,"failed to send alert request (reason: %d)" , + ret); + }else { + ret = count; + } +
I'm not familiar with the hid_hw_raw_request() API, but I think that a kfree(send_buf) is missing here.
Quoted 2 lines
(quoted diff) +2
-0+ return ret; +}
...
drivers/hid/hid-corsair-void.c line 426
Quoted 7 lines
drivers/hid/hid-corsair-void.c new file +7
-0+static int corsair_void_set_sidetone_wireless (struct device *dev,const char *buf, +unsigned char sidetone) +{ +struct corsair_void_drvdata *drvdata =dev_get_drvdata (dev); +struct hid_device *hid_dev = drvdata->hid_dev; + unsigned char *send_buf __free(kfree) = NULL; +int ret =0 ;
Nitpick: No need to init.
Quoted 12 lines
drivers/hid/hid-corsair-void.c new file +12
-0+ + send_buf =kmalloc (12 ,GFP_KERNEL ); +if (!send_buf) +return -ENOMEM ; + +/* Packet format to set sidetone for wireless headsets */ + send_buf[0 ] =CORSAIR_VOID_SIDETONE_REQUEST_ID ; + send_buf[1 ] =0x0B ; + send_buf[2 ] =0x00 ; + send_buf[3 ] =0xFF ; + send_buf[4 ] =0x04 ; + send_buf[5 ] =0x0E ;
drivers/hid/hid-corsair-void.c line 448
Quoted 10 lines
drivers/hid/hid-corsair-void.c new file +10
-0+ send_buf[6 ] =0xFF ; + send_buf[7 ] =0x05 ; + send_buf[8 ] =0x01 ; + send_buf[9 ] =0x04 ; + send_buf[10 ] =0x00 ; + send_buf[11 ] = sidetone +200 ; + + ret =hid_hw_raw_request (hid_dev,CORSAIR_VOID_SIDETONE_REQUEST_ID , + send_buf,12 ,HID_FEATURE_REPORT , +HID_REQ_SET_REPORT );
Missing kfree(send_buf)?
Quoted 3 lines
drivers/hid/hid-corsair-void.c new file +3
-0+ +return ret; +}
...
Quoted 23 lines
drivers/hid/hid-corsair-void.c new file +23
-0+static int corsair_void_request_status (struct hid_device *hid_dev,int id) +{ + unsigned char *send_buf __free(kfree) = NULL; +int ret; + + send_buf =kmalloc (2 ,GFP_KERNEL ); +if (!send_buf) +return -ENOMEM ; + +/* Packet format to request data item (status / firmware) refresh */ + send_buf[0 ] =CORSAIR_VOID_STATUS_REQUEST_ID ; + send_buf[1 ] = id; + +/* Send request for data refresh */ + ret =hid_hw_raw_request (hid_dev,CORSAIR_VOID_STATUS_REQUEST_ID , + send_buf,2 ,HID_OUTPUT_REPORT ,HID_REQ_SET_REPORT ); +if (ret <0 ) { +switch (id) { +case CORSAIR_VOID_STATUS_REPORT_ID : +hid_warn (hid_dev,"failed to request battery (reason: %d)" , + ret); +break ; +case CORSAIR_VOID_FIRMWARE_REPORT_ID :
drivers/hid/hid-corsair-void.c line 514
Quoted 10 lines
drivers/hid/hid-corsair-void.c new file +10
-0+hid_warn (hid_dev,"failed to request firmware (reason: %d)" , + ret); +break ; +default : +hid_warn (hid_dev,"failed to send report %d (reason: %d)" , + id, ret); +break ; + } + }else { + ret =0 ;
You could save 2 lines if ret was initialized when declared.
Review
Quoted 2 lines
(quoted diff) +2
-0+ } +
Missing kfree(send_buf)?
Quoted 2 lines
(quoted diff) +2
-0+ return ret; +}
...
Quoted 38 lines
drivers/hid/hid-corsair-void.c new file +38
-0+static int corsair_void_probe (struct hid_device *hid_dev, +const struct hid_device_id *hid_id) +{ +int ret; +struct corsair_void_drvdata *drvdata; +char *name; +int name_length; + +if (!hid_is_usb (hid_dev)) +return -EINVAL ; + + drvdata =devm_kzalloc (&hid_dev->dev,sizeof (*drvdata), +GFP_KERNEL ); +if (!drvdata) +return -ENOMEM ; + +hid_set_drvdata (hid_dev, drvdata); +dev_set_drvdata (&hid_dev->dev, drvdata); + + drvdata->dev = &hid_dev->dev; + drvdata->hid_dev = hid_dev; + drvdata->is_wired = hid_id->driver_data ==CORSAIR_VOID_WIRED ; + + drvdata->sidetone_max =CORSAIR_VOID_SIDETONE_MAX_WIRELESS ; +if (drvdata->is_wired) + drvdata->sidetone_max =CORSAIR_VOID_SIDETONE_MAX_WIRED ; + +/* Set initial values for no wireless headset attached */ +/* If a headset is attached, it'll be prompted later */ +corsair_void_set_unknown_wireless_data (drvdata); +corsair_void_set_unknown_batt (drvdata); + +/* Receiver version won't be reset after init */ +/* Headset version already set via set_unknown_wireless_data */ + drvdata->fw_receiver_major =0 ; + drvdata->fw_receiver_minor =0 ; + + ret =hid_parse (hid_dev);
drivers/hid/hid-corsair-void.c line 685
Quoted 10 lines
drivers/hid/hid-corsair-void.c new file +10
-0+if (ret) { +hid_err (hid_dev,"parse failed (reason: %d)\n" , ret); +return ret; + } + + name_length =snprintf (NULL ,0 ,"corsair-void-%d-battery" , hid_dev->id); + name =devm_kzalloc (drvdata->dev, name_length +1 ,GFP_KERNEL ); +if (!name) +return -ENOMEM ; +snprintf (name, name_length +1 ,"corsair-void-%d-battery" , hid_dev->id);
devm_kasprintf() would simplify this.
Quoted 35 lines
drivers/hid/hid-corsair-void.c new file +35
-0+ + drvdata->battery_desc.name = name; + drvdata->battery_desc.type =POWER_SUPPLY_TYPE_BATTERY ; + drvdata->battery_desc.properties = corsair_void_battery_props; + drvdata->battery_desc.num_properties =ARRAY_SIZE (corsair_void_battery_props); + drvdata->battery_desc.get_property = corsair_void_battery_get_property; + + drvdata->battery =NULL ; +INIT_WORK (&drvdata->battery_remove_work, + corsair_void_battery_remove_work_handler); +INIT_WORK (&drvdata->battery_add_work, + corsair_void_battery_add_work_handler); + ret =devm_mutex_init (drvdata->dev, &drvdata->battery_mutex); +if (ret) +return ret; + + ret =sysfs_create_group (&hid_dev->dev.kobj, &corsair_void_attr_group); +if (ret) +return ret; + +/* Any failures after here will need to call hid_hw_stop */ + ret =hid_hw_start (hid_dev,HID_CONNECT_DEFAULT ); +if (ret) { +hid_err (hid_dev,"hid_hw_start failed (reason: %d)\n" , ret); + goto failed_after_sysfs; + } + +/* Refresh battery data, in case wireless headset is already connected */ +INIT_DELAYED_WORK (&drvdata->delayed_status_work, + corsair_void_status_work_handler); +schedule_delayed_work (&drvdata->delayed_status_work, +msecs_to_jiffies (100 )); + +/* Refresh firmware versions */ + INIT_DELAYED_WORK(&drvdata->delayed_firmware_work,
drivers/hid/hid-corsair-void.c line 730
Quoted 10 lines
drivers/hid/hid-corsair-void.c new file +10
-0+ corsair_void_firmware_work_handler); +schedule_delayed_work (&drvdata->delayed_firmware_work, +msecs_to_jiffies (100 )); + +return 0 ; + +failed_after_sysfs: +sysfs_remove_group (&hid_dev->dev.kobj, &corsair_void_attr_group); +return ret; +}
...
CJ
Quoted 4 lines
Quoted 4 lines
drivers/hid/hid-corsair-void.c new file +4
-0+static ssize_t send_alert_store (struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{
…
Quoted 1 line
Quoted 1 line
+ unsigned char
*send_buf__free(kfree)= NULL;
…
Quoted 1 line
Quoted 1 line
+
send_buf=kmalloc(3, GFP_KERNEL);
…
Quoted 3 lines
Quoted 3 lines
+ ret = hid_hw_raw_request(hid_dev,
CORSAIR_VOID_NOTIF_REQUEST_ID, +send_buf, 3,HID_OUTPUT_REPORT,
+HID_REQ_SET_REPORT);
…
Quoted 1 line
I'm not familiar with the
hid_hw_raw_request()API, but I think that akfree(send_buf)is missing here.
* Please take another look at the usage of scope-based resource management.
* Would you eventually prefer any other programming interfaces here?
Regards,
Markus
Quoted 2 lines
Maybe
is_wiredcould be close to other bools below, to improve avoid holes in the structure?
Possibly, I put name, is_wired and sidetone_max together, since they only depend on the device model and are set once.
Quoted 1 line
Missing newline.
Done
Quoted 2 lines
I'm not familiar with the
hid_hw_raw_request()API, but I think that akfree(send_buf)is missing here.
The __free(kfree) on the declaration should take care of that
Quoted 1 line
Nitpick: No need to init.
Thanks, but that ret won't be in the next revision anyway
Quoted 1 line
You could save 2 lines if ret was initialized when declared.
Could I? Wouldn't it get overwritten by hid_hw_raw_request?
Quoted 1 line
devm_kasprintf()would simplify this.
Well that's a lot simpler...
Thanks,
Stuart
Le 22/08/2024 à 03:03, Stuart a écrit :
Quoted 6 lines
Quoted 2 lines
Maybe
is_wiredcould be close to other bools below, to improve avoid holes in the structure?Possibly, I put
name,is_wiredandsidetone_maxtogether, since they only depend on the device model and are set once.
Quoted 8 lines
Quoted 1 line
Missing newline.
Done
Quoted 2 lines
I'm not familiar with the
hid_hw_raw_request()API, but I think that akfree(send_buf)is missing here.The
__free(kfree)on the declaration should take care of that
Sorry, I missed that.
You are right.
Quoted 8 lines
Quoted 2 lines
Nitpick: No need to init.
Thanks, but that
retwon't be in the next revision anywayQuoted 1 line
You could save 2 lines if ret was initialized when declared.
Could I? Wouldn't it get overwritten by
hid_hw_raw_request?
No, you are obviously right.
BTW, the return value of corsair_void_request_status() is not used. Does it make sense to change it into avoid function?
Quoted 9 lines
Quoted 2 lines
devm_kasprintf()would simplify this.Well that's a lot simpler...
Thanks,
Stuart
Quoted 2 lines
BTW, the return value of
corsair_void_request_status()is not used. Does it make sense to change it into avoid function?
Good catch, I've moved the error reporting to its callers instead, that means we can ditch the default case then and simplify it
Stuart