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