HID: corsair-void: Add Corsair Void headset family driver
Subsystems (2)
PATCH v2 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
---
I have 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
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 | 857 ++++++++++++++++++ 4 files changed, 906 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 +857 -0
857 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,857 @@
…
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) +{
…
Review
Quoted 1 line
(quoted diff) +1
-0+ unsigned char *send_buf;
* How do you think about to use the attribute “__free(kfree)” at more places accordingly?
https://elixir.bootlin.com/linux/v6.11-rc4/source/include/linux/slab.h#L282
* Would you like to reduce scopes for such local variables?
…
drivers/hid/hid-corsair-void.c line 384
Quoted 12 lines
drivers/hid/hid-corsair-void.c new file +12
-0+if (!drvdata->connected) +return -ENODEV ; + +if (drvdata->is_wired) +return -ENODEV ; + +if (kstrtou8 (buf,10 , &alert_id)) +return -EINVAL ; + +/* Only accept 0 or 1 for alert ID */ +if (alert_id >=2 ) +return -EINVAL ;
Can condition checks be merged with the same return value (for less statements)?
drivers/hid/hid-corsair-void.c line 386
Quoted 1 line
drivers/hid/hid-corsair-void.c new file +1
-0+ send_buf =kmalloc (3 ,GFP_KERNEL );
Can such a size determination be explained better?
…
Quoted 3 lines
drivers/hid/hid-corsair-void.c new file +3
-0+kfree (send_buf); +return ret; +}
…
Quoted 3 lines
drivers/hid/hid-corsair-void.c new file +3
-0+static void corsair_void_battery_add_work_handler (struct work_struct *work) +{ +struct corsair_void_drvdata *drvdata;
…
Quoted 5 lines
drivers/hid/hid-corsair-void.c new file +5
-0+ drvdata->battery =power_supply_register (drvdata->dev, + &drvdata->battery_desc, + &psy_cfg); + +if (IS_ERR (drvdata->battery)) {
…
Review
Quoted 1 line
(quoted diff) +1
-0+ drvdata->battery = NULL;
I suggest to use another local variable for the previous return value so that such a reset can be avoided.
Quoted 2 lines
(quoted diff) +2
-0+ return; + }
…
Quoted 9 lines
drivers/hid/hid-corsair-void.c new file +9
-0+static DEVICE_ATTR_RO (fw_version_receiver); +static DEVICE_ATTR_RO (fw_version_headset); +static DEVICE_ATTR_RO (microphone_up); +static DEVICE_ATTR_RO (sidetone_max); + +static DEVICE_ATTR_WO (send_alert); +static DEVICE_ATTR_WO (set_sidetone); + +static struct attribute *corsair_void_attrs[] = {
…
drivers/hid/hid-corsair-void.c line 647
Quoted 5 lines
drivers/hid/hid-corsair-void.c new file +5
-0+}; + +static const struct attribute_group corsair_void_attr_group = { + .attrs = corsair_void_attrs, +};
Is there a need to organise device attributes into separate subgroups?
…
drivers/hid/hid-corsair-void.c line 652
Quoted 4 lines
drivers/hid/hid-corsair-void.c new file +4
-0+static int corsair_void_probe (struct hid_device *hid_dev, +const struct hid_device_id *hid_id) +{ +int ret =0 ;
I propose to omit the explicit initialisation for this local variable.
Quoted 1 line
(quoted diff) +1
-0+ struct corsair_void_drvdata *drvdata;
…
drivers/hid/hid-corsair-void.c line 661
Quoted 2 lines
drivers/hid/hid-corsair-void.c new file +2
-0+ drvdata =devm_kzalloc (&hid_dev->dev,sizeof (struct corsair_void_drvdata ), +GFP_KERNEL );
Please improve such a size determination.
…
Review
Quoted 1 line
(quoted diff) +1
-0+ goto success;
Please apply the statement “return 0;” instead.
drivers/hid/hid-corsair-void.c line 740
Quoted 2 lines
drivers/hid/hid-corsair-void.c new file +2
-0+/*failed_after_hid_start: +hid_hw_stop(hid_dev);*/
Please reconsider the need once more also for this information.
Regards,
Markus
Quoted 2 lines
* How do you think about to use the attribute “__free(kfree)” at more places accordingly?
https://elixir.bootlin.com/linux/v6.11-rc4/source/include/linux/slab.h#L282
Done, thanks
Quoted 1 line
* Would you like to reduce scopes for such local variables?
I thought the kernel programming style put the local variables at the start of the function?
Quoted 1 line
Can condition checks be merged with the same return value (for less statements)?
Done
Quoted 1 line
Can such a size determination be explained better?
Well it allocates 3 bytes and immediately fills them, so I'm not sure what I'd write except stating the obvious.
Unless you mean why it has to be allocated rather than just using the stack?
Quoted 2 lines
I suggest to use another local variable for the previous return value so that such a reset can be avoided.
Done
Quoted 1 line
Is there a need to organise device attributes into separate subgroups?
If the driver ends up registering / deregistering the attributes depending on if it's wired or wireless headset and the headset's connection state, I'd say so.
Otherwise if the driver sticks with returning -ENODEV, then I don't see a reason to split them up.
Quoted 1 line
I propose to omit the explicit initialisation for this local variable.
Done
Quoted 4 lines
Please improve such a size determination.
Assuming you just wanted me to use sizeof(*drvdata), done
Quoted 1 line
Please apply the statement “return 0;” instead.
Done
Quoted 4 lines
drivers/hid/hid-corsair-void.c line 740
Quoted 2 lines
drivers/hid/hid-corsair-void.c new file +2
-0+/*failed_after_hid_start: +hid_hw_stop(hid_dev);*/ Please reconsider the need once more also for this information.
Removed it
Thanks,
Stuart
Quoted 4 lines
Quoted 1 line
* Would you like to reduce scopes for such local variables?
I thought the kernel programming style put the local variables at the start of the function?
This was the case for a while.
Increasing applications of scope-based resource management provide further opportunities for smaller scopes according to some local variables, don't they?
Quoted 5 lines
Quoted 1 line
Can such a size determination be explained better?
Well it allocates 3 bytes and immediately fills them, so I'm not sure what I'd write except stating the obvious.
Unless you mean why it has to be allocated rather than just using the stack?
How do you think about to collaborate with other data structures than character arrays?
Regards,
Markus
Quoted 5 lines
This was the case for a while.
Increasing applications of scope-based resource management provide further opportunities for smaller scopes according to some local variables, don't they?
Personally I'd rather it just fits in with the rest of the kernel, but if the general consensus is that new drivers should use tighter scopes, I can do that instead.
Quoted 5 lines
How do you think about to collaborate with other data structures than character arrays?
Hm, I picked a character array since all it's doing is sending a buffer to the device.
There's no published specification to follow, only "Well the Windows driver sends these bytes and this happens".
So there isn't really a structure that really comes naturally, especially with all the magic numbers.
Unless you're suggesting I just do unsigned char send_buf[3] = {...}? I checked the docs, apparently I misread somewhere that hid_hw_raw_request couldn't use stack allocated memory safely, whoops.
Thanks for the review,
Stuart
Quoted 9 lines
Quoted 5 lines
This was the case for a while.
Increasing applications of scope-based resource management provide further opportunities for smaller scopes according to some local variables, don't they?
Personally I'd rather it just fits in with the rest of the kernel, but if the general consensus is that new drivers should use tighter scopes, I can do that instead.
There are the usual communication challenges to consider also especially with collateral evolution in such software areas.
Quoted 6 lines
Quoted 5 lines
How do you think about to collaborate with other data structures than character arrays?
Quoted 6 lines
Hm, I picked a character array since all it's doing is sending a buffer to the device.
There's no published specification to follow, only "Well the Windows driver sends these bytes and this happens".
So there isn't really a structure that really comes naturally, especially with all the magic numbers.
I imagine that further development concerns can be adjusted accordingly.
Quoted 1 line
Unless you're suggesting I just do
unsigned char send_buf[3] = {...}?
Such a programming approach might also look promising.
Quoted 3 lines
I checked the docs, apparently I misread somewhere that
hid_hw_raw_requestcouldn't use stack allocated memory safely, whoops.
Will safer API usage be clarified further?
Can applications of advanced data structures become more appealing?
Regards,
Markus
Le 21/08/2024 à 09:26, Markus Elfring a écrit :
Quoted 20 lines
Quoted 9 lines
Quoted 5 lines
This was the case for a while.
Increasing applications of scope-based resource management provide further opportunities for smaller scopes according to some local variables, don't they?
Personally I'd rather it just fits in with the rest of the kernel, but if the general consensus is that new drivers should use tighter scopes, I can do that instead.
There are the usual communication challenges to consider also especially with collateral evolution in such software areas.
Quoted 6 lines
Quoted 5 lines
How do you think about to collaborate with other data structures than character arrays?
Quoted 12 lines
Quoted 6 lines
Hm, I picked a character array since all it's doing is sending a buffer to the device.
There's no published specification to follow, only "Well the Windows driver sends these bytes and this happens".
So there isn't really a structure that really comes naturally, especially with all the magic numbers.I imagine that further development concerns can be adjusted accordingly.
Quoted 1 line
Unless you're suggesting I just do
unsigned char send_buf[3] = {...}?
Hi,
If I recollect correctly, there may be an alignment issue and just using the stack is not enough to guaranty what is needed.
CJ
Quoted 2 lines
Such a programming approach might also look promising.
Quoted 2 lines
If I recollect correctly, there may be an alignment issue and just using the stack is not enough to guaranty what is needed.
I can't find any reference to issues with it, I'm not sure what I saw before. Also, it seems like the hid-asus driver is using it:
https://elixir.bootlin.com/linux/v6.11-rc4/source/drivers/hid/hid-asus.c#L391
Stuart
Le 22/08/2024 à 03:11, Stuart a écrit :
Quoted 10 lines
Quoted 2 lines
If I recollect correctly, there may be an alignment issue and just using the stack is not enough to guaranty what is needed.
I can't find any reference to issues with it, I'm not sure what I saw before. Also, it seems like the hid-asus driver is using it:
https://elixir.bootlin.com/linux/v6.11-rc4/source/drivers/hid/hid-asus.c#L391Stuart
It's not.
asus_kbd_set_report() is called, and there, the array is explicitly kmemdup()'ed to a variable called dmabuf before calling hid_hw_raw_request().
I've not found an explicit documentation, but here are a few results from Google related to HID and DMA related issues:
[1]: https://linux.kernel.narkive.com/2bRjLz9p/patch-hid-lenovo-don-t-use-stack-variables-for-dma-buffers
[2]: https://bugzilla.kernel.org/show_bug.cgi?id=87991
CJ
Jiri Kosina
maintainerQuoted 6 lines · Stuart
Quoted 2 lines
If I recollect correctly, there may be an alignment issue and just using the stack is not enough to guaranty what is needed.
I can't find any reference to issues with it, I'm not sure what I saw before. Also, it seems like the hid-asus driver is using it:
https://elixir.bootlin.com/linux/v6.11-rc4/source/drivers/hid/hid-asus.c#L391
You can't do DMA from stack; see section "What memory is DMA'able?" of Documentation/core-api/dma-api-howto.rst
Signature · 2 lines
Jiri Kosina SUSE Labs
Quoted 9 lines · Jiri Kosina
Quoted 6 lines
Quoted 2 lines
If I recollect correctly, there may be an alignment issue and just using the stack is not enough to guaranty what is needed.
I can't find any reference to issues with it, I'm not sure what I saw before. Also, it seems like the hid-asus driver is using it:
https://elixir.bootlin.com/linux/v6.11-rc4/source/drivers/hid/hid-asus.c#L391You can't do DMA from stack; see section "What memory is DMA'able?" of
Documentation/core-api/dma-api-howto.rst
Thanks for the link, I knew I'd seen something somewhere, I'll leave the allocation as-is then
Stuart
Quoted 6 lines
Quoted 5 lines
How do you think about to collaborate with other data structures than character arrays?
Quoted 6 lines
Hm, I picked a character array since all it's doing is sending a buffer to the device.
There's no published specification to follow, only "Well the Windows driver sends these bytes and this happens".
So there isn't really a structure that really comes naturally, especially with all the magic numbers.
Is there still a desire and need to achieve a more meaningful interpretation for involved data exchanges?
https://en.wikipedia.org/wiki/Header_(computing)
Regards,
Markus