2
0
mirror of https://github.com/lm-sensors/lm-sensors synced 2025-09-01 14:55:27 +00:00

Support more bus types (part 1 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 first part introduces sensors_bus_id, and updates
sensors_chip_name to use it.


git-svn-id: http://lm-sensors.org/svn/lm-sensors/branches/lm-sensors-3.0.0@4686 7894878c-1315-0410-8ee3-d5d059ff63e0
This commit is contained in:
Jean Delvare
2007-08-19 15:03:50 +00:00
parent af2880326f
commit 7ee15371df
6 changed files with 92 additions and 67 deletions

View File

@@ -1,6 +1,7 @@
/* /*
access.c - Part of libsensors, a Linux library for reading sensor data. access.c - Part of libsensors, a Linux library for reading sensor data.
Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
Copyright (C) 2007 Jean Delvare <khali@linux-fr.org>
This program is free software; you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@@ -41,22 +42,15 @@ int sensors_match_chip(const sensors_chip_name *chip1,
strcasecmp(chip1->prefix, chip2->prefix)) strcasecmp(chip1->prefix, chip2->prefix))
return 0; return 0;
if ((chip1->bus != SENSORS_CHIP_NAME_BUS_ANY) && if ((chip1->bus.type != SENSORS_BUS_TYPE_ANY) &&
(chip2->bus != SENSORS_CHIP_NAME_BUS_ANY) && (chip2->bus.type != SENSORS_BUS_TYPE_ANY) &&
(chip1->bus != chip2->bus)) { (chip1->bus.type != chip2->bus.type))
return 0;
if ((chip1->bus == SENSORS_CHIP_NAME_BUS_ISA) || if ((chip1->bus.nr != SENSORS_BUS_NR_ANY) &&
(chip2->bus == SENSORS_CHIP_NAME_BUS_ISA)) (chip2->bus.nr != SENSORS_BUS_NR_ANY) &&
return 0; (chip1->bus.nr != chip2->bus.nr))
return 0;
if ((chip1->bus == SENSORS_CHIP_NAME_BUS_PCI) ||
(chip2->bus == SENSORS_CHIP_NAME_BUS_PCI))
return 0;
if ((chip1->bus != SENSORS_CHIP_NAME_BUS_ANY_I2C) &&
(chip2->bus != SENSORS_CHIP_NAME_BUS_ANY_I2C))
return 0;
}
if ((chip1->addr != chip2->addr) && if ((chip1->addr != chip2->addr) &&
(chip1->addr != SENSORS_CHIP_NAME_ADDR_ANY) && (chip1->addr != SENSORS_CHIP_NAME_ADDR_ANY) &&
@@ -136,8 +130,8 @@ sensors_lookup_feature_name(const sensors_chip_name *chip, const char *feature)
int sensors_chip_name_has_wildcards(const sensors_chip_name *chip) int sensors_chip_name_has_wildcards(const sensors_chip_name *chip)
{ {
if ((chip->prefix == SENSORS_CHIP_NAME_PREFIX_ANY) || if ((chip->prefix == SENSORS_CHIP_NAME_PREFIX_ANY) ||
(chip->bus == SENSORS_CHIP_NAME_BUS_ANY) || (chip->bus.type == SENSORS_BUS_TYPE_ANY) ||
(chip->bus == SENSORS_CHIP_NAME_BUS_ANY_I2C) || (chip->bus.nr == SENSORS_BUS_NR_ANY) ||
(chip->addr == SENSORS_CHIP_NAME_ADDR_ANY)) (chip->addr == SENSORS_CHIP_NAME_ADDR_ANY))
return 1; return 1;
else else
@@ -318,16 +312,21 @@ const sensors_chip_name *sensors_get_detected_chips(int *nr)
return res; return res;
} }
const char *sensors_get_adapter_name(int bus_nr) const char *sensors_get_adapter_name(const sensors_bus_id *bus)
{ {
int i; int i;
if (bus_nr == SENSORS_CHIP_NAME_BUS_ISA) /* bus types with a single instance */
switch (bus->type) {
case SENSORS_BUS_TYPE_ISA:
return "ISA adapter"; return "ISA adapter";
if (bus_nr == SENSORS_CHIP_NAME_BUS_PCI) case SENSORS_BUS_TYPE_PCI:
return "PCI adapter"; return "PCI adapter";
}
/* bus types with several instances */
for (i = 0; i < sensors_proc_bus_count; i++) for (i = 0; i < sensors_proc_bus_count; i++)
if (sensors_proc_bus[i].number == bus_nr) if (sensors_proc_bus[i].number == bus->nr)
return sensors_proc_bus[i].adapter; return sensors_proc_bus[i].adapter;
return NULL; return NULL;
} }

