Skip to contentSkip to conversation

i8k: Integrate with the hwmon subsystem

PATCH Superseded Jean Delvare 2 participants 5 messages Apr 13, 2011 – Apr 16, 2011

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 i8k: Integrate with the hwmon subsystem

Let i8k create an hwmon class device so that libsensors will expose the CPU temperature and fan speeds to monitoring applications.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Massimo Dal Zotto <[email protected]>

---
Note: I don't get why CONFIG_I8K is the "Processor type and features" menu, and why the driver lives in drivers/char. Wouldn't drivers/arch/x86 be a better place?

 arch/x86/Kconfig   |    1 
 drivers/char/i8k.c |  148 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 149 insertions(+)
drivers/char/i8k.c +148 -0
@@ -5,6 +5,9 @@
  *
  * Copyright (C) 2001  Massimo Dal Zotto <[email protected]>
  *
+ * Hwmon integration:
+ * Copyright (C) 2011  Jean Delvare <[email protected]>
+ *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
  * Free Software Foundation; either version 2, or (at your option) any
@@ -24,6 +27,8 @@
 #include <linux/dmi.h>
 #include <linux/capability.h>
 #include <linux/mutex.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
 #include <asm/uaccess.h>
 #include <asm/io.h>
 
@@ -58,6 +63,7 @@
 
 static DEFINE_MUTEX(i8k_mutex);
 static char bios_version[4];
+static struct device *i8k_hwmon_dev;
 
 MODULE_AUTHOR("Massimo Dal Zotto ([email protected])");
 MODULE_DESCRIPTION("Driver for accessing SMM BIOS on Dell laptops");
@@ -455,6 +461,142 @@ static int i8k_open_fs(struct inode *ino
 	return single_open(file, i8k_proc_show, NULL);
 }
 
