From a74addb5d48a367362fdcb9e8c3fad87f7f86b72 Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Sun, 19 Aug 2007 15:04:29 +0000 Subject: [PATCH] Support more bus types (part 2 of 2). Originally libsensors was very i2c-centric. Make it more neutral so that we can cleanly support additional bus types such as SPI or One-Wire. This second part updates sensors_bus to use sensors_bus_id. Thanks to Mark M. Hoffman for showing me how the configuration file parser could be modified to support that change. git-svn-id: http://lm-sensors.org/svn/lm-sensors/branches/lm-sensors-3.0.0@4687 7894878c-1315-0410-8ee3-d5d059ff63e0 --- lib/access.c | 3 ++- lib/conf-parse.y | 14 +++++++------- lib/data.c | 15 ++++++++------- lib/data.h | 8 ++++---- lib/sysfs.c | 5 +++-- 5 files changed, 24 insertions(+), 21 deletions(-) diff --git a/lib/access.c b/lib/access.c index 7b0a4b10..0e7ba06c 100644 --- a/lib/access.c +++ b/lib/access.c @@ -326,7 +326,8 @@ const char *sensors_get_adapter_name(const sensors_bus_id *bus) /* bus types with several instances */ for (i = 0; i < sensors_proc_bus_count; i++) - if (sensors_proc_bus[i].number == bus->nr) + if (sensors_proc_bus[i].bus.type == bus->type && + sensors_proc_bus[i].bus.nr == bus->nr) return sensors_proc_bus[i].adapter; return NULL; } diff --git a/lib/conf-parse.y b/lib/conf-parse.y index 88a1cf49..3df1f808 100644 --- a/lib/conf-parse.y +++ b/lib/conf-parse.y @@ -94,7 +94,7 @@ static sensors_chip *current_chip = NULL; void *nothing; sensors_chip_name_list chips; sensors_expr *expr; - int bus; + sensors_bus_id bus; sensors_chip_name chip; int line; } @@ -118,7 +118,7 @@ static sensors_chip *current_chip = NULL; %type chip_name_list %type expression -%type i2cbus_name +%type bus_id %type adapter_name %type function_name %type string @@ -141,10 +141,10 @@ line: bus_statement EOL | error EOL ; -bus_statement: BUS i2cbus_name adapter_name +bus_statement: BUS bus_id adapter_name { sensors_bus new_el; new_el.lineno = $1; - new_el.number = $2; + new_el.bus = $2; new_el.adapter = $3; bus_add_el(&new_el); } @@ -287,11 +287,11 @@ expression: FLOAT } ; -i2cbus_name: NAME - { int res = sensors_parse_i2cbus_name($1,&$$); +bus_id: NAME + { int res = sensors_parse_bus_id($1,&$$); free($1); if (res) { - sensors_yyerror("Parse error in i2c bus name"); + sensors_yyerror("Parse error in bus id"); YYERROR; } } diff --git a/lib/data.c b/lib/data.c index 9a2230eb..53996b2e 100644 --- a/lib/data.c +++ b/lib/data.c @@ -167,7 +167,7 @@ int sensors_snprintf_chip_name(char *str, size_t size, return -SENSORS_ERR_CHIP_NAME; } -int sensors_parse_i2cbus_name(const char *name, int *res) +int sensors_parse_bus_id(const char *name, sensors_bus_id *bus) { char *endptr; @@ -175,8 +175,9 @@ int sensors_parse_i2cbus_name(const char *name, int *res) return -SENSORS_ERR_BUS_NAME; } name += 4; - *res = strtoul(name, &endptr, 10); - if (*name == '\0' || *endptr != '\0' || *res < 0) + bus->type = SENSORS_BUS_TYPE_I2C; + bus->nr = strtoul(name, &endptr, 10); + if (*name == '\0' || *endptr != '\0' || bus->nr < 0) return -SENSORS_ERR_BUS_NAME; return 0; } @@ -185,12 +186,12 @@ int sensors_substitute_chip(sensors_chip_name *name, int lineno) { int i, j; for (i = 0; i < sensors_config_busses_count; i++) - if (name->bus.type == SENSORS_BUS_TYPE_I2C && - sensors_config_busses[i].number == name->bus.nr) + if (sensors_config_busses[i].bus.type == name->bus.type && + sensors_config_busses[i].bus.nr == name->bus.nr) break; if (i == sensors_config_busses_count) { - sensors_parse_error("Undeclared i2c bus referenced", lineno); + sensors_parse_error("Undeclared bus id referenced", lineno); name->bus.nr = sensors_proc_bus_count; return -SENSORS_ERR_BUS_NAME; } @@ -199,7 +200,7 @@ int sensors_substitute_chip(sensors_chip_name *name, int lineno) for (j = 0; j < sensors_proc_bus_count; j++) { if (!strcmp(sensors_config_busses[i].adapter, sensors_proc_bus[j].adapter)) { - name->bus.nr = sensors_proc_bus[j].number; + name->bus.nr = sensors_proc_bus[j].bus.nr; return 0; } } diff --git a/lib/data.h b/lib/data.h index 5bf2b0c9..1af232ac 100644 --- a/lib/data.h +++ b/lib/data.h @@ -110,10 +110,10 @@ typedef struct sensors_chip { int lineno; } sensors_chip; -/* Config file bus declaration: the i2c bus number, combined with adapter +/* Config file bus declaration: the bus type and number, combined with adapter name */ typedef struct sensors_bus { - int number; + sensors_bus_id bus; char *adapter; int lineno; } sensors_bus; @@ -175,8 +175,8 @@ extern int sensors_proc_bus_max; int sensors_substitute_busses(void); -/* Parse an i2c bus name into its components. Returns 0 on succes, a value from +/* Parse a bus id into its components. Returns 0 on succes, a value from error.h on failure. */ -int sensors_parse_i2cbus_name(const char *name, int *res); +int sensors_parse_bus_id(const char *name, sensors_bus_id *bus); #endif /* def LIB_SENSORS_DATA_H */ diff --git a/lib/sysfs.c b/lib/sysfs.c index ae67e9b6..e6544b73 100644 --- a/lib/sysfs.c +++ b/lib/sysfs.c @@ -380,9 +380,10 @@ int sensors_read_sysfs_bus(void) (attr = sysfs_get_device_attr(dev, "name")))) continue; - if (sscanf(clsdev->name, "i2c-%d", &entry.number) != 1 || - entry.number == 9191) /* legacy ISA */ + if (sscanf(clsdev->name, "i2c-%hd", &entry.bus.nr) != 1 || + entry.bus.nr == 9191) /* legacy ISA */ continue; + entry.bus.type = SENSORS_BUS_TYPE_I2C; /* NB: attr->value[attr->len-1] == '\n'; chop that off */ entry.adapter = strndup(attr->value, attr->len - 1);