View File

@@ -92,7 +92,8 @@ int sensors_parse_chip_name(const char *name, sensors_chip_name *res)
/* Then we have either a sole "*" (all chips with this name) or a bus /* Then we have either a sole "*" (all chips with this name) or a bus
type and an address. */ type and an address. */
if (!strcmp(name, "*")) { if (!strcmp(name, "*")) {
res->bus = SENSORS_CHIP_NAME_BUS_ANY; res->bus.type = SENSORS_BUS_TYPE_ANY;
res->bus.nr = SENSORS_BUS_NR_ANY;
res->addr = SENSORS_CHIP_NAME_ADDR_ANY; res->addr = SENSORS_CHIP_NAME_ADDR_ANY;
return 0; return 0;
} }
@@ -100,11 +101,11 @@ int sensors_parse_chip_name(const char *name, sensors_chip_name *res)
if (!(dash = strchr(name, '-'))) if (!(dash = strchr(name, '-')))
goto ERROR; goto ERROR;
if (!strncmp(name, "i2c", dash - name)) if (!strncmp(name, "i2c", dash - name))
res->bus = SENSORS_CHIP_NAME_BUS_ANY_I2C; res->bus.type = SENSORS_BUS_TYPE_I2C;
else if (!strncmp(name, "isa", dash - name)) else if (!strncmp(name, "isa", dash - name))
res->bus = SENSORS_CHIP_NAME_BUS_ISA; res->bus.type = SENSORS_BUS_TYPE_ISA;
else if (!strncmp(name, "pci", dash - name)) else if (!strncmp(name, "pci", dash - name))
res->bus = SENSORS_CHIP_NAME_BUS_PCI; res->bus.type = SENSORS_BUS_TYPE_PCI;
else else
goto ERROR; goto ERROR;
name = dash + 1; name = dash + 1;
@@ -112,17 +113,21 @@ int sensors_parse_chip_name(const char *name, sensors_chip_name *res)
/* Some bus types (i2c) have an additional bus number. For these, the /* Some bus types (i2c) have an additional bus number. For these, the
next part is either a "*" (any bus of that type) or a decimal next part is either a "*" (any bus of that type) or a decimal
number. */ number. */
switch (res->bus) { switch (res->bus.type) {
case SENSORS_CHIP_NAME_BUS_ANY_I2C: case SENSORS_BUS_TYPE_I2C:
if (!strncmp(name, "*-", 2)) { if (!strncmp(name, "*-", 2)) {
res->bus.nr = SENSORS_BUS_NR_ANY;
name += 2; name += 2;
break; break;
} }
res->bus = strtoul(name, &dash, 10); res->bus.nr = strtoul(name, &dash, 10);
if (*name == '\0' || *dash != '-' || res->bus < 0) if (*name == '\0' || *dash != '-' || res->bus.nr < 0)
goto ERROR; goto ERROR;
name = dash + 1; name = dash + 1;
break;
default:
res->bus.nr = SENSORS_BUS_NR_ANY;
} }
/* Last part is the chip address, or "*" for any address. */ /* Last part is the chip address, or "*" for any address. */
@@ -147,17 +152,19 @@ int sensors_snprintf_chip_name(char *str, size_t size,
if (sensors_chip_name_has_wildcards(chip)) if (sensors_chip_name_has_wildcards(chip))
return -SENSORS_ERR_WILDCARDS; return -SENSORS_ERR_WILDCARDS;
switch (chip->bus) { switch (chip->bus.type) {
case SENSORS_CHIP_NAME_BUS_ISA: case SENSORS_BUS_TYPE_ISA:
return snprintf(str, size, "%s-isa-%04x", chip->prefix, return snprintf(str, size, "%s-isa-%04x", chip->prefix,
chip->addr); chip->addr);
case SENSORS_CHIP_NAME_BUS_PCI: case SENSORS_BUS_TYPE_PCI:
return snprintf(str, size, "%s-pci-%04x", chip->prefix, return snprintf(str, size, "%s-pci-%04x", chip->prefix,
chip->addr); chip->addr);
default: case SENSORS_BUS_TYPE_I2C:
return snprintf(str, size, "%s-i2c-%d-%02x", chip->prefix, return snprintf(str, size, "%s-i2c-%hd-%02x", chip->prefix,
chip->bus, chip->addr); chip->bus.nr, chip->addr);
} }
return -SENSORS_ERR_CHIP_NAME;
} }
int sensors_parse_i2cbus_name(const char *name, int *res) int sensors_parse_i2cbus_name(const char *name, int *res)
@@ -178,12 +185,13 @@ int sensors_substitute_chip(sensors_chip_name *name, int lineno)
{ {
int i, j; int i, j;
for (i = 0; i < sensors_config_busses_count; i++) for (i = 0; i < sensors_config_busses_count; i++)
if (sensors_config_busses[i].number == name->bus) if (name->bus.type == SENSORS_BUS_TYPE_I2C &&
sensors_config_busses[i].number == name->bus.nr)
break; break;
if (i == sensors_config_busses_count) { if (i == sensors_config_busses_count) {
sensors_parse_error("Undeclared i2c bus referenced", lineno); sensors_parse_error("Undeclared i2c bus referenced", lineno);
name->bus = sensors_proc_bus_count; name->bus.nr = sensors_proc_bus_count;
return -SENSORS_ERR_BUS_NAME; return -SENSORS_ERR_BUS_NAME;
} }
@@ -191,14 +199,14 @@ int sensors_substitute_chip(sensors_chip_name *name, int lineno)
for (j = 0; j < sensors_proc_bus_count; j++) { for (j = 0; j < sensors_proc_bus_count; j++) {
if (!strcmp(sensors_config_busses[i].adapter, if (!strcmp(sensors_config_busses[i].adapter,
sensors_proc_bus[j].adapter)) { sensors_proc_bus[j].adapter)) {
name->bus = sensors_proc_bus[j].number; name->bus.nr = sensors_proc_bus[j].number;
return 0; return 0;
} }
} }
/* We did not find anything. sensors_proc_bus_count is not /* We did not find anything. sensors_proc_bus_count is not
a valid bus number, so it will never be matched. Good. */ a valid bus number, so it will never be matched. Good. */
name->bus = sensors_proc_bus_count; name->bus.nr = sensors_proc_bus_count;
return 0; return 0;
} }
@@ -211,14 +219,16 @@ int sensors_substitute_busses(void)
for (i = 0; i < sensors_config_chips_count; i++) { for (i = 0; i < sensors_config_chips_count; i++) {
lineno = sensors_config_chips[i].lineno; lineno = sensors_config_chips[i].lineno;
chips = &sensors_config_chips[i].chips; chips = &sensors_config_chips[i].chips;
for (j = 0; j < chips->fits_count; j++) for (j = 0; j < chips->fits_count; j++) {
if (chips->fits[j].bus != SENSORS_CHIP_NAME_BUS_ISA && /* We can only substitute if a specific bus number
chips->fits[j].bus != SENSORS_CHIP_NAME_BUS_PCI && is given. */
chips->fits[j].bus != SENSORS_CHIP_NAME_BUS_ANY && if (chips->fits[j].bus.nr == SENSORS_BUS_NR_ANY)
chips->fits[j].bus != SENSORS_CHIP_NAME_BUS_ANY_I2C) continue;
if ((err = sensors_substitute_chip(chips->fits+j,
lineno))) err = sensors_substitute_chip(&chips->fits[j], lineno);
res = err; if (err)
res = err;
}
} }
return res; return res;
} }

