Skip to contentSkip to conversation

hwmon: add fan/pwm driver for corsair h100i platinum

PATCH Superseded jaap aarts 2 participants 3 messages Aug 16, 2020

Subsystems (2)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

jaap aarts

author
#0 lore raw

PATCH v6 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.

v6:
- fixed memory leak
- more time convertions
- removed unnecesary fields
- interuptable lock
- moved setting rpm/pwm variables until after applying
- no more debug prints
- fix switch statements
- fixed sizeof
- add support for hwmon_fan_max

---
 drivers/hwmon/Kconfig               |   7 +
 drivers/hwmon/Makefile              |   1 +
 drivers/hwmon/corsair_hydro_i_pro.c | 731 ++++++++++++++++++++++++++++
 3 files changed, 739 insertions(+)
 create mode 100644 drivers/hwmon/corsair_hydro_i_pro.c
drivers/hwmon/Kconfig +7 -0
index 288ae9f63588..f466b72d0f67 100644
@@ -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
index 3e32c21f5efe..ec63294b3ef1 100644
@@ -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 +728 -0

728 lines not on this page · raw message

Signature · 1 line
2.28.0
#1 lore raw
Quoted 8 lines

[...]

drivers/hwmon/corsair_hydro_i_pro.c new file +7 -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;

drivers/hwmon/corsair_hydro_i_pro.c line 538

Quoted 10 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +10 -0
+
+	data = devm_kzalloc(&hdev->udev->dev, sizeof(*data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+	hwmon_info =
+		devm_kzalloc(&hdev->udev->dev, sizeof(hwmon_info), GFP_KERNEL);
+	if (!hwmon_info) {
+		free(data);
+		return -ENOMEM;
+	}

If you use devm_kzalloc() there is no need to free it. (But see my comments below.) And you missed a '*' in the sizeof.

Quoted 25 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +25 -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(fan), GFP_KERNEL);
+		if (!fan)
+			return -ENOMEM;
+
+		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);
+	if (IS_ERR(hwmon_dev))
+		return PTR_ERR(hwmon_dev);

drivers/hwmon/corsair_hydro_i_pro.c line 612

