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