View File

@@ -1,6 +1,7 @@
/* /*
sensors.h - Part of libsensors, a Linux library for reading sensor data. sensors.h - Part of libsensors, a Linux library for reading sensor data.
Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
Copyright (C) 2007 Jean Delvare <khali@linux-fr.org>
This program is free software; you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@@ -26,22 +27,29 @@
/* Publicly accessible library functions */ /* Publicly accessible library functions */
#define SENSORS_CHIP_NAME_PREFIX_ANY NULL #define SENSORS_CHIP_NAME_PREFIX_ANY NULL
#define SENSORS_CHIP_NAME_BUS_ISA -1
#define SENSORS_CHIP_NAME_BUS_ANY -2
#define SENSORS_CHIP_NAME_BUS_ANY_I2C -3
#define SENSORS_CHIP_NAME_BUS_PCI -5
#define SENSORS_CHIP_NAME_ADDR_ANY -1 #define SENSORS_CHIP_NAME_ADDR_ANY -1
#define SENSORS_BUS_TYPE_ANY (-1)
#define SENSORS_BUS_TYPE_I2C 0
#define SENSORS_BUS_TYPE_ISA 1
#define SENSORS_BUS_TYPE_PCI 2
#define SENSORS_BUS_NR_ANY (-1)
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif /* __cplusplus */ #endif /* __cplusplus */
extern const char *libsensors_version; extern const char *libsensors_version;
typedef struct sensors_bus_id {
short type;
short nr;
} sensors_bus_id;
/* A chip name is encoded in this structure */ /* A chip name is encoded in this structure */
typedef struct sensors_chip_name { typedef struct sensors_chip_name {
char *prefix; char *prefix;
int bus; sensors_bus_id bus;
int addr; int addr;
char *path; char *path;
} sensors_chip_name; } sensors_chip_name;
@@ -70,10 +78,10 @@ int sensors_snprintf_chip_name(char *str, size_t size,
int sensors_match_chip(const sensors_chip_name *chip1, int sensors_match_chip(const sensors_chip_name *chip1,
const sensors_chip_name *chip2); const sensors_chip_name *chip2);
/* This function returns the adapter name of a bus number, /* This function returns the adapter name of a bus,
as used within the sensors_chip_name structure. If it could not be found, as used within the sensors_chip_name structure. If it could not be found,
it returns NULL */ it returns NULL */
const char *sensors_get_adapter_name(int bus_nr); const char *sensors_get_adapter_name(const sensors_bus_id *bus);
/* Look up the label which belongs to this chip. Note that chip should not /* Look up the label which belongs to this chip. Note that chip should not
contain wildcard values! *result is newly allocated (free it yourself). contain wildcard values! *result is newly allocated (free it yourself).

View File

@@ -226,14 +226,16 @@ static int sensors_read_one_sysfs_chip(struct sysfs_device *dev)
if (!entry.chip.path) if (!entry.chip.path)
sensors_fatal_error(__FUNCTION__, "out of memory"); sensors_fatal_error(__FUNCTION__, "out of memory");
if (sscanf(dev->name, "%d-%x", &entry.chip.bus, &entry.chip.addr) == 2) { if (sscanf(dev->name, "%hd-%x", &entry.chip.bus.nr, &entry.chip.addr) == 2) {
/* find out if legacy ISA or not */ /* find out if legacy ISA or not */
if (entry.chip.bus == 9191) if (entry.chip.bus.nr == 9191) {
entry.chip.bus = SENSORS_CHIP_NAME_BUS_ISA; entry.chip.bus.type = SENSORS_BUS_TYPE_ISA;
else { entry.chip.bus.nr = 0;
} else {
entry.chip.bus.type = SENSORS_BUS_TYPE_I2C;
snprintf(bus_path, sizeof(bus_path), snprintf(bus_path, sizeof(bus_path),
"%s/class/i2c-adapter/i2c-%d/device/name", "%s/class/i2c-adapter/i2c-%d/device/name",
sensors_sysfs_mount, entry.chip.bus); sensors_sysfs_mount, entry.chip.bus.nr);
if ((bus_attr = sysfs_open_attribute(bus_path))) { if ((bus_attr = sysfs_open_attribute(bus_path))) {
if (sysfs_read_attribute(bus_attr)) { if (sysfs_read_attribute(bus_attr)) {
@@ -242,19 +244,23 @@ static int sensors_read_one_sysfs_chip(struct sysfs_device *dev)
} }
if (bus_attr->value if (bus_attr->value
&& !strncmp(bus_attr->value, "ISA ", 4)) && !strncmp(bus_attr->value, "ISA ", 4)) {
entry.chip.bus = SENSORS_CHIP_NAME_BUS_ISA; entry.chip.bus.type = SENSORS_BUS_TYPE_ISA;
entry.chip.bus.nr = 0;
}
sysfs_close_attribute(bus_attr); sysfs_close_attribute(bus_attr);
} }
} }
} else if (sscanf(dev->name, "%*[a-z0-9_].%d", &entry.chip.addr) == 1) { } else if (sscanf(dev->name, "%*[a-z0-9_].%d", &entry.chip.addr) == 1) {
/* must be new ISA (platform driver) */ /* must be new ISA (platform driver) */
entry.chip.bus = SENSORS_CHIP_NAME_BUS_ISA; entry.chip.bus.type = SENSORS_BUS_TYPE_ISA;
entry.chip.bus.nr = 0;
} else if (sscanf(dev->name, "%x:%x:%x.%x", &domain, &bus, &slot, &fn) == 4) { } else if (sscanf(dev->name, "%x:%x:%x.%x", &domain, &bus, &slot, &fn) == 4) {
/* PCI */ /* PCI */
entry.chip.addr = (domain << 16) + (bus << 8) + (slot << 3) + fn; entry.chip.addr = (domain << 16) + (bus << 8) + (slot << 3) + fn;
entry.chip.bus = SENSORS_CHIP_NAME_BUS_PCI; entry.chip.bus.type = SENSORS_BUS_TYPE_PCI;
entry.chip.bus.nr = 0;
} else } else
goto exit_free; goto exit_free;