Quoted 10 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +9 -0
+
+	dev_info(&hdev->udev->dev, "setup hwmon for %s\n", hdev->config->name);
+	return 0;
+}
[...]
+static void astk_delete(struct hydro_i_pro_device *hdev)
+{
+	usb_put_intf(hdev->interface);
+	usb_put_dev(hdev->udev);
+	mutex_destroy(&hdev->io_mutex);

Now that you changed 'hdev' to be devm allocated, this may lead to a problem here, since "putting" the usb device may cause all associated allocations to be freed, which means that 'hdev' may be freed by the time you want to destroy the mutex.

Quoted 61 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +61 -0
+}
+
+static int astk_probe(struct usb_interface *interface,
+		      const struct usb_device_id *id)
+{
+	struct usb_device *udev = usb_get_dev(interface_to_usbdev(interface));
+	struct hydro_i_pro_device *hdev;
+	int retval;
+	struct usb_endpoint_descriptor *bulk_in, *bulk_out;
+
+	hdev = devm_kzalloc(&udev->dev, sizeof(*hdev), GFP_KERNEL);
+
+	if (!hdev) {
+		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 = ;
+	hdev->interface = usb_get_intf(interface);
+
+	/*
+	 * 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 =
+		devm_kzalloc(&hdev->udev->dev, hdev->bulk_in_size, GFP_KERNEL);
+	hdev->bulk_in_endpointAddr = bulk_in->bEndpointAddress;
+	if (!hdev->bulk_in_buffer) {
+		kfree(hdev);
+		goto exit;
+	}
+
+	hdev->bulk_out_size = usb_endpoint_maxp(bulk_out);
+	hdev->bulk_out_buffer =
+		devm_kzalloc(&hdev->udev->dev, hdev->bulk_out_size, GFP_KERNEL);
+	hdev->bulk_out_endpointAddr = bulk_out->bEndpointAddress;
+	if (!hdev->bulk_out_buffer) {
+		kfree(hdev);
+		kfree(hdev->bulk_in_buffer);
+		goto exit;
+	}
+
+	retval = init_device(hdev->udev);
+	if (retval) {
+		dev_err(&interface->dev, "failed initialising %s:%d\n",
+			hdev->config->name, retval);
+		kfree(hdev);
+		kfree(hdev->bulk_in_buffer);
+		kfree(hdev->bulk_out_buffer);

drivers/hwmon/corsair_hydro_i_pro.c line 683

Quoted 10 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +10 -0
+		goto exit;
+	}
+
+	retval = hwmon_init(hdev);
+	if (retval) {
+		dev_err(&interface->dev, "failed initialising hwmon %s:%d\n",
+			hdev->config->name, retval);
+		kfree(hdev);
+		kfree(hdev->bulk_in_buffer);
+		kfree(hdev->bulk_out_buffer);

I hope what I said wasn't confusing, but - like you said - if you use devm_* you don't need to explicitly free it. Furthermore, you access a pointer that has already been freed here.

devm_k.alloc() and devm_kfree() OR k.alloc() and kfree(), do not mix the two. In case of failure I think it makes sense to free the resources either way, but it is not strictly necessary since devm resources will be freed when the device is gone.

drivers/hwmon/corsair_hydro_i_pro.c line 691

Quoted 9 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +8 -0
+		goto exit;
+	}
+
+	usb_set_intfdata(interface, hdev);
+	mutex_init(&hdev->io_mutex);
+exit:
+	return retval;
+}
[...]

Please think more about memory management and how it should work in this module. Only manage devm resources between "getting" and "putting" the device. Don't do it after "putting" or before "getting" the device.
And please compile and test your code (among other things) before submitting as per
https://www.kernel.org/doc/html/latest/hwmon/submitting-patches.html

Barnabás Pőcze

author
#2 lore raw
Quoted 9 lines · Barnabás Pőcze

[...]

drivers/hwmon/corsair_hydro_i_pro.c line 683

Quoted 7 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +7 -0
+     retval = hwmon_init(hdev);
+     if (retval) {
+             dev_err(&interface->dev, "failed initialising hwmon %s:%d\n",
+                     hdev->config->name, retval);
+             kfree(hdev);
+             kfree(hdev->bulk_in_buffer);
+             kfree(hdev->bulk_out_buffer);
Quoted 3 lines

I hope what I said wasn't confusing, but - like you said - if you use devm_* you don't need to explicitly free it. Furthermore, you access a pointer that has already been freed here.

well since I didn't put the device it wouldn't have been freed. This in itself was a
mistake of course since I got the device I should put it as well.

Quoted 4 lines

devm_k.alloc() and devm_kfree() OR k.alloc() and kfree(), do not mix the two. In case of failure I think it makes sense to free the resources either way, but it is not strictly necessary since devm resources will be freed when the device is gone.

I couldn't find any clear documentation on this, but since .disconnect doesn't get
called if probe fails I can just restructure the code so I can call astk_delete on
errors. This makes everything much cleaner and nicer.

Quoted 8 lines
Quoted 8 lines
drivers/hwmon/corsair_hydro_i_pro.c new file +8 -0
+             goto exit;
+     }
+
+     usb_set_intfdata(interface, hdev);
+     mutex_init(&hdev->io_mutex);
+exit:
+     return retval;
+}
Quoted 8 lines
Quoted 1 line

[...]

Please think more about memory management and how it should work in this module. Only manage devm resources between "getting" and "putting" the device. Don't do it after "putting" or before "getting" the device.
And please compile and test your code (among other things) before submitting as per
https://www.kernel.org/doc/html/latest/hwmon/submitting-patches.html

Thanks for the link to the page, I did compile and test the code before submitting, but I
must have ctrl-z ed or something for it not to compile. I always make sure everything
works before submitting (at least setting mode, rpm, pwm and reading rpm).

After looking at the link a bit, I will try and write documentation tomorrow, and see
how the MAINTAINERS file is structured to add myself. After that I will post another version, if there is anything else let me know so I can
incorporate it in the patch.

Kind Regards,

Jaap Aarts