Changeset 3094

Show
Ignore:
Timestamp:
09/18/05 22:37:42 (8 years ago)
Author:
mmh
Message:

(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.

Location:
lm-sensors/trunk/lib
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • lm-sensors/trunk/lib/init.c

    r3093 r3094  
    4242  int res; 
    4343  sensors_cleanup(); 
    44   sensors_init_sysfs(); 
    4544  if ((res = sensors_read_proc_chips())) 
    4645    return res; 
    47   if ((res = sensors_read_proc_bus())) 
    48     return res; 
     46  if (sensors_init_sysfs()) { 
     47    if ((res = sensors_read_sysfs_bus())) 
     48      return res; 
     49  } else { 
     50    if ((res = sensors_read_proc_bus())) 
     51      return res; 
     52  } 
    4953  sensors_yyin = input; 
    5054  if ((res = sensors_yyparse())) 
  • lm-sensors/trunk/lib/proc.c

    r3093 r3094  
    215215int sensors_read_proc_bus(void) 
    216216{ 
    217         struct dirent *de; 
    218         DIR *dir; 
    219         FILE *f; 
    220         char line[255]; 
    221         char *border; 
    222         sensors_bus entry; 
    223         int lineno; 
    224         char sysfs[NAME_MAX], n[NAME_MAX]; 
    225         char dirname[NAME_MAX]; 
    226  
    227         if (sensors_found_sysfs) { 
    228                 strncpy(sysfs, sensors_sysfs_mount, sizeof(sysfs) - 1); 
    229                 sysfs[sizeof(sysfs) - 1] = 0; 
    230                 strncat(sysfs, "/class/i2c-adapter", 
    231                                 sizeof(sysfs) - strlen(sysfs) - 1); 
    232  
    233                 /* Then read from it */ 
    234                 dir = opendir(sysfs); 
    235                 if (! dir) 
    236                         goto proc; 
    237  
    238                 while ((de = readdir(dir)) != NULL) { 
    239                         if (!strcmp(de->d_name, ".")) 
    240                                 continue; 
    241                         if (!strcmp(de->d_name, "..")) 
    242                                 continue; 
    243  
    244                         strcpy(n, sysfs); 
    245                         strcat(n, "/"); 
    246                         strcat(n, de->d_name); 
    247                         strcpy(dirname, n); 
    248                         strcat(n, "/device/name"); 
    249  
    250                         if ((f = fopen(n, "r")) != NULL) { 
    251                                 char    x[120]; 
    252                                 fgets(x, 120, f); 
    253                                 fclose(f); 
    254                                 if((border = index(x, '\n')) != NULL) 
    255                                         *border = 0; 
    256                                 entry.adapter=strdup(x); 
    257                                 if(!strncmp(x, "ISA ", 4)) { 
    258                                         entry.number = SENSORS_CHIP_NAME_BUS_ISA; 
    259                                         entry.algorithm = strdup("ISA bus algorithm"); 
    260                                 } else if(!sscanf(de->d_name, "i2c-%d", &entry.number)) { 
    261                                         entry.number = SENSORS_CHIP_NAME_BUS_DUMMY; 
    262                                         entry.algorithm = strdup("Dummy bus algorithm"); 
    263                                 } else 
    264                                         entry.algorithm = strdup("Unavailable from sysfs"); 
    265                                 if (entry.algorithm == NULL) 
    266                                         goto FAT_ERROR_SYS; 
    267                                 sensors_add_proc_bus(&entry); 
    268                         } 
    269                 } 
    270                 closedir(dir); 
    271                 return 0; 
    272 FAT_ERROR_SYS: 
    273                 sensors_fatal_error("sensors_read_proc_bus", "Allocating entry"); 
    274                 closedir(dir); 
    275                 return -SENSORS_ERR_PROC; 
    276         } 
    277  
    278 proc: 
     217  FILE *f; 
     218  char line[255]; 
     219  char *border; 
     220  sensors_bus entry; 
     221  int lineno; 
    279222 
    280223  f = fopen("/proc/bus/i2c","r"); 
  • lm-sensors/trunk/lib/sysfs.c

    r3093 r3094  
    2121#include <limits.h> 
    2222#include <sysfs/libsysfs.h> 
     23#include "data.h" 
     24#include "error.h" 
     25#include "access.h" 
     26#include "general.h" 
    2327#include "sysfs.h" 
    2428 
     
    3539        return sensors_found_sysfs; 
    3640} 
     41 
     42/* returns 0 if successful, !0 otherwise */ 
     43int sensors_read_sysfs_bus(void) 
     44{ 
     45        struct sysfs_class *cls; 
     46        struct dlist *clsdevs; 
     47        struct sysfs_class_device *clsdev; 
     48        sensors_bus entry; 
     49        int ret = 0; 
     50 
     51        if (!(cls = sysfs_open_class("i2c-adapter"))) { 
     52                ret = -SENSORS_ERR_PROC; 
     53                goto exit0; 
     54        } 
     55 
     56        if (!(clsdevs = sysfs_get_class_devices(cls))) { 
     57                ret = -SENSORS_ERR_PROC; 
     58                goto exit1; 
     59        } 
     60 
     61        dlist_for_each_data(clsdevs, clsdev, struct sysfs_class_device) { 
     62                struct sysfs_device *dev; 
     63                struct sysfs_attribute *attr; 
     64 
     65                if (!(dev = sysfs_get_classdev_device(clsdev))) 
     66                        continue; 
     67                if (!(attr = sysfs_get_device_attr(dev, "name"))) 
     68                        continue; 
     69 
     70                entry.adapter = strdup(attr->value); 
     71                if (!entry.adapter) 
     72                        sensors_fatal_error(__FUNCTION__, "out of memory"); 
     73 
     74                if (!strncmp(entry.adapter, "ISA ", 4)) { 
     75                        entry.number = SENSORS_CHIP_NAME_BUS_ISA; 
     76                        entry.algorithm = strdup("ISA bus algorithm"); 
     77                } else if (sscanf(clsdev->name, "i2c-%d", &entry.number) != 1) { 
     78                        entry.number = SENSORS_CHIP_NAME_BUS_DUMMY; 
     79                        entry.algorithm = strdup("Dummy bus algorithm"); 
     80                } else 
     81                        entry.algorithm = strdup("Unavailable from sysfs"); 
     82 
     83                if (!entry.algorithm) 
     84                        sensors_fatal_error(__FUNCTION__, "out of memory"); 
     85 
     86                sensors_add_proc_bus(&entry); 
     87        } 
     88 
     89exit1: 
     90        /* this frees *cls _and_ *clsdevs */ 
     91        sysfs_close_class(cls); 
     92 
     93exit0: 
     94        return ret; 
     95} 
     96 
  • lm-sensors/trunk/lib/sysfs.h

    r3093 r3094  
    2727extern int sensors_init_sysfs(void); 
    2828 
     29extern int sensors_read_sysfs_bus(void); 
     30 
    2931#endif /* !SENSORS_LIB_SYSFS_H */