From c277761d1515cb514276bc0416a4c5b03a579f9f Mon Sep 17 00:00:00 2001 From: "Mark M. Hoffman" Date: Sun, 18 Sep 2005 20:37:42 +0000 Subject: [PATCH] (mmh) This patch replaces the I2C bus enumeration (through sysfs) in lib/proc.c with a new function in lib/sysfs (using libsysfs). The patch also updates the init code accordingly. git-svn-id: http://lm-sensors.org/svn/lm-sensors/trunk@3094 7894878c-1315-0410-8ee3-d5d059ff63e0 --- lib/init.c | 10 +++++--- lib/proc.c | 67 ++++------------------------------------------------- lib/sysfs.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++ lib/sysfs.h | 2 ++ 4 files changed, 74 insertions(+), 65 deletions(-) diff --git a/lib/init.c b/lib/init.c index 0bcd0d8e..170dbde3 100644 --- a/lib/init.c +++ b/lib/init.c @@ -41,11 +41,15 @@ int sensors_init(FILE *input) { int res; sensors_cleanup(); - sensors_init_sysfs(); if ((res = sensors_read_proc_chips())) return res; - if ((res = sensors_read_proc_bus())) - return res; + if (sensors_init_sysfs()) { + if ((res = sensors_read_sysfs_bus())) + return res; + } else { + if ((res = sensors_read_proc_bus())) + return res; + } sensors_yyin = input; if ((res = sensors_yyparse())) return -SENSORS_ERR_PARSE; diff --git a/lib/proc.c b/lib/proc.c index 5adaa461..8315c02e 100644 --- a/lib/proc.c +++ b/lib/proc.c @@ -214,68 +214,11 @@ proc: int sensors_read_proc_bus(void) { - struct dirent *de; - DIR *dir; - FILE *f; - char line[255]; - char *border; - sensors_bus entry; - int lineno; - char sysfs[NAME_MAX], n[NAME_MAX]; - char dirname[NAME_MAX]; - - if (sensors_found_sysfs) { - strncpy(sysfs, sensors_sysfs_mount, sizeof(sysfs) - 1); - sysfs[sizeof(sysfs) - 1] = 0; - strncat(sysfs, "/class/i2c-adapter", - sizeof(sysfs) - strlen(sysfs) - 1); - - /* Then read from it */ - dir = opendir(sysfs); - if (! dir) - goto proc; - - while ((de = readdir(dir)) != NULL) { - if (!strcmp(de->d_name, ".")) - continue; - if (!strcmp(de->d_name, "..")) - continue; - - strcpy(n, sysfs); - strcat(n, "/"); - strcat(n, de->d_name); - strcpy(dirname, n); - strcat(n, "/device/name"); - - if ((f = fopen(n, "r")) != NULL) { - char x[120]; - fgets(x, 120, f); - fclose(f); - if((border = index(x, '\n')) != NULL) - *border = 0; - entry.adapter=strdup(x); - if(!strncmp(x, "ISA ", 4)) { - entry.number = SENSORS_CHIP_NAME_BUS_ISA; - entry.algorithm = strdup("ISA bus algorithm"); - } else if(!sscanf(de->d_name, "i2c-%d", &entry.number)) { - entry.number = SENSORS_CHIP_NAME_BUS_DUMMY; - entry.algorithm = strdup("Dummy bus algorithm"); - } else - entry.algorithm = strdup("Unavailable from sysfs"); - if (entry.algorithm == NULL) - goto FAT_ERROR_SYS; - sensors_add_proc_bus(&entry); - } - } - closedir(dir); - return 0; -FAT_ERROR_SYS: - sensors_fatal_error("sensors_read_proc_bus", "Allocating entry"); - closedir(dir); - return -SENSORS_ERR_PROC; - } - -proc: + FILE *f; + char line[255]; + char *border; + sensors_bus entry; + int lineno; f = fopen("/proc/bus/i2c","r"); if (!f) diff --git a/lib/sysfs.c b/lib/sysfs.c index 5960edce..1fd3a024 100644 --- a/lib/sysfs.c +++ b/lib/sysfs.c @@ -20,6 +20,10 @@ #include #include #include +#include "data.h" +#include "error.h" +#include "access.h" +#include "general.h" #include "sysfs.h" int sensors_found_sysfs = 0; @@ -34,3 +38,59 @@ int sensors_init_sysfs(void) return sensors_found_sysfs; } + +/* returns 0 if successful, !0 otherwise */ +int sensors_read_sysfs_bus(void) +{ + struct sysfs_class *cls; + struct dlist *clsdevs; + struct sysfs_class_device *clsdev; + sensors_bus entry; + int ret = 0; + + if (!(cls = sysfs_open_class("i2c-adapter"))) { + ret = -SENSORS_ERR_PROC; + goto exit0; + } + + if (!(clsdevs = sysfs_get_class_devices(cls))) { + ret = -SENSORS_ERR_PROC; + goto exit1; + } + + dlist_for_each_data(clsdevs, clsdev, struct sysfs_class_device) { + struct sysfs_device *dev; + struct sysfs_attribute *attr; + + if (!(dev = sysfs_get_classdev_device(clsdev))) + continue; + if (!(attr = sysfs_get_device_attr(dev, "name"))) + continue; + + entry.adapter = strdup(attr->value); + if (!entry.adapter) + sensors_fatal_error(__FUNCTION__, "out of memory"); + + if (!strncmp(entry.adapter, "ISA ", 4)) { + entry.number = SENSORS_CHIP_NAME_BUS_ISA; + entry.algorithm = strdup("ISA bus algorithm"); + } else if (sscanf(clsdev->name, "i2c-%d", &entry.number) != 1) { + entry.number = SENSORS_CHIP_NAME_BUS_DUMMY; + entry.algorithm = strdup("Dummy bus algorithm"); + } else + entry.algorithm = strdup("Unavailable from sysfs"); + + if (!entry.algorithm) + sensors_fatal_error(__FUNCTION__, "out of memory"); + + sensors_add_proc_bus(&entry); + } + +exit1: + /* this frees *cls _and_ *clsdevs */ + sysfs_close_class(cls); + +exit0: + return ret; +} + diff --git a/lib/sysfs.h b/lib/sysfs.h index 933232e2..711ad354 100644 --- a/lib/sysfs.h +++ b/lib/sysfs.h @@ -26,4 +26,6 @@ extern char sensors_sysfs_mount[]; extern int sensors_init_sysfs(void); +extern int sensors_read_sysfs_bus(void); + #endif /* !SENSORS_LIB_SYSFS_H */