hwmon(asus_atk0110): debugfs interface
Expose the raw GGRP/GITM interface via debugfs. The hwmon interface is reverse engineered and the driver tends to break on newer boards... Using this interface it's possible to poke directly at the ACPI methods without the need to recompile, reducing the guesswork and the round trips needed to support a new revisions of the interface.
---
Does it make sense to have this upstream?
drivers/hwmon/asus_atk0110.c | 197 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+)
Index: linux-2.6.git/drivers/hwmon/asus_atk0110.c
=================================--- linux-2.6.git.orig/drivers/hwmon/asus_atk0110.c 2009-12-07 14:49:49.000000000 +0100
+++ linux-2.6.git/drivers/hwmon/asus_atk0110.c 2009-12-07 15:03:37.000000000 +0100
@@ -5,6 +5,7 @@
* See COPYING in the top level directory of the kernel tree. */
+#include <linux/debugfs.h>
#include <linux/kernel.h>
#include <linux/hwmon.h>
#include <linux/list.h>
@@ -101,6 +102,11 @@
int temperature_count;
int fan_count;
struct list_head sensor_list;
+
+ struct {
+ struct dentry *root;
+ u32 id;
+ } debugfs;
};
@@ -624,6 +630,193 @@
return err;
}
+static int atk_debugfs_gitm_get(void *p, u64 *val)
+{
+ struct atk_data *data = p;
+ union acpi_object *ret;
+ struct atk_acpi_ret_buffer *buf;
+ int err = 0;
+
+ if (!data->read_handle)
+ return -ENODEV;
+
+ if (!data->debugfs.id)
+ return -EINVAL;
+
+ ret = atk_gitm(data, data->debugfs.id);
+ if (IS_ERR(ret))
+ return PTR_ERR(ret);
+
+ buf = (void *)ret->buffer.pointer;
+ if (buf->flags)
+ *val = buf->value;
+ else
+ err = -EIO;
+
+ return err;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(atk_debugfs_gitm,
+ atk_debugfs_gitm_get,
+ NULL,
+ "0x%08llx\n")
+
+static int atk_acpi_print(char *buf, size_t sz, union acpi_object *obj) +{
+ int ret = 0;
+
+ switch (obj->type) {
+ case ACPI_TYPE_INTEGER:
+ ret = snprintf(buf, sz, "0x%08llx\n", obj->integer.value);
+ break;
+ case ACPI_TYPE_STRING:
+ ret = snprintf(buf, sz, "%s\n", obj->string.pointer);
+ break;
+ }
+
+ return ret;
+}
+
+static void atk_pack_print(char *buf, size_t sz, union acpi_object *pack) +{
+ int ret;
+ int i;
+
+ for (i = 0; i < pack->package.count; i++) {
+ union acpi_object *obj = &pack->package.elements[i];
+
+ ret = atk_acpi_print(buf, sz, obj);
+ if (ret >= sz)
+ break;
+ buf += ret;
+ sz -= ret;
+ }
+}
+
+static int atk_debugfs_ggrp_open(struct inode *inode, struct file *file) +{
+ struct atk_data *data = inode->i_private;
+ union acpi_object *ret = NULL;
+ u8 cls;
+ char *buf = NULL;
+ size_t size = 512;
+ int err;
+ int i;
+
+ if (!data->enumerate_handle)
+ return -ENODEV;
+ if (!data->debugfs.id)
+ return -EINVAL;
+
+ cls = (data->debugfs.id & 0xff000000) >> 24;
+ ret = atk_ggrp(data, cls);
+ if (IS_ERR(ret))
+ return PTR_ERR(ret);
+
+ for (i = 0; i < ret->package.count; i++) {
+ union acpi_object *pack = &ret->package.elements[i];
+ union acpi_object *id;
+
+ if (pack->type != ACPI_TYPE_PACKAGE)
+ continue;
+ id = &pack->package.elements[0];
+ if (id->integer.value = data->debugfs.id) {
+ /* Print the package */
+ buf = kzalloc(size, GFP_KERNEL);
+ if (!buf) {
+ ACPI_FREE(ret);
+ err = -ENOMEM;
+ goto outerr;
+ }
+ atk_pack_print(buf, size, pack);
+ break;
+ }
+ }
+ ACPI_FREE(ret);
+
+ if (!buf) {
+ err = -EINVAL;
+ goto outerr;
+ }
+ file->private_data = buf;
+
+ return nonseekable_open(inode, file);
+outerr:
+ return err;
+}
+
+static ssize_t atk_debugfs_ggrp_read(struct file *file, char __user *buf,
+ size_t count, loff_t *pos)
+{
+ char *str = file->private_data;
+ size_t len;
+
+ if (!str)
+ return -ENODEV;
+
+ len = strlen(str);
+
+ return simple_read_from_buffer(buf, count, pos, str, len);
+}
+
+static int atk_debugfs_ggrp_release(struct inode *inode, struct file *file) +{
+ kfree(file->private_data);
+ return 0;
+}
+
+static struct file_operations atk_debugfs_ggrp_fops = {
+ .read = atk_debugfs_ggrp_read,
+ .open = atk_debugfs_ggrp_open,
+ .release = atk_debugfs_ggrp_release,
+};
+
+static int atk_debugfs_init(struct atk_data *data)
+{
+ struct dentry *d;
+ struct dentry *f;
+ int err;
+
+ d = debugfs_create_dir("asus_atk0110", NULL);
+ if (!d)
+ return -ENODEV;
+ if (IS_ERR(d))
+ return PTR_ERR(d);
+
+ f = debugfs_create_x32("id", S_IWUSR, d, &data->debugfs.id);
+ if (!f || IS_ERR(f)) {
+ err = -ENODEV;
+ goto cleanup;
+ }
+
+ f = debugfs_create_file("gitm", S_IRUSR, d, data,
+ &atk_debugfs_gitm);
+ if (!f || IS_ERR(f)) {
+ err = -ENODEV;
+ goto cleanup;
+ }
+
+ f = debugfs_create_file("ggrp", S_IRUSR, d, data,
+ &atk_debugfs_ggrp_fops);
+ if (!f || IS_ERR(f)) {
+ err = -ENODEV;
+ goto cleanup;
+ }
+
+ data->debugfs.root = d;
+ data->debugfs.id = 0;
+
+ return 0;
+cleanup:
+ debugfs_remove_recursive(d);
+ return err;
+}
+
+static void atk_debugfs_cleanup(struct atk_data *data)
+{
+ debugfs_remove_recursive(data->debugfs.root);
+}
+
static int atk_add_sensor(struct atk_data *data, union acpi_object *obj) {
struct device *dev = &data->acpi_dev->dev;
@@ -1180,6 +1373,8 @@
if (err)
goto cleanup;
+ atk_debugfs_init(data);
+
device->driver_data = data;
return 0;
cleanup:
@@ -1198,6 +1393,8 @@
device->driver_data = NULL;
+ atk_debugfs_cleanup(data);
+
atk_remove_files(data);
atk_free_sensors(data);
hwmon_device_unregister(data->hwmon_dev);
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Hi Luca,
Quoted 9 lines · Luca Tettamanti
Expose the raw GGRP/GITM interface via debugfs. The hwmon interface is reverse engineered and the driver tends to break on newer boards... Using this interface it's possible to poke directly at the ACPI methods without the need to recompile, reducing the guesswork and the round trips needed to support a new revisions of the interface.
Signed-off-by Luca Tettamanti <[email protected]>---
Does it make sense to have this upstream?
If it makes your life easier, then yes, it certainly makes sense. The size increase is reasonable.
However, I would like the size increase to be kept minimal when debugfs is not enabled. At the moment, all the debugging code is still included in that case. Functions atk_debugfs_ggrp_open, atk_debugfs_gitm_get, atk_debugfs_ggrp_read, atk_debugfs_gitm_open and atk_debugfs_ggrp_release are always included even when they will never be called.
Review (note: I don't know anything about debugfs):
Quoted 39 lines
drivers/hwmon/asus_atk0110.c | 197 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+)Index:
linux-2.6.git/drivers/hwmon/asus_atk0110.c
=================================> ---linux-2.6.git.orig/drivers/hwmon/asus_atk0110.c2009-12-07 14:49:49.000000000 +0100
+++linux-2.6.git/drivers/hwmon/asus_atk0110.c2009-12-07 15:03:37.000000000 +0100(quoted diff) +14
-0@@ -5,6 +5,7 @@ * See COPYING in the top level directory of the kernel tree. */ +#include <linux/debugfs.h> #include <linux/kernel.h> #include <linux/hwmon.h> #include <linux/list.h>@@ -101,6 +102,11 @@ int temperature_count; int fan_count; struct list_head sensor_list; + + struct { + struct dentry *root; + u32 id; + } debugfs; };@@ -624,6 +630,193 @@ return err; } +static int atk_debugfs_gitm_get(void *p, u64 *val) +{ + struct atk_data *data = p; + union acpi_object *ret; + struct atk_acpi_ret_buffer *buf; + int err = 0; + + if (!data->read_handle)
Review line 650
Quoted 10 lines
(quoted diff) +10
-0+ return -ENODEV; + + if (!data->debugfs.id) + return -EINVAL; + + ret = atk_gitm(data, data->debugfs.id); + if (IS_ERR(ret)) + return PTR_ERR(ret); + + buf = (void *)ret->buffer.pointer;
This cast looks wrong. If a cast is really needed, you should cast to the right pointer type.
Quoted 39 lines
(quoted diff) +39
-0+ if (buf->flags) + *val = buf->value; + else + err = -EIO; + + return err; +} + +DEFINE_SIMPLE_ATTRIBUTE(atk_debugfs_gitm, + atk_debugfs_gitm_get, + NULL, + "0x%08llx\n") + +static int atk_acpi_print(char *buf, size_t sz, union acpi_object *obj) +{ + int ret = 0; + + switch (obj->type) { + case ACPI_TYPE_INTEGER: + ret = snprintf(buf, sz, "0x%08llx\n", obj->integer.value); + break; + case ACPI_TYPE_STRING: + ret = snprintf(buf, sz, "%s\n", obj->string.pointer); + break; + } + + return ret; +} + +static void atk_pack_print(char *buf, size_t sz, union acpi_object *pack) +{ + int ret; + int i; + + for (i = 0; i < pack->package.count; i++) { + union acpi_object *obj = &pack->package.elements[i]; + + ret = atk_acpi_print(buf, sz, obj); + if (ret >= sz)
Review line 699
Quoted 10 lines
(quoted diff) +10
-0+ break; + buf += ret; + sz -= ret; + } +} + +static int atk_debugfs_ggrp_open(struct inode *inode, struct file *file) +{ + struct atk_data *data = inode->i_private; + union acpi_object *ret = NULL;
Initialization isn't needed.
Review line 702
Quoted 3 lines
(quoted diff) +3
-0+ u8 cls; + char *buf = NULL; + size_t size = 512;
Makes little sense to have a variable for this.
Quoted 10 lines
(quoted diff) +10
-0+ int err; + int i; + + if (!data->enumerate_handle) + return -ENODEV; + if (!data->debugfs.id) + return -EINVAL; + + cls = (data->debugfs.id & 0xff000000) >> 24; + ret = atk_ggrp(data, cls);
Review line 722
Quoted 10 lines
(quoted diff) +10
-0+ if (IS_ERR(ret)) + return PTR_ERR(ret); + + for (i = 0; i < ret->package.count; i++) { + union acpi_object *pack = &ret->package.elements[i]; + union acpi_object *id; + + if (pack->type != ACPI_TYPE_PACKAGE) + continue; + id = &pack->package.elements[0];
Are packages guaranteed to always contain at least one element?
Quoted 13 lines
(quoted diff) +13
-0+ if (id->integer.value = data->debugfs.id) { + /* Print the package */ + buf = kzalloc(size, GFP_KERNEL); + if (!buf) { + ACPI_FREE(ret); + err = -ENOMEM; + goto outerr; + } + atk_pack_print(buf, size, pack); + break; + } + } + ACPI_FREE(ret);
Review line 745
Quoted 10 lines
(quoted diff) +10
-0+ + if (!buf) { + err = -EINVAL; + goto outerr; + } + file->private_data = buf; + + return nonseekable_open(inode, file); +outerr: + return err;
It makes little sense to have a common error path if you don't do anything in it.
Review line 755
Quoted 10 lines
(quoted diff) +10
-0+} + +static ssize_t atk_debugfs_ggrp_read(struct file *file, char __user *buf, + size_t count, loff_t *pos) +{ + char *str = file->private_data; + size_t len; + + if (!str) + return -ENODEV;
This can't happen. If file->private_data is NULL, opening the file has failed.
Review line 768
Quoted 13 lines
(quoted diff) +13
-0+ + len = strlen(str); + + return simple_read_from_buffer(buf, count, pos, str, len); +} + +static int atk_debugfs_ggrp_release(struct inode *inode, struct file *file) +{ + kfree(file->private_data); + return 0; +} + +static struct file_operations atk_debugfs_ggrp_fops = {
Unless the API prevents it, this could be marked const.
Quoted 8 lines
(quoted diff) +8
-0+ .read = atk_debugfs_ggrp_read, + .open = atk_debugfs_ggrp_open, + .release = atk_debugfs_ggrp_release, +}; + +static int atk_debugfs_init(struct atk_data *data) +{ + struct dentry *d;
Review line 786
Quoted 10 lines
(quoted diff) +10
-0+ struct dentry *f; + int err; + + d = debugfs_create_dir("asus_atk0110", NULL); + if (!d) + return -ENODEV; + if (IS_ERR(d)) + return PTR_ERR(d); + + f = debugfs_create_x32("id", S_IWUSR, d, &data->debugfs.id);
A write-only file, really? I would think that reading from this file would be valuable as well, and it wouldn't cost much.
Quoted 11 lines
(quoted diff) +11
-0+ if (!f || IS_ERR(f)) { + err = -ENODEV; + goto cleanup; + } + + f = debugfs_create_file("gitm", S_IRUSR, d, data, + &atk_debugfs_gitm); + if (!f || IS_ERR(f)) { + err = -ENODEV; + goto cleanup; + }
Review line 807
Quoted 10 lines
(quoted diff) +10
-0+ + f = debugfs_create_file("ggrp", S_IRUSR, d, data, + &atk_debugfs_ggrp_fops); + if (!f || IS_ERR(f)) { + err = -ENODEV; + goto cleanup; + } + + data->debugfs.root = d; + data->debugfs.id = 0;
I suspect you have a race condition here. You initialize debugfs.id only now, however the sysfs files which use it are already created, and user-space may have accessed them already.
(In fact you are lucky that the data structure was kzalloc'd so debugfs.id is already 0. But still...)
Quoted 16 lines
(quoted diff) +12
-0+ + return 0; +cleanup: + debugfs_remove_recursive(d); + return err; +} + +static void atk_debugfs_cleanup(struct atk_data *data) +{ + debugfs_remove_recursive(data->debugfs.root); +} + static int atk_add_sensor(struct atk_data *data, union acpi_object *obj) { struct device *dev = &data->acpi_dev->dev;
Review line 1376
Quoted 4 lines
(quoted diff) +1
-0@@ -1180,6 +1373,8 @@ if (err) goto cleanup; + atk_debugfs_init(data);
This function can fail, but you do not check the returned value. Apparently your code will deal fine with this condition, but it is still somewhat confusing, and it also means atk_debugfs_init() could be simplified quite a bit: you could omit error handling almost entirely in this function, if I'm not mistaken.
Quoted 13 lines
(quoted diff) +3
-0+ device->driver_data = data; return 0; cleanup:@@ -1198,6 +1393,8 @@ device->driver_data = NULL; + atk_debugfs_cleanup(data); + atk_remove_files(data); atk_free_sensors(data); hwmon_device_unregister(data->hwmon_dev);
Signature · 6 lines
Jean Delvare _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Quoted 16 lines · Jean Delvare
Hi Luca,
Quoted 9 lines · Luca Tettamanti
Expose the raw GGRP/GITM interface via debugfs. The hwmon interface is reverse engineered and the driver tends to break on newer boards... Using this interface it's possible to poke directly at the ACPI methods without the need to recompile, reducing the guesswork and the round trips needed to support a new revisions of the interface.
Signed-off-by Luca Tettamanti <[email protected]>---
Does it make sense to have this upstream?If it makes your life easier, then yes, it certainly makes sense. The size increase is reasonable.
Quoted 6 lines
However, I would like the size increase to be kept minimal when debugfs is not enabled. At the moment, all the debugging code is still included in that case. Functions
atk_debugfs_ggrp_open,atk_debugfs_gitm_get,atk_debugfs_ggrp_read,atk_debugfs_gitm_openandatk_debugfs_ggrp_releaseare always included even when they will never be called.
Ok, I can wrap them up inside CONFIG_DEBUGFS.
Quoted 18 lines
Review (note: I don't know anything about debugfs):
Quoted 17 lines
(quoted diff) +13
-0@@ -624,6 +630,193 @@ return err; } +static int atk_debugfs_gitm_get(void *p, u64 *val) +{ + struct atk_data *data = p; + union acpi_object *ret; + struct atk_acpi_ret_buffer *buf; + int err = 0; + + if (!data->read_handle) + return -ENODEV; + + if (!data->debugfs.id) + return -EINVAL; +
Quoted 8 lines
Review line 650
Quoted 5 lines
(quoted diff) +5
-0+ ret = atk_gitm(data, data->debugfs.id); + if (IS_ERR(ret)) + return PTR_ERR(ret); + + buf = (void *)ret->buffer.pointer;This cast looks wrong. If a cast is really needed, you should cast to the right pointer type.
Right.
Quoted 6 lines
Review line 654
Quoted 4 lines
(quoted diff) +4
-0+static int atk_debugfs_ggrp_open(struct inode *inode, struct file *file) +{ + struct atk_data *data = inode->i_private; + union acpi_object *ret = NULL;Initialization isn't needed.
Ok.
Quoted 5 lines
Review line 657
Quoted 3 lines
(quoted diff) +3
-0+ u8 cls; + char *buf = NULL; + size_t size = 512;Makes little sense to have a variable for this.
true :)
Quoted 14 lines
Quoted 14 lines
(quoted diff) +14
-0+ int err; + int i; + + if (!data->enumerate_handle) + return -ENODEV; + if (!data->debugfs.id) + return -EINVAL; + + cls = (data->debugfs.id & 0xff000000) >> 24; + ret = atk_ggrp(data, cls); + if (IS_ERR(ret)) + return PTR_ERR(ret); + + for (i = 0; i < ret->package.count; i++) {
Quoted 8 lines
Review line 677
Quoted 6 lines
(quoted diff) +6
-0+ union acpi_object *pack = &ret->package.elements[i]; + union acpi_object *id; + + if (pack->type != ACPI_TYPE_PACKAGE) + continue; + id = &pack->package.elements[0];Are packages guaranteed to always contain at least one element?
Good question. An empty package wouldn't make any sense in this context, but it's better to check it anyway.
Quoted 18 lines
Quoted 18 lines
(quoted diff) +18
-0+ if (id->integer.value = data->debugfs.id) { + /* Print the package */ + buf = kzalloc(size, GFP_KERNEL); + if (!buf) { + ACPI_FREE(ret); + err = -ENOMEM; + goto outerr; + } + atk_pack_print(buf, size, pack); + break; + } + } + ACPI_FREE(ret); + + if (!buf) { + err = -EINVAL; + goto outerr; + }
Quoted 8 lines
Review line 700
Quoted 5 lines
(quoted diff) +5
-0+ file->private_data = buf; + + return nonseekable_open(inode, file); +outerr: + return err;It makes little sense to have a common error path if you don't do anything in it.
Ok.
Quoted 11 lines
Review line 708
Quoted 8 lines
(quoted diff) +8
-0+static ssize_t atk_debugfs_ggrp_read(struct file *file, char __user *buf, + size_t count, loff_t *pos) +{ + char *str = file->private_data; + size_t len; + + if (!str) + return -ENODEV;This can't happen. If
file->private_datais NULL, opening the file has failed.
I thought so :) It's the first time I fiddle the _fops interface.
Quoted 7 lines
Quoted 7 lines
(quoted diff) +7
-0+ + len = strlen(str); + + return simple_read_from_buffer(buf, count, pos, str, len); +} + +static int atk_debugfs_ggrp_release(struct inode *inode, struct file *file)
Quoted 8 lines
Review line 721
Quoted 6 lines
(quoted diff) +6
-0+{ + kfree(file->private_data); + return 0; +} + +static struct file_operations atk_debugfs_ggrp_fops = {Unless the API prevents it, this could be marked const.
Sure.
Quoted 13 lines
Quoted 13 lines
(quoted diff) +13
-0+ .read = atk_debugfs_ggrp_read, + .open = atk_debugfs_ggrp_open, + .release = atk_debugfs_ggrp_release, +}; + +static int atk_debugfs_init(struct atk_data *data) +{ + struct dentry *d; + struct dentry *f; + int err; + + d = debugfs_create_dir("asus_atk0110", NULL); + if (!d)
Quoted 8 lines
Review line 739
Quoted 5 lines
(quoted diff) +5
-0+ return -ENODEV; + if (IS_ERR(d)) + return PTR_ERR(d); + + f = debugfs_create_x32("id", S_IWUSR, d, &data->debugfs.id);A write-only file, really? I would think that reading from this file would be valuable as well, and it wouldn't cost much.
The idea is that the user writes the ID in this file and read the data from the others; the value in this file does not change; but as you said it costs nothing to make it rw.
Quoted 26 lines
Quoted 11 lines
(quoted diff) +11
-0+ if (!f || IS_ERR(f)) { + err = -ENODEV; + goto cleanup; + } + + f = debugfs_create_file("gitm", S_IRUSR, d, data, + &atk_debugfs_gitm); + if (!f || IS_ERR(f)) { + err = -ENODEV; + goto cleanup; + }Review line 760
Quoted 10 lines
(quoted diff) +10
-0+ + f = debugfs_create_file("ggrp", S_IRUSR, d, data, + &atk_debugfs_ggrp_fops); + if (!f || IS_ERR(f)) { + err = -ENODEV; + goto cleanup; + } + + data->debugfs.root = d; + data->debugfs.id = 0;I suspect you have a race condition here. You initialize debugfs.id only now, however the sysfs files which use it are already created, and user-space may have accessed them already.
Quoted 2 lines
(In fact you are lucky that the data structure was kzalloc'd so debugfs.id is already 0. But still...)
Yes, right. Will fix.
Quoted 19 lines
Quoted 19 lines
(quoted diff) +12
-0+ + return 0; +cleanup: + debugfs_remove_recursive(d); + return err; +} + +static void atk_debugfs_cleanup(struct atk_data *data) +{ + debugfs_remove_recursive(data->debugfs.root); +} + static int atk_add_sensor(struct atk_data *data, union acpi_object *obj) { struct device *dev = &data->acpi_dev->dev;@@ -1180,6 +1373,8 @@ if (err) goto cleanup;
Quoted 7 lines
Review line 1376
Quoted 1 line
(quoted diff) +1
-0+ atk_debugfs_init(data);This function can fail, but you do not check the returned value. Apparently your code will deal fine with this condition, but it is still somewhat confusing, and it also means
atk_debugfs_init()could be simplified quite a bit: you could omit error handling almost entirely in this function, if I'm not mistaken.
atk_debugfs_init has to cleanup in case of errors so it doesn't leave a half initialized debugfs directory; the function can also fail if debugfs is not enabled. Anyway the debugfs interface is not vital for the driver (i.e. it does work fine without it), so at this point I don't care if debugfs is initialized or not.
I will change the return value to void.
Thanks for the review Jean, I will send you an updated version of the patch.
Luca
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors