diff mbox series

[4/6] automatic fan count detection

Message ID 20200713193227.189751-4-jaap.aarts1@gmail.com (mailing list archive)
State Superseded
Headers show
Series [1/6] skeleton for asetek gen 6 driver | expand

Commit Message

jaap aarts July 13, 2020, 7:32 p.m. UTC
Signed-off-by: Jaap Aarts <jaap.aarts1@example.com>
---
 drivers/hwmon/asetek_gen6.c | 146 ++++++++++++++++++++++++------------
 1 file changed, 96 insertions(+), 50 deletions(-)
diff mbox series

Patch

diff --git a/drivers/hwmon/asetek_gen6.c b/drivers/hwmon/asetek_gen6.c
index 5c3bd456f881..d657ecc78908 100644
--- a/drivers/hwmon/asetek_gen6.c
+++ b/drivers/hwmon/asetek_gen6.c
@@ -9,11 +9,11 @@ 
 
 /*
  * Supports following chips: 
- * driver h100i platinum
+ * h100i platinum
  * 
  * Other products should work with this driver but no testing has been done.
  * 
- * Note: platinum is the codename name for pro within driver
+ * Note: platinum is the codename name for pro within driver, so h100i platinum = h1ooi pro
  * 
  * Note: fan curve controll has not been implemented
  */
@@ -50,7 +50,7 @@  struct curve_point {
 	uint8_t pwm;
 };
 
