1 /* 2 * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring 3 * 4 * This file defines the sysfs class "hwmon", for use by sensors drivers. 5 * 6 * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; version 2 of the License. 11 */ 12 13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 14 15 #include <linux/bitops.h> 16 #include <linux/device.h> 17 #include <linux/err.h> 18 #include <linux/gfp.h> 19 #include <linux/hwmon.h> 20 #include <linux/idr.h> 21 #include <linux/module.h> 22 #include <linux/pci.h> 23 #include <linux/slab.h> 24 #include <linux/string.h> 25 #include <linux/thermal.h> 26 27 #define HWMON_ID_PREFIX "hwmon" 28 #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d" 29 30 struct hwmon_device { 31 const char *name; 32 struct device dev; 33 const struct hwmon_chip_info *chip; 34 35 struct attribute_group group; 36 const struct attribute_group **groups; 37 }; 38 39 #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev) 40 41 struct hwmon_device_attribute { 42 struct device_attribute dev_attr; 43 const struct hwmon_ops *ops; 44 enum hwmon_sensor_types type; 45 u32 attr; 46 int index; 47 }; 48 49 #define to_hwmon_attr(d) \ 50 container_of(d, struct hwmon_device_attribute, dev_attr) 51 52 /* 53 * Thermal zone information 54 * In addition to the reference to the hwmon device, 55 * also provides the sensor index. 56 */ 57 struct hwmon_thermal_data { 58 struct hwmon_device *hwdev; /* Reference to hwmon device */ 59 int index; /* sensor index */ 60 }; 61 62 static ssize_t 63 show_name(struct device *dev, struct device_attribute *attr, char *buf) 64 { 65 return sprintf(buf, "%s\n", to_hwmon_device(dev)->name); 66 } 67 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); 68 69 static struct attribute *hwmon_dev_attrs[] = { 70 &dev_attr_name.attr, 71 NULL 72 }; 73 74 static umode_t hwmon_dev_name_is_visible(struct kobject *kobj, 75 struct attribute *attr, int n) 76 { 77 struct device *dev = container_of(kobj, struct device, kobj); 78 79 if (to_hwmon_device(dev)->name == NULL) 80 return 0; 81 82 return attr->mode; 83 } 84 85 static struct attribute_group hwmon_dev_attr_group = { 86 .attrs = hwmon_dev_attrs, 87 .is_visible = hwmon_dev_name_is_visible, 88 }; 89 90 static const struct attribute_group *hwmon_dev_attr_groups[] = { 91 &hwmon_dev_attr_group, 92 NULL 93 }; 94 95 static void hwmon_dev_release(struct device *dev) 96 { 97 kfree(to_hwmon_device(dev)); 98 } 99 100 static struct class hwmon_class = { 101 .name = "hwmon", 102 .owner = THIS_MODULE, 103 .dev_groups = hwmon_dev_attr_groups, 104 .dev_release = hwmon_dev_release, 105 }; 106 107 static DEFINE_IDA(hwmon_ida); 108 109 /* Thermal zone handling */ 110 111 /* 112 * The complex conditional is necessary to avoid a cyclic dependency 113 * between hwmon and thermal_sys modules. 114 */ 115 #if IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) && \ 116 (!defined(CONFIG_THERMAL_HWMON) || \ 117 !(defined(MODULE) && IS_MODULE(CONFIG_THERMAL))) 118 static int hwmon_thermal_get_temp(void *data, int *temp) 119 { 120 struct hwmon_thermal_data *tdata = data; 121 struct hwmon_device *hwdev = tdata->hwdev; 122 int ret; 123 long t; 124 125 ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input, 126 tdata->index, &t); 127 if (ret < 0) 128 return ret; 129 130 *temp = t; 131 132 return 0; 133 } 134 135 static struct thermal_zone_of_device_ops hwmon_thermal_ops = { 136 .get_temp = hwmon_thermal_get_temp, 137 }; 138 139 static int hwmon_thermal_add_sensor(struct device *dev, 140 struct hwmon_device *hwdev, int index) 141 { 142 struct hwmon_thermal_data *tdata; 143 144 tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL); 145 if (!tdata) 146 return -ENOMEM; 147 148 tdata->hwdev = hwdev; 149 tdata->index = index; 150 151 devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata, 152 &hwmon_thermal_ops); 153 154 return 0; 155 } 156 #else 157 static int hwmon_thermal_add_sensor(struct device *dev, 158 struct hwmon_device *hwdev, int index) 159 { 160 return 0; 161 } 162 #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */ 163 164 /* sysfs attribute management */ 165 166 static ssize_t hwmon_attr_show(struct device *dev, 167 struct device_attribute *devattr, char *buf) 168 { 169 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 170 long val; 171 int ret; 172 173 ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index, 174 &val); 175 if (ret < 0) 176 return ret; 177 178 return sprintf(buf, "%ld\n", val); 179 } 180 181 static ssize_t hwmon_attr_store(struct device *dev, 182 struct device_attribute *devattr, 183 const char *buf, size_t count) 184 { 185 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 186 long val; 187 int ret; 188 189 ret = kstrtol(buf, 10, &val); 190 if (ret < 0) 191 return ret; 192 193 ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index, 194 val); 195 if (ret < 0) 196 return ret; 197 198 return count; 199 } 200 201 static int hwmon_attr_base(enum hwmon_sensor_types type) 202 { 203 if (type == hwmon_in) 204 return 0; 205 return 1; 206 } 207 208 static struct attribute *hwmon_genattr(struct device *dev, 209 const void *drvdata, 210 enum hwmon_sensor_types type, 211 u32 attr, 212 int index, 213 const char *template, 214 const struct hwmon_ops *ops) 215 { 216 struct hwmon_device_attribute *hattr; 217 struct device_attribute *dattr; 218 struct attribute *a; 219 umode_t mode; 220 char *name; 221 222 /* The attribute is invisible if there is no template string */ 223 if (!template) 224 return ERR_PTR(-ENOENT); 225 226 mode = ops->is_visible(drvdata, type, attr, index); 227 if (!mode) 228 return ERR_PTR(-ENOENT); 229 230 if ((mode & S_IRUGO) && !ops->read) 231 return ERR_PTR(-EINVAL); 232 if ((mode & S_IWUGO) && !ops->write) 233 return ERR_PTR(-EINVAL); 234 235 if (type == hwmon_chip) { 236 name = (char *)template; 237 } else { 238 name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL); 239 if (!name) 240 return ERR_PTR(-ENOMEM); 241 scnprintf(name, strlen(template) + 16, template, 242 index + hwmon_attr_base(type)); 243 } 244 245 hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL); 246 if (!hattr) 247 return ERR_PTR(-ENOMEM); 248 249 hattr->type = type; 250 hattr->attr = attr; 251 hattr->index = index; 252 hattr->ops = ops; 253 254 dattr = &hattr->dev_attr; 255 dattr->show = hwmon_attr_show; 256 dattr->store = hwmon_attr_store; 257 258 a = &dattr->attr; 259 sysfs_attr_init(a); 260 a->name = name; 261 a->mode = mode; 262 263 return a; 264 } 265 266 static const char * const hwmon_chip_attr_templates[] = { 267 [hwmon_chip_temp_reset_history] = "temp_reset_history", 268 [hwmon_chip_in_reset_history] = "in_reset_history", 269 [hwmon_chip_curr_reset_history] = "curr_reset_history", 270 [hwmon_chip_power_reset_history] = "power_reset_history", 271 [hwmon_chip_update_interval] = "update_interval", 272 [hwmon_chip_alarms] = "alarms", 273 }; 274 275 static const char * const hwmon_temp_attr_templates[] = { 276 [hwmon_temp_input] = "temp%d_input", 277 [hwmon_temp_type] = "temp%d_type", 278 [hwmon_temp_lcrit] = "temp%d_lcrit", 279 [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst", 280 [hwmon_temp_min] = "temp%d_min", 281 [hwmon_temp_min_hyst] = "temp%d_min_hyst", 282 [hwmon_temp_max] = "temp%d_max", 283 [hwmon_temp_max_hyst] = "temp%d_max_hyst", 284 [hwmon_temp_crit] = "temp%d_crit", 285 [hwmon_temp_crit_hyst] = "temp%d_crit_hyst", 286 [hwmon_temp_emergency] = "temp%d_emergency", 287 [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst", 288 [hwmon_temp_alarm] = "temp%d_alarm", 289 [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm", 290 [hwmon_temp_min_alarm] = "temp%d_min_alarm", 291 [hwmon_temp_max_alarm] = "temp%d_max_alarm", 292 [hwmon_temp_crit_alarm] = "temp%d_crit_alarm", 293 [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm", 294 [hwmon_temp_fault] = "temp%d_fault", 295 [hwmon_temp_offset] = "temp%d_offset", 296 [hwmon_temp_label] = "temp%d_label", 297 [hwmon_temp_lowest] = "temp%d_lowest", 298 [hwmon_temp_highest] = "temp%d_highest", 299 [hwmon_temp_reset_history] = "temp%d_reset_history", 300 }; 301 302 static const char * const hwmon_in_attr_templates[] = { 303 [hwmon_in_input] = "in%d_input", 304 [hwmon_in_min] = "in%d_min", 305 [hwmon_in_max] = "in%d_max", 306 [hwmon_in_lcrit] = "in%d_lcrit", 307 [hwmon_in_crit] = "in%d_crit", 308 [hwmon_in_average] = "in%d_average", 309 [hwmon_in_lowest] = "in%d_lowest", 310 [hwmon_in_highest] = "in%d_highest", 311 [hwmon_in_reset_history] = "in%d_reset_history", 312 [hwmon_in_label] = "in%d_label", 313 [hwmon_in_alarm] = "in%d_alarm", 314 [hwmon_in_min_alarm] = "in%d_min_alarm", 315 [hwmon_in_max_alarm] = "in%d_max_alarm", 316 [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm", 317 [hwmon_in_crit_alarm] = "in%d_crit_alarm", 318 }; 319 320 static const char * const hwmon_curr_attr_templates[] = { 321 [hwmon_curr_input] = "curr%d_input", 322 [hwmon_curr_min] = "curr%d_min", 323 [hwmon_curr_max] = "curr%d_max", 324 [hwmon_curr_lcrit] = "curr%d_lcrit", 325 [hwmon_curr_crit] = "curr%d_crit", 326 [hwmon_curr_average] = "curr%d_average", 327 [hwmon_curr_lowest] = "curr%d_lowest", 328 [hwmon_curr_highest] = "curr%d_highest", 329 [hwmon_curr_reset_history] = "curr%d_reset_history", 330 [hwmon_curr_label] = "curr%d_label", 331 [hwmon_curr_alarm] = "curr%d_alarm", 332 [hwmon_curr_min_alarm] = "curr%d_min_alarm", 333 [hwmon_curr_max_alarm] = "curr%d_max_alarm", 334 [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm", 335 [hwmon_curr_crit_alarm] = "curr%d_crit_alarm", 336 }; 337 338 static const char * const hwmon_power_attr_templates[] = { 339 [hwmon_power_average] = "power%d_average", 340 [hwmon_power_average_interval] = "power%d_average_interval", 341 [hwmon_power_average_interval_max] = "power%d_interval_max", 342 [hwmon_power_average_interval_min] = "power%d_interval_min", 343 [hwmon_power_average_highest] = "power%d_average_highest", 344 [hwmon_power_average_lowest] = "power%d_average_lowest", 345 [hwmon_power_average_max] = "power%d_average_max", 346 [hwmon_power_average_min] = "power%d_average_min", 347 [hwmon_power_input] = "power%d_input", 348 [hwmon_power_input_highest] = "power%d_input_highest", 349 [hwmon_power_input_lowest] = "power%d_input_lowest", 350 [hwmon_power_reset_history] = "power%d_reset_history", 351 [hwmon_power_accuracy] = "power%d_accuracy", 352 [hwmon_power_cap] = "power%d_cap", 353 [hwmon_power_cap_hyst] = "power%d_cap_hyst", 354 [hwmon_power_cap_max] = "power%d_cap_max", 355 [hwmon_power_cap_min] = "power%d_cap_min", 356 [hwmon_power_max] = "power%d_max", 357 [hwmon_power_crit] = "power%d_crit", 358 [hwmon_power_label] = "power%d_label", 359 [hwmon_power_alarm] = "power%d_alarm", 360 [hwmon_power_cap_alarm] = "power%d_cap_alarm", 361 [hwmon_power_max_alarm] = "power%d_max_alarm", 362 [hwmon_power_crit_alarm] = "power%d_crit_alarm", 363 }; 364 365 static const char * const hwmon_energy_attr_templates[] = { 366 [hwmon_energy_input] = "energy%d_input", 367 [hwmon_energy_label] = "energy%d_label", 368 }; 369 370 static const char * const hwmon_humidity_attr_templates[] = { 371 [hwmon_humidity_input] = "humidity%d_input", 372 [hwmon_humidity_label] = "humidity%d_label", 373 [hwmon_humidity_min] = "humidity%d_min", 374 [hwmon_humidity_min_hyst] = "humidity%d_min_hyst", 375 [hwmon_humidity_max] = "humidity%d_max", 376 [hwmon_humidity_max_hyst] = "humidity%d_max_hyst", 377 [hwmon_humidity_alarm] = "humidity%d_alarm", 378 [hwmon_humidity_fault] = "humidity%d_fault", 379 }; 380 381 static const char * const hwmon_fan_attr_templates[] = { 382 [hwmon_fan_input] = "fan%d_input", 383 [hwmon_fan_label] = "fan%d_label", 384 [hwmon_fan_min] = "fan%d_min", 385 [hwmon_fan_max] = "fan%d_max", 386 [hwmon_fan_div] = "fan%d_div", 387 [hwmon_fan_pulses] = "fan%d_pulses", 388 [hwmon_fan_target] = "fan%d_target", 389 [hwmon_fan_alarm] = "fan%d_alarm", 390 [hwmon_fan_min_alarm] = "fan%d_min_alarm", 391 [hwmon_fan_max_alarm] = "fan%d_max_alarm", 392 [hwmon_fan_fault] = "fan%d_fault", 393 }; 394 395 static const char * const hwmon_pwm_attr_templates[] = { 396 [hwmon_pwm_input] = "pwm%d", 397 [hwmon_pwm_enable] = "pwm%d_enable", 398 [hwmon_pwm_mode] = "pwm%d_mode", 399 [hwmon_pwm_freq] = "pwm%d_freq", 400 }; 401 402 static const char * const *__templates[] = { 403 [hwmon_chip] = hwmon_chip_attr_templates, 404 [hwmon_temp] = hwmon_temp_attr_templates, 405 [hwmon_in] = hwmon_in_attr_templates, 406 [hwmon_curr] = hwmon_curr_attr_templates, 407 [hwmon_power] = hwmon_power_attr_templates, 408 [hwmon_energy] = hwmon_energy_attr_templates, 409 [hwmon_humidity] = hwmon_humidity_attr_templates, 410 [hwmon_fan] = hwmon_fan_attr_templates, 411 [hwmon_pwm] = hwmon_pwm_attr_templates, 412 }; 413 414 static const int __templates_size[] = { 415 [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attr_templates), 416 [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates), 417 [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates), 418 [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates), 419 [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates), 420 [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates), 421 [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates), 422 [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates), 423 [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates), 424 }; 425 426 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info) 427 { 428 int i, n; 429 430 for (i = n = 0; info->config[i]; i++) 431 n += hweight32(info->config[i]); 432 433 return n; 434 } 435 436 static int hwmon_genattrs(struct device *dev, 437 const void *drvdata, 438 struct attribute **attrs, 439 const struct hwmon_ops *ops, 440 const struct hwmon_channel_info *info) 441 { 442 const char * const *templates; 443 int template_size; 444 int i, aindex = 0; 445 446 if (info->type >= ARRAY_SIZE(__templates)) 447 return -EINVAL; 448 449 templates = __templates[info->type]; 450 template_size = __templates_size[info->type]; 451 452 for (i = 0; info->config[i]; i++) { 453 u32 attr_mask = info->config[i]; 454 u32 attr; 455 456 while (attr_mask) { 457 struct attribute *a; 458 459 attr = __ffs(attr_mask); 460 attr_mask &= ~BIT(attr); 461 if (attr >= template_size) 462 return -EINVAL; 463 a = hwmon_genattr(dev, drvdata, info->type, attr, i, 464 templates[attr], ops); 465 if (IS_ERR(a)) { 466 if (PTR_ERR(a) != -ENOENT) 467 return PTR_ERR(a); 468 continue; 469 } 470 attrs[aindex++] = a; 471 } 472 } 473 return aindex; 474 } 475 476 static struct attribute ** 477 __hwmon_create_attrs(struct device *dev, const void *drvdata, 478 const struct hwmon_chip_info *chip) 479 { 480 int ret, i, aindex = 0, nattrs = 0; 481 struct attribute **attrs; 482 483 for (i = 0; chip->info[i]; i++) 484 nattrs += hwmon_num_channel_attrs(chip->info[i]); 485 486 if (nattrs == 0) 487 return ERR_PTR(-EINVAL); 488 489 attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL); 490 if (!attrs) 491 return ERR_PTR(-ENOMEM); 492 493 for (i = 0; chip->info[i]; i++) { 494 ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops, 495 chip->info[i]); 496 if (ret < 0) 497 return ERR_PTR(ret); 498 aindex += ret; 499 } 500 501 return attrs; 502 } 503 504 static struct device * 505 __hwmon_device_register(struct device *dev, const char *name, void *drvdata, 506 const struct hwmon_chip_info *chip, 507 const struct attribute_group **groups) 508 { 509 struct hwmon_device *hwdev; 510 struct device *hdev; 511 int i, j, err, id; 512 513 /* Do not accept invalid characters in hwmon name attribute */ 514 if (name && (!strlen(name) || strpbrk(name, "-* \t\n"))) 515 return ERR_PTR(-EINVAL); 516 517 id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL); 518 if (id < 0) 519 return ERR_PTR(id); 520 521 hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL); 522 if (hwdev == NULL) { 523 err = -ENOMEM; 524 goto ida_remove; 525 } 526 527 hdev = &hwdev->dev; 528 529 if (chip && chip->ops->is_visible) { 530 struct attribute **attrs; 531 int ngroups = 2; 532 533 if (groups) 534 for (i = 0; groups[i]; i++) 535 ngroups++; 536 537 hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups), 538 GFP_KERNEL); 539 if (!hwdev->groups) 540 return ERR_PTR(-ENOMEM); 541 542 attrs = __hwmon_create_attrs(dev, drvdata, chip); 543 if (IS_ERR(attrs)) { 544 err = PTR_ERR(attrs); 545 goto free_hwmon; 546 } 547 548 hwdev->group.attrs = attrs; 549 ngroups = 0; 550 hwdev->groups[ngroups++] = &hwdev->group; 551 552 if (groups) { 553 for (i = 0; groups[i]; i++) 554 hwdev->groups[ngroups++] = groups[i]; 555 } 556 557 hdev->groups = hwdev->groups; 558 } else { 559 hdev->groups = groups; 560 } 561 562 hwdev->name = name; 563 hdev->class = &hwmon_class; 564 hdev->parent = dev; 565 hdev->of_node = dev ? dev->of_node : NULL; 566 hwdev->chip = chip; 567 dev_set_drvdata(hdev, drvdata); 568 dev_set_name(hdev, HWMON_ID_FORMAT, id); 569 err = device_register(hdev); 570 if (err) 571 goto free_hwmon; 572 573 if (chip && chip->ops->is_visible && chip->ops->read && 574 chip->info[0]->type == hwmon_chip && 575 (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) { 576 const struct hwmon_channel_info **info = chip->info; 577 578 for (i = 1; info[i]; i++) { 579 if (info[i]->type != hwmon_temp) 580 continue; 581 582 for (j = 0; info[i]->config[j]; j++) { 583 if (!chip->ops->is_visible(drvdata, hwmon_temp, 584 hwmon_temp_input, j)) 585 continue; 586 if (info[i]->config[j] & HWMON_T_INPUT) 587 hwmon_thermal_add_sensor(dev, hwdev, j); 588 } 589 } 590 } 591 592 return hdev; 593 594 free_hwmon: 595 kfree(hwdev); 596 ida_remove: 597 ida_simple_remove(&hwmon_ida, id); 598 return ERR_PTR(err); 599 } 600 601 /** 602 * hwmon_device_register_with_groups - register w/ hwmon 603 * @dev: the parent device 604 * @name: hwmon name attribute 605 * @drvdata: driver data to attach to created device 606 * @groups: List of attribute groups to create 607 * 608 * hwmon_device_unregister() must be called when the device is no 609 * longer needed. 610 * 611 * Returns the pointer to the new device. 612 */ 613 struct device * 614 hwmon_device_register_with_groups(struct device *dev, const char *name, 615 void *drvdata, 616 const struct attribute_group **groups) 617 { 618 return __hwmon_device_register(dev, name, drvdata, NULL, groups); 619 } 620 EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups); 621 622 /** 623 * hwmon_device_register_with_info - register w/ hwmon 624 * @dev: the parent device 625 * @name: hwmon name attribute 626 * @drvdata: driver data to attach to created device 627 * @info: Pointer to hwmon chip information 628 * @groups - pointer to list of driver specific attribute groups 629 * 630 * hwmon_device_unregister() must be called when the device is no 631 * longer needed. 632 * 633 * Returns the pointer to the new device. 634 */ 635 struct device * 636 hwmon_device_register_with_info(struct device *dev, const char *name, 637 void *drvdata, 638 const struct hwmon_chip_info *chip, 639 const struct attribute_group **groups) 640 { 641 if (chip && (!chip->ops || !chip->info)) 642 return ERR_PTR(-EINVAL); 643 644 return __hwmon_device_register(dev, name, drvdata, chip, groups); 645 } 646 EXPORT_SYMBOL_GPL(hwmon_device_register_with_info); 647 648 /** 649 * hwmon_device_register - register w/ hwmon 650 * @dev: the device to register 651 * 652 * hwmon_device_unregister() must be called when the device is no 653 * longer needed. 654 * 655 * Returns the pointer to the new device. 656 */ 657 struct device *hwmon_device_register(struct device *dev) 658 { 659 return hwmon_device_register_with_groups(dev, NULL, NULL, NULL); 660 } 661 EXPORT_SYMBOL_GPL(hwmon_device_register); 662 663 /** 664 * hwmon_device_unregister - removes the previously registered class device 665 * 666 * @dev: the class device to destroy 667 */ 668 void hwmon_device_unregister(struct device *dev) 669 { 670 int id; 671 672 if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) { 673 device_unregister(dev); 674 ida_simple_remove(&hwmon_ida, id); 675 } else 676 dev_dbg(dev->parent, 677 "hwmon_device_unregister() failed: bad class ID!\n"); 678 } 679 EXPORT_SYMBOL_GPL(hwmon_device_unregister); 680 681 static void devm_hwmon_release(struct device *dev, void *res) 682 { 683 struct device *hwdev = *(struct device **)res; 684 685 hwmon_device_unregister(hwdev); 686 } 687 688 /** 689 * devm_hwmon_device_register_with_groups - register w/ hwmon 690 * @dev: the parent device 691 * @name: hwmon name attribute 692 * @drvdata: driver data to attach to created device 693 * @groups: List of attribute groups to create 694 * 695 * Returns the pointer to the new device. The new device is automatically 696 * unregistered with the parent device. 697 */ 698 struct device * 699 devm_hwmon_device_register_with_groups(struct device *dev, const char *name, 700 void *drvdata, 701 const struct attribute_group **groups) 702 { 703 struct device **ptr, *hwdev; 704 705 if (!dev) 706 return ERR_PTR(-EINVAL); 707 708 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); 709 if (!ptr) 710 return ERR_PTR(-ENOMEM); 711 712 hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups); 713 if (IS_ERR(hwdev)) 714 goto error; 715 716 *ptr = hwdev; 717 devres_add(dev, ptr); 718 return hwdev; 719 720 error: 721 devres_free(ptr); 722 return hwdev; 723 } 724 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups); 725 726 /** 727 * devm_hwmon_device_register_with_info - register w/ hwmon 728 * @dev: the parent device 729 * @name: hwmon name attribute 730 * @drvdata: driver data to attach to created device 731 * @info: Pointer to hwmon chip information 732 * @groups - pointer to list of driver specific attribute groups 733 * 734 * Returns the pointer to the new device. The new device is automatically 735 * unregistered with the parent device. 736 */ 737 struct device * 738 devm_hwmon_device_register_with_info(struct device *dev, const char *name, 739 void *drvdata, 740 const struct hwmon_chip_info *chip, 741 const struct attribute_group **groups) 742 { 743 struct device **ptr, *hwdev; 744 745 if (!dev) 746 return ERR_PTR(-EINVAL); 747 748 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); 749 if (!ptr) 750 return ERR_PTR(-ENOMEM); 751 752 hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip, 753 groups); 754 if (IS_ERR(hwdev)) 755 goto error; 756 757 *ptr = hwdev; 758 devres_add(dev, ptr); 759 760 return hwdev; 761 762 error: 763 devres_free(ptr); 764 return hwdev; 765 } 766 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info); 767 768 static int devm_hwmon_match(struct device *dev, void *res, void *data) 769 { 770 struct device **hwdev = res; 771 772 return *hwdev == data; 773 } 774 775 /** 776 * devm_hwmon_device_unregister - removes a previously registered hwmon device 777 * 778 * @dev: the parent device of the device to unregister 779 */ 780 void devm_hwmon_device_unregister(struct device *dev) 781 { 782 WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev)); 783 } 784 EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister); 785 786 static void __init hwmon_pci_quirks(void) 787 { 788 #if defined CONFIG_X86 && defined CONFIG_PCI 789 struct pci_dev *sb; 790 u16 base; 791 u8 enable; 792 793 /* Open access to 0x295-0x296 on MSI MS-7031 */ 794 sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL); 795 if (sb) { 796 if (sb->subsystem_vendor == 0x1462 && /* MSI */ 797 sb->subsystem_device == 0x0031) { /* MS-7031 */ 798 pci_read_config_byte(sb, 0x48, &enable); 799 pci_read_config_word(sb, 0x64, &base); 800 801 if (base == 0 && !(enable & BIT(2))) { 802 dev_info(&sb->dev, 803 "Opening wide generic port at 0x295\n"); 804 pci_write_config_word(sb, 0x64, 0x295); 805 pci_write_config_byte(sb, 0x48, 806 enable | BIT(2)); 807 } 808 } 809 pci_dev_put(sb); 810 } 811 #endif 812 } 813 814 static int __init hwmon_init(void) 815 { 816 int err; 817 818 hwmon_pci_quirks(); 819 820 err = class_register(&hwmon_class); 821 if (err) { 822 pr_err("couldn't register hwmon sysfs class\n"); 823 return err; 824 } 825 return 0; 826 } 827 828 static void __exit hwmon_exit(void) 829 { 830 class_unregister(&hwmon_class); 831 } 832 833 subsys_initcall(hwmon_init); 834 module_exit(hwmon_exit); 835 836 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); 837 MODULE_DESCRIPTION("hardware monitoring sysfs/class support"); 838 MODULE_LICENSE("GPL"); 839 840