| 1 | // SPDX-License-Identifier: GPL-2.0-only | 
|---|
| 2 | /* | 
|---|
| 3 | * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring | 
|---|
| 4 | * | 
|---|
| 5 | * This file defines the sysfs class "hwmon", for use by sensors drivers. | 
|---|
| 6 | * | 
|---|
| 7 | * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com> | 
|---|
| 8 | */ | 
|---|
| 9 |  | 
|---|
| 10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | 
|---|
| 11 |  | 
|---|
| 12 | #include <linux/bitops.h> | 
|---|
| 13 | #include <linux/device.h> | 
|---|
| 14 | #include <linux/err.h> | 
|---|
| 15 | #include <linux/gfp.h> | 
|---|
| 16 | #include <linux/hwmon.h> | 
|---|
| 17 | #include <linux/i2c.h> | 
|---|
| 18 | #include <linux/idr.h> | 
|---|
| 19 | #include <linux/kstrtox.h> | 
|---|
| 20 | #include <linux/list.h> | 
|---|
| 21 | #include <linux/module.h> | 
|---|
| 22 | #include <linux/mutex.h> | 
|---|
| 23 | #include <linux/pci.h> | 
|---|
| 24 | #include <linux/property.h> | 
|---|
| 25 | #include <linux/slab.h> | 
|---|
| 26 | #include <linux/string.h> | 
|---|
| 27 | #include <linux/thermal.h> | 
|---|
| 28 |  | 
|---|
| 29 | #define CREATE_TRACE_POINTS | 
|---|
| 30 | #include <trace/events/hwmon.h> | 
|---|
| 31 |  | 
|---|
| 32 | #define HWMON_ID_PREFIX "hwmon" | 
|---|
| 33 | #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d" | 
|---|
| 34 |  | 
|---|
| 35 | struct hwmon_device { | 
|---|
| 36 | const char *name; | 
|---|
| 37 | const char *label; | 
|---|
| 38 | struct device dev; | 
|---|
| 39 | const struct hwmon_chip_info *chip; | 
|---|
| 40 | struct mutex lock; | 
|---|
| 41 | struct list_head tzdata; | 
|---|
| 42 | struct attribute_group group; | 
|---|
| 43 | const struct attribute_group **groups; | 
|---|
| 44 | }; | 
|---|
| 45 |  | 
|---|
| 46 | #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev) | 
|---|
| 47 |  | 
|---|
| 48 | #define MAX_SYSFS_ATTR_NAME_LENGTH	32 | 
|---|
| 49 |  | 
|---|
| 50 | struct hwmon_device_attribute { | 
|---|
| 51 | struct device_attribute dev_attr; | 
|---|
| 52 | const struct hwmon_ops *ops; | 
|---|
| 53 | enum hwmon_sensor_types type; | 
|---|
| 54 | u32 attr; | 
|---|
| 55 | int index; | 
|---|
| 56 | char name[MAX_SYSFS_ATTR_NAME_LENGTH]; | 
|---|
| 57 | }; | 
|---|
| 58 |  | 
|---|
| 59 | #define to_hwmon_attr(d) \ | 
|---|
| 60 | container_of(d, struct hwmon_device_attribute, dev_attr) | 
|---|
| 61 | #define to_dev_attr(a) container_of(a, struct device_attribute, attr) | 
|---|
| 62 |  | 
|---|
| 63 | /* | 
|---|
| 64 | * Thermal zone information | 
|---|
| 65 | */ | 
|---|
| 66 | struct hwmon_thermal_data { | 
|---|
| 67 | struct list_head node;		/* hwmon tzdata list entry */ | 
|---|
| 68 | struct device *dev;		/* Reference to hwmon device */ | 
|---|
| 69 | int index;			/* sensor index */ | 
|---|
| 70 | struct thermal_zone_device *tzd;/* thermal zone device */ | 
|---|
| 71 | }; | 
|---|
| 72 |  | 
|---|
| 73 | static ssize_t | 
|---|
| 74 | name_show(struct device *dev, struct device_attribute *attr, char *buf) | 
|---|
| 75 | { | 
|---|
| 76 | return sprintf(buf, fmt: "%s\n", to_hwmon_device(dev)->name); | 
|---|
| 77 | } | 
|---|
| 78 | static DEVICE_ATTR_RO(name); | 
|---|
| 79 |  | 
|---|
| 80 | static ssize_t | 
|---|
| 81 | label_show(struct device *dev, struct device_attribute *attr, char *buf) | 
|---|
| 82 | { | 
|---|
| 83 | return sysfs_emit(buf, fmt: "%s\n", to_hwmon_device(dev)->label); | 
|---|
| 84 | } | 
|---|
| 85 | static DEVICE_ATTR_RO(label); | 
|---|
| 86 |  | 
|---|
| 87 | static struct attribute *hwmon_dev_attrs[] = { | 
|---|
| 88 | &dev_attr_name.attr, | 
|---|
| 89 | &dev_attr_label.attr, | 
|---|
| 90 | NULL | 
|---|
| 91 | }; | 
|---|
| 92 |  | 
|---|
| 93 | static umode_t hwmon_dev_attr_is_visible(struct kobject *kobj, | 
|---|
| 94 | struct attribute *attr, int n) | 
|---|
| 95 | { | 
|---|
| 96 | struct device *dev = kobj_to_dev(kobj); | 
|---|
| 97 | struct hwmon_device *hdev = to_hwmon_device(dev); | 
|---|
| 98 |  | 
|---|
| 99 | if (attr == &dev_attr_name.attr && hdev->name == NULL) | 
|---|
| 100 | return 0; | 
|---|
| 101 |  | 
|---|
| 102 | if (attr == &dev_attr_label.attr && hdev->label == NULL) | 
|---|
| 103 | return 0; | 
|---|
| 104 |  | 
|---|
| 105 | return attr->mode; | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | static const struct attribute_group hwmon_dev_attr_group = { | 
|---|
| 109 | .attrs		= hwmon_dev_attrs, | 
|---|
| 110 | .is_visible	= hwmon_dev_attr_is_visible, | 
|---|
| 111 | }; | 
|---|
| 112 |  | 
|---|
| 113 | static const struct attribute_group *hwmon_dev_attr_groups[] = { | 
|---|
| 114 | &hwmon_dev_attr_group, | 
|---|
| 115 | NULL | 
|---|
| 116 | }; | 
|---|
| 117 |  | 
|---|
| 118 | static void hwmon_free_attrs(struct attribute **attrs) | 
|---|
| 119 | { | 
|---|
| 120 | int i; | 
|---|
| 121 |  | 
|---|
| 122 | for (i = 0; attrs[i]; i++) { | 
|---|
| 123 | struct device_attribute *dattr = to_dev_attr(attrs[i]); | 
|---|
| 124 | struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr); | 
|---|
| 125 |  | 
|---|
| 126 | kfree(objp: hattr); | 
|---|
| 127 | } | 
|---|
| 128 | kfree(objp: attrs); | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | static void hwmon_dev_release(struct device *dev) | 
|---|
| 132 | { | 
|---|
| 133 | struct hwmon_device *hwdev = to_hwmon_device(dev); | 
|---|
| 134 |  | 
|---|
| 135 | if (hwdev->group.attrs) | 
|---|
| 136 | hwmon_free_attrs(attrs: hwdev->group.attrs); | 
|---|
| 137 | kfree(objp: hwdev->groups); | 
|---|
| 138 | kfree(objp: hwdev->label); | 
|---|
| 139 | kfree(objp: hwdev); | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | static const struct class hwmon_class = { | 
|---|
| 143 | .name = "hwmon", | 
|---|
| 144 | .dev_groups = hwmon_dev_attr_groups, | 
|---|
| 145 | .dev_release = hwmon_dev_release, | 
|---|
| 146 | }; | 
|---|
| 147 |  | 
|---|
| 148 | static DEFINE_IDA(hwmon_ida); | 
|---|
| 149 |  | 
|---|
| 150 | static umode_t hwmon_is_visible(const struct hwmon_ops *ops, | 
|---|
| 151 | const void *drvdata, | 
|---|
| 152 | enum hwmon_sensor_types type, | 
|---|
| 153 | u32 attr, int channel) | 
|---|
| 154 | { | 
|---|
| 155 | if (ops->visible) | 
|---|
| 156 | return ops->visible; | 
|---|
| 157 |  | 
|---|
| 158 | return ops->is_visible(drvdata, type, attr, channel); | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | /* Thermal zone handling */ | 
|---|
| 162 |  | 
|---|
| 163 | static int hwmon_thermal_get_temp(struct thermal_zone_device *tz, int *temp) | 
|---|
| 164 | { | 
|---|
| 165 | struct hwmon_thermal_data *tdata = thermal_zone_device_priv(tzd: tz); | 
|---|
| 166 | struct hwmon_device *hwdev = to_hwmon_device(tdata->dev); | 
|---|
| 167 | int ret; | 
|---|
| 168 | long t; | 
|---|
| 169 |  | 
|---|
| 170 | guard(mutex)(T: &hwdev->lock); | 
|---|
| 171 |  | 
|---|
| 172 | ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input, | 
|---|
| 173 | tdata->index, &t); | 
|---|
| 174 | if (ret < 0) | 
|---|
| 175 | return ret; | 
|---|
| 176 |  | 
|---|
| 177 | *temp = t; | 
|---|
| 178 |  | 
|---|
| 179 | return 0; | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | static int hwmon_thermal_set_trips(struct thermal_zone_device *tz, int low, int high) | 
|---|
| 183 | { | 
|---|
| 184 | struct hwmon_thermal_data *tdata = thermal_zone_device_priv(tzd: tz); | 
|---|
| 185 | struct hwmon_device *hwdev = to_hwmon_device(tdata->dev); | 
|---|
| 186 | const struct hwmon_chip_info *chip = hwdev->chip; | 
|---|
| 187 | const struct hwmon_channel_info * const *info = chip->info; | 
|---|
| 188 | unsigned int i; | 
|---|
| 189 | int err; | 
|---|
| 190 |  | 
|---|
| 191 | if (!chip->ops->write) | 
|---|
| 192 | return 0; | 
|---|
| 193 |  | 
|---|
| 194 | for (i = 0; info[i] && info[i]->type != hwmon_temp; i++) | 
|---|
| 195 | continue; | 
|---|
| 196 |  | 
|---|
| 197 | if (!info[i]) | 
|---|
| 198 | return 0; | 
|---|
| 199 |  | 
|---|
| 200 | guard(mutex)(T: &hwdev->lock); | 
|---|
| 201 |  | 
|---|
| 202 | if (info[i]->config[tdata->index] & HWMON_T_MIN) { | 
|---|
| 203 | err = chip->ops->write(tdata->dev, hwmon_temp, | 
|---|
| 204 | hwmon_temp_min, tdata->index, low); | 
|---|
| 205 | if (err && err != -EOPNOTSUPP) | 
|---|
| 206 | return err; | 
|---|
| 207 | } | 
|---|
| 208 |  | 
|---|
| 209 | if (info[i]->config[tdata->index] & HWMON_T_MAX) { | 
|---|
| 210 | err = chip->ops->write(tdata->dev, hwmon_temp, | 
|---|
| 211 | hwmon_temp_max, tdata->index, high); | 
|---|
| 212 | if (err && err != -EOPNOTSUPP) | 
|---|
| 213 | return err; | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 | return 0; | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 | static const struct thermal_zone_device_ops hwmon_thermal_ops = { | 
|---|
| 220 | .get_temp = hwmon_thermal_get_temp, | 
|---|
| 221 | .set_trips = hwmon_thermal_set_trips, | 
|---|
| 222 | }; | 
|---|
| 223 |  | 
|---|
| 224 | static void hwmon_thermal_remove_sensor(void *data) | 
|---|
| 225 | { | 
|---|
| 226 | list_del(entry: data); | 
|---|
| 227 | } | 
|---|
| 228 |  | 
|---|
| 229 | static int hwmon_thermal_add_sensor(struct device *dev, int index) | 
|---|
| 230 | { | 
|---|
| 231 | struct hwmon_device *hwdev = to_hwmon_device(dev); | 
|---|
| 232 | struct hwmon_thermal_data *tdata; | 
|---|
| 233 | struct thermal_zone_device *tzd; | 
|---|
| 234 | int err; | 
|---|
| 235 |  | 
|---|
| 236 | tdata = devm_kzalloc(dev, size: sizeof(*tdata), GFP_KERNEL); | 
|---|
| 237 | if (!tdata) | 
|---|
| 238 | return -ENOMEM; | 
|---|
| 239 |  | 
|---|
| 240 | tdata->dev = dev; | 
|---|
| 241 | tdata->index = index; | 
|---|
| 242 |  | 
|---|
| 243 | tzd = devm_thermal_of_zone_register(dev, id: index, data: tdata, | 
|---|
| 244 | ops: &hwmon_thermal_ops); | 
|---|
| 245 | if (IS_ERR(ptr: tzd)) { | 
|---|
| 246 | if (PTR_ERR(ptr: tzd) != -ENODEV) | 
|---|
| 247 | return PTR_ERR(ptr: tzd); | 
|---|
| 248 | dev_info(dev, "temp%d_input not attached to any thermal zone\n", | 
|---|
| 249 | index + 1); | 
|---|
| 250 | devm_kfree(dev, p: tdata); | 
|---|
| 251 | return 0; | 
|---|
| 252 | } | 
|---|
| 253 |  | 
|---|
| 254 | err = devm_add_action(dev, hwmon_thermal_remove_sensor, &tdata->node); | 
|---|
| 255 | if (err) | 
|---|
| 256 | return err; | 
|---|
| 257 |  | 
|---|
| 258 | tdata->tzd = tzd; | 
|---|
| 259 | list_add(new: &tdata->node, head: &hwdev->tzdata); | 
|---|
| 260 |  | 
|---|
| 261 | return 0; | 
|---|
| 262 | } | 
|---|
| 263 |  | 
|---|
| 264 | static int hwmon_thermal_register_sensors(struct device *dev) | 
|---|
| 265 | { | 
|---|
| 266 | struct hwmon_device *hwdev = to_hwmon_device(dev); | 
|---|
| 267 | const struct hwmon_chip_info *chip = hwdev->chip; | 
|---|
| 268 | const struct hwmon_channel_info * const *info = chip->info; | 
|---|
| 269 | void *drvdata = dev_get_drvdata(dev); | 
|---|
| 270 | int i; | 
|---|
| 271 |  | 
|---|
| 272 | if (!IS_ENABLED(CONFIG_THERMAL_OF)) | 
|---|
| 273 | return 0; | 
|---|
| 274 |  | 
|---|
| 275 | for (i = 1; info[i]; i++) { | 
|---|
| 276 | int j; | 
|---|
| 277 |  | 
|---|
| 278 | if (info[i]->type != hwmon_temp) | 
|---|
| 279 | continue; | 
|---|
| 280 |  | 
|---|
| 281 | for (j = 0; info[i]->config[j]; j++) { | 
|---|
| 282 | int err; | 
|---|
| 283 |  | 
|---|
| 284 | if (!(info[i]->config[j] & HWMON_T_INPUT) || | 
|---|
| 285 | !hwmon_is_visible(ops: chip->ops, drvdata, type: hwmon_temp, | 
|---|
| 286 | attr: hwmon_temp_input, channel: j)) | 
|---|
| 287 | continue; | 
|---|
| 288 |  | 
|---|
| 289 | err = hwmon_thermal_add_sensor(dev, index: j); | 
|---|
| 290 | if (err) | 
|---|
| 291 | return err; | 
|---|
| 292 | } | 
|---|
| 293 | } | 
|---|
| 294 |  | 
|---|
| 295 | return 0; | 
|---|
| 296 | } | 
|---|
| 297 |  | 
|---|
| 298 | static void hwmon_thermal_notify(struct device *dev, int index) | 
|---|
| 299 | { | 
|---|
| 300 | struct hwmon_device *hwdev = to_hwmon_device(dev); | 
|---|
| 301 | struct hwmon_thermal_data *tzdata; | 
|---|
| 302 |  | 
|---|
| 303 | if (!IS_ENABLED(CONFIG_THERMAL_OF)) | 
|---|
| 304 | return; | 
|---|
| 305 |  | 
|---|
| 306 | list_for_each_entry(tzdata, &hwdev->tzdata, node) { | 
|---|
| 307 | if (tzdata->index == index) { | 
|---|
| 308 | thermal_zone_device_update(tzdata->tzd, | 
|---|
| 309 | THERMAL_EVENT_UNSPECIFIED); | 
|---|
| 310 | } | 
|---|
| 311 | } | 
|---|
| 312 | } | 
|---|
| 313 |  | 
|---|
| 314 | static int hwmon_attr_base(enum hwmon_sensor_types type) | 
|---|
| 315 | { | 
|---|
| 316 | if (type == hwmon_in || type == hwmon_intrusion) | 
|---|
| 317 | return 0; | 
|---|
| 318 | return 1; | 
|---|
| 319 | } | 
|---|
| 320 |  | 
|---|
| 321 | #if IS_REACHABLE(CONFIG_I2C) | 
|---|
| 322 |  | 
|---|
| 323 | /* | 
|---|
| 324 | * PEC support | 
|---|
| 325 | * | 
|---|
| 326 | * The 'pec' attribute is attached to I2C client devices. It is only provided | 
|---|
| 327 | * if the i2c controller supports PEC. | 
|---|
| 328 | * | 
|---|
| 329 | * The mutex ensures that PEC configuration between i2c device and the hardware | 
|---|
| 330 | * is consistent. Use a single mutex because attribute writes are supposed to be | 
|---|
| 331 | * rare, and maintaining a separate mutex for each hardware monitoring device | 
|---|
| 332 | * would add substantial complexity to the driver for little if any gain. | 
|---|
| 333 | * | 
|---|
| 334 | * The hardware monitoring device is identified as child of the i2c client | 
|---|
| 335 | * device. This assumes that only a single hardware monitoring device is | 
|---|
| 336 | * attached to an i2c client device. | 
|---|
| 337 | */ | 
|---|
| 338 |  | 
|---|
| 339 | static int hwmon_match_device(struct device *dev, const void *data) | 
|---|
| 340 | { | 
|---|
| 341 | return dev->class == &hwmon_class; | 
|---|
| 342 | } | 
|---|
| 343 |  | 
|---|
| 344 | static ssize_t pec_show(struct device *dev, struct device_attribute *dummy, | 
|---|
| 345 | char *buf) | 
|---|
| 346 | { | 
|---|
| 347 | struct i2c_client *client = to_i2c_client(dev); | 
|---|
| 348 |  | 
|---|
| 349 | return sysfs_emit(buf, fmt: "%d\n", !!(client->flags & I2C_CLIENT_PEC)); | 
|---|
| 350 | } | 
|---|
| 351 |  | 
|---|
| 352 | static ssize_t pec_store(struct device *dev, struct device_attribute *devattr, | 
|---|
| 353 | const char *buf, size_t count) | 
|---|
| 354 | { | 
|---|
| 355 | struct i2c_client *client = to_i2c_client(dev); | 
|---|
| 356 | struct hwmon_device *hwdev; | 
|---|
| 357 | struct device *hdev; | 
|---|
| 358 | bool val; | 
|---|
| 359 | int err; | 
|---|
| 360 |  | 
|---|
| 361 | err = kstrtobool(s: buf, res: &val); | 
|---|
| 362 | if (err < 0) | 
|---|
| 363 | return err; | 
|---|
| 364 |  | 
|---|
| 365 | hdev = device_find_child(parent: dev, NULL, match: hwmon_match_device); | 
|---|
| 366 | if (!hdev) | 
|---|
| 367 | return -ENODEV; | 
|---|
| 368 |  | 
|---|
| 369 | /* | 
|---|
| 370 | * If there is no write function, we assume that chip specific | 
|---|
| 371 | * handling is not required. | 
|---|
| 372 | */ | 
|---|
| 373 | hwdev = to_hwmon_device(hdev); | 
|---|
| 374 | guard(mutex)(T: &hwdev->lock); | 
|---|
| 375 | if (hwdev->chip->ops->write) { | 
|---|
| 376 | err = hwdev->chip->ops->write(hdev, hwmon_chip, hwmon_chip_pec, 0, val); | 
|---|
| 377 | if (err && err != -EOPNOTSUPP) | 
|---|
| 378 | goto put; | 
|---|
| 379 | } | 
|---|
| 380 |  | 
|---|
| 381 | if (!val) | 
|---|
| 382 | client->flags &= ~I2C_CLIENT_PEC; | 
|---|
| 383 | else | 
|---|
| 384 | client->flags |= I2C_CLIENT_PEC; | 
|---|
| 385 |  | 
|---|
| 386 | err = count; | 
|---|
| 387 | put: | 
|---|
| 388 | put_device(dev: hdev); | 
|---|
| 389 |  | 
|---|
| 390 | return err; | 
|---|
| 391 | } | 
|---|
| 392 |  | 
|---|
| 393 | static DEVICE_ATTR_RW(pec); | 
|---|
| 394 |  | 
|---|
| 395 | static void hwmon_remove_pec(void *dev) | 
|---|
| 396 | { | 
|---|
| 397 | device_remove_file(dev, attr: &dev_attr_pec); | 
|---|
| 398 | } | 
|---|
| 399 |  | 
|---|
| 400 | static int hwmon_pec_register(struct device *hdev) | 
|---|
| 401 | { | 
|---|
| 402 | struct i2c_client *client = i2c_verify_client(dev: hdev->parent); | 
|---|
| 403 | int err; | 
|---|
| 404 |  | 
|---|
| 405 | if (!client) | 
|---|
| 406 | return -EINVAL; | 
|---|
| 407 |  | 
|---|
| 408 | if (!i2c_check_functionality(adap: client->adapter, I2C_FUNC_SMBUS_PEC)) | 
|---|
| 409 | return 0; | 
|---|
| 410 |  | 
|---|
| 411 | err = device_create_file(device: &client->dev, entry: &dev_attr_pec); | 
|---|
| 412 | if (err) | 
|---|
| 413 | return err; | 
|---|
| 414 |  | 
|---|
| 415 | return devm_add_action_or_reset(hdev, hwmon_remove_pec, &client->dev); | 
|---|
| 416 | } | 
|---|
| 417 |  | 
|---|
| 418 | #else /* CONFIG_I2C */ | 
|---|
| 419 | static int hwmon_pec_register(struct device *hdev) | 
|---|
| 420 | { | 
|---|
| 421 | return -EINVAL; | 
|---|
| 422 | } | 
|---|
| 423 | #endif /* CONFIG_I2C */ | 
|---|
| 424 |  | 
|---|
| 425 | /* sysfs attribute management */ | 
|---|
| 426 |  | 
|---|
| 427 | static ssize_t hwmon_attr_show(struct device *dev, | 
|---|
| 428 | struct device_attribute *devattr, char *buf) | 
|---|
| 429 | { | 
|---|
| 430 | struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); | 
|---|
| 431 | struct hwmon_device *hwdev = to_hwmon_device(dev); | 
|---|
| 432 | s64 val64; | 
|---|
| 433 | long val; | 
|---|
| 434 | int ret; | 
|---|
| 435 |  | 
|---|
| 436 | guard(mutex)(T: &hwdev->lock); | 
|---|
| 437 |  | 
|---|
| 438 | ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index, | 
|---|
| 439 | (hattr->type == hwmon_energy64) ? (long *)&val64 : &val); | 
|---|
| 440 | if (ret < 0) | 
|---|
| 441 | return ret; | 
|---|
| 442 |  | 
|---|
| 443 | if (hattr->type != hwmon_energy64) | 
|---|
| 444 | val64 = val; | 
|---|
| 445 |  | 
|---|
| 446 | trace_hwmon_attr_show(index: hattr->index + hwmon_attr_base(type: hattr->type), | 
|---|
| 447 | attr_name: hattr->name, val: val64); | 
|---|
| 448 |  | 
|---|
| 449 | return sprintf(buf, fmt: "%lld\n", val64); | 
|---|
| 450 | } | 
|---|
| 451 |  | 
|---|
| 452 | static ssize_t hwmon_attr_show_string(struct device *dev, | 
|---|
| 453 | struct device_attribute *devattr, | 
|---|
| 454 | char *buf) | 
|---|
| 455 | { | 
|---|
| 456 | struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); | 
|---|
| 457 | struct hwmon_device *hwdev = to_hwmon_device(dev); | 
|---|
| 458 | enum hwmon_sensor_types type = hattr->type; | 
|---|
| 459 | const char *s; | 
|---|
| 460 | int ret; | 
|---|
| 461 |  | 
|---|
| 462 | guard(mutex)(T: &hwdev->lock); | 
|---|
| 463 |  | 
|---|
| 464 | ret = hattr->ops->read_string(dev, hattr->type, hattr->attr, | 
|---|
| 465 | hattr->index, &s); | 
|---|
| 466 | if (ret < 0) | 
|---|
| 467 | return ret; | 
|---|
| 468 |  | 
|---|
| 469 | trace_hwmon_attr_show_string(index: hattr->index + hwmon_attr_base(type), | 
|---|
| 470 | attr_name: hattr->name, s); | 
|---|
| 471 |  | 
|---|
| 472 | return sprintf(buf, fmt: "%s\n", s); | 
|---|
| 473 | } | 
|---|
| 474 |  | 
|---|
| 475 | static ssize_t hwmon_attr_store(struct device *dev, | 
|---|
| 476 | struct device_attribute *devattr, | 
|---|
| 477 | const char *buf, size_t count) | 
|---|
| 478 | { | 
|---|
| 479 | struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); | 
|---|
| 480 | struct hwmon_device *hwdev = to_hwmon_device(dev); | 
|---|
| 481 | long val; | 
|---|
| 482 | int ret; | 
|---|
| 483 |  | 
|---|
| 484 | ret = kstrtol(s: buf, base: 10, res: &val); | 
|---|
| 485 | if (ret < 0) | 
|---|
| 486 | return ret; | 
|---|
| 487 |  | 
|---|
| 488 | guard(mutex)(T: &hwdev->lock); | 
|---|
| 489 |  | 
|---|
| 490 | ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index, | 
|---|
| 491 | val); | 
|---|
| 492 | if (ret < 0) | 
|---|
| 493 | return ret; | 
|---|
| 494 |  | 
|---|
| 495 | trace_hwmon_attr_store(index: hattr->index + hwmon_attr_base(type: hattr->type), | 
|---|
| 496 | attr_name: hattr->name, val: (s64)val); | 
|---|
| 497 |  | 
|---|
| 498 | return count; | 
|---|
| 499 | } | 
|---|
| 500 |  | 
|---|
| 501 | static bool is_string_attr(enum hwmon_sensor_types type, u32 attr) | 
|---|
| 502 | { | 
|---|
| 503 | return (type == hwmon_temp && attr == hwmon_temp_label) || | 
|---|
| 504 | (type == hwmon_in && attr == hwmon_in_label) || | 
|---|
| 505 | (type == hwmon_curr && attr == hwmon_curr_label) || | 
|---|
| 506 | (type == hwmon_power && attr == hwmon_power_label) || | 
|---|
| 507 | (type == hwmon_energy && attr == hwmon_energy_label) || | 
|---|
| 508 | (type == hwmon_humidity && attr == hwmon_humidity_label) || | 
|---|
| 509 | (type == hwmon_fan && attr == hwmon_fan_label); | 
|---|
| 510 | } | 
|---|
| 511 |  | 
|---|
| 512 | static struct attribute *hwmon_genattr(const void *drvdata, | 
|---|
| 513 | enum hwmon_sensor_types type, | 
|---|
| 514 | u32 attr, | 
|---|
| 515 | int index, | 
|---|
| 516 | const char *template, | 
|---|
| 517 | const struct hwmon_ops *ops) | 
|---|
| 518 | { | 
|---|
| 519 | struct hwmon_device_attribute *hattr; | 
|---|
| 520 | struct device_attribute *dattr; | 
|---|
| 521 | struct attribute *a; | 
|---|
| 522 | umode_t mode; | 
|---|
| 523 | const char *name; | 
|---|
| 524 | bool is_string = is_string_attr(type, attr); | 
|---|
| 525 |  | 
|---|
| 526 | mode = hwmon_is_visible(ops, drvdata, type, attr, channel: index); | 
|---|
| 527 | if (!mode) | 
|---|
| 528 | return ERR_PTR(error: -ENOENT); | 
|---|
| 529 |  | 
|---|
| 530 | if ((mode & 0444) && ((is_string && !ops->read_string) || | 
|---|
| 531 | (!is_string && !ops->read))) | 
|---|
| 532 | return ERR_PTR(error: -EINVAL); | 
|---|
| 533 | if ((mode & 0222) && !ops->write) | 
|---|
| 534 | return ERR_PTR(error: -EINVAL); | 
|---|
| 535 |  | 
|---|
| 536 | hattr = kzalloc(sizeof(*hattr), GFP_KERNEL); | 
|---|
| 537 | if (!hattr) | 
|---|
| 538 | return ERR_PTR(error: -ENOMEM); | 
|---|
| 539 |  | 
|---|
| 540 | if (type == hwmon_chip) { | 
|---|
| 541 | name = template; | 
|---|
| 542 | } else { | 
|---|
| 543 | scnprintf(buf: hattr->name, size: sizeof(hattr->name), fmt: template, | 
|---|
| 544 | index + hwmon_attr_base(type)); | 
|---|
| 545 | name = hattr->name; | 
|---|
| 546 | } | 
|---|
| 547 |  | 
|---|
| 548 | hattr->type = type; | 
|---|
| 549 | hattr->attr = attr; | 
|---|
| 550 | hattr->index = index; | 
|---|
| 551 | hattr->ops = ops; | 
|---|
| 552 |  | 
|---|
| 553 | dattr = &hattr->dev_attr; | 
|---|
| 554 | dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show; | 
|---|
| 555 | dattr->store = hwmon_attr_store; | 
|---|
| 556 |  | 
|---|
| 557 | a = &dattr->attr; | 
|---|
| 558 | sysfs_attr_init(a); | 
|---|
| 559 | a->name = name; | 
|---|
| 560 | a->mode = mode; | 
|---|
| 561 |  | 
|---|
| 562 | return a; | 
|---|
| 563 | } | 
|---|
| 564 |  | 
|---|
| 565 | /* | 
|---|
| 566 | * Chip attributes are not attribute templates but actual sysfs attributes. | 
|---|
| 567 | * See hwmon_genattr() for special handling. | 
|---|
| 568 | */ | 
|---|
| 569 | static const char * const hwmon_chip_attrs[] = { | 
|---|
| 570 | [hwmon_chip_temp_reset_history] = "temp_reset_history", | 
|---|
| 571 | [hwmon_chip_in_reset_history] = "in_reset_history", | 
|---|
| 572 | [hwmon_chip_curr_reset_history] = "curr_reset_history", | 
|---|
| 573 | [hwmon_chip_power_reset_history] = "power_reset_history", | 
|---|
| 574 | [hwmon_chip_update_interval] = "update_interval", | 
|---|
| 575 | [hwmon_chip_alarms] = "alarms", | 
|---|
| 576 | [hwmon_chip_samples] = "samples", | 
|---|
| 577 | [hwmon_chip_curr_samples] = "curr_samples", | 
|---|
| 578 | [hwmon_chip_in_samples] = "in_samples", | 
|---|
| 579 | [hwmon_chip_power_samples] = "power_samples", | 
|---|
| 580 | [hwmon_chip_temp_samples] = "temp_samples", | 
|---|
| 581 | [hwmon_chip_beep_enable] = "beep_enable", | 
|---|
| 582 | }; | 
|---|
| 583 |  | 
|---|
| 584 | static const char * const hwmon_temp_attr_templates[] = { | 
|---|
| 585 | [hwmon_temp_enable] = "temp%d_enable", | 
|---|
| 586 | [hwmon_temp_input] = "temp%d_input", | 
|---|
| 587 | [hwmon_temp_type] = "temp%d_type", | 
|---|
| 588 | [hwmon_temp_lcrit] = "temp%d_lcrit", | 
|---|
| 589 | [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst", | 
|---|
| 590 | [hwmon_temp_min] = "temp%d_min", | 
|---|
| 591 | [hwmon_temp_min_hyst] = "temp%d_min_hyst", | 
|---|
| 592 | [hwmon_temp_max] = "temp%d_max", | 
|---|
| 593 | [hwmon_temp_max_hyst] = "temp%d_max_hyst", | 
|---|
| 594 | [hwmon_temp_crit] = "temp%d_crit", | 
|---|
| 595 | [hwmon_temp_crit_hyst] = "temp%d_crit_hyst", | 
|---|
| 596 | [hwmon_temp_emergency] = "temp%d_emergency", | 
|---|
| 597 | [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst", | 
|---|
| 598 | [hwmon_temp_alarm] = "temp%d_alarm", | 
|---|
| 599 | [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm", | 
|---|
| 600 | [hwmon_temp_min_alarm] = "temp%d_min_alarm", | 
|---|
| 601 | [hwmon_temp_max_alarm] = "temp%d_max_alarm", | 
|---|
| 602 | [hwmon_temp_crit_alarm] = "temp%d_crit_alarm", | 
|---|
| 603 | [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm", | 
|---|
| 604 | [hwmon_temp_fault] = "temp%d_fault", | 
|---|
| 605 | [hwmon_temp_offset] = "temp%d_offset", | 
|---|
| 606 | [hwmon_temp_label] = "temp%d_label", | 
|---|
| 607 | [hwmon_temp_lowest] = "temp%d_lowest", | 
|---|
| 608 | [hwmon_temp_highest] = "temp%d_highest", | 
|---|
| 609 | [hwmon_temp_reset_history] = "temp%d_reset_history", | 
|---|
| 610 | [hwmon_temp_rated_min] = "temp%d_rated_min", | 
|---|
| 611 | [hwmon_temp_rated_max] = "temp%d_rated_max", | 
|---|
| 612 | [hwmon_temp_beep] = "temp%d_beep", | 
|---|
| 613 | }; | 
|---|
| 614 |  | 
|---|
| 615 | static const char * const hwmon_in_attr_templates[] = { | 
|---|
| 616 | [hwmon_in_enable] = "in%d_enable", | 
|---|
| 617 | [hwmon_in_input] = "in%d_input", | 
|---|
| 618 | [hwmon_in_min] = "in%d_min", | 
|---|
| 619 | [hwmon_in_max] = "in%d_max", | 
|---|
| 620 | [hwmon_in_lcrit] = "in%d_lcrit", | 
|---|
| 621 | [hwmon_in_crit] = "in%d_crit", | 
|---|
| 622 | [hwmon_in_average] = "in%d_average", | 
|---|
| 623 | [hwmon_in_lowest] = "in%d_lowest", | 
|---|
| 624 | [hwmon_in_highest] = "in%d_highest", | 
|---|
| 625 | [hwmon_in_reset_history] = "in%d_reset_history", | 
|---|
| 626 | [hwmon_in_label] = "in%d_label", | 
|---|
| 627 | [hwmon_in_alarm] = "in%d_alarm", | 
|---|
| 628 | [hwmon_in_min_alarm] = "in%d_min_alarm", | 
|---|
| 629 | [hwmon_in_max_alarm] = "in%d_max_alarm", | 
|---|
| 630 | [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm", | 
|---|
| 631 | [hwmon_in_crit_alarm] = "in%d_crit_alarm", | 
|---|
| 632 | [hwmon_in_rated_min] = "in%d_rated_min", | 
|---|
| 633 | [hwmon_in_rated_max] = "in%d_rated_max", | 
|---|
| 634 | [hwmon_in_beep] = "in%d_beep", | 
|---|
| 635 | [hwmon_in_fault] = "in%d_fault", | 
|---|
| 636 | }; | 
|---|
| 637 |  | 
|---|
| 638 | static const char * const hwmon_curr_attr_templates[] = { | 
|---|
| 639 | [hwmon_curr_enable] = "curr%d_enable", | 
|---|
| 640 | [hwmon_curr_input] = "curr%d_input", | 
|---|
| 641 | [hwmon_curr_min] = "curr%d_min", | 
|---|
| 642 | [hwmon_curr_max] = "curr%d_max", | 
|---|
| 643 | [hwmon_curr_lcrit] = "curr%d_lcrit", | 
|---|
| 644 | [hwmon_curr_crit] = "curr%d_crit", | 
|---|
| 645 | [hwmon_curr_average] = "curr%d_average", | 
|---|
| 646 | [hwmon_curr_lowest] = "curr%d_lowest", | 
|---|
| 647 | [hwmon_curr_highest] = "curr%d_highest", | 
|---|
| 648 | [hwmon_curr_reset_history] = "curr%d_reset_history", | 
|---|
| 649 | [hwmon_curr_label] = "curr%d_label", | 
|---|
| 650 | [hwmon_curr_alarm] = "curr%d_alarm", | 
|---|
| 651 | [hwmon_curr_min_alarm] = "curr%d_min_alarm", | 
|---|
| 652 | [hwmon_curr_max_alarm] = "curr%d_max_alarm", | 
|---|
| 653 | [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm", | 
|---|
| 654 | [hwmon_curr_crit_alarm] = "curr%d_crit_alarm", | 
|---|
| 655 | [hwmon_curr_rated_min] = "curr%d_rated_min", | 
|---|
| 656 | [hwmon_curr_rated_max] = "curr%d_rated_max", | 
|---|
| 657 | [hwmon_curr_beep] = "curr%d_beep", | 
|---|
| 658 | }; | 
|---|
| 659 |  | 
|---|
| 660 | static const char * const hwmon_power_attr_templates[] = { | 
|---|
| 661 | [hwmon_power_enable] = "power%d_enable", | 
|---|
| 662 | [hwmon_power_average] = "power%d_average", | 
|---|
| 663 | [hwmon_power_average_interval] = "power%d_average_interval", | 
|---|
| 664 | [hwmon_power_average_interval_max] = "power%d_average_interval_max", | 
|---|
| 665 | [hwmon_power_average_interval_min] = "power%d_average_interval_min", | 
|---|
| 666 | [hwmon_power_average_highest] = "power%d_average_highest", | 
|---|
| 667 | [hwmon_power_average_lowest] = "power%d_average_lowest", | 
|---|
| 668 | [hwmon_power_average_max] = "power%d_average_max", | 
|---|
| 669 | [hwmon_power_average_min] = "power%d_average_min", | 
|---|
| 670 | [hwmon_power_input] = "power%d_input", | 
|---|
| 671 | [hwmon_power_input_highest] = "power%d_input_highest", | 
|---|
| 672 | [hwmon_power_input_lowest] = "power%d_input_lowest", | 
|---|
| 673 | [hwmon_power_reset_history] = "power%d_reset_history", | 
|---|
| 674 | [hwmon_power_accuracy] = "power%d_accuracy", | 
|---|
| 675 | [hwmon_power_cap] = "power%d_cap", | 
|---|
| 676 | [hwmon_power_cap_hyst] = "power%d_cap_hyst", | 
|---|
| 677 | [hwmon_power_cap_max] = "power%d_cap_max", | 
|---|
| 678 | [hwmon_power_cap_min] = "power%d_cap_min", | 
|---|
| 679 | [hwmon_power_min] = "power%d_min", | 
|---|
| 680 | [hwmon_power_max] = "power%d_max", | 
|---|
| 681 | [hwmon_power_lcrit] = "power%d_lcrit", | 
|---|
| 682 | [hwmon_power_crit] = "power%d_crit", | 
|---|
| 683 | [hwmon_power_label] = "power%d_label", | 
|---|
| 684 | [hwmon_power_alarm] = "power%d_alarm", | 
|---|
| 685 | [hwmon_power_cap_alarm] = "power%d_cap_alarm", | 
|---|
| 686 | [hwmon_power_min_alarm] = "power%d_min_alarm", | 
|---|
| 687 | [hwmon_power_max_alarm] = "power%d_max_alarm", | 
|---|
| 688 | [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm", | 
|---|
| 689 | [hwmon_power_crit_alarm] = "power%d_crit_alarm", | 
|---|
| 690 | [hwmon_power_rated_min] = "power%d_rated_min", | 
|---|
| 691 | [hwmon_power_rated_max] = "power%d_rated_max", | 
|---|
| 692 | }; | 
|---|
| 693 |  | 
|---|
| 694 | static const char * const hwmon_energy_attr_templates[] = { | 
|---|
| 695 | [hwmon_energy_enable] = "energy%d_enable", | 
|---|
| 696 | [hwmon_energy_input] = "energy%d_input", | 
|---|
| 697 | [hwmon_energy_label] = "energy%d_label", | 
|---|
| 698 | }; | 
|---|
| 699 |  | 
|---|
| 700 | static const char * const hwmon_humidity_attr_templates[] = { | 
|---|
| 701 | [hwmon_humidity_enable] = "humidity%d_enable", | 
|---|
| 702 | [hwmon_humidity_input] = "humidity%d_input", | 
|---|
| 703 | [hwmon_humidity_label] = "humidity%d_label", | 
|---|
| 704 | [hwmon_humidity_min] = "humidity%d_min", | 
|---|
| 705 | [hwmon_humidity_min_hyst] = "humidity%d_min_hyst", | 
|---|
| 706 | [hwmon_humidity_max] = "humidity%d_max", | 
|---|
| 707 | [hwmon_humidity_max_hyst] = "humidity%d_max_hyst", | 
|---|
| 708 | [hwmon_humidity_alarm] = "humidity%d_alarm", | 
|---|
| 709 | [hwmon_humidity_fault] = "humidity%d_fault", | 
|---|
| 710 | [hwmon_humidity_rated_min] = "humidity%d_rated_min", | 
|---|
| 711 | [hwmon_humidity_rated_max] = "humidity%d_rated_max", | 
|---|
| 712 | [hwmon_humidity_min_alarm] = "humidity%d_min_alarm", | 
|---|
| 713 | [hwmon_humidity_max_alarm] = "humidity%d_max_alarm", | 
|---|
| 714 | }; | 
|---|
| 715 |  | 
|---|
| 716 | static const char * const hwmon_fan_attr_templates[] = { | 
|---|
| 717 | [hwmon_fan_enable] = "fan%d_enable", | 
|---|
| 718 | [hwmon_fan_input] = "fan%d_input", | 
|---|
| 719 | [hwmon_fan_label] = "fan%d_label", | 
|---|
| 720 | [hwmon_fan_min] = "fan%d_min", | 
|---|
| 721 | [hwmon_fan_max] = "fan%d_max", | 
|---|
| 722 | [hwmon_fan_div] = "fan%d_div", | 
|---|
| 723 | [hwmon_fan_pulses] = "fan%d_pulses", | 
|---|
| 724 | [hwmon_fan_target] = "fan%d_target", | 
|---|
| 725 | [hwmon_fan_alarm] = "fan%d_alarm", | 
|---|
| 726 | [hwmon_fan_min_alarm] = "fan%d_min_alarm", | 
|---|
| 727 | [hwmon_fan_max_alarm] = "fan%d_max_alarm", | 
|---|
| 728 | [hwmon_fan_fault] = "fan%d_fault", | 
|---|
| 729 | [hwmon_fan_beep] = "fan%d_beep", | 
|---|
| 730 | }; | 
|---|
| 731 |  | 
|---|
| 732 | static const char * const hwmon_pwm_attr_templates[] = { | 
|---|
| 733 | [hwmon_pwm_input] = "pwm%d", | 
|---|
| 734 | [hwmon_pwm_enable] = "pwm%d_enable", | 
|---|
| 735 | [hwmon_pwm_mode] = "pwm%d_mode", | 
|---|
| 736 | [hwmon_pwm_freq] = "pwm%d_freq", | 
|---|
| 737 | [hwmon_pwm_auto_channels_temp] = "pwm%d_auto_channels_temp", | 
|---|
| 738 | }; | 
|---|
| 739 |  | 
|---|
| 740 | static const char * const hwmon_intrusion_attr_templates[] = { | 
|---|
| 741 | [hwmon_intrusion_alarm] = "intrusion%d_alarm", | 
|---|
| 742 | [hwmon_intrusion_beep]  = "intrusion%d_beep", | 
|---|
| 743 | }; | 
|---|
| 744 |  | 
|---|
| 745 | static const char * const *__templates[] = { | 
|---|
| 746 | [hwmon_chip] = hwmon_chip_attrs, | 
|---|
| 747 | [hwmon_temp] = hwmon_temp_attr_templates, | 
|---|
| 748 | [hwmon_in] = hwmon_in_attr_templates, | 
|---|
| 749 | [hwmon_curr] = hwmon_curr_attr_templates, | 
|---|
| 750 | [hwmon_power] = hwmon_power_attr_templates, | 
|---|
| 751 | [hwmon_energy] = hwmon_energy_attr_templates, | 
|---|
| 752 | [hwmon_energy64] = hwmon_energy_attr_templates, | 
|---|
| 753 | [hwmon_humidity] = hwmon_humidity_attr_templates, | 
|---|
| 754 | [hwmon_fan] = hwmon_fan_attr_templates, | 
|---|
| 755 | [hwmon_pwm] = hwmon_pwm_attr_templates, | 
|---|
| 756 | [hwmon_intrusion] = hwmon_intrusion_attr_templates, | 
|---|
| 757 | }; | 
|---|
| 758 |  | 
|---|
| 759 | static const int __templates_size[] = { | 
|---|
| 760 | [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs), | 
|---|
| 761 | [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates), | 
|---|
| 762 | [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates), | 
|---|
| 763 | [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates), | 
|---|
| 764 | [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates), | 
|---|
| 765 | [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates), | 
|---|
| 766 | [hwmon_energy64] = ARRAY_SIZE(hwmon_energy_attr_templates), | 
|---|
| 767 | [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates), | 
|---|
| 768 | [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates), | 
|---|
| 769 | [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates), | 
|---|
| 770 | [hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates), | 
|---|
| 771 | }; | 
|---|
| 772 |  | 
|---|
| 773 | int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type, | 
|---|
| 774 | u32 attr, int channel) | 
|---|
| 775 | { | 
|---|
| 776 | char event[MAX_SYSFS_ATTR_NAME_LENGTH + 5]; | 
|---|
| 777 | char sattr[MAX_SYSFS_ATTR_NAME_LENGTH]; | 
|---|
| 778 | char *envp[] = { event, NULL }; | 
|---|
| 779 | const char * const *templates; | 
|---|
| 780 | const char *template; | 
|---|
| 781 | int base; | 
|---|
| 782 |  | 
|---|
| 783 | if (type >= ARRAY_SIZE(__templates)) | 
|---|
| 784 | return -EINVAL; | 
|---|
| 785 | if (attr >= __templates_size[type]) | 
|---|
| 786 | return -EINVAL; | 
|---|
| 787 |  | 
|---|
| 788 | templates = __templates[type]; | 
|---|
| 789 | template = templates[attr]; | 
|---|
| 790 |  | 
|---|
| 791 | base = hwmon_attr_base(type); | 
|---|
| 792 |  | 
|---|
| 793 | scnprintf(buf: sattr, MAX_SYSFS_ATTR_NAME_LENGTH, fmt: template, base + channel); | 
|---|
| 794 | scnprintf(buf: event, size: sizeof(event), fmt: "NAME=%s", sattr); | 
|---|
| 795 | sysfs_notify(kobj: &dev->kobj, NULL, attr: sattr); | 
|---|
| 796 | kobject_uevent_env(kobj: &dev->kobj, action: KOBJ_CHANGE, envp); | 
|---|
| 797 |  | 
|---|
| 798 | if (type == hwmon_temp) | 
|---|
| 799 | hwmon_thermal_notify(dev, index: channel); | 
|---|
| 800 |  | 
|---|
| 801 | return 0; | 
|---|
| 802 | } | 
|---|
| 803 | EXPORT_SYMBOL_GPL(hwmon_notify_event); | 
|---|
| 804 |  | 
|---|
| 805 | void hwmon_lock(struct device *dev) | 
|---|
| 806 | { | 
|---|
| 807 | struct hwmon_device *hwdev = to_hwmon_device(dev); | 
|---|
| 808 |  | 
|---|
| 809 | mutex_lock(lock: &hwdev->lock); | 
|---|
| 810 | } | 
|---|
| 811 | EXPORT_SYMBOL_GPL(hwmon_lock); | 
|---|
| 812 |  | 
|---|
| 813 | void hwmon_unlock(struct device *dev) | 
|---|
| 814 | { | 
|---|
| 815 | struct hwmon_device *hwdev = to_hwmon_device(dev); | 
|---|
| 816 |  | 
|---|
| 817 | mutex_unlock(lock: &hwdev->lock); | 
|---|
| 818 | } | 
|---|
| 819 | EXPORT_SYMBOL_GPL(hwmon_unlock); | 
|---|
| 820 |  | 
|---|
| 821 | static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info) | 
|---|
| 822 | { | 
|---|
| 823 | int i, n; | 
|---|
| 824 |  | 
|---|
| 825 | for (i = n = 0; info->config[i]; i++) | 
|---|
| 826 | n += hweight32(info->config[i]); | 
|---|
| 827 |  | 
|---|
| 828 | return n; | 
|---|
| 829 | } | 
|---|
| 830 |  | 
|---|
| 831 | static int hwmon_genattrs(const void *drvdata, | 
|---|
| 832 | struct attribute **attrs, | 
|---|
| 833 | const struct hwmon_ops *ops, | 
|---|
| 834 | const struct hwmon_channel_info *info) | 
|---|
| 835 | { | 
|---|
| 836 | const char * const *templates; | 
|---|
| 837 | int template_size; | 
|---|
| 838 | int i, aindex = 0; | 
|---|
| 839 |  | 
|---|
| 840 | if (info->type >= ARRAY_SIZE(__templates)) | 
|---|
| 841 | return -EINVAL; | 
|---|
| 842 |  | 
|---|
| 843 | templates = __templates[info->type]; | 
|---|
| 844 | template_size = __templates_size[info->type]; | 
|---|
| 845 |  | 
|---|
| 846 | for (i = 0; info->config[i]; i++) { | 
|---|
| 847 | u32 attr_mask = info->config[i]; | 
|---|
| 848 | u32 attr; | 
|---|
| 849 |  | 
|---|
| 850 | while (attr_mask) { | 
|---|
| 851 | struct attribute *a; | 
|---|
| 852 |  | 
|---|
| 853 | attr = __ffs(attr_mask); | 
|---|
| 854 | attr_mask &= ~BIT(attr); | 
|---|
| 855 | if (attr >= template_size || !templates[attr]) | 
|---|
| 856 | continue;	/* attribute is invisible */ | 
|---|
| 857 | a = hwmon_genattr(drvdata, type: info->type, attr, index: i, | 
|---|
| 858 | template: templates[attr], ops); | 
|---|
| 859 | if (IS_ERR(ptr: a)) { | 
|---|
| 860 | if (PTR_ERR(ptr: a) != -ENOENT) | 
|---|
| 861 | return PTR_ERR(ptr: a); | 
|---|
| 862 | continue; | 
|---|
| 863 | } | 
|---|
| 864 | attrs[aindex++] = a; | 
|---|
| 865 | } | 
|---|
| 866 | } | 
|---|
| 867 | return aindex; | 
|---|
| 868 | } | 
|---|
| 869 |  | 
|---|
| 870 | static struct attribute ** | 
|---|
| 871 | __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip) | 
|---|
| 872 | { | 
|---|
| 873 | int ret, i, aindex = 0, nattrs = 0; | 
|---|
| 874 | struct attribute **attrs; | 
|---|
| 875 |  | 
|---|
| 876 | for (i = 0; chip->info[i]; i++) | 
|---|
| 877 | nattrs += hwmon_num_channel_attrs(info: chip->info[i]); | 
|---|
| 878 |  | 
|---|
| 879 | if (nattrs == 0) | 
|---|
| 880 | return ERR_PTR(error: -EINVAL); | 
|---|
| 881 |  | 
|---|
| 882 | attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL); | 
|---|
| 883 | if (!attrs) | 
|---|
| 884 | return ERR_PTR(error: -ENOMEM); | 
|---|
| 885 |  | 
|---|
| 886 | for (i = 0; chip->info[i]; i++) { | 
|---|
| 887 | ret = hwmon_genattrs(drvdata, attrs: &attrs[aindex], ops: chip->ops, | 
|---|
| 888 | info: chip->info[i]); | 
|---|
| 889 | if (ret < 0) { | 
|---|
| 890 | hwmon_free_attrs(attrs); | 
|---|
| 891 | return ERR_PTR(error: ret); | 
|---|
| 892 | } | 
|---|
| 893 | aindex += ret; | 
|---|
| 894 | } | 
|---|
| 895 |  | 
|---|
| 896 | return attrs; | 
|---|
| 897 | } | 
|---|
| 898 |  | 
|---|
| 899 | static struct device * | 
|---|
| 900 | __hwmon_device_register(struct device *dev, const char *name, void *drvdata, | 
|---|
| 901 | const struct hwmon_chip_info *chip, | 
|---|
| 902 | const struct attribute_group **groups) | 
|---|
| 903 | { | 
|---|
| 904 | struct hwmon_device *hwdev; | 
|---|
| 905 | const char *label; | 
|---|
| 906 | struct device *hdev; | 
|---|
| 907 | struct device *tdev = dev; | 
|---|
| 908 | int i, err, id; | 
|---|
| 909 |  | 
|---|
| 910 | /* Complain about invalid characters in hwmon name attribute */ | 
|---|
| 911 | if (name && (!strlen(name) || strpbrk(name, "-* \t\n"))) | 
|---|
| 912 | dev_warn(dev, | 
|---|
| 913 | "hwmon: '%s' is not a valid name attribute, please fix\n", | 
|---|
| 914 | name); | 
|---|
| 915 |  | 
|---|
| 916 | id = ida_alloc(ida: &hwmon_ida, GFP_KERNEL); | 
|---|
| 917 | if (id < 0) | 
|---|
| 918 | return ERR_PTR(error: id); | 
|---|
| 919 |  | 
|---|
| 920 | hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL); | 
|---|
| 921 | if (hwdev == NULL) { | 
|---|
| 922 | err = -ENOMEM; | 
|---|
| 923 | goto ida_remove; | 
|---|
| 924 | } | 
|---|
| 925 |  | 
|---|
| 926 | hdev = &hwdev->dev; | 
|---|
| 927 |  | 
|---|
| 928 | if (chip) { | 
|---|
| 929 | struct attribute **attrs; | 
|---|
| 930 | int ngroups = 2; /* terminating NULL plus &hwdev->groups */ | 
|---|
| 931 |  | 
|---|
| 932 | if (groups) | 
|---|
| 933 | for (i = 0; groups[i]; i++) | 
|---|
| 934 | ngroups++; | 
|---|
| 935 |  | 
|---|
| 936 | hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL); | 
|---|
| 937 | if (!hwdev->groups) { | 
|---|
| 938 | err = -ENOMEM; | 
|---|
| 939 | goto free_hwmon; | 
|---|
| 940 | } | 
|---|
| 941 |  | 
|---|
| 942 | attrs = __hwmon_create_attrs(drvdata, chip); | 
|---|
| 943 | if (IS_ERR(ptr: attrs)) { | 
|---|
| 944 | err = PTR_ERR(ptr: attrs); | 
|---|
| 945 | goto free_hwmon; | 
|---|
| 946 | } | 
|---|
| 947 |  | 
|---|
| 948 | hwdev->group.attrs = attrs; | 
|---|
| 949 | ngroups = 0; | 
|---|
| 950 | hwdev->groups[ngroups++] = &hwdev->group; | 
|---|
| 951 |  | 
|---|
| 952 | if (groups) { | 
|---|
| 953 | for (i = 0; groups[i]; i++) | 
|---|
| 954 | hwdev->groups[ngroups++] = groups[i]; | 
|---|
| 955 | } | 
|---|
| 956 |  | 
|---|
| 957 | hdev->groups = hwdev->groups; | 
|---|
| 958 | } else { | 
|---|
| 959 | hdev->groups = groups; | 
|---|
| 960 | } | 
|---|
| 961 |  | 
|---|
| 962 | if (dev && device_property_present(dev, propname: "label")) { | 
|---|
| 963 | err = device_property_read_string(dev, propname: "label", val: &label); | 
|---|
| 964 | if (err < 0) | 
|---|
| 965 | goto free_hwmon; | 
|---|
| 966 |  | 
|---|
| 967 | hwdev->label = kstrdup(s: label, GFP_KERNEL); | 
|---|
| 968 | if (hwdev->label == NULL) { | 
|---|
| 969 | err = -ENOMEM; | 
|---|
| 970 | goto free_hwmon; | 
|---|
| 971 | } | 
|---|
| 972 | } | 
|---|
| 973 |  | 
|---|
| 974 | hwdev->name = name; | 
|---|
| 975 | hdev->class = &hwmon_class; | 
|---|
| 976 | hdev->parent = dev; | 
|---|
| 977 | while (tdev && !tdev->of_node) | 
|---|
| 978 | tdev = tdev->parent; | 
|---|
| 979 | hdev->of_node = tdev ? tdev->of_node : NULL; | 
|---|
| 980 | hwdev->chip = chip; | 
|---|
| 981 | mutex_init(&hwdev->lock); | 
|---|
| 982 | dev_set_drvdata(dev: hdev, data: drvdata); | 
|---|
| 983 | dev_set_name(dev: hdev, HWMON_ID_FORMAT, id); | 
|---|
| 984 | err = device_register(dev: hdev); | 
|---|
| 985 | if (err) { | 
|---|
| 986 | put_device(dev: hdev); | 
|---|
| 987 | goto ida_remove; | 
|---|
| 988 | } | 
|---|
| 989 |  | 
|---|
| 990 | INIT_LIST_HEAD(list: &hwdev->tzdata); | 
|---|
| 991 |  | 
|---|
| 992 | if (hdev->of_node && chip && chip->ops->read && | 
|---|
| 993 | chip->info[0]->type == hwmon_chip) { | 
|---|
| 994 | u32 config = chip->info[0]->config[0]; | 
|---|
| 995 |  | 
|---|
| 996 | if (config & HWMON_C_REGISTER_TZ) { | 
|---|
| 997 | err = hwmon_thermal_register_sensors(dev: hdev); | 
|---|
| 998 | if (err) { | 
|---|
| 999 | device_unregister(dev: hdev); | 
|---|
| 1000 | /* | 
|---|
| 1001 | * Don't worry about hwdev; hwmon_dev_release(), | 
|---|
| 1002 | * called from device_unregister(), will free it. | 
|---|
| 1003 | */ | 
|---|
| 1004 | goto ida_remove; | 
|---|
| 1005 | } | 
|---|
| 1006 | } | 
|---|
| 1007 | if (config & HWMON_C_PEC) { | 
|---|
| 1008 | err = hwmon_pec_register(hdev); | 
|---|
| 1009 | if (err) { | 
|---|
| 1010 | device_unregister(dev: hdev); | 
|---|
| 1011 | goto ida_remove; | 
|---|
| 1012 | } | 
|---|
| 1013 | } | 
|---|
| 1014 | } | 
|---|
| 1015 |  | 
|---|
| 1016 | return hdev; | 
|---|
| 1017 |  | 
|---|
| 1018 | free_hwmon: | 
|---|
| 1019 | hwmon_dev_release(dev: hdev); | 
|---|
| 1020 | ida_remove: | 
|---|
| 1021 | ida_free(&hwmon_ida, id); | 
|---|
| 1022 | return ERR_PTR(error: err); | 
|---|
| 1023 | } | 
|---|
| 1024 |  | 
|---|
| 1025 | /** | 
|---|
| 1026 | * hwmon_device_register_with_groups - register w/ hwmon | 
|---|
| 1027 | * @dev: the parent device | 
|---|
| 1028 | * @name: hwmon name attribute | 
|---|
| 1029 | * @drvdata: driver data to attach to created device | 
|---|
| 1030 | * @groups: List of attribute groups to create | 
|---|
| 1031 | * | 
|---|
| 1032 | * hwmon_device_unregister() must be called when the device is no | 
|---|
| 1033 | * longer needed. | 
|---|
| 1034 | * | 
|---|
| 1035 | * Returns the pointer to the new device. | 
|---|
| 1036 | */ | 
|---|
| 1037 | struct device * | 
|---|
| 1038 | hwmon_device_register_with_groups(struct device *dev, const char *name, | 
|---|
| 1039 | void *drvdata, | 
|---|
| 1040 | const struct attribute_group **groups) | 
|---|
| 1041 | { | 
|---|
| 1042 | if (!name) | 
|---|
| 1043 | return ERR_PTR(error: -EINVAL); | 
|---|
| 1044 |  | 
|---|
| 1045 | return __hwmon_device_register(dev, name, drvdata, NULL, groups); | 
|---|
| 1046 | } | 
|---|
| 1047 | EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups); | 
|---|
| 1048 |  | 
|---|
| 1049 | /** | 
|---|
| 1050 | * hwmon_device_register_with_info - register w/ hwmon | 
|---|
| 1051 | * @dev: the parent device (mandatory) | 
|---|
| 1052 | * @name: hwmon name attribute (mandatory) | 
|---|
| 1053 | * @drvdata: driver data to attach to created device (optional) | 
|---|
| 1054 | * @chip: pointer to hwmon chip information (mandatory) | 
|---|
| 1055 | * @extra_groups: pointer to list of additional non-standard attribute groups | 
|---|
| 1056 | *	(optional) | 
|---|
| 1057 | * | 
|---|
| 1058 | * hwmon_device_unregister() must be called when the device is no | 
|---|
| 1059 | * longer needed. | 
|---|
| 1060 | * | 
|---|
| 1061 | * Returns the pointer to the new device. | 
|---|
| 1062 | */ | 
|---|
| 1063 | struct device * | 
|---|
| 1064 | hwmon_device_register_with_info(struct device *dev, const char *name, | 
|---|
| 1065 | void *drvdata, | 
|---|
| 1066 | const struct hwmon_chip_info *chip, | 
|---|
| 1067 | const struct attribute_group **) | 
|---|
| 1068 | { | 
|---|
| 1069 | if (!dev || !name || !chip) | 
|---|
| 1070 | return ERR_PTR(error: -EINVAL); | 
|---|
| 1071 |  | 
|---|
| 1072 | if (!chip->ops || !(chip->ops->visible || chip->ops->is_visible) || !chip->info) | 
|---|
| 1073 | return ERR_PTR(error: -EINVAL); | 
|---|
| 1074 |  | 
|---|
| 1075 | return __hwmon_device_register(dev, name, drvdata, chip, groups: extra_groups); | 
|---|
| 1076 | } | 
|---|
| 1077 | EXPORT_SYMBOL_GPL(hwmon_device_register_with_info); | 
|---|
| 1078 |  | 
|---|
| 1079 | /** | 
|---|
| 1080 | * hwmon_device_register_for_thermal - register hwmon device for thermal subsystem | 
|---|
| 1081 | * @dev: the parent device | 
|---|
| 1082 | * @name: hwmon name attribute | 
|---|
| 1083 | * @drvdata: driver data to attach to created device | 
|---|
| 1084 | * | 
|---|
| 1085 | * The use of this function is restricted. It is provided for legacy reasons | 
|---|
| 1086 | * and must only be called from the thermal subsystem. | 
|---|
| 1087 | * | 
|---|
| 1088 | * hwmon_device_unregister() must be called when the device is no | 
|---|
| 1089 | * longer needed. | 
|---|
| 1090 | * | 
|---|
| 1091 | * Returns the pointer to the new device. | 
|---|
| 1092 | */ | 
|---|
| 1093 | struct device * | 
|---|
| 1094 | hwmon_device_register_for_thermal(struct device *dev, const char *name, | 
|---|
| 1095 | void *drvdata) | 
|---|
| 1096 | { | 
|---|
| 1097 | if (!name || !dev) | 
|---|
| 1098 | return ERR_PTR(error: -EINVAL); | 
|---|
| 1099 |  | 
|---|
| 1100 | return __hwmon_device_register(dev, name, drvdata, NULL, NULL); | 
|---|
| 1101 | } | 
|---|
| 1102 | EXPORT_SYMBOL_NS_GPL(hwmon_device_register_for_thermal, "HWMON_THERMAL"); | 
|---|
| 1103 |  | 
|---|
| 1104 | /** | 
|---|
| 1105 | * hwmon_device_register - register w/ hwmon | 
|---|
| 1106 | * @dev: the device to register | 
|---|
| 1107 | * | 
|---|
| 1108 | * hwmon_device_unregister() must be called when the device is no | 
|---|
| 1109 | * longer needed. | 
|---|
| 1110 | * | 
|---|
| 1111 | * Returns the pointer to the new device. | 
|---|
| 1112 | */ | 
|---|
| 1113 | struct device *hwmon_device_register(struct device *dev) | 
|---|
| 1114 | { | 
|---|
| 1115 | dev_warn(dev, | 
|---|
| 1116 | "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n"); | 
|---|
| 1117 |  | 
|---|
| 1118 | return __hwmon_device_register(dev, NULL, NULL, NULL, NULL); | 
|---|
| 1119 | } | 
|---|
| 1120 | EXPORT_SYMBOL_GPL(hwmon_device_register); | 
|---|
| 1121 |  | 
|---|
| 1122 | /** | 
|---|
| 1123 | * hwmon_device_unregister - removes the previously registered class device | 
|---|
| 1124 | * | 
|---|
| 1125 | * @dev: the class device to destroy | 
|---|
| 1126 | */ | 
|---|
| 1127 | void hwmon_device_unregister(struct device *dev) | 
|---|
| 1128 | { | 
|---|
| 1129 | int id; | 
|---|
| 1130 |  | 
|---|
| 1131 | if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) { | 
|---|
| 1132 | device_unregister(dev); | 
|---|
| 1133 | ida_free(&hwmon_ida, id); | 
|---|
| 1134 | } else | 
|---|
| 1135 | dev_dbg(dev->parent, | 
|---|
| 1136 | "hwmon_device_unregister() failed: bad class ID!\n"); | 
|---|
| 1137 | } | 
|---|
| 1138 | EXPORT_SYMBOL_GPL(hwmon_device_unregister); | 
|---|
| 1139 |  | 
|---|
| 1140 | static void devm_hwmon_release(struct device *dev, void *res) | 
|---|
| 1141 | { | 
|---|
| 1142 | struct device *hwdev = *(struct device **)res; | 
|---|
| 1143 |  | 
|---|
| 1144 | hwmon_device_unregister(hwdev); | 
|---|
| 1145 | } | 
|---|
| 1146 |  | 
|---|
| 1147 | /** | 
|---|
| 1148 | * devm_hwmon_device_register_with_groups - register w/ hwmon | 
|---|
| 1149 | * @dev: the parent device | 
|---|
| 1150 | * @name: hwmon name attribute | 
|---|
| 1151 | * @drvdata: driver data to attach to created device | 
|---|
| 1152 | * @groups: List of attribute groups to create | 
|---|
| 1153 | * | 
|---|
| 1154 | * Returns the pointer to the new device. The new device is automatically | 
|---|
| 1155 | * unregistered with the parent device. | 
|---|
| 1156 | */ | 
|---|
| 1157 | struct device * | 
|---|
| 1158 | devm_hwmon_device_register_with_groups(struct device *dev, const char *name, | 
|---|
| 1159 | void *drvdata, | 
|---|
| 1160 | const struct attribute_group **groups) | 
|---|
| 1161 | { | 
|---|
| 1162 | struct device **ptr, *hwdev; | 
|---|
| 1163 |  | 
|---|
| 1164 | if (!dev) | 
|---|
| 1165 | return ERR_PTR(error: -EINVAL); | 
|---|
| 1166 |  | 
|---|
| 1167 | ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); | 
|---|
| 1168 | if (!ptr) | 
|---|
| 1169 | return ERR_PTR(error: -ENOMEM); | 
|---|
| 1170 |  | 
|---|
| 1171 | hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups); | 
|---|
| 1172 | if (IS_ERR(ptr: hwdev)) | 
|---|
| 1173 | goto error; | 
|---|
| 1174 |  | 
|---|
| 1175 | *ptr = hwdev; | 
|---|
| 1176 | devres_add(dev, res: ptr); | 
|---|
| 1177 | return hwdev; | 
|---|
| 1178 |  | 
|---|
| 1179 | error: | 
|---|
| 1180 | devres_free(res: ptr); | 
|---|
| 1181 | return hwdev; | 
|---|
| 1182 | } | 
|---|
| 1183 | EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups); | 
|---|
| 1184 |  | 
|---|
| 1185 | /** | 
|---|
| 1186 | * devm_hwmon_device_register_with_info - register w/ hwmon | 
|---|
| 1187 | * @dev:	the parent device | 
|---|
| 1188 | * @name:	hwmon name attribute | 
|---|
| 1189 | * @drvdata:	driver data to attach to created device | 
|---|
| 1190 | * @chip:	pointer to hwmon chip information | 
|---|
| 1191 | * @extra_groups: pointer to list of driver specific attribute groups | 
|---|
| 1192 | * | 
|---|
| 1193 | * Returns the pointer to the new device. The new device is automatically | 
|---|
| 1194 | * unregistered with the parent device. | 
|---|
| 1195 | */ | 
|---|
| 1196 | struct device * | 
|---|
| 1197 | devm_hwmon_device_register_with_info(struct device *dev, const char *name, | 
|---|
| 1198 | void *drvdata, | 
|---|
| 1199 | const struct hwmon_chip_info *chip, | 
|---|
| 1200 | const struct attribute_group **) | 
|---|
| 1201 | { | 
|---|
| 1202 | struct device **ptr, *hwdev; | 
|---|
| 1203 |  | 
|---|
| 1204 | if (!dev) | 
|---|
| 1205 | return ERR_PTR(error: -EINVAL); | 
|---|
| 1206 |  | 
|---|
| 1207 | if (!name) { | 
|---|
| 1208 | name = devm_hwmon_sanitize_name(dev, name: dev_name(dev)); | 
|---|
| 1209 | if (IS_ERR(ptr: name)) | 
|---|
| 1210 | return ERR_CAST(ptr: name); | 
|---|
| 1211 | } | 
|---|
| 1212 |  | 
|---|
| 1213 | ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); | 
|---|
| 1214 | if (!ptr) | 
|---|
| 1215 | return ERR_PTR(error: -ENOMEM); | 
|---|
| 1216 |  | 
|---|
| 1217 | hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip, | 
|---|
| 1218 | extra_groups); | 
|---|
| 1219 | if (IS_ERR(ptr: hwdev)) | 
|---|
| 1220 | goto error; | 
|---|
| 1221 |  | 
|---|
| 1222 | *ptr = hwdev; | 
|---|
| 1223 | devres_add(dev, res: ptr); | 
|---|
| 1224 |  | 
|---|
| 1225 | return hwdev; | 
|---|
| 1226 |  | 
|---|
| 1227 | error: | 
|---|
| 1228 | devres_free(res: ptr); | 
|---|
| 1229 | return hwdev; | 
|---|
| 1230 | } | 
|---|
| 1231 | EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info); | 
|---|
| 1232 |  | 
|---|
| 1233 | static char *__hwmon_sanitize_name(struct device *dev, const char *old_name) | 
|---|
| 1234 | { | 
|---|
| 1235 | char *name, *p; | 
|---|
| 1236 |  | 
|---|
| 1237 | if (dev) | 
|---|
| 1238 | name = devm_kstrdup(dev, s: old_name, GFP_KERNEL); | 
|---|
| 1239 | else | 
|---|
| 1240 | name = kstrdup(s: old_name, GFP_KERNEL); | 
|---|
| 1241 | if (!name) | 
|---|
| 1242 | return ERR_PTR(error: -ENOMEM); | 
|---|
| 1243 |  | 
|---|
| 1244 | for (p = name; *p; p++) | 
|---|
| 1245 | if (hwmon_is_bad_char(ch: *p)) | 
|---|
| 1246 | *p = '_'; | 
|---|
| 1247 |  | 
|---|
| 1248 | return name; | 
|---|
| 1249 | } | 
|---|
| 1250 |  | 
|---|
| 1251 | /** | 
|---|
| 1252 | * hwmon_sanitize_name - Replaces invalid characters in a hwmon name | 
|---|
| 1253 | * @name: NUL-terminated name | 
|---|
| 1254 | * | 
|---|
| 1255 | * Allocates a new string where any invalid characters will be replaced | 
|---|
| 1256 | * by an underscore. It is the responsibility of the caller to release | 
|---|
| 1257 | * the memory. | 
|---|
| 1258 | * | 
|---|
| 1259 | * Returns newly allocated name, or ERR_PTR on error. | 
|---|
| 1260 | */ | 
|---|
| 1261 | char *hwmon_sanitize_name(const char *name) | 
|---|
| 1262 | { | 
|---|
| 1263 | return __hwmon_sanitize_name(NULL, old_name: name); | 
|---|
| 1264 | } | 
|---|
| 1265 | EXPORT_SYMBOL_GPL(hwmon_sanitize_name); | 
|---|
| 1266 |  | 
|---|
| 1267 | /** | 
|---|
| 1268 | * devm_hwmon_sanitize_name - resource managed hwmon_sanitize_name() | 
|---|
| 1269 | * @dev: device to allocate memory for | 
|---|
| 1270 | * @name: NUL-terminated name | 
|---|
| 1271 | * | 
|---|
| 1272 | * Allocates a new string where any invalid characters will be replaced | 
|---|
| 1273 | * by an underscore. | 
|---|
| 1274 | * | 
|---|
| 1275 | * Returns newly allocated name, or ERR_PTR on error. | 
|---|
| 1276 | */ | 
|---|
| 1277 | char *devm_hwmon_sanitize_name(struct device *dev, const char *name) | 
|---|
| 1278 | { | 
|---|
| 1279 | if (!dev) | 
|---|
| 1280 | return ERR_PTR(error: -EINVAL); | 
|---|
| 1281 |  | 
|---|
| 1282 | return __hwmon_sanitize_name(dev, old_name: name); | 
|---|
| 1283 | } | 
|---|
| 1284 | EXPORT_SYMBOL_GPL(devm_hwmon_sanitize_name); | 
|---|
| 1285 |  | 
|---|
| 1286 | static void __init hwmon_pci_quirks(void) | 
|---|
| 1287 | { | 
|---|
| 1288 | #if defined CONFIG_X86 && defined CONFIG_PCI | 
|---|
| 1289 | struct pci_dev *sb; | 
|---|
| 1290 | u16 base; | 
|---|
| 1291 | u8 enable; | 
|---|
| 1292 |  | 
|---|
| 1293 | /* Open access to 0x295-0x296 on MSI MS-7031 */ | 
|---|
| 1294 | sb = pci_get_device(PCI_VENDOR_ID_ATI, device: 0x436c, NULL); | 
|---|
| 1295 | if (sb) { | 
|---|
| 1296 | if (sb->subsystem_vendor == 0x1462 &&	/* MSI */ | 
|---|
| 1297 | sb->subsystem_device == 0x0031) {	/* MS-7031 */ | 
|---|
| 1298 | pci_read_config_byte(dev: sb, where: 0x48, val: &enable); | 
|---|
| 1299 | pci_read_config_word(dev: sb, where: 0x64, val: &base); | 
|---|
| 1300 |  | 
|---|
| 1301 | if (base == 0 && !(enable & BIT(2))) { | 
|---|
| 1302 | dev_info(&sb->dev, | 
|---|
| 1303 | "Opening wide generic port at 0x295\n"); | 
|---|
| 1304 | pci_write_config_word(dev: sb, where: 0x64, val: 0x295); | 
|---|
| 1305 | pci_write_config_byte(dev: sb, where: 0x48, | 
|---|
| 1306 | val: enable | BIT(2)); | 
|---|
| 1307 | } | 
|---|
| 1308 | } | 
|---|
| 1309 | pci_dev_put(dev: sb); | 
|---|
| 1310 | } | 
|---|
| 1311 | #endif | 
|---|
| 1312 | } | 
|---|
| 1313 |  | 
|---|
| 1314 | static int __init hwmon_init(void) | 
|---|
| 1315 | { | 
|---|
| 1316 | int err; | 
|---|
| 1317 |  | 
|---|
| 1318 | hwmon_pci_quirks(); | 
|---|
| 1319 |  | 
|---|
| 1320 | err = class_register(class: &hwmon_class); | 
|---|
| 1321 | if (err) { | 
|---|
| 1322 | pr_err( "couldn't register hwmon sysfs class\n"); | 
|---|
| 1323 | return err; | 
|---|
| 1324 | } | 
|---|
| 1325 | return 0; | 
|---|
| 1326 | } | 
|---|
| 1327 |  | 
|---|
| 1328 | static void __exit hwmon_exit(void) | 
|---|
| 1329 | { | 
|---|
| 1330 | class_unregister(class: &hwmon_class); | 
|---|
| 1331 | } | 
|---|
| 1332 |  | 
|---|
| 1333 | subsys_initcall(hwmon_init); | 
|---|
| 1334 | module_exit(hwmon_exit); | 
|---|
| 1335 |  | 
|---|
| 1336 | MODULE_AUTHOR( "Mark M. Hoffman <mhoffman@lightlink.com>"); | 
|---|
| 1337 | MODULE_DESCRIPTION( "hardware monitoring sysfs/class support"); | 
|---|
| 1338 | MODULE_LICENSE( "GPL"); | 
|---|
| 1339 |  | 
|---|
| 1340 |  | 
|---|