Changeset 4686

Show
Ignore:
Timestamp:
08/19/07 17:03:50 (1 year ago)
Author:
khali
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • lm-sensors/branches/lm-sensors-3.0.0/lib/access.c

    r4676 r4686  
    22    access.c - Part of libsensors, a Linux library for reading sensor data. 
    33    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl> 
     4    Copyright (C) 2007        Jean Delvare <khali@linux-fr.org> 
    45 
    56    This program is free software; you can redistribute it and/or modify 
     
    4243                return 0; 
    4344 
    44         if ((chip1->bus != SENSORS_CHIP_NAME_BUS_ANY) && 
    45             (chip2->bus != SENSORS_CHIP_NAME_BUS_ANY) && 
    46             (chip1->bus != chip2->bus)) { 
    47  
    48                 if ((chip1->bus == SENSORS_CHIP_NAME_BUS_ISA) || 
    49                     (chip2->bus == SENSORS_CHIP_NAME_BUS_ISA)) 
    50                         return 0; 
    51  
    52                 if ((chip1->bus == SENSORS_CHIP_NAME_BUS_PCI) || 
    53                     (chip2->bus == SENSORS_CHIP_NAME_BUS_PCI)) 
    54                         return 0; 
    55  
    56                 if ((chip1->bus != SENSORS_CHIP_NAME_BUS_ANY_I2C) && 
    57                     (chip2->bus != SENSORS_CHIP_NAME_BUS_ANY_I2C)) 
    58                         return 0; 
    59         } 
     45        if ((chip1->bus.type != SENSORS_BUS_TYPE_ANY) && 
     46            (chip2->bus.type != SENSORS_BUS_TYPE_ANY) && 
     47            (chip1->bus.type != chip2->bus.type)) 
     48                return 0; 
     49 
     50        if ((chip1->bus.nr != SENSORS_BUS_NR_ANY) && 
     51            (chip2->bus.nr != SENSORS_BUS_NR_ANY) && 
     52            (chip1->bus.nr != chip2->bus.nr)) 
     53                return 0; 
    6054 
    6155        if ((chip1->addr != chip2->addr) && 
     
    137131{ 
    138132        if ((chip->prefix == SENSORS_CHIP_NAME_PREFIX_ANY) || 
    139             (chip->bus == SENSORS_CHIP_NAME_BUS_ANY) || 
    140             (chip->bus == SENSORS_CHIP_NAME_BUS_ANY_I2C) || 
     133            (chip->bus.type == SENSORS_BUS_TYPE_ANY) || 
     134            (chip->bus.nr == SENSORS_BUS_NR_ANY) || 
    141135            (chip->addr == SENSORS_CHIP_NAME_ADDR_ANY)) 
    142136                return 1; 
     
    319313} 
    320314 
    321 const char *sensors_get_adapter_name(int bus_nr
     315const char *sensors_get_adapter_name(const sensors_bus_id *bus
    322316{ 
    323317        int i; 
    324318 
    325         if (bus_nr == SENSORS_CHIP_NAME_BUS_ISA) 
     319        /* bus types with a single instance */ 
     320        switch (bus->type) { 
     321        case SENSORS_BUS_TYPE_ISA: 
    326322                return "ISA adapter"; 
    327         if (bus_nr == SENSORS_CHIP_NAME_BUS_PCI) 
     323        case SENSORS_BUS_TYPE_PCI: 
    328324                return "PCI adapter"; 
     325        } 
     326 
     327        /* bus types with several instances */ 
    329328        for (i = 0; i < sensors_proc_bus_count; i++) 
    330                 if (sensors_proc_bus[i].number == bus_nr) 
     329                if (sensors_proc_bus[i].number == bus->nr) 
    331330                        return sensors_proc_bus[i].adapter; 
    332331        return NULL; 
  • lm-sensors/branches/lm-sensors-3.0.0/lib/data.c

    r4678 r4686  
    9393           type and an address. */ 
    9494        if (!strcmp(name, "*")) { 
    95                 res->bus = SENSORS_CHIP_NAME_BUS_ANY; 
     95                res->bus.type = SENSORS_BUS_TYPE_ANY; 
     96                res->bus.nr = SENSORS_BUS_NR_ANY; 
    9697                res->addr = SENSORS_CHIP_NAME_ADDR_ANY; 
    9798                return 0; 
     
    101102                goto ERROR; 
    102103        if (!strncmp(name, "i2c", dash - name)) 
    103                 res->bus = SENSORS_CHIP_NAME_BUS_ANY_I2C; 
     104                res->bus.type = SENSORS_BUS_TYPE_I2C; 
    104105        else if (!strncmp(name, "isa", dash - name)) 
    105                 res->bus = SENSORS_CHIP_NAME_BUS_ISA; 
     106                res->bus.type = SENSORS_BUS_TYPE_ISA; 
    106107        else if (!strncmp(name, "pci", dash - name)) 
    107                 res->bus = SENSORS_CHIP_NAME_BUS_PCI; 
     108                res->bus.type = SENSORS_BUS_TYPE_PCI; 
    108109        else 
    109110                goto ERROR; 
     
    113114           next part is either a "*" (any bus of that type) or a decimal 
    114115           number. */ 
    115         switch (res->bus) { 
    116         case SENSORS_CHIP_NAME_BUS_ANY_I2C: 
     116        switch (res->bus.type) { 
     117        case SENSORS_BUS_TYPE_I2C: 
    117118                if (!strncmp(name, "*-", 2)) { 
     119                        res->bus.nr = SENSORS_BUS_NR_ANY; 
    118120                        name += 2; 
    119121                        break; 
    120122                } 
    121123 
    122                 res->bus = strtoul(name, &dash, 10); 
    123                 if (*name == '\0' || *dash != '-' || res->bus < 0) 
     124                res->bus.nr = strtoul(name, &dash, 10); 
     125                if (*name == '\0' || *dash != '-' || res->bus.nr < 0) 
    124126                        goto ERROR; 
    125127                name = dash + 1; 
     128                break; 
     129        default: 
     130                res->bus.nr = SENSORS_BUS_NR_ANY; 
    126131        } 
    127132 
     
    148153                return -SENSORS_ERR_WILDCARDS; 
    149154 
    150         switch (chip->bus) { 
    151         case SENSORS_CHIP_NAME_BUS_ISA: 
     155        switch (chip->bus.type) { 
     156        case SENSORS_BUS_TYPE_ISA: 
    152157                return snprintf(str, size, "%s-isa-%04x", chip->prefix, 
    153158                                chip->addr); 
    154         case SENSORS_CHIP_NAME_BUS_PCI: 
     159        case SENSORS_BUS_TYPE_PCI: 
    155160                return snprintf(str, size, "%s-pci-%04x", chip->prefix, 
    156161                                chip->addr); 
    157         default: 
    158                 return snprintf(str, size, "%s-i2c-%d-%02x", chip->prefix, 
    159                                 chip->bus, chip->addr); 
    160         } 
     162        case SENSORS_BUS_TYPE_I2C: 
     163                return snprintf(str, size, "%s-i2c-%hd-%02x", chip->prefix, 
     164                                chip->bus.nr, chip->addr); 
     165        } 
     166 
     167        return -SENSORS_ERR_CHIP_NAME; 
    161168} 
    162169 
     
    179186        int i, j; 
    180187        for (i = 0; i < sensors_config_busses_count; i++) 
    181                 if (sensors_config_busses[i].number == name->bus) 
     188                if (name->bus.type == SENSORS_BUS_TYPE_I2C && 
     189                    sensors_config_busses[i].number == name->bus.nr) 
    182190                        break; 
    183191 
    184192        if (i == sensors_config_busses_count) { 
    185193                sensors_parse_error("Undeclared i2c bus referenced", lineno); 
    186                 name->bus = sensors_proc_bus_count; 
     194                name->bus.nr = sensors_proc_bus_count; 
    187195                return -SENSORS_ERR_BUS_NAME; 
    188196        } 
     
    192200                if (!strcmp(sensors_config_busses[i].adapter, 
    193201                            sensors_proc_bus[j].adapter)) { 
    194                         name->bus = sensors_proc_bus[j].number; 
     202                        name->bus.nr = sensors_proc_bus[j].number; 
    195203                        return 0; 
    196204                } 
     
    199207        /* We did not find anything. sensors_proc_bus_count is not 
    200208           a valid bus number, so it will never be matched. Good. */ 
    201         name->bus = sensors_proc_bus_count; 
     209        name->bus.nr = sensors_proc_bus_count; 
    202210        return 0; 
    203211} 
     
    212220                lineno = sensors_config_chips[i].lineno; 
    213221                chips = &sensors_config_chips[i].chips; 
    214                 for (j = 0; j < chips->fits_count; j++) 
    215                         if (chips->fits[j].bus != SENSORS_CHIP_NAME_BUS_ISA && 
    216                             chips->fits[j].bus != SENSORS_CHIP_NAME_BUS_PCI && 
    217                             chips->fits[j].bus != SENSORS_CHIP_NAME_BUS_ANY && 
    218                             chips->fits[j].bus != SENSORS_CHIP_NAME_BUS_ANY_I2C) 
    219                                 if ((err = sensors_substitute_chip(chips->fits+j, 
    220                                                                    lineno))) 
    221                                         res = err; 
     222                for (j = 0; j < chips->fits_count; j++) { 
     223                        /* We can only substitute if a specific bus number 
     224                           is given. */ 
     225                        if (chips->fits[j].bus.nr == SENSORS_BUS_NR_ANY) 
     226                                continue; 
     227 
     228                        err = sensors_substitute_chip(&chips->fits[j], lineno); 
     229                        if (err) 
     230                                res = err; 
     231                } 
    222232        } 
    223233        return res; 
  • lm-sensors/branches/lm-sensors-3.0.0/lib/sensors.h

    r4684 r4686  
    22    sensors.h - Part of libsensors, a Linux library for reading sensor data. 
    33    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl> 
     4    Copyright (C) 2007        Jean Delvare <khali@linux-fr.org> 
    45 
    56    This program is free software; you can redistribute it and/or modify 
     
    2728 
    2829#define SENSORS_CHIP_NAME_PREFIX_ANY NULL 
    29 #define SENSORS_CHIP_NAME_BUS_ISA -1 
    30 #define SENSORS_CHIP_NAME_BUS_ANY -2 
    31 #define SENSORS_CHIP_NAME_BUS_ANY_I2C -3 
    32 #define SENSORS_CHIP_NAME_BUS_PCI -5 
    3330#define SENSORS_CHIP_NAME_ADDR_ANY -1 
     31 
     32#define SENSORS_BUS_TYPE_ANY    (-1) 
     33#define SENSORS_BUS_TYPE_I2C    0 
     34#define SENSORS_BUS_TYPE_ISA    1 
     35#define SENSORS_BUS_TYPE_PCI    2 
     36#define SENSORS_BUS_NR_ANY      (-1) 
    3437 
    3538#ifdef __cplusplus 
     
    3942extern const char *libsensors_version; 
    4043 
     44typedef struct sensors_bus_id { 
     45        short type; 
     46        short nr; 
     47} sensors_bus_id; 
     48 
    4149/* A chip name is encoded in this structure */ 
    4250typedef struct sensors_chip_name { 
    4351        char *prefix; 
    44         int bus; 
     52        sensors_bus_id bus; 
    4553        int addr; 
    4654        char *path; 
     
    7179                       const sensors_chip_name *chip2); 
    7280 
    73 /* This function returns the adapter name of a bus number
     81/* This function returns the adapter name of a bus
    7482   as used within the sensors_chip_name structure. If it could not be found, 
    7583   it returns NULL */ 
    76 const char *sensors_get_adapter_name(int bus_nr); 
     84const char *sensors_get_adapter_name(const sensors_bus_id *bus); 
    7785 
    7886/* Look up the label which belongs to this chip. Note that chip should not 
  • lm-sensors/branches/lm-sensors-3.0.0/lib/sysfs.c

    r4674 r4686  
    227227                sensors_fatal_error(__FUNCTION__, "out of memory"); 
    228228 
    229         if (sscanf(dev->name, "%d-%x", &entry.chip.bus, &entry.chip.addr) == 2) { 
     229        if (sscanf(dev->name, "%hd-%x", &entry.chip.bus.nr, &entry.chip.addr) == 2) { 
    230230                /* find out if legacy ISA or not */ 
    231                 if (entry.chip.bus == 9191) 
    232                         entry.chip.bus = SENSORS_CHIP_NAME_BUS_ISA; 
    233                 else { 
     231                if (entry.chip.bus.nr == 9191) { 
     232                        entry.chip.bus.type = SENSORS_BUS_TYPE_ISA; 
     233                        entry.chip.bus.nr = 0; 
     234                } else { 
     235                        entry.chip.bus.type = SENSORS_BUS_TYPE_I2C; 
    234236                        snprintf(bus_path, sizeof(bus_path), 
    235237                                "%s/class/i2c-adapter/i2c-%d/device/name", 
    236                                 sensors_sysfs_mount, entry.chip.bus); 
     238                                sensors_sysfs_mount, entry.chip.bus.nr); 
    237239 
    238240                        if ((bus_attr = sysfs_open_attribute(bus_path))) { 
     
    243245 
    244246                                if (bus_attr->value 
    245                                  && !strncmp(bus_attr->value, "ISA ", 4)) 
    246                                         entry.chip.bus = SENSORS_CHIP_NAME_BUS_ISA; 
     247                                 && !strncmp(bus_attr->value, "ISA ", 4)) { 
     248                                        entry.chip.bus.type = SENSORS_BUS_TYPE_ISA; 
     249                                        entry.chip.bus.nr = 0; 
     250                                } 
    247251 
    248252                                sysfs_close_attribute(bus_attr); 
     
    251255        } else if (sscanf(dev->name, "%*[a-z0-9_].%d", &entry.chip.addr) == 1) { 
    252256                /* must be new ISA (platform driver) */ 
    253                 entry.chip.bus = SENSORS_CHIP_NAME_BUS_ISA; 
     257                entry.chip.bus.type = SENSORS_BUS_TYPE_ISA; 
     258                entry.chip.bus.nr = 0; 
    254259        } else if (sscanf(dev->name, "%x:%x:%x.%x", &domain, &bus, &slot, &fn) == 4) { 
    255260                /* PCI */ 
    256261                entry.chip.addr = (domain << 16) + (bus << 8) + (slot << 3) + fn; 
    257                 entry.chip.bus = SENSORS_CHIP_NAME_BUS_PCI; 
     262                entry.chip.bus.type = SENSORS_BUS_TYPE_PCI; 
     263                entry.chip.bus.nr = 0; 
    258264        } else 
    259265                goto exit_free; 
  • lm-sensors/branches/lm-sensors-3.0.0/prog/sensord/args.c

    r2503 r4686  
    289289  if (optind == argc) { 
    290290    chipNames[0].prefix = SENSORS_CHIP_NAME_PREFIX_ANY; 
    291     chipNames[0].bus = SENSORS_CHIP_NAME_BUS_ANY; 
     291    chipNames[0].bus.type = SENSORS_BUS_TYPE_ANY; 
     292    chipNames[0].bus.nr = SENSORS_BUS_NR_ANY; 
    292293    chipNames[0].addr = SENSORS_CHIP_NAME_ADDR_ANY; 
    293294    numChipNames = 1; 
  • lm-sensors/branches/lm-sensors-3.0.0/prog/sensors/main.c

    r4681 r4686  
    210210  if (optind == argc) { 
    211211    chips[0].prefix = SENSORS_CHIP_NAME_PREFIX_ANY; 
    212     chips[0].bus = SENSORS_CHIP_NAME_BUS_ANY; 
     212    chips[0].bus.type = SENSORS_BUS_TYPE_ANY; 
     213    chips[0].bus.nr = SENSORS_BUS_NR_ANY; 
    213214    chips[0].addr = SENSORS_CHIP_NAME_ADDR_ANY; 
    214215    chips_count = 1; 
     
    311312  printf("%s\n",sprintf_chip_name(name)); 
    312313  if (!hide_adapter) { 
    313     const char *adap = sensors_get_adapter_name(name->bus); 
     314    const char *adap = sensors_get_adapter_name(&name->bus); 
    314315    if (adap) 
    315316      printf("Adapter: %s\n", adap); 
    316317    else 
    317       fprintf(stderr, "Can't get adapter name for bus %d\n", name->bus); 
     318      fprintf(stderr, "Can't get adapter name\n"); 
    318319  } 
    319320  if (do_unknown)