View File

@@ -288,7 +288,8 @@ parseChips
(int argc, char **argv) { (int argc, char **argv) {
if (optind == argc) { if (optind == argc) {
chipNames[0].prefix = SENSORS_CHIP_NAME_PREFIX_ANY; chipNames[0].prefix = SENSORS_CHIP_NAME_PREFIX_ANY;
chipNames[0].bus = SENSORS_CHIP_NAME_BUS_ANY; chipNames[0].bus.type = SENSORS_BUS_TYPE_ANY;
chipNames[0].bus.nr = SENSORS_BUS_NR_ANY;
chipNames[0].addr = SENSORS_CHIP_NAME_ADDR_ANY; chipNames[0].addr = SENSORS_CHIP_NAME_ADDR_ANY;
numChipNames = 1; numChipNames = 1;
} else { } else {

View File

@@ -209,7 +209,8 @@ int main (int argc, char *argv[])
if (optind == argc) { if (optind == argc) {
chips[0].prefix = SENSORS_CHIP_NAME_PREFIX_ANY; chips[0].prefix = SENSORS_CHIP_NAME_PREFIX_ANY;
chips[0].bus = SENSORS_CHIP_NAME_BUS_ANY; chips[0].bus.type = SENSORS_BUS_TYPE_ANY;
chips[0].bus.nr = SENSORS_BUS_NR_ANY;
chips[0].addr = SENSORS_CHIP_NAME_ADDR_ANY; chips[0].addr = SENSORS_CHIP_NAME_ADDR_ANY;
chips_count = 1; chips_count = 1;
} else } else
@@ -310,11 +311,11 @@ void do_a_print(const sensors_chip_name *name)
printf("%s\n",sprintf_chip_name(name)); printf("%s\n",sprintf_chip_name(name));
if (!hide_adapter) { if (!hide_adapter) {
const char *adap = sensors_get_adapter_name(name->bus); const char *adap = sensors_get_adapter_name(&name->bus);
if (adap) if (adap)
printf("Adapter: %s\n", adap); printf("Adapter: %s\n", adap);
else else
fprintf(stderr, "Can't get adapter name for bus %d\n", name->bus); fprintf(stderr, "Can't get adapter name\n");
} }
if (do_unknown) if (do_unknown)
print_unknown_chip(name); print_unknown_chip(name);