Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring
0004  *
0005  * This file defines the sysfs class "hwmon", for use by sensors drivers.
0006  *
0007  * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
0008  */
0009 
0010 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0011 
0012 #include <linux/bitops.h>
0013 #include <linux/device.h>
0014 #include <linux/err.h>
0015 #include <linux/gfp.h>
0016 #include <linux/hwmon.h>
0017 #include <linux/idr.h>
0018 #include <linux/list.h>
0019 #include <linux/module.h>
0020 #include <linux/pci.h>
0021 #include <linux/property.h>
0022 #include <linux/slab.h>
0023 #include <linux/string.h>
0024 #include <linux/thermal.h>
0025 
0026 #define CREATE_TRACE_POINTS
0027 #include <trace/events/hwmon.h>
0028 
0029 #define HWMON_ID_PREFIX "hwmon"
0030 #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
0031 
0032 struct hwmon_device {
0033     const char *name;
0034     const char *label;
0035     struct device dev;
0036     const struct hwmon_chip_info *chip;
0037     struct list_head tzdata;
0038     struct attribute_group group;
0039     const struct attribute_group **groups;
0040 };
0041 
0042 #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
0043 
0044 #define MAX_SYSFS_ATTR_NAME_LENGTH  32
0045 
0046 struct hwmon_device_attribute {
0047     struct device_attribute dev_attr;
0048     const struct hwmon_ops *ops;
0049     enum hwmon_sensor_types type;
0050     u32 attr;
0051     int index;
0052     char name[MAX_SYSFS_ATTR_NAME_LENGTH];
0053 };
0054 
0055 #define to_hwmon_attr(d) \
0056     container_of(d, struct hwmon_device_attribute, dev_attr)
0057 #define to_dev_attr(a) container_of(a, struct device_attribute, attr)
0058 
0059 /*
0060  * Thermal zone information
0061  */
0062 struct hwmon_thermal_data {
0063     struct list_head node;      /* hwmon tzdata list entry */
0064     struct device *dev;     /* Reference to hwmon device */
0065     int index;          /* sensor index */
0066     struct thermal_zone_device *tzd;/* thermal zone device */
0067 };
0068 
0069 static ssize_t
0070 name_show(struct device *dev, struct device_attribute *attr, char *buf)
0071 {
0072     return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
0073 }
0074 static DEVICE_ATTR_RO(name);
0075 
0076 static ssize_t
0077 label_show(struct device *dev, struct device_attribute *attr, char *buf)
0078 {
0079     return sysfs_emit(buf, "%s\n", to_hwmon_device(dev)->label);
0080 }
0081 static DEVICE_ATTR_RO(label);
0082 
0083 static struct attribute *hwmon_dev_attrs[] = {
0084     &dev_attr_name.attr,
0085     &dev_attr_label.attr,
0086     NULL
0087 };
0088 
0089 static umode_t hwmon_dev_attr_is_visible(struct kobject *kobj,
0090                      struct attribute *attr, int n)
0091 {
0092     struct device *dev = kobj_to_dev(kobj);
0093     struct hwmon_device *hdev = to_hwmon_device(dev);
0094 
0095     if (attr == &dev_attr_name.attr && hdev->name == NULL)
0096         return 0;
0097 
0098     if (attr == &dev_attr_label.attr && hdev->label == NULL)
0099         return 0;
0100 
0101     return attr->mode;
0102 }
0103 
0104 static const struct attribute_group hwmon_dev_attr_group = {
0105     .attrs      = hwmon_dev_attrs,
0106     .is_visible = hwmon_dev_attr_is_visible,
0107 };
0108 
0109 static const struct attribute_group *hwmon_dev_attr_groups[] = {
0110     &hwmon_dev_attr_group,
0111     NULL
0112 };
0113 
0114 static void hwmon_free_attrs(struct attribute **attrs)
0115 {
0116     int i;
0117 
0118     for (i = 0; attrs[i]; i++) {
0119         struct device_attribute *dattr = to_dev_attr(attrs[i]);
0120         struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr);
0121 
0122         kfree(hattr);
0123     }
0124     kfree(attrs);
0125 }
0126 
0127 static void hwmon_dev_release(struct device *dev)
0128 {
0129     struct hwmon_device *hwdev = to_hwmon_device(dev);
0130 
0131     if (hwdev->group.attrs)
0132         hwmon_free_attrs(hwdev->group.attrs);
0133     kfree(hwdev->groups);
0134     kfree(hwdev->label);
0135     kfree(hwdev);
0136 }
0137 
0138 static struct class hwmon_class = {
0139     .name = "hwmon",
0140     .owner = THIS_MODULE,
0141     .dev_groups = hwmon_dev_attr_groups,
0142     .dev_release = hwmon_dev_release,
0143 };
0144 
0145 static DEFINE_IDA(hwmon_ida);
0146 
0147 /* Thermal zone handling */
0148 
0149 /*
0150  * The complex conditional is necessary to avoid a cyclic dependency
0151  * between hwmon and thermal_sys modules.
0152  */
0153 #ifdef CONFIG_THERMAL_OF
0154 static int hwmon_thermal_get_temp(void *data, int *temp)
0155 {
0156     struct hwmon_thermal_data *tdata = data;
0157     struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
0158     int ret;
0159     long t;
0160 
0161     ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input,
0162                      tdata->index, &t);
0163     if (ret < 0)
0164         return ret;
0165 
0166     *temp = t;
0167 
0168     return 0;
0169 }
0170 
0171 static int hwmon_thermal_set_trips(void *data, int low, int high)
0172 {
0173     struct hwmon_thermal_data *tdata = data;
0174     struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
0175     const struct hwmon_chip_info *chip = hwdev->chip;
0176     const struct hwmon_channel_info **info = chip->info;
0177     unsigned int i;
0178     int err;
0179 
0180     if (!chip->ops->write)
0181         return 0;
0182 
0183     for (i = 0; info[i] && info[i]->type != hwmon_temp; i++)
0184         continue;
0185 
0186     if (!info[i])
0187         return 0;
0188 
0189     if (info[i]->config[tdata->index] & HWMON_T_MIN) {
0190         err = chip->ops->write(tdata->dev, hwmon_temp,
0191                        hwmon_temp_min, tdata->index, low);
0192         if (err && err != -EOPNOTSUPP)
0193             return err;
0194     }
0195 
0196     if (info[i]->config[tdata->index] & HWMON_T_MAX) {
0197         err = chip->ops->write(tdata->dev, hwmon_temp,
0198                        hwmon_temp_max, tdata->index, high);
0199         if (err && err != -EOPNOTSUPP)
0200             return err;
0201     }
0202 
0203     return 0;
0204 }
0205 
0206 static const struct thermal_zone_of_device_ops hwmon_thermal_ops = {
0207     .get_temp = hwmon_thermal_get_temp,
0208     .set_trips = hwmon_thermal_set_trips,
0209 };
0210 
0211 static void hwmon_thermal_remove_sensor(void *data)
0212 {
0213     list_del(data);
0214 }
0215 
0216 static int hwmon_thermal_add_sensor(struct device *dev, int index)
0217 {
0218     struct hwmon_device *hwdev = to_hwmon_device(dev);
0219     struct hwmon_thermal_data *tdata;
0220     struct thermal_zone_device *tzd;
0221     int err;
0222 
0223     tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
0224     if (!tdata)
0225         return -ENOMEM;
0226 
0227     tdata->dev = dev;
0228     tdata->index = index;
0229 
0230     tzd = devm_thermal_zone_of_sensor_register(dev, index, tdata,
0231                            &hwmon_thermal_ops);
0232     if (IS_ERR(tzd)) {
0233         if (PTR_ERR(tzd) != -ENODEV)
0234             return PTR_ERR(tzd);
0235         dev_info(dev, "temp%d_input not attached to any thermal zone\n",
0236              index + 1);
0237         devm_kfree(dev, tdata);
0238         return 0;
0239     }
0240 
0241     err = devm_add_action(dev, hwmon_thermal_remove_sensor, &tdata->node);
0242     if (err)
0243         return err;
0244 
0245     tdata->tzd = tzd;
0246     list_add(&tdata->node, &hwdev->tzdata);
0247 
0248     return 0;
0249 }
0250 
0251 static int hwmon_thermal_register_sensors(struct device *dev)
0252 {
0253     struct hwmon_device *hwdev = to_hwmon_device(dev);
0254     const struct hwmon_chip_info *chip = hwdev->chip;
0255     const struct hwmon_channel_info **info = chip->info;
0256     void *drvdata = dev_get_drvdata(dev);
0257     int i;
0258 
0259     for (i = 1; info[i]; i++) {
0260         int j;
0261 
0262         if (info[i]->type != hwmon_temp)
0263             continue;
0264 
0265         for (j = 0; info[i]->config[j]; j++) {
0266             int err;
0267 
0268             if (!(info[i]->config[j] & HWMON_T_INPUT) ||
0269                 !chip->ops->is_visible(drvdata, hwmon_temp,
0270                            hwmon_temp_input, j))
0271                 continue;
0272 
0273             err = hwmon_thermal_add_sensor(dev, j);
0274             if (err)
0275                 return err;
0276         }
0277     }
0278 
0279     return 0;
0280 }
0281 
0282 static void hwmon_thermal_notify(struct device *dev, int index)
0283 {
0284     struct hwmon_device *hwdev = to_hwmon_device(dev);
0285     struct hwmon_thermal_data *tzdata;
0286 
0287     list_for_each_entry(tzdata, &hwdev->tzdata, node) {
0288         if (tzdata->index == index) {
0289             thermal_zone_device_update(tzdata->tzd,
0290                            THERMAL_EVENT_UNSPECIFIED);
0291         }
0292     }
0293 }
0294 
0295 #else
0296 static int hwmon_thermal_register_sensors(struct device *dev)
0297 {
0298     return 0;
0299 }
0300 
0301 static void hwmon_thermal_notify(struct device *dev, int index) { }
0302 
0303 #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
0304 
0305 static int hwmon_attr_base(enum hwmon_sensor_types type)
0306 {
0307     if (type == hwmon_in || type == hwmon_intrusion)
0308         return 0;
0309     return 1;
0310 }
0311 
0312 /* sysfs attribute management */
0313 
0314 static ssize_t hwmon_attr_show(struct device *dev,
0315                    struct device_attribute *devattr, char *buf)
0316 {
0317     struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
0318     long val;
0319     int ret;
0320 
0321     ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
0322                    &val);
0323     if (ret < 0)
0324         return ret;
0325 
0326     trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
0327                   hattr->name, val);
0328 
0329     return sprintf(buf, "%ld\n", val);
0330 }
0331 
0332 static ssize_t hwmon_attr_show_string(struct device *dev,
0333                       struct device_attribute *devattr,
0334                       char *buf)
0335 {
0336     struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
0337     enum hwmon_sensor_types type = hattr->type;
0338     const char *s;
0339     int ret;
0340 
0341     ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
0342                       hattr->index, &s);
0343     if (ret < 0)
0344         return ret;
0345 
0346     trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
0347                      hattr->name, s);
0348 
0349     return sprintf(buf, "%s\n", s);
0350 }
0351 
0352 static ssize_t hwmon_attr_store(struct device *dev,
0353                 struct device_attribute *devattr,
0354                 const char *buf, size_t count)
0355 {
0356     struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
0357     long val;
0358     int ret;
0359 
0360     ret = kstrtol(buf, 10, &val);
0361     if (ret < 0)
0362         return ret;
0363 
0364     ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
0365                 val);
0366     if (ret < 0)
0367         return ret;
0368 
0369     trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
0370                    hattr->name, val);
0371 
0372     return count;
0373 }
0374 
0375 static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
0376 {
0377     return (type == hwmon_temp && attr == hwmon_temp_label) ||
0378            (type == hwmon_in && attr == hwmon_in_label) ||
0379            (type == hwmon_curr && attr == hwmon_curr_label) ||
0380            (type == hwmon_power && attr == hwmon_power_label) ||
0381            (type == hwmon_energy && attr == hwmon_energy_label) ||
0382            (type == hwmon_humidity && attr == hwmon_humidity_label) ||
0383            (type == hwmon_fan && attr == hwmon_fan_label);
0384 }
0385 
0386 static struct attribute *hwmon_genattr(const void *drvdata,
0387                        enum hwmon_sensor_types type,
0388                        u32 attr,
0389                        int index,
0390                        const char *template,
0391                        const struct hwmon_ops *ops)
0392 {
0393     struct hwmon_device_attribute *hattr;
0394     struct device_attribute *dattr;
0395     struct attribute *a;
0396     umode_t mode;
0397     const char *name;
0398     bool is_string = is_string_attr(type, attr);
0399 
0400     /* The attribute is invisible if there is no template string */
0401     if (!template)
0402         return ERR_PTR(-ENOENT);
0403 
0404     mode = ops->is_visible(drvdata, type, attr, index);
0405     if (!mode)
0406         return ERR_PTR(-ENOENT);
0407 
0408     if ((mode & 0444) && ((is_string && !ops->read_string) ||
0409                  (!is_string && !ops->read)))
0410         return ERR_PTR(-EINVAL);
0411     if ((mode & 0222) && !ops->write)
0412         return ERR_PTR(-EINVAL);
0413 
0414     hattr = kzalloc(sizeof(*hattr), GFP_KERNEL);
0415     if (!hattr)
0416         return ERR_PTR(-ENOMEM);
0417 
0418     if (type == hwmon_chip) {
0419         name = template;
0420     } else {
0421         scnprintf(hattr->name, sizeof(hattr->name), template,
0422               index + hwmon_attr_base(type));
0423         name = hattr->name;
0424     }
0425 
0426     hattr->type = type;
0427     hattr->attr = attr;
0428     hattr->index = index;
0429     hattr->ops = ops;
0430 
0431     dattr = &hattr->dev_attr;
0432     dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
0433     dattr->store = hwmon_attr_store;
0434 
0435     a = &dattr->attr;
0436     sysfs_attr_init(a);
0437     a->name = name;
0438     a->mode = mode;
0439 
0440     return a;
0441 }
0442 
0443 /*
0444  * Chip attributes are not attribute templates but actual sysfs attributes.
0445  * See hwmon_genattr() for special handling.
0446  */
0447 static const char * const hwmon_chip_attrs[] = {
0448     [hwmon_chip_temp_reset_history] = "temp_reset_history",
0449     [hwmon_chip_in_reset_history] = "in_reset_history",
0450     [hwmon_chip_curr_reset_history] = "curr_reset_history",
0451     [hwmon_chip_power_reset_history] = "power_reset_history",
0452     [hwmon_chip_update_interval] = "update_interval",
0453     [hwmon_chip_alarms] = "alarms",
0454     [hwmon_chip_samples] = "samples",
0455     [hwmon_chip_curr_samples] = "curr_samples",
0456     [hwmon_chip_in_samples] = "in_samples",
0457     [hwmon_chip_power_samples] = "power_samples",
0458     [hwmon_chip_temp_samples] = "temp_samples",
0459 };
0460 
0461 static const char * const hwmon_temp_attr_templates[] = {
0462     [hwmon_temp_enable] = "temp%d_enable",
0463     [hwmon_temp_input] = "temp%d_input",
0464     [hwmon_temp_type] = "temp%d_type",
0465     [hwmon_temp_lcrit] = "temp%d_lcrit",
0466     [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
0467     [hwmon_temp_min] = "temp%d_min",
0468     [hwmon_temp_min_hyst] = "temp%d_min_hyst",
0469     [hwmon_temp_max] = "temp%d_max",
0470     [hwmon_temp_max_hyst] = "temp%d_max_hyst",
0471     [hwmon_temp_crit] = "temp%d_crit",
0472     [hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
0473     [hwmon_temp_emergency] = "temp%d_emergency",
0474     [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
0475     [hwmon_temp_alarm] = "temp%d_alarm",
0476     [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
0477     [hwmon_temp_min_alarm] = "temp%d_min_alarm",
0478     [hwmon_temp_max_alarm] = "temp%d_max_alarm",
0479     [hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
0480     [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
0481     [hwmon_temp_fault] = "temp%d_fault",
0482     [hwmon_temp_offset] = "temp%d_offset",
0483     [hwmon_temp_label] = "temp%d_label",
0484     [hwmon_temp_lowest] = "temp%d_lowest",
0485     [hwmon_temp_highest] = "temp%d_highest",
0486     [hwmon_temp_reset_history] = "temp%d_reset_history",
0487     [hwmon_temp_rated_min] = "temp%d_rated_min",
0488     [hwmon_temp_rated_max] = "temp%d_rated_max",
0489 };
0490 
0491 static const char * const hwmon_in_attr_templates[] = {
0492     [hwmon_in_enable] = "in%d_enable",
0493     [hwmon_in_input] = "in%d_input",
0494     [hwmon_in_min] = "in%d_min",
0495     [hwmon_in_max] = "in%d_max",
0496     [hwmon_in_lcrit] = "in%d_lcrit",
0497     [hwmon_in_crit] = "in%d_crit",
0498     [hwmon_in_average] = "in%d_average",
0499     [hwmon_in_lowest] = "in%d_lowest",
0500     [hwmon_in_highest] = "in%d_highest",
0501     [hwmon_in_reset_history] = "in%d_reset_history",
0502     [hwmon_in_label] = "in%d_label",
0503     [hwmon_in_alarm] = "in%d_alarm",
0504     [hwmon_in_min_alarm] = "in%d_min_alarm",
0505     [hwmon_in_max_alarm] = "in%d_max_alarm",
0506     [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
0507     [hwmon_in_crit_alarm] = "in%d_crit_alarm",
0508     [hwmon_in_rated_min] = "in%d_rated_min",
0509     [hwmon_in_rated_max] = "in%d_rated_max",
0510 };
0511 
0512 static const char * const hwmon_curr_attr_templates[] = {
0513     [hwmon_curr_enable] = "curr%d_enable",
0514     [hwmon_curr_input] = "curr%d_input",
0515     [hwmon_curr_min] = "curr%d_min",
0516     [hwmon_curr_max] = "curr%d_max",
0517     [hwmon_curr_lcrit] = "curr%d_lcrit",
0518     [hwmon_curr_crit] = "curr%d_crit",
0519     [hwmon_curr_average] = "curr%d_average",
0520     [hwmon_curr_lowest] = "curr%d_lowest",
0521     [hwmon_curr_highest] = "curr%d_highest",
0522     [hwmon_curr_reset_history] = "curr%d_reset_history",
0523     [hwmon_curr_label] = "curr%d_label",
0524     [hwmon_curr_alarm] = "curr%d_alarm",
0525     [hwmon_curr_min_alarm] = "curr%d_min_alarm",
0526     [hwmon_curr_max_alarm] = "curr%d_max_alarm",
0527     [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
0528     [hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
0529     [hwmon_curr_rated_min] = "curr%d_rated_min",
0530     [hwmon_curr_rated_max] = "curr%d_rated_max",
0531 };
0532 
0533 static const char * const hwmon_power_attr_templates[] = {
0534     [hwmon_power_enable] = "power%d_enable",
0535     [hwmon_power_average] = "power%d_average",
0536     [hwmon_power_average_interval] = "power%d_average_interval",
0537     [hwmon_power_average_interval_max] = "power%d_interval_max",
0538     [hwmon_power_average_interval_min] = "power%d_interval_min",
0539     [hwmon_power_average_highest] = "power%d_average_highest",
0540     [hwmon_power_average_lowest] = "power%d_average_lowest",
0541     [hwmon_power_average_max] = "power%d_average_max",
0542     [hwmon_power_average_min] = "power%d_average_min",
0543     [hwmon_power_input] = "power%d_input",
0544     [hwmon_power_input_highest] = "power%d_input_highest",
0545     [hwmon_power_input_lowest] = "power%d_input_lowest",
0546     [hwmon_power_reset_history] = "power%d_reset_history",
0547     [hwmon_power_accuracy] = "power%d_accuracy",
0548     [hwmon_power_cap] = "power%d_cap",
0549     [hwmon_power_cap_hyst] = "power%d_cap_hyst",
0550     [hwmon_power_cap_max] = "power%d_cap_max",
0551     [hwmon_power_cap_min] = "power%d_cap_min",
0552     [hwmon_power_min] = "power%d_min",
0553     [hwmon_power_max] = "power%d_max",
0554     [hwmon_power_lcrit] = "power%d_lcrit",
0555     [hwmon_power_crit] = "power%d_crit",
0556     [hwmon_power_label] = "power%d_label",
0557     [hwmon_power_alarm] = "power%d_alarm",
0558     [hwmon_power_cap_alarm] = "power%d_cap_alarm",
0559     [hwmon_power_min_alarm] = "power%d_min_alarm",
0560     [hwmon_power_max_alarm] = "power%d_max_alarm",
0561     [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
0562     [hwmon_power_crit_alarm] = "power%d_crit_alarm",
0563     [hwmon_power_rated_min] = "power%d_rated_min",
0564     [hwmon_power_rated_max] = "power%d_rated_max",
0565 };
0566 
0567 static const char * const hwmon_energy_attr_templates[] = {
0568     [hwmon_energy_enable] = "energy%d_enable",
0569     [hwmon_energy_input] = "energy%d_input",
0570     [hwmon_energy_label] = "energy%d_label",
0571 };
0572 
0573 static const char * const hwmon_humidity_attr_templates[] = {
0574     [hwmon_humidity_enable] = "humidity%d_enable",
0575     [hwmon_humidity_input] = "humidity%d_input",
0576     [hwmon_humidity_label] = "humidity%d_label",
0577     [hwmon_humidity_min] = "humidity%d_min",
0578     [hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
0579     [hwmon_humidity_max] = "humidity%d_max",
0580     [hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
0581     [hwmon_humidity_alarm] = "humidity%d_alarm",
0582     [hwmon_humidity_fault] = "humidity%d_fault",
0583     [hwmon_humidity_rated_min] = "humidity%d_rated_min",
0584     [hwmon_humidity_rated_max] = "humidity%d_rated_max",
0585 };
0586 
0587 static const char * const hwmon_fan_attr_templates[] = {
0588     [hwmon_fan_enable] = "fan%d_enable",
0589     [hwmon_fan_input] = "fan%d_input",
0590     [hwmon_fan_label] = "fan%d_label",
0591     [hwmon_fan_min] = "fan%d_min",
0592     [hwmon_fan_max] = "fan%d_max",
0593     [hwmon_fan_div] = "fan%d_div",
0594     [hwmon_fan_pulses] = "fan%d_pulses",
0595     [hwmon_fan_target] = "fan%d_target",
0596     [hwmon_fan_alarm] = "fan%d_alarm",
0597     [hwmon_fan_min_alarm] = "fan%d_min_alarm",
0598     [hwmon_fan_max_alarm] = "fan%d_max_alarm",
0599     [hwmon_fan_fault] = "fan%d_fault",
0600 };
0601 
0602 static const char * const hwmon_pwm_attr_templates[] = {
0603     [hwmon_pwm_input] = "pwm%d",
0604     [hwmon_pwm_enable] = "pwm%d_enable",
0605     [hwmon_pwm_mode] = "pwm%d_mode",
0606     [hwmon_pwm_freq] = "pwm%d_freq",
0607     [hwmon_pwm_auto_channels_temp] = "pwm%d_auto_channels_temp",
0608 };
0609 
0610 static const char * const hwmon_intrusion_attr_templates[] = {
0611     [hwmon_intrusion_alarm] = "intrusion%d_alarm",
0612     [hwmon_intrusion_beep]  = "intrusion%d_beep",
0613 };
0614 
0615 static const char * const *__templates[] = {
0616     [hwmon_chip] = hwmon_chip_attrs,
0617     [hwmon_temp] = hwmon_temp_attr_templates,
0618     [hwmon_in] = hwmon_in_attr_templates,
0619     [hwmon_curr] = hwmon_curr_attr_templates,
0620     [hwmon_power] = hwmon_power_attr_templates,
0621     [hwmon_energy] = hwmon_energy_attr_templates,
0622     [hwmon_humidity] = hwmon_humidity_attr_templates,
0623     [hwmon_fan] = hwmon_fan_attr_templates,
0624     [hwmon_pwm] = hwmon_pwm_attr_templates,
0625     [hwmon_intrusion] = hwmon_intrusion_attr_templates,
0626 };
0627 
0628 static const int __templates_size[] = {
0629     [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
0630     [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
0631     [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
0632     [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
0633     [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
0634     [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
0635     [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
0636     [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
0637     [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
0638     [hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
0639 };
0640 
0641 int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
0642                u32 attr, int channel)
0643 {
0644     char event[MAX_SYSFS_ATTR_NAME_LENGTH + 5];
0645     char sattr[MAX_SYSFS_ATTR_NAME_LENGTH];
0646     char *envp[] = { event, NULL };
0647     const char * const *templates;
0648     const char *template;
0649     int base;
0650 
0651     if (type >= ARRAY_SIZE(__templates))
0652         return -EINVAL;
0653     if (attr >= __templates_size[type])
0654         return -EINVAL;
0655 
0656     templates = __templates[type];
0657     template = templates[attr];
0658 
0659     base = hwmon_attr_base(type);
0660 
0661     scnprintf(sattr, MAX_SYSFS_ATTR_NAME_LENGTH, template, base + channel);
0662     scnprintf(event, sizeof(event), "NAME=%s", sattr);
0663     sysfs_notify(&dev->kobj, NULL, sattr);
0664     kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
0665 
0666     if (type == hwmon_temp)
0667         hwmon_thermal_notify(dev, channel);
0668 
0669     return 0;
0670 }
0671 EXPORT_SYMBOL_GPL(hwmon_notify_event);
0672 
0673 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
0674 {
0675     int i, n;
0676 
0677     for (i = n = 0; info->config[i]; i++)
0678         n += hweight32(info->config[i]);
0679 
0680     return n;
0681 }
0682 
0683 static int hwmon_genattrs(const void *drvdata,
0684               struct attribute **attrs,
0685               const struct hwmon_ops *ops,
0686               const struct hwmon_channel_info *info)
0687 {
0688     const char * const *templates;
0689     int template_size;
0690     int i, aindex = 0;
0691 
0692     if (info->type >= ARRAY_SIZE(__templates))
0693         return -EINVAL;
0694 
0695     templates = __templates[info->type];
0696     template_size = __templates_size[info->type];
0697 
0698     for (i = 0; info->config[i]; i++) {
0699         u32 attr_mask = info->config[i];
0700         u32 attr;
0701 
0702         while (attr_mask) {
0703             struct attribute *a;
0704 
0705             attr = __ffs(attr_mask);
0706             attr_mask &= ~BIT(attr);
0707             if (attr >= template_size)
0708                 return -EINVAL;
0709             a = hwmon_genattr(drvdata, info->type, attr, i,
0710                       templates[attr], ops);
0711             if (IS_ERR(a)) {
0712                 if (PTR_ERR(a) != -ENOENT)
0713                     return PTR_ERR(a);
0714                 continue;
0715             }
0716             attrs[aindex++] = a;
0717         }
0718     }
0719     return aindex;
0720 }
0721 
0722 static struct attribute **
0723 __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
0724 {
0725     int ret, i, aindex = 0, nattrs = 0;
0726     struct attribute **attrs;
0727 
0728     for (i = 0; chip->info[i]; i++)
0729         nattrs += hwmon_num_channel_attrs(chip->info[i]);
0730 
0731     if (nattrs == 0)
0732         return ERR_PTR(-EINVAL);
0733 
0734     attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
0735     if (!attrs)
0736         return ERR_PTR(-ENOMEM);
0737 
0738     for (i = 0; chip->info[i]; i++) {
0739         ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
0740                      chip->info[i]);
0741         if (ret < 0) {
0742             hwmon_free_attrs(attrs);
0743             return ERR_PTR(ret);
0744         }
0745         aindex += ret;
0746     }
0747 
0748     return attrs;
0749 }
0750 
0751 static struct device *
0752 __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
0753             const struct hwmon_chip_info *chip,
0754             const struct attribute_group **groups)
0755 {
0756     struct hwmon_device *hwdev;
0757     const char *label;
0758     struct device *hdev;
0759     int i, err, id;
0760 
0761     /* Complain about invalid characters in hwmon name attribute */
0762     if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
0763         dev_warn(dev,
0764              "hwmon: '%s' is not a valid name attribute, please fix\n",
0765              name);
0766 
0767     id = ida_alloc(&hwmon_ida, GFP_KERNEL);
0768     if (id < 0)
0769         return ERR_PTR(id);
0770 
0771     hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
0772     if (hwdev == NULL) {
0773         err = -ENOMEM;
0774         goto ida_remove;
0775     }
0776 
0777     hdev = &hwdev->dev;
0778 
0779     if (chip) {
0780         struct attribute **attrs;
0781         int ngroups = 2; /* terminating NULL plus &hwdev->groups */
0782 
0783         if (groups)
0784             for (i = 0; groups[i]; i++)
0785                 ngroups++;
0786 
0787         hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
0788         if (!hwdev->groups) {
0789             err = -ENOMEM;
0790             goto free_hwmon;
0791         }
0792 
0793         attrs = __hwmon_create_attrs(drvdata, chip);
0794         if (IS_ERR(attrs)) {
0795             err = PTR_ERR(attrs);
0796             goto free_hwmon;
0797         }
0798 
0799         hwdev->group.attrs = attrs;
0800         ngroups = 0;
0801         hwdev->groups[ngroups++] = &hwdev->group;
0802 
0803         if (groups) {
0804             for (i = 0; groups[i]; i++)
0805                 hwdev->groups[ngroups++] = groups[i];
0806         }
0807 
0808         hdev->groups = hwdev->groups;
0809     } else {
0810         hdev->groups = groups;
0811     }
0812 
0813     if (dev && device_property_present(dev, "label")) {
0814         err = device_property_read_string(dev, "label", &label);
0815         if (err < 0)
0816             goto free_hwmon;
0817 
0818         hwdev->label = kstrdup(label, GFP_KERNEL);
0819         if (hwdev->label == NULL) {
0820             err = -ENOMEM;
0821             goto free_hwmon;
0822         }
0823     }
0824 
0825     hwdev->name = name;
0826     hdev->class = &hwmon_class;
0827     hdev->parent = dev;
0828     hdev->of_node = dev ? dev->of_node : NULL;
0829     hwdev->chip = chip;
0830     dev_set_drvdata(hdev, drvdata);
0831     dev_set_name(hdev, HWMON_ID_FORMAT, id);
0832     err = device_register(hdev);
0833     if (err) {
0834         put_device(hdev);
0835         goto ida_remove;
0836     }
0837 
0838     INIT_LIST_HEAD(&hwdev->tzdata);
0839 
0840     if (dev && dev->of_node && chip && chip->ops->read &&
0841         chip->info[0]->type == hwmon_chip &&
0842         (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
0843         err = hwmon_thermal_register_sensors(hdev);
0844         if (err) {
0845             device_unregister(hdev);
0846             /*
0847              * Don't worry about hwdev; hwmon_dev_release(), called
0848              * from device_unregister(), will free it.
0849              */
0850             goto ida_remove;
0851         }
0852     }
0853 
0854     return hdev;
0855 
0856 free_hwmon:
0857     hwmon_dev_release(hdev);
0858 ida_remove:
0859     ida_free(&hwmon_ida, id);
0860     return ERR_PTR(err);
0861 }
0862 
0863 /**
0864  * hwmon_device_register_with_groups - register w/ hwmon
0865  * @dev: the parent device
0866  * @name: hwmon name attribute
0867  * @drvdata: driver data to attach to created device
0868  * @groups: List of attribute groups to create
0869  *
0870  * hwmon_device_unregister() must be called when the device is no
0871  * longer needed.
0872  *
0873  * Returns the pointer to the new device.
0874  */
0875 struct device *
0876 hwmon_device_register_with_groups(struct device *dev, const char *name,
0877                   void *drvdata,
0878                   const struct attribute_group **groups)
0879 {
0880     if (!name)
0881         return ERR_PTR(-EINVAL);
0882 
0883     return __hwmon_device_register(dev, name, drvdata, NULL, groups);
0884 }
0885 EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
0886 
0887 /**
0888  * hwmon_device_register_with_info - register w/ hwmon
0889  * @dev: the parent device (mandatory)
0890  * @name: hwmon name attribute (mandatory)
0891  * @drvdata: driver data to attach to created device (optional)
0892  * @chip: pointer to hwmon chip information (mandatory)
0893  * @extra_groups: pointer to list of additional non-standard attribute groups
0894  *  (optional)
0895  *
0896  * hwmon_device_unregister() must be called when the device is no
0897  * longer needed.
0898  *
0899  * Returns the pointer to the new device.
0900  */
0901 struct device *
0902 hwmon_device_register_with_info(struct device *dev, const char *name,
0903                 void *drvdata,
0904                 const struct hwmon_chip_info *chip,
0905                 const struct attribute_group **extra_groups)
0906 {
0907     if (!dev || !name || !chip)
0908         return ERR_PTR(-EINVAL);
0909 
0910     if (!chip->ops || !chip->ops->is_visible || !chip->info)
0911         return ERR_PTR(-EINVAL);
0912 
0913     return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
0914 }
0915 EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
0916 
0917 /**
0918  * hwmon_device_register_for_thermal - register hwmon device for thermal subsystem
0919  * @dev: the parent device
0920  * @name: hwmon name attribute
0921  * @drvdata: driver data to attach to created device
0922  *
0923  * The use of this function is restricted. It is provided for legacy reasons
0924  * and must only be called from the thermal subsystem.
0925  *
0926  * hwmon_device_unregister() must be called when the device is no
0927  * longer needed.
0928  *
0929  * Returns the pointer to the new device.
0930  */
0931 struct device *
0932 hwmon_device_register_for_thermal(struct device *dev, const char *name,
0933                   void *drvdata)
0934 {
0935     if (!name || !dev)
0936         return ERR_PTR(-EINVAL);
0937 
0938     return __hwmon_device_register(dev, name, drvdata, NULL, NULL);
0939 }
0940 EXPORT_SYMBOL_NS_GPL(hwmon_device_register_for_thermal, HWMON_THERMAL);
0941 
0942 /**
0943  * hwmon_device_register - register w/ hwmon
0944  * @dev: the device to register
0945  *
0946  * hwmon_device_unregister() must be called when the device is no
0947  * longer needed.
0948  *
0949  * Returns the pointer to the new device.
0950  */
0951 struct device *hwmon_device_register(struct device *dev)
0952 {
0953     dev_warn(dev,
0954          "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
0955 
0956     return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
0957 }
0958 EXPORT_SYMBOL_GPL(hwmon_device_register);
0959 
0960 /**
0961  * hwmon_device_unregister - removes the previously registered class device
0962  *
0963  * @dev: the class device to destroy
0964  */
0965 void hwmon_device_unregister(struct device *dev)
0966 {
0967     int id;
0968 
0969     if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
0970         device_unregister(dev);
0971         ida_free(&hwmon_ida, id);
0972     } else
0973         dev_dbg(dev->parent,
0974             "hwmon_device_unregister() failed: bad class ID!\n");
0975 }
0976 EXPORT_SYMBOL_GPL(hwmon_device_unregister);
0977 
0978 static void devm_hwmon_release(struct device *dev, void *res)
0979 {
0980     struct device *hwdev = *(struct device **)res;
0981 
0982     hwmon_device_unregister(hwdev);
0983 }
0984 
0985 /**
0986  * devm_hwmon_device_register_with_groups - register w/ hwmon
0987  * @dev: the parent device
0988  * @name: hwmon name attribute
0989  * @drvdata: driver data to attach to created device
0990  * @groups: List of attribute groups to create
0991  *
0992  * Returns the pointer to the new device. The new device is automatically
0993  * unregistered with the parent device.
0994  */
0995 struct device *
0996 devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
0997                        void *drvdata,
0998                        const struct attribute_group **groups)
0999 {
1000     struct device **ptr, *hwdev;
1001 
1002     if (!dev)
1003         return ERR_PTR(-EINVAL);
1004 
1005     ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
1006     if (!ptr)
1007         return ERR_PTR(-ENOMEM);
1008 
1009     hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
1010     if (IS_ERR(hwdev))
1011         goto error;
1012 
1013     *ptr = hwdev;
1014     devres_add(dev, ptr);
1015     return hwdev;
1016 
1017 error:
1018     devres_free(ptr);
1019     return hwdev;
1020 }
1021 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
1022 
1023 /**
1024  * devm_hwmon_device_register_with_info - register w/ hwmon
1025  * @dev:    the parent device
1026  * @name:   hwmon name attribute
1027  * @drvdata:    driver data to attach to created device
1028  * @chip:   pointer to hwmon chip information
1029  * @groups: pointer to list of driver specific attribute groups
1030  *
1031  * Returns the pointer to the new device. The new device is automatically
1032  * unregistered with the parent device.
1033  */
1034 struct device *
1035 devm_hwmon_device_register_with_info(struct device *dev, const char *name,
1036                      void *drvdata,
1037                      const struct hwmon_chip_info *chip,
1038                      const struct attribute_group **groups)
1039 {
1040     struct device **ptr, *hwdev;
1041 
1042     if (!dev)
1043         return ERR_PTR(-EINVAL);
1044 
1045     ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
1046     if (!ptr)
1047         return ERR_PTR(-ENOMEM);
1048 
1049     hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
1050                         groups);
1051     if (IS_ERR(hwdev))
1052         goto error;
1053 
1054     *ptr = hwdev;
1055     devres_add(dev, ptr);
1056 
1057     return hwdev;
1058 
1059 error:
1060     devres_free(ptr);
1061     return hwdev;
1062 }
1063 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
1064 
1065 static int devm_hwmon_match(struct device *dev, void *res, void *data)
1066 {
1067     struct device **hwdev = res;
1068 
1069     return *hwdev == data;
1070 }
1071 
1072 /**
1073  * devm_hwmon_device_unregister - removes a previously registered hwmon device
1074  *
1075  * @dev: the parent device of the device to unregister
1076  */
1077 void devm_hwmon_device_unregister(struct device *dev)
1078 {
1079     WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
1080 }
1081 EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
1082 
1083 static char *__hwmon_sanitize_name(struct device *dev, const char *old_name)
1084 {
1085     char *name, *p;
1086 
1087     if (dev)
1088         name = devm_kstrdup(dev, old_name, GFP_KERNEL);
1089     else
1090         name = kstrdup(old_name, GFP_KERNEL);
1091     if (!name)
1092         return ERR_PTR(-ENOMEM);
1093 
1094     for (p = name; *p; p++)
1095         if (hwmon_is_bad_char(*p))
1096             *p = '_';
1097 
1098     return name;
1099 }
1100 
1101 /**
1102  * hwmon_sanitize_name - Replaces invalid characters in a hwmon name
1103  * @name: NUL-terminated name
1104  *
1105  * Allocates a new string where any invalid characters will be replaced
1106  * by an underscore. It is the responsibility of the caller to release
1107  * the memory.
1108  *
1109  * Returns newly allocated name, or ERR_PTR on error.
1110  */
1111 char *hwmon_sanitize_name(const char *name)
1112 {
1113     return __hwmon_sanitize_name(NULL, name);
1114 }
1115 EXPORT_SYMBOL_GPL(hwmon_sanitize_name);
1116 
1117 /**
1118  * devm_hwmon_sanitize_name - resource managed hwmon_sanitize_name()
1119  * @dev: device to allocate memory for
1120  * @name: NUL-terminated name
1121  *
1122  * Allocates a new string where any invalid characters will be replaced
1123  * by an underscore.
1124  *
1125  * Returns newly allocated name, or ERR_PTR on error.
1126  */
1127 char *devm_hwmon_sanitize_name(struct device *dev, const char *name)
1128 {
1129     if (!dev)
1130         return ERR_PTR(-EINVAL);
1131 
1132     return __hwmon_sanitize_name(dev, name);
1133 }
1134 EXPORT_SYMBOL_GPL(devm_hwmon_sanitize_name);
1135 
1136 static void __init hwmon_pci_quirks(void)
1137 {
1138 #if defined CONFIG_X86 && defined CONFIG_PCI
1139     struct pci_dev *sb;
1140     u16 base;
1141     u8 enable;
1142 
1143     /* Open access to 0x295-0x296 on MSI MS-7031 */
1144     sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
1145     if (sb) {
1146         if (sb->subsystem_vendor == 0x1462 &&   /* MSI */
1147             sb->subsystem_device == 0x0031) {   /* MS-7031 */
1148             pci_read_config_byte(sb, 0x48, &enable);
1149             pci_read_config_word(sb, 0x64, &base);
1150 
1151             if (base == 0 && !(enable & BIT(2))) {
1152                 dev_info(&sb->dev,
1153                      "Opening wide generic port at 0x295\n");
1154                 pci_write_config_word(sb, 0x64, 0x295);
1155                 pci_write_config_byte(sb, 0x48,
1156                               enable | BIT(2));
1157             }
1158         }
1159         pci_dev_put(sb);
1160     }
1161 #endif
1162 }
1163 
1164 static int __init hwmon_init(void)
1165 {
1166     int err;
1167 
1168     hwmon_pci_quirks();
1169 
1170     err = class_register(&hwmon_class);
1171     if (err) {
1172         pr_err("couldn't register hwmon sysfs class\n");
1173         return err;
1174     }
1175     return 0;
1176 }
1177 
1178 static void __exit hwmon_exit(void)
1179 {
1180     class_unregister(&hwmon_class);
1181 }
1182 
1183 subsys_initcall(hwmon_init);
1184 module_exit(hwmon_exit);
1185 
1186 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
1187 MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
1188 MODULE_LICENSE("GPL");
1189