-struct fan_hwmon_data {
+struct hwmon_fan_data {
 	char fan_channel;
 	long fan_target;
 	unsigned char fan_pwm_target;
@@ -62,7 +62,6 @@  struct hwmon_data {
 	struct driver *dev;
 	int channel_count;
 	void **channel_data;
-	int fan_1_index;
 };
 
 struct curve_point default_curve[] = {
@@ -105,10 +104,10 @@  static bool check_succes(char command, char ret[SUCCES_LENGTH])
 {
 	char success[SUCCES_LENGTH] = { command };
 	strncpy(&success[1], SUCCESS, SUCCES_LENGTH - 1);
-	return strncmp(ret, success, SUCCES_LENGTH) == 0;
+	return strncmp(ret, success, SUCCES_LENGTH - 1) == 0;
 }
 
-int set_fan_rpm_curve(struct driver *cdev, struct fan_hwmon_data *fan_data,
+int set_fan_rpm_curve(struct driver *cdev, struct hwmon_fan_data *fan_data,
 		      struct curve_point point[7])
 {
 	int retval;
@@ -145,14 +144,12 @@  int set_fan_rpm_curve(struct driver *cdev, struct fan_hwmon_data *fan_data,
 	if (retval != 0)
 		return retval;
 
-	if (!check_succes(
-		    send_buf[0],
-		    recv_buf) /* || recv_buf[3] != fan_data->fan_channel */)
+	if (!check_succes(send_buf[0], recv_buf))
 		printk(KERN_INFO "[*] Failled setting fan curve %d,%d,%d/%d\n",
 		       recv_buf[0], recv_buf[1], recv_buf[2], recv_buf[3]);
 	return 0;
 }
-int set_fan_target_rpm(struct driver *cdev, struct fan_hwmon_data *fan_data,
+int set_fan_target_rpm(struct driver *cdev, struct hwmon_fan_data *fan_data,
 		       long val)
 {
 	int retval;
@@ -180,17 +177,16 @@  int set_fan_target_rpm(struct driver *cdev, struct fan_hwmon_data *fan_data,
 		return retval;
 
 	//no error
-	if (!check_succes(send_buf[0], recv_buf) ||
-	    recv_buf[3] != fan_data->fan_channel)
+	if (!check_succes(send_buf[0], recv_buf))
 		printk(KERN_INFO "[*] Failled setting fan rpm %d,%d,%d/%d\n",
 		       recv_buf[0], recv_buf[1], recv_buf[2], recv_buf[3]);
 	return 0;
 }
-void get_fan_target_rpm(struct fan_hwmon_data *fan_data, long *val)
+void get_fan_target_rpm(struct hwmon_fan_data *fan_data, long *val)
 {
 	*val = fan_data->fan_target;
 }
-int get_fan_current_rpm(struct driver *cdev, struct fan_hwmon_data *fan_data,
+int get_fan_current_rpm(struct driver *cdev, struct hwmon_fan_data *fan_data,
 			long *val)
 {
 	int retval;
@@ -221,7 +217,7 @@  int get_fan_current_rpm(struct driver *cdev, struct fan_hwmon_data *fan_data,
 	return 0;
 }
 
-int set_fan_target_pwm(struct driver *cdev, struct fan_hwmon_data *fan_data,
+int set_fan_target_pwm(struct driver *cdev, struct hwmon_fan_data *fan_data,
 		       long val)
 {
 	int retval;
@@ -248,8 +244,7 @@  int set_fan_target_pwm(struct driver *cdev, struct fan_hwmon_data *fan_data,
 		return retval;
 
 	//no error
-	if (!check_succes(send_buf[0], recv_buf) ||
-	    recv_buf[3] != fan_data->fan_channel)
+	if (!check_succes(send_buf[0], recv_buf))
 		printk(KERN_INFO "[*] Failled setting fan pwm %d,%d,%d/%d\n",
 		       recv_buf[0], recv_buf[1], recv_buf[2], recv_buf[3]);
 	return 0;
@@ -298,7 +293,7 @@  static int write_func(struct device *dev, enum hwmon_sensor_types type,
 {
 	struct hwmon_data *data = dev_get_drvdata(dev);
 	struct driver *cdev = data->dev;
-	struct fan_hwmon_data *fan_data;
+	struct hwmon_fan_data *fan_data;
 	int retval = 0;
 
 	switch (type) {
@@ -404,7 +399,7 @@  int read_func(struct device *dev, enum hwmon_sensor_types type, u32 attr,
 {
 	struct hwmon_data *data = dev_get_drvdata(dev);
 	struct driver *cdev = data->dev;
-	struct fan_hwmon_data *fan_data;
+	struct hwmon_fan_data *fan_data;
 	int retval = 0;
 	if (channel >= data->channel_count)
 		return -EAGAIN;
@@ -469,14 +464,8 @@  int read_func(struct device *dev, enum hwmon_sensor_types type, u32 attr,
 exit:
 	return retval;
 }
-static const struct hwmon_channel_info *dual_fan[] = {
-	HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_MIN,
-			   HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_MIN),
-	HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT | HWMON_PWM_MODE,
-			   HWMON_PWM_INPUT | HWMON_PWM_MODE),
-
-	NULL
-};
+#define fan_config HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_MIN
+#define pwm_config HWMON_PWM_INPUT | HWMON_PWM_MODE
 
 static const struct hwmon_ops asetek_6_ops = {
 	.is_visible = is_visible_func,
@@ -484,40 +473,93 @@  static const struct hwmon_ops asetek_6_ops = {
 	.write = write_func,
 };
 
-static const struct hwmon_chip_info lm75_chip_info = {
-	.ops = &asetek_6_ops,
-	.info = dual_fan,
-};
+bool does_fan_exist(struct driver *cdev, int channel)
+{
+	int retval;
+	int wrote;
+	int sndpipe = usb_sndbulkpipe(cdev->udev, cdev->bulk_out_endpointAddr);
+	int rcvpipe = usb_rcvbulkpipe(cdev->udev, cdev->bulk_in_endpointAddr);
+
+	char *send_buf = cdev->bulk_out_buffer;
+	char *recv_buf = cdev->bulk_in_buffer;
+
+	send_buf[0] = 0x43;
+	send_buf[1] = channel;
+	send_buf[2] = (600 >> 8);
+	send_buf[3] = 600;
+
+	retval = usb_bulk_msg(cdev->udev, sndpipe, send_buf, 4, &wrote, 100);
+	if (retval != 0)
+		return false;
+
+	retval = usb_bulk_msg(cdev->udev, rcvpipe, recv_buf, 6, &wrote, 100000);
+	if (retval != 0)
+		return false;
+
+	if (!check_succes(send_buf[0], recv_buf))
+		return false;
+	return true;
+}
+
+int get_fan_count(struct driver *dev)
+{
+	int fan;
+	for (fan = 0; does_fan_exist(dev, fan); fan += 1) {
+	}
+	return fan;
+}
 
 void hwmon_init(struct driver *dev)
 {
+	int fan_id;
 	struct device *hwmon_dev;
+	struct hwmon_fan_data *fan;
 	struct hwmon_data *data = devm_kzalloc(
 		&dev->udev->dev, sizeof(struct hwmon_data), GFP_KERNEL);
 	struct hwmon_chip_info *hwmon_info = devm_kzalloc(
 		&dev->udev->dev, sizeof(struct hwmon_chip_info), GFP_KERNEL);
-	struct fan_hwmon_data *fan1 = devm_kzalloc(
-		&dev->udev->dev, sizeof(struct fan_hwmon_data), GFP_KERNEL);
-	struct fan_hwmon_data *fan2 = devm_kzalloc(
-		&dev->udev->dev, sizeof(struct fan_hwmon_data), GFP_KERNEL);
-	data->channel_count = 2; //amount of fans
+	struct hwmon_channel_info **aio_info =
+		devm_kzalloc(&dev->udev->dev,
+			     sizeof(struct hwmon_channel_info *) * 2,
+			     GFP_KERNEL); //2==amount of channel infos.
+	u32 *fans_config = devm_kzalloc(&dev->udev->dev,
+					sizeof(u32) * (data->channel_count + 1),
+					GFP_KERNEL);
+	u32 *pwms_config = devm_kzalloc(&dev->udev->dev,
+					sizeof(u32) * (data->channel_count + 1),
+					GFP_KERNEL);
+
+	data->channel_count = get_fan_count(dev); //amount of fans
 	data->channel_data =
 		devm_kzalloc(&dev->udev->dev,
 			     sizeof(char *) * data->channel_count, GFP_KERNEL);
 
-	hwmon_info->ops = &asetek_6_ops;
-	hwmon_info->info = dual_fan;
+	for (fan_id = 0; fan_id <= data->channel_count; fan_id++) {
+		fan = devm_kzalloc(&dev->udev->dev,
+				   sizeof(struct hwmon_fan_data), GFP_KERNEL);
+		fan->fan_channel = fan_id;
+		fan->mode = 2;
+		data->channel_data[fan_id] = fan;
+		fans_config[fan_id] = fan_config;
+		pwms_config[fan_id] = pwm_config;
+	}
+	fans_config[data->channel_count] = 0;
+	pwms_config[data->channel_count] = 0;
 
-	fan1->fan_channel = 0;
-	fan1->mode = 2;
-	fan2->fan_channel = 1;
-	fan2->mode = 2;
+	aio_info[0] = devm_kzalloc(
+		&dev->udev->dev, sizeof(struct hwmon_channel_info), GFP_KERNEL);
+	aio_info[0]->type = hwmon_fan;
+	aio_info[0]->config = fans_config;
 
-	data->fan_1_index = 0;
-	data->dev = dev;
-	data->channel_data[0] = fan1;
-	data->channel_data[1] = fan2;
+	aio_info[1] = devm_kzalloc(
+		&dev->udev->dev, sizeof(struct hwmon_channel_info), GFP_KERNEL);
+	aio_info[1]->type = hwmon_pwm;
+	aio_info[1]->config = pwms_config;
+
+	hwmon_info->ops = &asetek_6_ops;
+	hwmon_info->info = (const struct hwmon_channel_info **)aio_info;
 
+	data->dev = dev;
 	hwmon_dev = devm_hwmon_device_register_with_info(
 		&dev->udev->dev, "driver_fan", data, hwmon_info, NULL);
 	printk(KERN_INFO "[*] Setup hwmon\n");
@@ -528,10 +570,14 @@  void hwmon_deinit(struct driver *dev)
 	hwmon_device_unregister(&dev->udev->dev);
 	printk(KERN_INFO "[*] HWMON DISCONNECT\n");
 }
-/* devices that work with this driver */
+
+/*
+	Devices that work with this driver.
+	More devices should work, however none have been tested.
+*/
 static const struct usb_device_id astk_table[] = {
 	{ USB_DEVICE(0x1b1c, 0x0c15) },
-	{} /* Terminating entry */
+	{},
 };
 
 MODULE_DEVICE_TABLE(usb, astk_table);
@@ -604,13 +650,14 @@  static int astk_probe(struct usb_interface *interface,
 	dev->bulk_out_buffer = kmalloc(dev->bulk_out_size, GFP_KERNEL);
 	dev->bulk_out_endpointAddr = bulk_out->bEndpointAddress;
 
-	hwmon_init(dev);
 	retval = init_device(dev->udev);
 	if (retval) {
 		dev_err(&interface->dev, "Failled initialising this device.\n");
 		goto exit;
 	}
 
+	hwmon_init(dev);
+
 	usb_set_intfdata(interface, dev);
 	kref_init(&dev->kref);
 	mutex_init(&dev->io_mutex);
@@ -626,7 +673,6 @@  static void astk_disconnect(struct usb_interface *interface)
 	usb_set_intfdata(interface, NULL);
 	kref_put(&dev->kref, astk_delete);
 	deinit_device(dev->udev);
-	/* let the user know what node this device is now attached to */
 }
 static int astk_resume(struct usb_interface *intf)
 {