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