summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOndřej Lysoněk <olysonek@redhat.com>2018-11-06 15:48:33 +0100
committerOndřej Lysoněk <olysonek@redhat.com>2018-11-23 11:33:05 +0100
commit0cb449735fada2f5e4754f6d2e0c7006c69cabaf (patch)
tree6ecfc958e657080b9d15064a27ef149f0ec0e3d1
parent874ab9548d159c205c0b68cdcb418fcd94a78638 (diff)
downloadlm-sensors-git-0cb449735fada2f5e4754f6d2e0c7006c69cabaf.tar.gz
libsensors: Refactor device classification into a separate function
Refactor device classification into a separate function in order to make the code more readable and easier to maintain. Signed-off-by: Ondřej Lysoněk <olysonek@redhat.com>
-rw-r--r--lib/sysfs.c158
1 files changed, 86 insertions, 72 deletions
diff --git a/lib/sysfs.c b/lib/sysfs.c
index 6a3c8fa8..7d8fdedd 100644
--- a/lib/sysfs.c
+++ b/lib/sysfs.c
@@ -615,15 +615,97 @@ int sensors_init_sysfs(void)
return 1;
}
+static int classify_device(const char *dev_name,
+ const char *subsys,
+ sensors_chip_features *entry)
+{
+ int domain, bus, slot, fn, vendor, product, id;
+ char bus_path[NAME_MAX];
+ char *bus_attr;
+ int ret = 1;
+
+ if ((!subsys || !strcmp(subsys, "i2c")) &&
+ sscanf(dev_name, "%hd-%x", &entry->chip.bus.nr,
+ &entry->chip.addr) == 2) {
+ /* find out if legacy ISA or not */
+ if (entry->chip.bus.nr == 9191) {
+ entry->chip.bus.type = SENSORS_BUS_TYPE_ISA;
+ entry->chip.bus.nr = 0;
+ } else {
+ entry->chip.bus.type = SENSORS_BUS_TYPE_I2C;
+ snprintf(bus_path, sizeof(bus_path),
+ "%s/class/i2c-adapter/i2c-%d/device",
+ sensors_sysfs_mount, entry->chip.bus.nr);
+
+ if ((bus_attr = sysfs_read_attr(bus_path, "name"))) {
+ if (!strncmp(bus_attr, "ISA ", 4)) {
+ entry->chip.bus.type = SENSORS_BUS_TYPE_ISA;
+ entry->chip.bus.nr = 0;
+ }
+
+ free(bus_attr);
+ }
+ }
+ } else
+ if ((!subsys || !strcmp(subsys, "spi")) &&
+ sscanf(dev_name, "spi%hd.%d", &entry->chip.bus.nr,
+ &entry->chip.addr) == 2) {
+ /* SPI */
+ entry->chip.bus.type = SENSORS_BUS_TYPE_SPI;
+ } else
+ if ((!subsys || !strcmp(subsys, "pci")) &&
+ sscanf(dev_name, "%x:%x:%x.%x", &domain, &bus, &slot, &fn) == 4) {
+ /* PCI */
+ entry->chip.addr = (domain << 16) + (bus << 8) + (slot << 3) + fn;
+ entry->chip.bus.type = SENSORS_BUS_TYPE_PCI;
+ entry->chip.bus.nr = 0;
+ } else
+ if ((!subsys || !strcmp(subsys, "platform") ||
+ !strcmp(subsys, "of_platform"))) {
+ /* must be new ISA (platform driver) */
+ if (sscanf(dev_name, "%*[a-z0-9_].%d", &entry->chip.addr) != 1)
+ entry->chip.addr = 0;
+ entry->chip.bus.type = SENSORS_BUS_TYPE_ISA;
+ entry->chip.bus.nr = 0;
+ } else if (subsys && !strcmp(subsys, "acpi")) {
+ entry->chip.bus.type = SENSORS_BUS_TYPE_ACPI;
+ /* For now we assume that acpi devices are unique */
+ entry->chip.bus.nr = 0;
+ entry->chip.addr = 0;
+ } else
+ if (subsys && !strcmp(subsys, "hid") &&
+ sscanf(dev_name, "%x:%x:%x.%x", &bus, &vendor, &product, &id) == 4) {
+ entry->chip.bus.type = SENSORS_BUS_TYPE_HID;
+ /* As of kernel 2.6.32, the hid device names don't look good */
+ entry->chip.bus.nr = bus;
+ entry->chip.addr = id;
+ } else
+ if (subsys && !strcmp(subsys, "mdio_bus")) {
+ if (sscanf(dev_name, "%*[^:]:%d", &entry->chip.addr) != 1)
+ entry->chip.addr = 0;
+ entry->chip.bus.type = SENSORS_BUS_TYPE_MDIO;
+ entry->chip.bus.nr = 0;
+ } else
+ if (subsys && !strcmp(subsys, "scsi") &&
+ sscanf(dev_name, "%d:%d:%d:%x", &domain, &bus, &slot, &fn) == 4) {
+ /* adapter(host), channel(bus), id(target), lun */
+ entry->chip.addr = (bus << 8) + (slot << 4) + fn;
+ entry->chip.bus.type = SENSORS_BUS_TYPE_SCSI;
+ entry->chip.bus.nr = domain;
+ } else {
+ /* Unknown device */
+ ret = 0;
+ }
+
+ return ret;
+}
+
/* returns: number of devices added (0 or 1) if successful, <0 otherwise */
static int sensors_read_one_sysfs_chip(const char *dev_path,
const char *dev_name,
const char *hwmon_path)
{
- int domain, bus, slot, fn, vendor, product, id;
int err = -SENSORS_ERR_KERNEL;
- char *bus_attr;
- char bus_path[NAME_MAX];
char linkpath[NAME_MAX];
char subsys_path[NAME_MAX], *subsys;
int sub_len;
@@ -666,75 +748,7 @@ static int sensors_read_one_sysfs_chip(const char *dev_path,
subsys = strrchr(subsys_path, '/') + 1;
}
- if ((!subsys || !strcmp(subsys, "i2c")) &&
- sscanf(dev_name, "%hd-%x", &entry.chip.bus.nr,
- &entry.chip.addr) == 2) {
- /* find out if legacy ISA or not */
- if (entry.chip.bus.nr == 9191) {
- entry.chip.bus.type = SENSORS_BUS_TYPE_ISA;
- entry.chip.bus.nr = 0;
- } else {
- entry.chip.bus.type = SENSORS_BUS_TYPE_I2C;
- snprintf(bus_path, sizeof(bus_path),
- "%s/class/i2c-adapter/i2c-%d/device",
- sensors_sysfs_mount, entry.chip.bus.nr);
-
- if ((bus_attr = sysfs_read_attr(bus_path, "name"))) {
- if (!strncmp(bus_attr, "ISA ", 4)) {
- entry.chip.bus.type = SENSORS_BUS_TYPE_ISA;
- entry.chip.bus.nr = 0;
- }
-
- free(bus_attr);
- }
- }
- } else
- if ((!subsys || !strcmp(subsys, "spi")) &&
- sscanf(dev_name, "spi%hd.%d", &entry.chip.bus.nr,
- &entry.chip.addr) == 2) {
- /* SPI */
- entry.chip.bus.type = SENSORS_BUS_TYPE_SPI;
- } else
- if ((!subsys || !strcmp(subsys, "pci")) &&
- sscanf(dev_name, "%x:%x:%x.%x", &domain, &bus, &slot, &fn) == 4) {
- /* PCI */
- entry.chip.addr = (domain << 16) + (bus << 8) + (slot << 3) + fn;
- entry.chip.bus.type = SENSORS_BUS_TYPE_PCI;
- entry.chip.bus.nr = 0;
- } else
- if ((!subsys || !strcmp(subsys, "platform") ||
- !strcmp(subsys, "of_platform"))) {
- /* must be new ISA (platform driver) */
- if (sscanf(dev_name, "%*[a-z0-9_].%d", &entry.chip.addr) != 1)
- entry.chip.addr = 0;
- entry.chip.bus.type = SENSORS_BUS_TYPE_ISA;
- entry.chip.bus.nr = 0;
- } else if (subsys && !strcmp(subsys, "acpi")) {
- entry.chip.bus.type = SENSORS_BUS_TYPE_ACPI;
- /* For now we assume that acpi devices are unique */
- entry.chip.bus.nr = 0;
- entry.chip.addr = 0;
- } else
- if (subsys && !strcmp(subsys, "hid") &&
- sscanf(dev_name, "%x:%x:%x.%x", &bus, &vendor, &product, &id) == 4) {
- entry.chip.bus.type = SENSORS_BUS_TYPE_HID;
- /* As of kernel 2.6.32, the hid device names don't look good */
- entry.chip.bus.nr = bus;
- entry.chip.addr = id;
- } else
- if (subsys && !strcmp(subsys, "mdio_bus")) {
- if (sscanf(dev_name, "%*[^:]:%d", &entry.chip.addr) != 1)
- entry.chip.addr = 0;
- entry.chip.bus.type = SENSORS_BUS_TYPE_MDIO;
- entry.chip.bus.nr = 0;
- } else
- if (subsys && !strcmp(subsys, "scsi") &&
- sscanf(dev_name, "%d:%d:%d:%x", &domain, &bus, &slot, &fn) == 4) {
- /* adapter(host), channel(bus), id(target), lun */
- entry.chip.addr = (bus << 8) + (slot << 4) + fn;
- entry.chip.bus.type = SENSORS_BUS_TYPE_SCSI;
- entry.chip.bus.nr = domain;
- } else {
+ if (!classify_device(dev_name, subsys, &entry)) {
/* Ignore unknown device */
err = 0;
goto exit_free;