| 1 | /* |
|---|
| 2 | sysfs.c - Part of libsensors, a library for reading Linux sensor data |
|---|
| 3 | Copyright (c) 2005 Mark M. Hoffman <mhoffman@lightlink.com> |
|---|
| 4 | |
|---|
| 5 | This program is free software; you can redistribute it and/or modify |
|---|
| 6 | it under the terms of the GNU General Public License as published by |
|---|
| 7 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | (at your option) any later version. |
|---|
| 9 | |
|---|
| 10 | This program is distributed in the hope that it will be useful, |
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | GNU General Public License for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU General Public License |
|---|
| 16 | along with this program; if not, write to the Free Software |
|---|
| 17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | /* this define needed for strndup() */ |
|---|
| 21 | #define _GNU_SOURCE |
|---|
| 22 | |
|---|
| 23 | #include <string.h> |
|---|
| 24 | #include <stdlib.h> |
|---|
| 25 | #include <limits.h> |
|---|
| 26 | #include <errno.h> |
|---|
| 27 | #include <sysfs/libsysfs.h> |
|---|
| 28 | #include "data.h" |
|---|
| 29 | #include "error.h" |
|---|
| 30 | #include "access.h" |
|---|
| 31 | #include "general.h" |
|---|
| 32 | #include "sysfs.h" |
|---|
| 33 | |
|---|
| 34 | int sensors_found_sysfs = 0; |
|---|
| 35 | |
|---|
| 36 | char sensors_sysfs_mount[NAME_MAX]; |
|---|
| 37 | |
|---|
| 38 | /* returns !0 if sysfs filesystem was found, 0 otherwise */ |
|---|
| 39 | int sensors_init_sysfs(void) |
|---|
| 40 | { |
|---|
| 41 | if (sysfs_get_mnt_path(sensors_sysfs_mount, NAME_MAX) == 0) |
|---|
| 42 | sensors_found_sysfs = 1; |
|---|
| 43 | |
|---|
| 44 | return sensors_found_sysfs; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | /* returns: 0 if successful, !0 otherwise */ |
|---|
| 48 | static int sensors_read_one_sysfs_chip(struct sysfs_device *dev) |
|---|
| 49 | { |
|---|
| 50 | int domain, bus, slot, fn; |
|---|
| 51 | struct sysfs_attribute *attr, *bus_attr; |
|---|
| 52 | char bus_path[SYSFS_PATH_MAX]; |
|---|
| 53 | sensors_proc_chips_entry entry; |
|---|
| 54 | |
|---|
| 55 | /* ignore any device without name attribute */ |
|---|
| 56 | if (!(attr = sysfs_get_device_attr(dev, "name"))) |
|---|
| 57 | return 0; |
|---|
| 58 | |
|---|
| 59 | /* ignore subclients */ |
|---|
| 60 | if (attr->len >= 11 && !strcmp(attr->value + attr->len - 11, |
|---|
| 61 | " subclient\n")) |
|---|
| 62 | return 0; |
|---|
| 63 | |
|---|
| 64 | /* also ignore eeproms */ |
|---|
| 65 | if (!strcmp(attr->value, "eeprom\n")) |
|---|
| 66 | return 0; |
|---|
| 67 | |
|---|
| 68 | /* NB: attr->value[attr->len-1] == '\n'; chop that off */ |
|---|
| 69 | entry.name.prefix = strndup(attr->value, attr->len - 1); |
|---|
| 70 | if (!entry.name.prefix) |
|---|
| 71 | sensors_fatal_error(__FUNCTION__, "out of memory"); |
|---|
| 72 | |
|---|
| 73 | entry.name.busname = strdup(dev->path); |
|---|
| 74 | if (!entry.name.busname) |
|---|
| 75 | sensors_fatal_error(__FUNCTION__, "out of memory"); |
|---|
| 76 | |
|---|
| 77 | if (sscanf(dev->name, "%d-%x", &entry.name.bus, &entry.name.addr) == 2) { |
|---|
| 78 | /* find out if legacy ISA or not */ |
|---|
| 79 | if (entry.name.bus == 9191) |
|---|
| 80 | entry.name.bus = SENSORS_CHIP_NAME_BUS_ISA; |
|---|
| 81 | else { |
|---|
| 82 | snprintf(bus_path, sizeof(bus_path), |
|---|
| 83 | "%s/class/i2c-adapter/i2c-%d/device/name", |
|---|
| 84 | sensors_sysfs_mount, entry.name.bus); |
|---|
| 85 | |
|---|
| 86 | if ((bus_attr = sysfs_open_attribute(bus_path))) { |
|---|
| 87 | if (sysfs_read_attribute(bus_attr)) { |
|---|
| 88 | sysfs_close_attribute(bus_attr); |
|---|
| 89 | goto exit_free; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | if (bus_attr->value |
|---|
| 93 | && !strncmp(bus_attr->value, "ISA ", 4)) |
|---|
| 94 | entry.name.bus = SENSORS_CHIP_NAME_BUS_ISA; |
|---|
| 95 | |
|---|
| 96 | sysfs_close_attribute(bus_attr); |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | } else if (sscanf(dev->name, "%*[a-z0-9_].%d", &entry.name.addr) == 1) { |
|---|
| 100 | /* must be new ISA (platform driver) */ |
|---|
| 101 | entry.name.bus = SENSORS_CHIP_NAME_BUS_ISA; |
|---|
| 102 | } else if (sscanf(dev->name, "%x:%x:%x.%x", &domain, &bus, &slot, &fn) == 4) { |
|---|
| 103 | /* PCI */ |
|---|
| 104 | entry.name.addr = (domain << 16) + (bus << 8) + (slot << 3) + fn; |
|---|
| 105 | entry.name.bus = SENSORS_CHIP_NAME_BUS_PCI; |
|---|
| 106 | } else |
|---|
| 107 | goto exit_free; |
|---|
| 108 | |
|---|
| 109 | sensors_add_proc_chips(&entry); |
|---|
| 110 | |
|---|
| 111 | return 0; |
|---|
| 112 | |
|---|
| 113 | exit_free: |
|---|
| 114 | free(entry.name.prefix); |
|---|
| 115 | free(entry.name.busname); |
|---|
| 116 | return -SENSORS_ERR_PARSE; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | /* returns 0 if successful, !0 otherwise */ |
|---|
| 120 | static int sensors_read_sysfs_chips_compat(void) |
|---|
| 121 | { |
|---|
| 122 | struct sysfs_bus *bus; |
|---|
| 123 | struct dlist *devs; |
|---|
| 124 | struct sysfs_device *dev; |
|---|
| 125 | int ret = 0; |
|---|
| 126 | |
|---|
| 127 | if (!(bus = sysfs_open_bus("i2c"))) { |
|---|
| 128 | if (errno && errno != ENOENT) |
|---|
| 129 | ret = -SENSORS_ERR_PROC; |
|---|
| 130 | goto exit0; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | if (!(devs = sysfs_get_bus_devices(bus))) { |
|---|
| 134 | if (errno && errno != ENOENT) |
|---|
| 135 | ret = -SENSORS_ERR_PROC; |
|---|
| 136 | goto exit1; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | dlist_for_each_data(devs, dev, struct sysfs_device) |
|---|
| 140 | if ((ret = sensors_read_one_sysfs_chip(dev))) |
|---|
| 141 | goto exit1; |
|---|
| 142 | |
|---|
| 143 | exit1: |
|---|
| 144 | /* this frees bus and devs */ |
|---|
| 145 | sysfs_close_bus(bus); |
|---|
| 146 | |
|---|
| 147 | exit0: |
|---|
| 148 | return ret; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | /* returns 0 if successful, !0 otherwise */ |
|---|
| 152 | int sensors_read_sysfs_chips(void) |
|---|
| 153 | { |
|---|
| 154 | struct sysfs_class *cls; |
|---|
| 155 | struct dlist *clsdevs; |
|---|
| 156 | struct sysfs_class_device *clsdev; |
|---|
| 157 | int ret = 0; |
|---|
| 158 | |
|---|
| 159 | if (!(cls = sysfs_open_class("hwmon"))) { |
|---|
| 160 | /* compatibility function for kernel 2.6.n where n <= 13 */ |
|---|
| 161 | return sensors_read_sysfs_chips_compat(); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | if (!(clsdevs = sysfs_get_class_devices(cls))) { |
|---|
| 165 | if (errno && errno != ENOENT) |
|---|
| 166 | ret = -SENSORS_ERR_PROC; |
|---|
| 167 | goto exit; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | dlist_for_each_data(clsdevs, clsdev, struct sysfs_class_device) { |
|---|
| 171 | struct sysfs_device *dev; |
|---|
| 172 | if (!(dev = sysfs_get_classdev_device(clsdev))) { |
|---|
| 173 | ret = -SENSORS_ERR_PROC; |
|---|
| 174 | goto exit; |
|---|
| 175 | } |
|---|
| 176 | if ((ret = sensors_read_one_sysfs_chip(dev))) |
|---|
| 177 | goto exit; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | exit: |
|---|
| 181 | /* this frees cls and clsdevs */ |
|---|
| 182 | sysfs_close_class(cls); |
|---|
| 183 | return ret; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | /* returns 0 if successful, !0 otherwise */ |
|---|
| 187 | int sensors_read_sysfs_bus(void) |
|---|
| 188 | { |
|---|
| 189 | struct sysfs_class *cls; |
|---|
| 190 | struct dlist *clsdevs; |
|---|
| 191 | struct sysfs_class_device *clsdev; |
|---|
| 192 | sensors_bus entry; |
|---|
| 193 | int ret = 0; |
|---|
| 194 | |
|---|
| 195 | if (!(cls = sysfs_open_class("i2c-adapter"))) { |
|---|
| 196 | if (errno && errno != ENOENT) |
|---|
| 197 | ret = -SENSORS_ERR_PROC; |
|---|
| 198 | goto exit0; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | if (!(clsdevs = sysfs_get_class_devices(cls))) { |
|---|
| 202 | if (errno && errno != ENOENT) |
|---|
| 203 | ret = -SENSORS_ERR_PROC; |
|---|
| 204 | goto exit1; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | dlist_for_each_data(clsdevs, clsdev, struct sysfs_class_device) { |
|---|
| 208 | struct sysfs_device *dev; |
|---|
| 209 | struct sysfs_attribute *attr; |
|---|
| 210 | |
|---|
| 211 | /* Get the adapter name from the classdev "name" attribute |
|---|
| 212 | * (Linux 2.6.20 and later). If it fails, fall back to |
|---|
| 213 | * the device "name" attribute (for older kernels). */ |
|---|
| 214 | if (!(attr = sysfs_get_classdev_attr(clsdev, "name")) |
|---|
| 215 | && !((dev = sysfs_get_classdev_device(clsdev)) && |
|---|
| 216 | (attr = sysfs_get_device_attr(dev, "name")))) |
|---|
| 217 | continue; |
|---|
| 218 | |
|---|
| 219 | /* NB: attr->value[attr->len-1] == '\n'; chop that off */ |
|---|
| 220 | entry.adapter = strndup(attr->value, attr->len - 1); |
|---|
| 221 | if (!entry.adapter) |
|---|
| 222 | sensors_fatal_error(__FUNCTION__, "out of memory"); |
|---|
| 223 | |
|---|
| 224 | if (!strncmp(entry.adapter, "ISA ", 4)) { |
|---|
| 225 | entry.number = SENSORS_CHIP_NAME_BUS_ISA; |
|---|
| 226 | } else if (sscanf(clsdev->name, "i2c-%d", &entry.number) != 1) { |
|---|
| 227 | entry.number = SENSORS_CHIP_NAME_BUS_DUMMY; |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | sensors_add_proc_bus(&entry); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | exit1: |
|---|
| 234 | /* this frees *cls _and_ *clsdevs */ |
|---|
| 235 | sysfs_close_class(cls); |
|---|
| 236 | |
|---|
| 237 | exit0: |
|---|
| 238 | return ret; |
|---|
| 239 | } |
|---|
| 240 | |
|---|