hwmon: add fan/pwm driver for corsair h100i platinum
Subsystems (2)
PATCH v5 1/1 hwmon: add fan/pwm driver for corsair h100i platinum
Adds fan/pwm support for H100i platinum.
Custom temp/fan curves are not supported.
v5:
- fixed #defines not being caps
- check amount of written bytes + fixed expecting wrong amount
- moved to sizeof(var) instead of sizeof(type)
- fixed bounds checks
- merged cases and simplefied switch statements
- more error checking
- destroy the mutex
--- drivers/hwmon/Kconfig | 7 + drivers/hwmon/Makefile | 1 + drivers/hwmon/corsair_hydro_i_pro.c | 701 ++++++++++++++++++++++++++++ 3 files changed, 709 insertions(+) create mode 100644 drivers/hwmon/corsair_hydro_i_pro.c
drivers/hwmon/Kconfig +7 -0
@@ -378,6 +378,13 @@ config SENSORS_ARM_SCPI and power sensors available on ARM Ltd's SCP based platforms. The actual number and type of sensors exported depend on the platform. +config SENSORS_CORSAIR_HYDRO_I_PRO + tristate "Corsair hydro HXXXi pro driver" + depends on USB + help + If you say yes here you get support for the corsair hydro HXXXi pro + range of devices. + config SENSORS_ASB100 tristate "Asus ASB100 Bach" depends on X86 && I2C
drivers/hwmon/Makefile +1 -0
@@ -20,6 +20,7 @@ obj-$(CONFIG_SENSORS_W83793) += w83793.o obj-$(CONFIG_SENSORS_W83795) += w83795.o obj-$(CONFIG_SENSORS_W83781D) += w83781d.o obj-$(CONFIG_SENSORS_W83791D) += w83791d.o +obj-$(CONFIG_SENSORS_CORSAIR_HYDRO_I_PRO) += corsair_hydro_i_pro.o obj-$(CONFIG_SENSORS_AB8500) += abx500.o ab8500.o obj-$(CONFIG_SENSORS_ABITUGURU) += abituguru.o
drivers/hwmon/corsair_hydro_i_pro.c new file +700 -0
700 lines not on this page · raw message
Signature · 1 line
2.28.0
Hello,
I added a couple notes about the code inline.
drivers/hwmon/corsair_hydro_i_pro.c line 61
Quoted 7 lines
[...]
drivers/hwmon/corsair_hydro_i_pro.c new file +6
-0+#define MAX_FAN_COUNT 2 +#define MAX_PWM_CHANNEL_COUNT MAX_FAN_COUNT + +struct hwmon_data { +struct hydro_i_pro_device *hdev; +int channel_count;
This is a nitpick, but the 'channel_count' value comes from the 'fancount' of an entry of 'config_table', which has type 'u8', so I don't see any need for it to be an 'int'.
drivers/hwmon/corsair_hydro_i_pro.c line 80
Quoted 4 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +3
-0+void *channel_data[MAX_PWM_CHANNEL_COUNT ]; +}; +struct curve_point quiet_curve[FAN_CURVE_COUNT ] = {
This should be 'static' (and possibly 'const'?).
Quoted 24 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +24
-0+ { + .temp = 0x1F, + .pwm = 0x15, + }, + { + .temp = 0x21, + .pwm = 0x1E, + }, + { + .temp = 0x24, + .pwm = 0x25, + }, + { + .temp = 0x27, + .pwm = 0x2D, + }, + { + .temp = 0x29, + .pwm = 0x38, + }, + { + .temp = 0x2C, + .pwm = 0x4A, + },
drivers/hwmon/corsair_hydro_i_pro.c line 125
Quoted 10 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +9
-0+ { + .temp = 0x2F, + .pwm = 0x64, + }, +}; +static const struct device_config config_table[] = { + { + .vendor_id =0x1b1c , + .product_id =0x0c15 ,
If I see it correctly, you never use 'vendor_id', nor 'product_id', right?
Quoted 4 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +4
-0+ .fancount = 2, + .rpm_min = 200, + .rpm_max = 3000, + .pwm_max = 100,
drivers/hwmon/corsair_hydro_i_pro.c line 147
Quoted 10 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +9
-0+ .name = "corsair_H100i_pro", + .hwmon_info = dual_fan, + }, +}; + +#define SUCCES_LENGTH 3 +#define SUCCES_CODE 0x12, 0x34 + +static bool check_succes (enum opcodes command,char ret[SUCCES_LENGTH ])
I missed this it seems, but my suggestion about the 'static' before the size applies here as well.
Quoted 5 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +5
-0+{ +char success[SUCCES_LENGTH ] = { command,SUCCES_CODE }; + +return memcmp (ret, success,SUCCES_LENGTH ) ==0 ; +}
drivers/hwmon/corsair_hydro_i_pro.c line 162
Quoted 10 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +10
-0+ +static int acquire_lock (struct hydro_i_pro_device *hdev) +{ +int retval =usb_autopm_get_interface (hdev->interface); + +if (retval) +return retval; + +mutex_lock (&hdev->io_mutex); +return 0 ;
I think this should be
return mutex_lock_interruptible(...);
as per
https://www.kernel.org/doc/html/latest/kernel-hacking/locking.html#locking-only-in-user-context
drivers/hwmon/corsair_hydro_i_pro.c line 299
Quoted 11 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +10
-0+} +static int set_fan_target_pwm (struct hydro_i_pro_device *hdev, +struct hwmon_fan_data *fan_data,u8 val) +{ +int retval; +unsigned char *send_buf = hdev->bulk_out_buffer; +unsigned char *recv_buf = hdev->bulk_in_buffer; + + fan_data->fan_target =0 ; + fan_data->fan_pwm_target = val;
Personally, I'd place these two lines at the end of the function so that they are not modified if the USB transaction fails.
(same applies to set_fan_pwm_curve() and set_fan_target_rpm())
drivers/hwmon/corsair_hydro_i_pro.c line 306
Quoted 7 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +7
-0+ + send_buf[0 ] =PWM_FAN_TARGET_CMD ; + send_buf[1 ] = fan_data->fan_channel; + send_buf[2 ] = fan_data->fan_pwm_target; +dev_info (&hdev->udev->dev,"debug:%d,%d,%d" , send_buf[0 ], send_buf[1 ], + send_buf[2 ]); +dev_info (&hdev->udev->dev,"val:%d" , fan_data->fan_pwm_target);
This should be dev_dbg() in my opinion if you intend to have such messages.
Quoted 47 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +46
-0+ + retval =transfer_usb (hdev, send_buf, recv_buf,3 ,3 ); +if (retval) +return retval; + +if (!check_succes (send_buf[0 ], recv_buf)) { +dev_warn ( + &hdev->udev->dev, +"failed setting fan pwm for %s on channel %d %d,%d,%d\n" , + hdev->config->name, fan_data->fan_channel, recv_buf[0 ], + recv_buf[1 ], recv_buf[2 ]); +return -EINVAL ; + } +return 0 ; +} +static int hwmon_write (struct device *dev,enum hwmon_sensor_types type, +u32 attr,int channel,long val) +{ +struct hwmon_data *data =dev_get_drvdata (dev); +struct hydro_i_pro_device *hdev = data->hdev; +struct hwmon_fan_data *fan_data; +int retval =0 ; + +switch (type) { +case hwmon_fan: +switch (attr) { +case hwmon_fan_target: + fan_data = data->channel_data[channel]; +if (fan_data->mode !=1 ) +return -EINVAL ; + + val =clamp_val (val, hdev->config->rpm_min, + hdev->config->rpm_max); + + retval =acquire_lock (hdev); +if (retval) + goto exit; + + retval =set_fan_target_rpm (hdev, fan_data, val); +if (retval) + goto cleanup; + +break ; +default : +return -EINVAL ; + }
drivers/hwmon/corsair_hydro_i_pro.c line 387
Quoted 10 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +10
-0+break ; +case hwmon_pwm: +switch (attr) { +case hwmon_pwm_input: + fan_data = data->channel_data[channel]; +if (fan_data->mode !=1 ) +return -EINVAL ; + + val =clamp_val (val,0 , hdev->config->pwm_max); +dev_info (&hdev->udev->dev,"val:%ld" , val);
Same here regarding dev_dbg().
Quoted 14 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +14
-0+ retval =acquire_lock (hdev); +if (retval) + goto exit; + + retval =set_fan_target_pwm (hdev, fan_data, val); +if (retval) + goto cleanup; + +break ; +case hwmon_pwm_enable: + fan_data = data->channel_data[channel]; + +switch (val) { +case 2 :
drivers/hwmon/corsair_hydro_i_pro.c line 411
Quoted 10 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +10
-0+case 0 : + retval =acquire_lock (hdev); +if (retval) + goto exit; + + retval =set_fan_pwm_curve (hdev, fan_data, +DEFAULT_CURVE ); +if (retval) + goto cleanup; + fan_data->mode =0 ;
This could be 'fan_data->mode = val', no?
Quoted 62 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +62
-0+break ; +case 1 : + retval =acquire_lock (hdev); +if (retval) + goto exit; + +if (fan_data->fan_target !=0 ) { + retval =set_fan_target_rpm ( + hdev, fan_data, + fan_data->fan_target); +if (retval) + goto cleanup; + }else if (fan_data->fan_pwm_target !=0 ) { + retval =set_fan_target_pwm ( + hdev, fan_data, + fan_data->fan_pwm_target); +if (retval) + goto cleanup; + } + fan_data->mode =1 ; +break ; +default : +return -EINVAL ; + } +break ; +default : +return -EINVAL ; + } +break ; +default : +return -EINVAL ; + } + +cleanup: +mutex_unlock (&hdev->io_mutex); +usb_autopm_put_interface (hdev->interface); +exit: +return retval; +} + +static int hwmon_read (struct device *dev,enum hwmon_sensor_types type, +u32 attr,int channel,long *val) +{ +struct hwmon_data *data =dev_get_drvdata (dev); +struct hydro_i_pro_device *hdev = data->hdev; +struct hwmon_fan_data *fan_data; +int retval =0 ; + +switch (type) { +case hwmon_fan: +switch (attr) { +case hwmon_fan_input: + fan_data = data->channel_data[channel]; + + retval =acquire_lock (hdev); +if (retval) + goto exit; + + retval =get_fan_current_rpm (hdev, fan_data, val); +break ; +case hwmon_fan_target: + fan_data = data->channel_data[channel];
drivers/hwmon/corsair_hydro_i_pro.c line 483
Quoted 10 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +10
-0+if (fan_data->mode !=1 ) { + *val =0 ; + goto exit; + } + *val = fan_data->fan_target; + goto exit; +case hwmon_fan_min: + *val = hdev->config->rpm_min; + goto exit; +
Any reason why you don't handle 'hwmon_fan_max' as well?
Quoted 32 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +32
-0+default : +return -EINVAL ; + } +break ; +case hwmon_pwm: +switch (attr) { +case hwmon_pwm_enable: + fan_data = data->channel_data[channel]; + *val = fan_data->mode; + goto exit; +default : +return -EINVAL ; + } + goto exit; + +default : +return -EINVAL ; + } + +mutex_unlock (&hdev->io_mutex); +usb_autopm_put_interface (hdev->interface); +exit: +return retval; +} + +static const struct hwmon_ops i_pro_ops = { + .is_visible = hwmon_is_visible, + .read = hwmon_read, + .write = hwmon_write, +}; + +static int hwmon_init (struct hydro_i_pro_device *hdev)
drivers/hwmon/corsair_hydro_i_pro.c line 525
Quoted 10 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +10
-0+{ +u8 fan_id; +struct device *hwmon_dev; +struct hwmon_fan_data *fan; +struct hwmon_data *data; +struct hwmon_chip_info *hwmon_info; + + data =devm_kzalloc (&hdev->udev->dev,sizeof (*data),GFP_KERNEL ); + hwmon_info =devm_kzalloc (&hdev->udev->dev, +sizeof (struct hwmon_chip_info ),GFP_KERNEL );
I think you missed this 'sizeof' when coverting to 'sizeof(*ptr)'.
drivers/hwmon/corsair_hydro_i_pro.c line 535
Quoted 10 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +10
-0+ +/* You did something bad!! Either adjust MAX_FAN_COUNT or the fancount for the config!*/ +WARN_ON (hdev->config->fancount >=MAX_PWM_CHANNEL_COUNT ); + data->channel_count = +clamp_val (hdev->config->fancount,0 ,MAX_PWM_CHANNEL_COUNT ); + +/* For each fan create a data channel a fan config entry and a pwm config entry */ +for (fan_id =0 ; fan_id < data->channel_count; fan_id++) { + fan =devm_kzalloc (&hdev->udev->dev, +sizeof (struct hwmon_fan_data ),GFP_KERNEL );
And this one as well.
Quoted 7 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +7
-0+ fan->fan_channel = fan_id; + fan->mode =0 ; + data->channel_data[fan_id] = fan; + } + + hwmon_info->ops = &i_pro_ops; + hwmon_info->info = hdev->config->hwmon_info;
drivers/hwmon/corsair_hydro_i_pro.c line 552
Quoted 10 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +10
-0+ + data->hdev = hdev; + hwmon_dev =devm_hwmon_device_register_with_info ( + &hdev->udev->dev, hdev->config->name, data, hwmon_info,NULL ); +if (IS_ERR (hwmon_dev)) +return PTR_ERR (hwmon_dev); + +dev_info (&hdev->udev->dev,"setup hwmon for %s\n" , hdev->config->name); +return 0 ; +}
It is still possible that hwmon_init() leaks memory on failure.
drivers/hwmon/corsair_hydro_i_pro.c line 613
Quoted 12 lines
[...]
drivers/hwmon/corsair_hydro_i_pro.c new file +11
-0+static int astk_probe (struct usb_interface *interface, +const struct usb_device_id *id) +{ +struct hydro_i_pro_device *hdev; +int retval; +struct usb_endpoint_descriptor *bulk_in, *bulk_out; + + hdev =kzalloc (sizeof (*hdev),GFP_KERNEL ); + +if (!hdev) { +kfree (hdev);
If this 'if' is taken, then hdev == NULL, so no need to free it.
Quoted 17 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +17
-0+ retval = -ENOMEM ; + goto exit; + } + + hdev->config = (const struct device_config *)id->driver_info; +/* You should set the driver_info to a pointer to the correct configuration!!*/ +WARN_ON (!hdev->config); + + retval =usb_find_common_endpoints (interface->cur_altsetting, &bulk_in, + &bulk_out,NULL ,NULL ); +if (retval) { +kfree (hdev); + goto exit; + } + + hdev->udev =usb_get_dev (interface_to_usbdev (interface)); + hdev->interface =usb_get_intf (interface);
drivers/hwmon/corsair_hydro_i_pro.c line 640
Quoted 10 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +10
-0+ +/* +* set up the endpoint information +* use only the first bulk-in and bulk-out endpoints +*/ + hdev->bulk_in_size =usb_endpoint_maxp (bulk_in); + hdev->bulk_in_buffer =kmalloc (hdev->bulk_in_size,GFP_KERNEL ); + hdev->bulk_in_endpointAddr = bulk_in->bEndpointAddress; + hdev->bulk_out_size =usb_endpoint_maxp (bulk_out); + hdev->bulk_out_buffer =kmalloc (hdev->bulk_out_size,GFP_KERNEL );
I think both allocations should be checked.
drivers/hwmon/corsair_hydro_i_pro.c line 653
Quoted 13 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +13
-0+ hdev->bulk_out_endpointAddr = bulk_out->bEndpointAddress; + + retval =init_device (hdev->udev); +if (retval) { +dev_err (&interface->dev,"failed initialising %s:%d\n" , + hdev->config->name, retval); +kfree (hdev); + goto exit; + } + + retval =hwmon_init (hdev); +if (retval) { + dev_err(&interface->dev, "failed initialising hwmon%s:%d\n",
^^^^^^
I think you need more spaces here.
drivers/hwmon/corsair_hydro_i_pro.c line 663
Quoted 11 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +10
-0+ hdev->config->name, retval); +kfree (hdev); + goto exit; + } + +usb_set_intfdata (interface, hdev); +mutex_init (&hdev->io_mutex); +exit: +return retval; +}
Barnabás Pőcze
Quoted 6 lines · Barnabás Pőcze
Hello,
I added a couple notes about the code inline.
Quoted 11 lines
drivers/hwmon/corsair_hydro_i_pro.c line 61
Quoted 7 lines
[...]
drivers/hwmon/corsair_hydro_i_pro.c new file +6
-0+#define MAX_FAN_COUNT 2 +#define MAX_PWM_CHANNEL_COUNT MAX_FAN_COUNT + +struct hwmon_data { +struct hydro_i_pro_device *hdev; +int channel_count;This is a nitpick, but the '
channel_count' value comes from the 'fancount' of an entry of 'config_table', which has type 'u8', so I don't see any need for it to be an 'int'.
In practice this will indeed never go above u8, even with a pump, didn't catch that one
Quoted 7 lines
[...]
drivers/hwmon/corsair_hydro_i_pro.c line 125
Quoted 4 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +4
-0+static const struct device_config config_table[] = { + { + .vendor_id =0x1b1c , + .product_id =0x0c15 ,If I see it correctly, you never use '
vendor_id', nor 'product_id', right?
This was a really sloppy patch apparently. Those were only there for finding them, so they should indeed go now.
Quoted 10 lines
[...]
drivers/hwmon/corsair_hydro_i_pro.c line 306
Quoted 7 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +7
-0+ + send_buf[0 ] =PWM_FAN_TARGET_CMD ; + send_buf[1 ] = fan_data->fan_channel; + send_buf[2 ] = fan_data->fan_pwm_target; +dev_info (&hdev->udev->dev,"debug:%d,%d,%d" , send_buf[0 ], send_buf[1 ], + send_buf[2 ]); +dev_info (&hdev->udev->dev,"val:%d" , fan_data->fan_pwm_target);This should be
dev_dbg()in my opinion if you intend to have such messages.
Those weren't even supposed to go in :(
Quoted 19 lines
[...]
Quoted 18 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +18
-0+ retval =acquire_lock (hdev); +if (retval) + goto exit; + + retval =set_fan_target_pwm (hdev, fan_data, val); +if (retval) + goto cleanup; + +break ; +case hwmon_pwm_enable: + fan_data = data->channel_data[channel]; + +switch (val) { +case 2 : +case 0 : + retval =acquire_lock (hdev); +if (retval) + goto exit;
Quoted 8 lines
drivers/hwmon/corsair_hydro_i_pro.c line 411
Quoted 6 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +6
-0+ + retval =set_fan_pwm_curve (hdev, fan_data, +DEFAULT_CURVE ); +if (retval) + goto cleanup; + fan_data->mode =0 ;This could be '
fan_data->mode= val', no?
I didnt think about that, the mode can be read from userspace and when userspace sets it to 2 they probably expect to read 2. I usually try to use constants as much as possible instead of using variables when the value is known, but I missed this when merging the statements.
Quoted 14 lines
[...]
drivers/hwmon/corsair_hydro_i_pro.c line 525
Quoted 11 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +11
-0+static int hwmon_init (struct hydro_i_pro_device *hdev) +{ +u8 fan_id; +struct device *hwmon_dev; +struct hwmon_fan_data *fan; +struct hwmon_data *data; +struct hwmon_chip_info *hwmon_info; + + data =devm_kzalloc (&hdev->udev->dev,sizeof (*data),GFP_KERNEL ); + hwmon_info =devm_kzalloc (&hdev->udev->dev, +sizeof (struct hwmon_chip_info ),GFP_KERNEL );I think you missed this 'sizeof' when coverting to '
sizeof(*ptr)'.
Yes, I did indeed mist that one :/
Quoted 12 lines
[...]
Quoted 11 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +11
-0+ fan->fan_channel = fan_id; + fan->mode =0 ; + data->channel_data[fan_id] = fan; + } + + hwmon_info->ops = &i_pro_ops; + hwmon_info->info = hdev->config->hwmon_info; + + data->hdev = hdev; + hwmon_dev =devm_hwmon_device_register_with_info ( + &hdev->udev->dev, hdev->config->name, data, hwmon_info,NULL );
Quoted 8 lines
drivers/hwmon/corsair_hydro_i_pro.c line 552
Quoted 6 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +6
-0+if (IS_ERR (hwmon_dev)) +return PTR_ERR (hwmon_dev); + +dev_info (&hdev->udev->dev,"setup hwmon for %s\n" , hdev->config->name); +return 0 ; +}It is still possible that
hwmon_init()leaks memory on failure.
you mean the allocated memory doesn't get deallocated until the driver detaches? I can't free the memory myself because when the driver does detach it will double-free.
I have looked at how other drivers handle this, none of them deallocate any memory.
If you mean error check the allocations, I added those now.
Quoted 1 line
[...]
Kind regards,
Jaap Aarts
Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +11
-0+ fan->fan_channel = fan_id; + fan->mode =0 ; + data->channel_data[fan_id] = fan; + } + + hwmon_info->ops = &i_pro_ops; + hwmon_info->info = hdev->config->hwmon_info; + + data->hdev = hdev; + hwmon_dev =devm_hwmon_device_register_with_info ( + &hdev->udev->dev, hdev->config->name, data, hwmon_info,NULL );Quoted 8 lines
drivers/hwmon/corsair_hydro_i_pro.c line 552
Quoted 6 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +6
-0+if (IS_ERR (hwmon_dev)) +return PTR_ERR (hwmon_dev); + +dev_info (&hdev->udev->dev,"setup hwmon for %s\n" , hdev->config->name); +return 0 ; +}It is still possible that
hwmon_init()leaks memory on failure.
Quoted 7 lines
you mean the allocated memory doesn't get deallocated until the driver detaches? I can't free the memory myself because when the driver does detach it will double-free.
I have looked at how other drivers handle this, none of them deallocate any memory.
If you mean error check the allocations, I added those now.
Sorry, my bad, the devm_ part went over my head when reading. I also forgot to mention that in astk_probe() you use kmalloc() to allocate the IO buffers, but those are not freed if init_device() or hwmon_init() fails.
Barnabás Pőcze