+
+/*
+ * Hwmon interface
+ */
+
+static ssize_t i8k_hwmon_show_temp(struct device *dev,
+				   struct device_attribute *devattr,
+				   char *buf)
+{
+	int cpu_temp;
+
+	cpu_temp = i8k_get_temp(0);
+	return sprintf(buf, "%d\n", cpu_temp * 1000);
+}
+
+static ssize_t i8k_hwmon_show_fan(struct device *dev,
+				  struct device_attribute *devattr,
+				  char *buf)
+{
+	int index = to_sensor_dev_attr(devattr)->index;
+	int fan_speed;
+
+	fan_speed = i8k_get_fan_speed(index);
+	return sprintf(buf, "%d\n", fan_speed);
+}
+
+static ssize_t i8k_hwmon_show_label(struct device *dev,
+				    struct device_attribute *devattr,
+				    char *buf)
+{
+	static const char *labels[4] = {
+		"i8k",
+		"CPU",
+		"Left Fan",
+		"Right Fan",
+	};
+	int index = to_sensor_dev_attr(devattr)->index;
+
+	return sprintf(buf, "%s\n", labels[index]);
+}
+
+static DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL);
+static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
+			  I8K_FAN_LEFT);
+static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
+			  I8K_FAN_RIGHT);
+static SENSOR_DEVICE_ATTR(name, S_IRUGO, i8k_hwmon_show_label, NULL, 0);
+static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, i8k_hwmon_show_label, NULL, 1);
+static SENSOR_DEVICE_ATTR(fan1_label, S_IRUGO, i8k_hwmon_show_label, NULL, 2);
+static SENSOR_DEVICE_ATTR(fan2_label, S_IRUGO, i8k_hwmon_show_label, NULL, 3);
+
+static void i8k_hwmon_remove_files(struct device *dev)
+{
+	device_remove_file(dev, &dev_attr_temp1_input);
+	device_remove_file(dev, &sensor_dev_attr_fan1_input.dev_attr);
+	device_remove_file(dev, &sensor_dev_attr_fan2_input.dev_attr);
+	device_remove_file(dev, &sensor_dev_attr_temp1_label.dev_attr);
+	device_remove_file(dev, &sensor_dev_attr_fan1_label.dev_attr);
+	device_remove_file(dev, &sensor_dev_attr_fan2_label.dev_attr);
+	device_remove_file(dev, &sensor_dev_attr_name.dev_attr);
+}
+
+static int __init i8k_init_hwmon(void)
+{
+	int err;
+
+	i8k_hwmon_dev = hwmon_device_register(NULL);
+	if (IS_ERR(i8k_hwmon_dev)) {
+		err = PTR_ERR(i8k_hwmon_dev);
+		i8k_hwmon_dev = NULL;
+		printk(KERN_ERR "i8k: hwmon registration failed (%d)\n", err);
+		return err;
+	}
+
+	/* Required name attribute */
+	err = device_create_file(i8k_hwmon_dev,
+				 &sensor_dev_attr_name.dev_attr);
+	if (err)
+		goto exit_unregister;
+
+	/* CPU temperature attributes */
+	err = device_create_file(i8k_hwmon_dev, &dev_attr_temp1_input);
+	if (err)
+		goto exit_remove_files;
+	err = device_create_file(i8k_hwmon_dev,
+				 &sensor_dev_attr_temp1_label.dev_attr);
+	if (err)
+		goto exit_remove_files;
+
+	/* Left fan attributes, if left fan is present */
+	err = i8k_get_fan_status(I8K_FAN_LEFT);
+	if (err < 0) {
+		dev_dbg(i8k_hwmon_dev,
+			"Not creating %s fan attributes (%d)\n", "left", err);
+	} else {
+		err = device_create_file(i8k_hwmon_dev,
+					 &sensor_dev_attr_fan1_input.dev_attr);
+		if (err)
+			goto exit_remove_files;
+		err = device_create_file(i8k_hwmon_dev,
+					 &sensor_dev_attr_fan1_label.dev_attr);
+		if (err)
+			goto exit_remove_files;
+	}
+
+	/* Right fan attributes, if right fan is present */
+	err = i8k_get_fan_status(I8K_FAN_RIGHT);
+	if (err < 0) {
+		dev_dbg(i8k_hwmon_dev,
+			"Not creating %s fan attributes (%d)\n", "right", err);
+	} else {
+		err = device_create_file(i8k_hwmon_dev,
+					 &sensor_dev_attr_fan2_input.dev_attr);
+		if (err)
+			goto exit_remove_files;
+		err = device_create_file(i8k_hwmon_dev,
+					 &sensor_dev_attr_fan2_label.dev_attr);
+		if (err)
+			goto exit_remove_files;
+	}
+
+	return 0;
+
+ exit_remove_files:
+	i8k_hwmon_remove_files(i8k_hwmon_dev);
+ exit_unregister:
+	hwmon_device_unregister(i8k_hwmon_dev);
+	return err;
+}
+
+static void __exit i8k_exit_hwmon(void)
+{
+	i8k_hwmon_remove_files(i8k_hwmon_dev);
+	hwmon_device_unregister(i8k_hwmon_dev);
+}
+
 static struct dmi_system_id __initdata i8k_dmi_table[] = {
 	{
 		.ident = "Dell Inspiron",
@@ -590,6 +732,11 @@ static int __init i8k_init(void)
 	if (!proc_i8k)
 		return -ENOENT;
 
+	if (i8k_init_hwmon()) {
+		remove_proc_entry("i8k", NULL);
+		return -ENODEV;
+	}
+
 	printk(KERN_INFO
 	       "Dell laptop SMM driver v%s Massimo Dal Zotto ([email protected])\n",
 	       I8K_VERSION);
@@ -599,6 +746,7 @@ static int __init i8k_init(void)
 
 static void __exit i8k_exit(void)
 {
+	i8k_exit_hwmon();
 	remove_proc_entry("i8k", NULL);
 }
 
arch/x86/Kconfig +1 -0
@@ -919,6 +919,7 @@ config TOSHIBA
 
 config I8K
 	tristate "Dell laptop support"
+	select HWMON
 	---help---
 	  This adds a driver to safely access the System Management Mode
 	  of the CPU on the Dell Inspiron 8000. The System Management Mode
Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#1 lore raw
Quoted 190 lines · Jean Delvare

Let i8k create an hwmon class device so that libsensors will expose the CPU temperature and fan speeds to monitoring applications.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Massimo Dal Zotto <[email protected]>

---
Note: I don't get why CONFIG_I8K is the "Processor type and features" menu, and why the driver lives in drivers/char. Wouldn't drivers/arch/x86 be a better place?

 arch/x86/Kconfig   |    1 
 drivers/char/i8k.c |  148 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 149 insertions(+)
drivers/char/i8k.c +142 -0
@@ -5,6 +5,9 @@
  *
  * Copyright (C) 2001  Massimo Dal Zotto <[email protected]>
  *
+ * Hwmon integration:
+ * Copyright (C) 2011  Jean Delvare <[email protected]>
+ *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
  * Free Software Foundation; either version 2, or (at your option) any
@@ -24,6 +27,8 @@
 #include <linux/dmi.h>
 #include <linux/capability.h>
 #include <linux/mutex.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
 #include <asm/uaccess.h>
 #include <asm/io.h>
 
@@ -58,6 +63,7 @@
 
 static DEFINE_MUTEX(i8k_mutex);
 static char bios_version[4];
+static struct device *i8k_hwmon_dev;
 
 MODULE_AUTHOR("Massimo Dal Zotto ([email protected])");
 MODULE_DESCRIPTION("Driver for accessing SMM BIOS on Dell laptops");
@@ -455,6 +461,142 @@ static int i8k_open_fs(struct inode *ino
 	return single_open(file, i8k_proc_show, NULL);
 }
 
+
+/*
+ * Hwmon interface
+ */
+
+static ssize_t i8k_hwmon_show_temp(struct device *dev,
+				   struct device_attribute *devattr,
+				   char *buf)
+{
+	int cpu_temp;
+
+	cpu_temp = i8k_get_temp(0);
+	return sprintf(buf, "%d\n", cpu_temp * 1000);
+}
+
+static ssize_t i8k_hwmon_show_fan(struct device *dev,
+				  struct device_attribute *devattr,
+				  char *buf)
+{
+	int index = to_sensor_dev_attr(devattr)->index;
+	int fan_speed;
+
+	fan_speed = i8k_get_fan_speed(index);
+	return sprintf(buf, "%d\n", fan_speed);
+}
+
+static ssize_t i8k_hwmon_show_label(struct device *dev,
+				    struct device_attribute *devattr,
+				    char *buf)
+{
+	static const char *labels[4] = {
+		"i8k",
+		"CPU",
+		"Left Fan",
+		"Right Fan",
+	};
+	int index = to_sensor_dev_attr(devattr)->index;
+
+	return sprintf(buf, "%s\n", labels[index]);
+}
+
+static DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL);
+static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
+			  I8K_FAN_LEFT);
+static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
+			  I8K_FAN_RIGHT);
+static SENSOR_DEVICE_ATTR(name, S_IRUGO, i8k_hwmon_show_label, NULL, 0);
+static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, i8k_hwmon_show_label, NULL, 1);
+static SENSOR_DEVICE_ATTR(fan1_label, S_IRUGO, i8k_hwmon_show_label, NULL, 2);
+static SENSOR_DEVICE_ATTR(fan2_label, S_IRUGO, i8k_hwmon_show_label, NULL, 3);
+
+static void i8k_hwmon_remove_files(struct device *dev)
+{
+	device_remove_file(dev, &dev_attr_temp1_input);
+	device_remove_file(dev, &sensor_dev_attr_fan1_input.dev_attr);
+	device_remove_file(dev, &sensor_dev_attr_fan2_input.dev_attr);
+	device_remove_file(dev, &sensor_dev_attr_temp1_label.dev_attr);
+	device_remove_file(dev, &sensor_dev_attr_fan1_label.dev_attr);
+	device_remove_file(dev, &sensor_dev_attr_fan2_label.dev_attr);
+	device_remove_file(dev, &sensor_dev_attr_name.dev_attr);
+}
+
+static int __init i8k_init_hwmon(void)
+{
+	int err;
+
+	i8k_hwmon_dev = hwmon_device_register(NULL);
+	if (IS_ERR(i8k_hwmon_dev)) {
+		err = PTR_ERR(i8k_hwmon_dev);
+		i8k_hwmon_dev = NULL;
+		printk(KERN_ERR "i8k: hwmon registration failed (%d)\n", err);
+		return err;
+	}
+
+	/* Required name attribute */
+	err = device_create_file(i8k_hwmon_dev,
+				 &sensor_dev_attr_name.dev_attr);
+	if (err)
+		goto exit_unregister;
+
+	/* CPU temperature attributes */
+	err = device_create_file(i8k_hwmon_dev, &dev_attr_temp1_input);
+	if (err)
+		goto exit_remove_files;
+	err = device_create_file(i8k_hwmon_dev,
+				 &sensor_dev_attr_temp1_label.dev_attr);
+	if (err)
+		goto exit_remove_files;
+
+	/* Left fan attributes, if left fan is present */
+	err = i8k_get_fan_status(I8K_FAN_LEFT);
+	if (err < 0) {
+		dev_dbg(i8k_hwmon_dev,
+			"Not creating %s fan attributes (%d)\n", "left", err);
+	} else {
+		err = device_create_file(i8k_hwmon_dev,
+					 &sensor_dev_attr_fan1_input.dev_attr);
+		if (err)
+			goto exit_remove_files;
+		err = device_create_file(i8k_hwmon_dev,
+					 &sensor_dev_attr_fan1_label.dev_attr);
+		if (err)
+			goto exit_remove_files;
+	}
+
+	/* Right fan attributes, if right fan is present */
+	err = i8k_get_fan_status(I8K_FAN_RIGHT);
+	if (err < 0) {
+		dev_dbg(i8k_hwmon_dev,
+			"Not creating %s fan attributes (%d)\n", "right", err);
+	} else {
+		err = device_create_file(i8k_hwmon_dev,
+					 &sensor_dev_attr_fan2_input.dev_attr);
+		if (err)
+			goto exit_remove_files;
+		err = device_create_file(i8k_hwmon_dev,
+					 &sensor_dev_attr_fan2_label.dev_attr);
+		if (err)
+			goto exit_remove_files;
+	}
+
+	return 0;
+
+ exit_remove_files:
+	i8k_hwmon_remove_files(i8k_hwmon_dev);
+ exit_unregister:
+	hwmon_device_unregister(i8k_hwmon_dev);
+	return err;
+}
+
+static void __exit i8k_exit_hwmon(void)
+{
+	i8k_hwmon_remove_files(i8k_hwmon_dev);
+	hwmon_device_unregister(i8k_hwmon_dev);
+}
+
 static struct dmi_system_id __initdata i8k_dmi_table[] = {
 	{
 		.ident = "Dell Inspiron",

drivers/char/i8k.c static int __init i8k_init(void) · line 739

Quoted 8 lines
drivers/char/i8k.c +5 -0
@@ -590,6 +732,11 @@ static int __init i8k_init(void)
 	if (!proc_i8k)
 		return -ENOENT;
 
+	if (i8k_init_hwmon()) {
+		remove_proc_entry("i8k", NULL);
+		return -ENODEV;
+	}
+

Should that be something like

	err = i8k_init_hwmon();
	if (err)
		goto remove_proc;

	...

remove_proc:

	remove_proc_entry("i8k", NULL);
	return err;
Quoted 16 lines
drivers/char/i8k.c +1 -0
 	printk(KERN_INFO
 	       "Dell laptop SMM driver v%s Massimo Dal Zotto ([email protected])\n",
 	       I8K_VERSION);
@@ -599,6 +746,7 @@ static int __init i8k_init(void)
 
 static void __exit i8k_exit(void)
 {
+	i8k_exit_hwmon();
 	remove_proc_entry("i8k", NULL);
 }
 

arch/x86/Kconfig config TOSHIBA · line 925

Quoted 7 lines
arch/x86/Kconfig +1 -0
@@ -919,6 +919,7 @@ config TOSHIBA
 
 config I8K
 	tristate "Dell laptop support"
+	select HWMON
 	---help---
 	  This adds a driver to safely access the System Management Mode
 	  of the CPU on the Dell Inspiron 8000. The System Management Mode

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#2 lore raw
In reply to Jean Delvare

Hi Guenter,

Thanks for the review.

Quoted 17 lines · Guenter Roeck

drivers/char/i8k.c static int __init i8k_init(void) · line 739

Quoted 9 lines · Jean Delvare
drivers/char/i8k.c +5 -0
@@ -590,6 +732,11 @@ static int __init i8k_init(void)
 	if (!proc_i8k)
 		return -ENOENT;
 
+	if (i8k_init_hwmon()) {
+		remove_proc_entry("i8k", NULL);
+		return -ENODEV;
+	}
+

Should that be something like

	err = i8k_init_hwmon();
	if (err)
		goto remove_proc;

	...
Quoted 3 lines

remove_proc:

	remove_proc_entry("i8k", NULL);
	return err;

I would have done exactly this if it were my driver. However I noticed that the error code from i8k_probe() is not preserved, so I decided to follow the same logic for consistency.

If you think this is a blocker, then I'll write a preliminary patch to preserve the error code returned by i8k_probe(), and then update my own patch in the way you suggested above.

Quoted 4 lines
Quoted 4 lines
drivers/char/i8k.c +0 -0
 	printk(KERN_INFO
 	       "Dell laptop SMM driver v%s Massimo Dal Zotto ([email protected])\n",
 	       I8K_VERSION);
Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Acked-by on v1 1/2
#3 lore raw
In reply to Jean Delvare

Hi Jean,

Quoted 22 lines · Jean Delvare

Hi Guenter,

Thanks for the review.

Quoted 17 lines · Guenter Roeck

drivers/char/i8k.c static int __init i8k_init(void) · line 739

Quoted 9 lines · Jean Delvare
drivers/char/i8k.c +5 -0
@@ -590,6 +732,11 @@ static int __init i8k_init(void)
 	if (!proc_i8k)
 		return -ENOENT;
 
+	if (i8k_init_hwmon()) {
+		remove_proc_entry("i8k", NULL);
+		return -ENODEV;
+	}
+

Should that be something like

	err = i8k_init_hwmon();
	if (err)
		goto remove_proc;

	...
Quoted 8 lines
Quoted 3 lines

remove_proc:

	remove_proc_entry("i8k", NULL);
	return err;

I would have done exactly this if it were my driver. However I noticed that the error code from i8k_probe() is not preserved, so I decided to follow the same logic for consistency.

Both are independent of each other. If a rule is not followed at one place it doesn't mean that it should not be followed elsewhere. Besides, my main point was to follow the one-function-exit rule; retaining the error code was a side effect as

	err = i8k_init_hwmon();
	if (err) {
		err = -ENODEV;
		goto remove_proc;
	}

didn't seem to make much sense.

Is there a generic rule that error codes shall be retained ? I looked for it, but did not find it. If not I should add it to the hwmon driver guidelines.

Quoted 4 lines

If you think this is a blocker, then I'll write a preliminary patch to preserve the error code returned by i8k_probe(), and then update my own patch in the way you suggested above.

I'll leave it up to you.

for both patches.

Thanks,
Guenter

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#4 lore raw
In reply to Jean Delvare

Hi Guenter,

Quoted 23 lines · Guenter Roeck
Quoted 22 lines · Jean Delvare
Quoted 20 lines · Guenter Roeck

drivers/char/i8k.c static int __init i8k_init(void) · line 739

Quoted 9 lines · Jean Delvare
drivers/char/i8k.c +5 -0
@@ -590,6 +732,11 @@ static int __init i8k_init(void)
 	if (!proc_i8k)
 		return -ENOENT;
 
+	if (i8k_init_hwmon()) {
+		remove_proc_entry("i8k", NULL);
+		return -ENODEV;
+	}
+

Should that be something like

	err = i8k_init_hwmon();
	if (err)
		goto remove_proc;

	...

remove_proc:

	remove_proc_entry("i8k", NULL);
	return err;
Quoted 6 lines
Quoted 4 lines

I would have done exactly this if it were my driver. However I noticed that the error code from i8k_probe() is not preserved, so I decided to follow the same logic for consistency.

Both are independent of each other. If a rule is not followed at one place it doesn't mean that it should not be followed elsewhere.

I don't necessarily agree. Mixing conventions within a single function can make things confusing and increase the risk of introducing bugs later.

Quoted 11 lines

Besides, my main point
was to follow the one-function-exit rule; retaining the error code was a side effect as

	err = i8k_init_hwmon();
	if (err) {
		err = -ENODEV;
		goto remove_proc;
	}

didn't seem to make much sense.

OK, I get it now. I'll send an updated patch.

Quoted 2 lines

Is there a generic rule that error codes shall be retained ? I looked for it, but did not find it. If not I should add it to the hwmon driver guidelines.

I don't remember reading it anywhere, it's just common sense for me after years of code review.

Quoted 9 lines
Quoted 3 lines

If you think this is a blocker, then I'll write a preliminary patch to preserve the error code returned by i8k_probe(), and then update my own patch in the way you suggested above.

I'll leave it up to you.

for both patches.

Thanks.

Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors