cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

hwmon.c (31733B)


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