2005-09-18 20:30:44 +00:00
|
|
|
/*
|
|
|
|
sysfs.c - Part of libsensors, a library for reading Linux sensor data
|
|
|
|
Copyright (c) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
|
|
|
|
|
|
|
|
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
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
2005-09-18 20:38:19 +00:00
|
|
|
/* this define needed for strndup() */
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
2007-06-25 13:58:55 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2005-09-18 20:30:44 +00:00
|
|
|
#include <string.h>
|
2007-04-10 11:51:07 +00:00
|
|
|
#include <stdlib.h>
|
2005-09-18 20:30:44 +00:00
|
|
|
#include <limits.h>
|
2006-02-25 21:32:26 +00:00
|
|
|
#include <errno.h>
|
2005-09-18 20:30:44 +00:00
|
|
|
#include <sysfs/libsysfs.h>
|
2005-09-18 20:37:42 +00:00
|
|
|
#include "data.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "access.h"
|
|
|
|
#include "general.h"
|
2005-09-18 20:30:44 +00:00
|
|
|
#include "sysfs.h"
|
|
|
|
|
|
|
|
char sensors_sysfs_mount[NAME_MAX];
|
|
|
|
|
2007-08-30 21:10:41 +00:00
|
|
|
#define MAX_SENSORS_PER_TYPE 16
|
2007-08-30 21:21:15 +00:00
|
|
|
#define MAX_SUB_FEATURES 22
|
2007-08-30 21:10:41 +00:00
|
|
|
/* Room for all 3 types (in, fan, temp) with all their subfeatures + VID */
|
2007-08-30 21:21:15 +00:00
|
|
|
#define ALL_POSSIBLE_FEATURES (MAX_SENSORS_PER_TYPE * MAX_SUB_FEATURES * 3 \
|
2007-08-30 21:10:41 +00:00
|
|
|
+ MAX_SENSORS_PER_TYPE)
|
2007-04-09 14:09:21 +00:00
|
|
|
|
2007-05-28 12:38:28 +00:00
|
|
|
static
|
|
|
|
int get_type_scaling(int type)
|
|
|
|
{
|
|
|
|
switch (type & 0xFF10) {
|
|
|
|
case SENSORS_FEATURE_IN:
|
|
|
|
case SENSORS_FEATURE_TEMP:
|
|
|
|
return 3;
|
|
|
|
case SENSORS_FEATURE_FAN:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case SENSORS_FEATURE_VID:
|
|
|
|
return 3;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-28 14:39:34 +00:00
|
|
|
static int sensors_read_dynamic_chip(sensors_chip_features *chip,
|
|
|
|
struct sysfs_device *sysdir)
|
2007-04-09 14:09:21 +00:00
|
|
|
{
|
2007-04-10 11:51:07 +00:00
|
|
|
int i, type, fnum = 1;
|
2007-04-09 14:09:21 +00:00
|
|
|
struct sysfs_attribute *attr;
|
|
|
|
struct dlist *attrs;
|
2007-08-30 21:10:41 +00:00
|
|
|
sensors_chip_feature *features;
|
2007-04-10 11:51:07 +00:00
|
|
|
sensors_chip_feature *dyn_features;
|
2007-04-09 14:09:21 +00:00
|
|
|
char *name;
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2007-04-09 14:09:21 +00:00
|
|
|
attrs = sysfs_get_device_attributes(sysdir);
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2007-04-09 14:09:21 +00:00
|
|
|
if (attrs == NULL)
|
2007-06-28 14:39:34 +00:00
|
|
|
return -ENOENT;
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2007-08-30 21:10:41 +00:00
|
|
|
/* We use a large sparse table at first to store all found features,
|
|
|
|
so that we can store them sorted at type and index and then later
|
|
|
|
create a dense sorted table. */
|
|
|
|
features = calloc(ALL_POSSIBLE_FEATURES, sizeof(sensors_chip_feature));
|
|
|
|
if (!features)
|
|
|
|
sensors_fatal_error(__FUNCTION__, "Out of memory");
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2007-04-09 14:09:21 +00:00
|
|
|
dlist_for_each_data(attrs, attr, struct sysfs_attribute) {
|
2007-07-03 16:03:22 +00:00
|
|
|
sensors_chip_feature feature;
|
2007-04-09 14:09:21 +00:00
|
|
|
name = attr->name;
|
2007-07-19 20:49:29 +00:00
|
|
|
int nr;
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2007-07-19 20:49:29 +00:00
|
|
|
type = sensors_feature_get_type(name, &nr);
|
|
|
|
if (type == SENSORS_FEATURE_UNKNOWN)
|
|
|
|
continue;
|
|
|
|
|
2007-07-03 16:03:22 +00:00
|
|
|
memset(&feature, 0, sizeof(sensors_chip_feature));
|
2007-04-10 11:51:07 +00:00
|
|
|
/* check for _input extension and remove */
|
|
|
|
i = strlen(name);
|
|
|
|
if (i > 6 && !strcmp(name + i - 6, "_input"))
|
|
|
|
feature.data.name = strndup(name, i-6);
|
|
|
|
else
|
|
|
|
feature.data.name = strdup(name);
|
|
|
|
|
2007-07-19 20:49:29 +00:00
|
|
|
/* Adjust the channel number */
|
2007-04-10 11:51:07 +00:00
|
|
|
switch (type & 0xFF00) {
|
|
|
|
case SENSORS_FEATURE_FAN:
|
|
|
|
case SENSORS_FEATURE_TEMP:
|
2007-07-19 20:49:29 +00:00
|
|
|
if (nr)
|
|
|
|
nr--;
|
2007-04-10 11:51:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2007-07-19 20:49:29 +00:00
|
|
|
if (nr >= MAX_SENSORS_PER_TYPE) {
|
2007-04-10 11:51:07 +00:00
|
|
|
fprintf(stderr, "libsensors error, more sensors of one"
|
|
|
|
" type then MAX_SENSORS_PER_TYPE, ignoring "
|
|
|
|
"feature: %s\n", name);
|
2007-06-28 13:35:46 +00:00
|
|
|
free(feature.data.name);
|
2007-04-10 11:51:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2007-04-10 11:51:07 +00:00
|
|
|
/* "calculate" a place to store the feature in our sparse,
|
|
|
|
sorted table */
|
2007-07-05 16:09:40 +00:00
|
|
|
if (type == SENSORS_FEATURE_VID) {
|
2007-08-30 21:21:15 +00:00
|
|
|
i = nr + MAX_SENSORS_PER_TYPE * MAX_SUB_FEATURES * 3;
|
2007-07-05 16:09:40 +00:00
|
|
|
} else {
|
|
|
|
i = (type >> 8) * MAX_SENSORS_PER_TYPE *
|
2007-08-30 21:21:15 +00:00
|
|
|
MAX_SUB_FEATURES + nr * MAX_SUB_FEATURES +
|
2007-07-05 16:09:40 +00:00
|
|
|
(type & 0xFF);
|
|
|
|
}
|
2007-08-31 14:42:20 +00:00
|
|
|
|
|
|
|
if (features[i].data.name) {
|
2007-04-10 11:51:07 +00:00
|
|
|
fprintf(stderr, "libsensors error, trying to add dupli"
|
|
|
|
"cate feature: %s to dynamic feature table\n",
|
|
|
|
name);
|
2007-06-28 13:35:46 +00:00
|
|
|
free(feature.data.name);
|
2007-04-10 11:51:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2007-04-10 11:51:07 +00:00
|
|
|
/* fill in the other feature members */
|
|
|
|
feature.data.number = i + 1;
|
2007-07-19 20:46:09 +00:00
|
|
|
feature.data.type = type;
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2007-07-05 16:09:40 +00:00
|
|
|
if ((type & 0x00FF) == 0) {
|
|
|
|
/* main feature */
|
2007-04-10 11:51:07 +00:00
|
|
|
feature.data.mapping = SENSORS_NO_MAPPING;
|
|
|
|
feature.data.compute_mapping = SENSORS_NO_MAPPING;
|
|
|
|
} else if (type & 0x10) {
|
|
|
|
/* sub feature without compute mapping */
|
2007-08-30 21:21:15 +00:00
|
|
|
feature.data.mapping = i - i % MAX_SUB_FEATURES + 1;
|
2007-04-10 11:51:07 +00:00
|
|
|
feature.data.compute_mapping = SENSORS_NO_MAPPING;
|
2007-04-09 14:09:21 +00:00
|
|
|
} else {
|
2007-08-30 21:21:15 +00:00
|
|
|
feature.data.mapping = i - i % MAX_SUB_FEATURES + 1;
|
2007-04-10 11:51:07 +00:00
|
|
|
feature.data.compute_mapping = feature.data.mapping;
|
2007-04-09 14:09:21 +00:00
|
|
|
}
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2007-06-29 12:11:43 +00:00
|
|
|
if (attr->method & SYSFS_METHOD_SHOW)
|
|
|
|
feature.data.mode |= SENSORS_MODE_R;
|
|
|
|
if (attr->method & SYSFS_METHOD_STORE)
|
|
|
|
feature.data.mode |= SENSORS_MODE_W;
|
2007-04-10 11:51:07 +00:00
|
|
|
|
2007-05-28 12:38:28 +00:00
|
|
|
feature.scaling = get_type_scaling(type);
|
|
|
|
|
2007-04-10 11:51:07 +00:00
|
|
|
features[i] = feature;
|
2007-04-09 14:09:21 +00:00
|
|
|
fnum++;
|
|
|
|
}
|
2007-04-10 11:51:07 +00:00
|
|
|
|
2007-07-21 09:28:12 +00:00
|
|
|
if (fnum == 1) { /* No feature */
|
|
|
|
chip->feature = NULL;
|
2007-08-30 21:10:41 +00:00
|
|
|
goto exit_free;
|
2007-07-21 09:28:12 +00:00
|
|
|
}
|
|
|
|
|
2007-04-10 11:51:07 +00:00
|
|
|
dyn_features = calloc(fnum, sizeof(sensors_chip_feature));
|
2007-04-09 14:09:21 +00:00
|
|
|
if (dyn_features == NULL) {
|
2007-08-31 14:42:20 +00:00
|
|
|
sensors_fatal_error(__FUNCTION__, "Out of memory");
|
2007-04-09 14:09:21 +00:00
|
|
|
}
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2007-04-10 11:51:07 +00:00
|
|
|
fnum = 0;
|
2007-08-31 14:42:20 +00:00
|
|
|
for (i = 0; i < ALL_POSSIBLE_FEATURES; i++) {
|
2007-04-10 11:51:07 +00:00
|
|
|
if (features[i].data.name) {
|
|
|
|
dyn_features[fnum] = features[i];
|
|
|
|
fnum++;
|
|
|
|
}
|
2007-04-09 14:09:21 +00:00
|
|
|
}
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2007-06-28 14:39:34 +00:00
|
|
|
chip->feature = dyn_features;
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2007-08-30 21:10:41 +00:00
|
|
|
exit_free:
|
|
|
|
free(features);
|
2007-06-28 14:39:34 +00:00
|
|
|
return 0;
|
2007-04-09 14:09:21 +00:00
|
|
|
}
|
|
|
|
|
2005-09-18 20:30:44 +00:00
|
|
|
/* returns !0 if sysfs filesystem was found, 0 otherwise */
|
|
|
|
int sensors_init_sysfs(void)
|
|
|
|
{
|
2007-06-25 13:58:55 +00:00
|
|
|
struct stat statbuf;
|
2005-09-18 20:30:44 +00:00
|
|
|
|
2007-06-25 13:58:55 +00:00
|
|
|
/* libsysfs will return success even if sysfs is not mounted,
|
|
|
|
so we have to double-check */
|
|
|
|
if (sysfs_get_mnt_path(sensors_sysfs_mount, NAME_MAX)
|
|
|
|
|| stat(sensors_sysfs_mount, &statbuf) < 0
|
|
|
|
|| statbuf.st_nlink <= 2) /* Empty directory */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
2005-09-18 20:30:44 +00:00
|
|
|
}
|
2005-09-18 20:37:42 +00:00
|
|
|
|
2005-09-18 20:38:19 +00:00
|
|
|
/* returns: 0 if successful, !0 otherwise */
|
|
|
|
static int sensors_read_one_sysfs_chip(struct sysfs_device *dev)
|
|
|
|
{
|
2007-06-28 14:39:34 +00:00
|
|
|
int domain, bus, slot, fn;
|
2007-07-21 09:28:12 +00:00
|
|
|
int err = -SENSORS_ERR_PARSE;
|
2005-09-18 20:38:19 +00:00
|
|
|
struct sysfs_attribute *attr, *bus_attr;
|
|
|
|
char bus_path[SYSFS_PATH_MAX];
|
2007-06-28 14:39:34 +00:00
|
|
|
sensors_chip_features entry;
|
2005-09-18 20:38:19 +00:00
|
|
|
|
|
|
|
/* ignore any device without name attribute */
|
|
|
|
if (!(attr = sysfs_get_device_attr(dev, "name")))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* NB: attr->value[attr->len-1] == '\n'; chop that off */
|
2007-06-28 14:39:34 +00:00
|
|
|
entry.chip.prefix = strndup(attr->value, attr->len - 1);
|
|
|
|
if (!entry.chip.prefix)
|
2005-09-18 20:38:19 +00:00
|
|
|
sensors_fatal_error(__FUNCTION__, "out of memory");
|
|
|
|
|
2007-08-16 09:56:07 +00:00
|
|
|
entry.chip.path = strdup(dev->path);
|
|
|
|
if (!entry.chip.path)
|
2005-09-18 20:38:19 +00:00
|
|
|
sensors_fatal_error(__FUNCTION__, "out of memory");
|
|
|
|
|
2007-08-19 15:03:50 +00:00
|
|
|
if (sscanf(dev->name, "%hd-%x", &entry.chip.bus.nr, &entry.chip.addr) == 2) {
|
2006-01-09 19:55:18 +00:00
|
|
|
/* find out if legacy ISA or not */
|
2007-08-19 15:03:50 +00:00
|
|
|
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;
|
2007-04-03 12:55:30 +00:00
|
|
|
snprintf(bus_path, sizeof(bus_path),
|
2006-01-09 19:55:18 +00:00
|
|
|
"%s/class/i2c-adapter/i2c-%d/device/name",
|
2007-08-19 15:03:50 +00:00
|
|
|
sensors_sysfs_mount, entry.chip.bus.nr);
|
2006-01-09 19:55:18 +00:00
|
|
|
|
2007-04-03 12:55:30 +00:00
|
|
|
if ((bus_attr = sysfs_open_attribute(bus_path))) {
|
2007-07-21 08:44:31 +00:00
|
|
|
if (sysfs_read_attribute(bus_attr)) {
|
|
|
|
sysfs_close_attribute(bus_attr);
|
|
|
|
goto exit_free;
|
|
|
|
}
|
2006-01-09 19:55:18 +00:00
|
|
|
|
2007-04-03 12:55:30 +00:00
|
|
|
if (bus_attr->value
|
2007-08-19 15:03:50 +00:00
|
|
|
&& !strncmp(bus_attr->value, "ISA ", 4)) {
|
|
|
|
entry.chip.bus.type = SENSORS_BUS_TYPE_ISA;
|
|
|
|
entry.chip.bus.nr = 0;
|
|
|
|
}
|
2006-01-09 19:55:18 +00:00
|
|
|
|
2007-04-03 12:55:30 +00:00
|
|
|
sysfs_close_attribute(bus_attr);
|
|
|
|
}
|
2006-01-09 19:55:18 +00:00
|
|
|
}
|
2007-08-19 15:07:05 +00:00
|
|
|
} else if (sscanf(dev->name, "spi%hd.%d", &entry.chip.bus.nr,
|
|
|
|
&entry.chip.addr) == 2) {
|
|
|
|
/* SPI */
|
|
|
|
entry.chip.bus.type = SENSORS_BUS_TYPE_SPI;
|
2007-06-28 14:39:34 +00:00
|
|
|
} else if (sscanf(dev->name, "%*[a-z0-9_].%d", &entry.chip.addr) == 1) {
|
2006-01-09 19:55:18 +00:00
|
|
|
/* must be new ISA (platform driver) */
|
2007-08-19 15:03:50 +00:00
|
|
|
entry.chip.bus.type = SENSORS_BUS_TYPE_ISA;
|
|
|
|
entry.chip.bus.nr = 0;
|
2006-08-19 15:23:10 +00:00
|
|
|
} else if (sscanf(dev->name, "%x:%x:%x.%x", &domain, &bus, &slot, &fn) == 4) {
|
|
|
|
/* PCI */
|
2007-06-28 14:39:34 +00:00
|
|
|
entry.chip.addr = (domain << 16) + (bus << 8) + (slot << 3) + fn;
|
2007-08-19 15:03:50 +00:00
|
|
|
entry.chip.bus.type = SENSORS_BUS_TYPE_PCI;
|
|
|
|
entry.chip.bus.nr = 0;
|
2006-01-09 19:55:18 +00:00
|
|
|
} else
|
2007-07-21 08:44:31 +00:00
|
|
|
goto exit_free;
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2007-06-28 14:39:34 +00:00
|
|
|
if (sensors_read_dynamic_chip(&entry, dev) < 0)
|
2007-07-21 08:44:31 +00:00
|
|
|
goto exit_free;
|
2007-07-21 09:28:12 +00:00
|
|
|
if (!entry.feature) { /* No feature, discard chip */
|
|
|
|
err = 0;
|
|
|
|
goto exit_free;
|
|
|
|
}
|
2007-06-28 14:39:34 +00:00
|
|
|
sensors_add_proc_chips(&entry);
|
2005-09-18 20:38:19 +00:00
|
|
|
|
|
|
|
return 0;
|
2007-07-21 08:44:31 +00:00
|
|
|
|
|
|
|
exit_free:
|
|
|
|
free(entry.chip.prefix);
|
2007-08-16 09:56:07 +00:00
|
|
|
free(entry.chip.path);
|
2007-07-21 09:28:12 +00:00
|
|
|
return err;
|
2005-09-18 20:38:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* returns 0 if successful, !0 otherwise */
|
|
|
|
static int sensors_read_sysfs_chips_compat(void)
|
|
|
|
{
|
|
|
|
struct sysfs_bus *bus;
|
|
|
|
struct dlist *devs;
|
|
|
|
struct sysfs_device *dev;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!(bus = sysfs_open_bus("i2c"))) {
|
2006-09-19 10:01:12 +00:00
|
|
|
if (errno && errno != ENOENT)
|
|
|
|
ret = -SENSORS_ERR_PROC;
|
2005-09-18 20:38:19 +00:00
|
|
|
goto exit0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(devs = sysfs_get_bus_devices(bus))) {
|
2006-09-19 10:01:12 +00:00
|
|
|
if (errno && errno != ENOENT)
|
2006-06-06 11:23:59 +00:00
|
|
|
ret = -SENSORS_ERR_PROC;
|
2005-09-18 20:38:19 +00:00
|
|
|
goto exit1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dlist_for_each_data(devs, dev, struct sysfs_device)
|
|
|
|
if ((ret = sensors_read_one_sysfs_chip(dev)))
|
|
|
|
goto exit1;
|
|
|
|
|
|
|
|
exit1:
|
|
|
|
/* this frees bus and devs */
|
|
|
|
sysfs_close_bus(bus);
|
|
|
|
|
|
|
|
exit0:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns 0 if successful, !0 otherwise */
|
|
|
|
int sensors_read_sysfs_chips(void)
|
|
|
|
{
|
|
|
|
struct sysfs_class *cls;
|
|
|
|
struct dlist *clsdevs;
|
|
|
|
struct sysfs_class_device *clsdev;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!(cls = sysfs_open_class("hwmon"))) {
|
|
|
|
/* compatibility function for kernel 2.6.n where n <= 13 */
|
|
|
|
return sensors_read_sysfs_chips_compat();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(clsdevs = sysfs_get_class_devices(cls))) {
|
2006-09-19 10:01:12 +00:00
|
|
|
if (errno && errno != ENOENT)
|
2006-06-06 11:23:59 +00:00
|
|
|
ret = -SENSORS_ERR_PROC;
|
2005-09-18 20:38:19 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
dlist_for_each_data(clsdevs, clsdev, struct sysfs_class_device) {
|
|
|
|
struct sysfs_device *dev;
|
|
|
|
if (!(dev = sysfs_get_classdev_device(clsdev))) {
|
|
|
|
ret = -SENSORS_ERR_PROC;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if ((ret = sensors_read_one_sysfs_chip(dev)))
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
/* this frees cls and clsdevs */
|
|
|
|
sysfs_close_class(cls);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-09-18 20:37:42 +00:00
|
|
|
/* returns 0 if successful, !0 otherwise */
|
|
|
|
int sensors_read_sysfs_bus(void)
|
|
|
|
{
|
|
|
|
struct sysfs_class *cls;
|
|
|
|
struct dlist *clsdevs;
|
|
|
|
struct sysfs_class_device *clsdev;
|
|
|
|
sensors_bus entry;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!(cls = sysfs_open_class("i2c-adapter"))) {
|
2006-02-25 21:32:26 +00:00
|
|
|
if (errno && errno != ENOENT)
|
|
|
|
ret = -SENSORS_ERR_PROC;
|
2005-09-18 20:37:42 +00:00
|
|
|
goto exit0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(clsdevs = sysfs_get_class_devices(cls))) {
|
2006-09-19 10:01:12 +00:00
|
|
|
if (errno && errno != ENOENT)
|
2006-02-25 21:32:26 +00:00
|
|
|
ret = -SENSORS_ERR_PROC;
|
2005-09-18 20:37:42 +00:00
|
|
|
goto exit1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dlist_for_each_data(clsdevs, clsdev, struct sysfs_class_device) {
|
|
|
|
struct sysfs_device *dev;
|
|
|
|
struct sysfs_attribute *attr;
|
|
|
|
|
2006-12-21 08:24:01 +00:00
|
|
|
/* Get the adapter name from the classdev "name" attribute
|
|
|
|
* (Linux 2.6.20 and later). If it fails, fall back to
|
|
|
|
* the device "name" attribute (for older kernels). */
|
|
|
|
if (!(attr = sysfs_get_classdev_attr(clsdev, "name"))
|
2007-04-03 12:55:30 +00:00
|
|
|
&& !((dev = sysfs_get_classdev_device(clsdev)) &&
|
|
|
|
(attr = sysfs_get_device_attr(dev, "name"))))
|
2005-09-18 20:37:42 +00:00
|
|
|
continue;
|
|
|
|
|
2007-08-19 15:04:29 +00:00
|
|
|
if (sscanf(clsdev->name, "i2c-%hd", &entry.bus.nr) != 1 ||
|
|
|
|
entry.bus.nr == 9191) /* legacy ISA */
|
2007-08-16 09:57:56 +00:00
|
|
|
continue;
|
2007-08-19 15:04:29 +00:00
|
|
|
entry.bus.type = SENSORS_BUS_TYPE_I2C;
|
2007-08-16 09:57:56 +00:00
|
|
|
|
2006-09-03 13:05:26 +00:00
|
|
|
/* NB: attr->value[attr->len-1] == '\n'; chop that off */
|
|
|
|
entry.adapter = strndup(attr->value, attr->len - 1);
|
2005-09-18 20:37:42 +00:00
|
|
|
if (!entry.adapter)
|
|
|
|
sensors_fatal_error(__FUNCTION__, "out of memory");
|
|
|
|
|
|
|
|
sensors_add_proc_bus(&entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit1:
|
|
|
|
/* this frees *cls _and_ *clsdevs */
|
|
|
|
sysfs_close_class(cls);
|
|
|
|
|
|
|
|
exit0:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-08-13 20:16:42 +00:00
|
|
|
int sensors_read_sysfs_attr(const sensors_chip_name *name, int feature,
|
|
|
|
double *value)
|
2007-07-05 15:03:22 +00:00
|
|
|
{
|
|
|
|
const sensors_chip_feature *the_feature;
|
|
|
|
int mag;
|
|
|
|
char n[NAME_MAX];
|
|
|
|
FILE *f;
|
|
|
|
const char *suffix = "";
|
|
|
|
|
2007-08-13 20:16:42 +00:00
|
|
|
if (!(the_feature = sensors_lookup_feature_nr(name, feature)))
|
2007-07-05 15:03:22 +00:00
|
|
|
return -SENSORS_ERR_NO_ENTRY;
|
|
|
|
|
|
|
|
/* REVISIT: this is a ugly hack */
|
2007-09-02 11:09:16 +00:00
|
|
|
if (the_feature->data.type == SENSORS_FEATURE_IN
|
|
|
|
|| the_feature->data.type == SENSORS_FEATURE_FAN
|
|
|
|
|| the_feature->data.type == SENSORS_FEATURE_TEMP)
|
2007-07-05 15:03:22 +00:00
|
|
|
suffix = "_input";
|
|
|
|
|
2007-08-16 09:56:07 +00:00
|
|
|
snprintf(n, NAME_MAX, "%s/%s%s", name->path, the_feature->data.name,
|
2007-07-05 15:03:22 +00:00
|
|
|
suffix);
|
|
|
|
if ((f = fopen(n, "r"))) {
|
|
|
|
int res = fscanf(f, "%lf", value);
|
|
|
|
fclose(f);
|
|
|
|
if (res != 1)
|
|
|
|
return -SENSORS_ERR_PROC;
|
|
|
|
for (mag = the_feature->scaling; mag > 0; mag --)
|
|
|
|
*value /= 10.0;
|
|
|
|
} else
|
|
|
|
return -SENSORS_ERR_PROC;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2007-08-13 20:16:42 +00:00
|
|
|
int sensors_write_sysfs_attr(const sensors_chip_name *name, int feature,
|
|
|
|
double value)
|
2007-07-05 15:03:22 +00:00
|
|
|
{
|
|
|
|
const sensors_chip_feature *the_feature;
|
|
|
|
int mag;
|
|
|
|
char n[NAME_MAX];
|
|
|
|
FILE *f;
|
|
|
|
const char *suffix = "";
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2007-08-13 20:16:42 +00:00
|
|
|
if (!(the_feature = sensors_lookup_feature_nr(name, feature)))
|
2007-07-05 15:03:22 +00:00
|
|
|
return -SENSORS_ERR_NO_ENTRY;
|
|
|
|
|
|
|
|
/* REVISIT: this is a ugly hack */
|
2007-09-02 11:09:16 +00:00
|
|
|
if (the_feature->data.type == SENSORS_FEATURE_IN
|
|
|
|
|| the_feature->data.type == SENSORS_FEATURE_FAN
|
|
|
|
|| the_feature->data.type == SENSORS_FEATURE_TEMP)
|
2007-07-05 15:03:22 +00:00
|
|
|
suffix = "_input";
|
|
|
|
|
2007-08-16 09:56:07 +00:00
|
|
|
snprintf(n, NAME_MAX, "%s/%s%s", name->path, the_feature->data.name,
|
2007-07-05 15:03:22 +00:00
|
|
|
suffix);
|
|
|
|
if ((f = fopen(n, "w"))) {
|
|
|
|
for (mag = the_feature->scaling; mag > 0; mag --)
|
|
|
|
value *= 10.0;
|
|
|
|
fprintf(f, "%d", (int) value);
|
|
|
|
fclose(f);
|
|
|
|
} else
|
|
|
|
return -SENSORS_ERR_PROC;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|