1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring
4  *
5  * This file defines the sysfs class "hwmon", for use by sensors drivers.
6  *
7  * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
8  */
9 
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 
12 #include <linux/bitops.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/gfp.h>
16 #include <linux/hwmon.h>
17 #include <linux/i2c.h>
18 #include <linux/idr.h>
19 #include <linux/kstrtox.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/pci.h>
23 #include <linux/property.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/thermal.h>
27 
28 #define CREATE_TRACE_POINTS
29 #include <trace/events/hwmon.h>
30 
31 #define HWMON_ID_PREFIX "hwmon"
32 #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
33 
34 struct hwmon_device {
35 	const char *name;
36 	const char *label;
37 	struct device dev;
38 	const struct hwmon_chip_info *chip;
39 	struct list_head tzdata;
40 	struct attribute_group group;
41 	const struct attribute_group **groups;
42 };
43 
44 #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
45 
46 #define MAX_SYSFS_ATTR_NAME_LENGTH	32
47 
48 struct hwmon_device_attribute {
49 	struct device_attribute dev_attr;
50 	const struct hwmon_ops *ops;
51 	enum hwmon_sensor_types type;
52 	u32 attr;
53 	int index;
54 	char name[MAX_SYSFS_ATTR_NAME_LENGTH];
55 };
56 
57 #define to_hwmon_attr(d) \
58 	container_of(d, struct hwmon_device_attribute, dev_attr)
59 #define to_dev_attr(a) container_of(a, struct device_attribute, attr)
60 
61 /*
62  * Thermal zone information
63  */
64 struct hwmon_thermal_data {
65 	struct list_head node;		/* hwmon tzdata list entry */
66 	struct device *dev;		/* Reference to hwmon device */
67 	int index;			/* sensor index */
68 	struct thermal_zone_device *tzd;/* thermal zone device */
69 };
70 
71 static ssize_t
name_show(struct device * dev,struct device_attribute * attr,char * buf)72 name_show(struct device *dev, struct device_attribute *attr, char *buf)
73 {
74 	return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
75 }
76 static DEVICE_ATTR_RO(name);
77 
78 static ssize_t
label_show(struct device * dev,struct device_attribute * attr,char * buf)79 label_show(struct device *dev, struct device_attribute *attr, char *buf)
80 {
81 	return sysfs_emit(buf, "%s\n", to_hwmon_device(dev)->label);
82 }
83 static DEVICE_ATTR_RO(label);
84 
85 static struct attribute *hwmon_dev_attrs[] = {
86 	&dev_attr_name.attr,
87 	&dev_attr_label.attr,
88 	NULL
89 };
90 
hwmon_dev_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)91 static umode_t hwmon_dev_attr_is_visible(struct kobject *kobj,
92 					 struct attribute *attr, int n)
93 {
94 	struct device *dev = kobj_to_dev(kobj);
95 	struct hwmon_device *hdev = to_hwmon_device(dev);
96 
97 	if (attr == &dev_attr_name.attr && hdev->name == NULL)
98 		return 0;
99 
100 	if (attr == &dev_attr_label.attr && hdev->label == NULL)
101 		return 0;
102 
103 	return attr->mode;
104 }
105 
106 static const struct attribute_group hwmon_dev_attr_group = {
107 	.attrs		= hwmon_dev_attrs,
108 	.is_visible	= hwmon_dev_attr_is_visible,
109 };
110 
111 static const struct attribute_group *hwmon_dev_attr_groups[] = {
112 	&hwmon_dev_attr_group,
113 	NULL
114 };
115 
hwmon_free_attrs(struct attribute ** attrs)116 static void hwmon_free_attrs(struct attribute **attrs)
117 {
118 	int i;
119 
120 	for (i = 0; attrs[i]; i++) {
121 		struct device_attribute *dattr = to_dev_attr(attrs[i]);
122 		struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr);
123 
124 		kfree(hattr);
125 	}
126 	kfree(attrs);
127 }
128 
hwmon_dev_release(struct device * dev)129 static void hwmon_dev_release(struct device *dev)
130 {
131 	struct hwmon_device *hwdev = to_hwmon_device(dev);
132 
133 	if (hwdev->group.attrs)
134 		hwmon_free_attrs(hwdev->group.attrs);
135 	kfree(hwdev->groups);
136 	kfree(hwdev->label);
137 	kfree(hwdev);
138 }
139 
140 static const struct class hwmon_class = {
141 	.name = "hwmon",
142 	.dev_groups = hwmon_dev_attr_groups,
143 	.dev_release = hwmon_dev_release,
144 };
145 
146 static DEFINE_IDA(hwmon_ida);
147 
148 /* Thermal zone handling */
149 
150 /*
151  * The complex conditional is necessary to avoid a cyclic dependency
152  * between hwmon and thermal_sys modules.
153  */
154 #ifdef CONFIG_THERMAL_OF
hwmon_thermal_get_temp(struct thermal_zone_device * tz,int * temp)155 static int hwmon_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
156 {
157 	struct hwmon_thermal_data *tdata = thermal_zone_device_priv(tz);
158 	struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
159 	int ret;
160 	long t;
161 
162 	ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input,
163 				     tdata->index, &t);
164 	if (ret < 0)
165 		return ret;
166 
167 	*temp = t;
168 
169 	return 0;
170 }
171 
hwmon_thermal_set_trips(struct thermal_zone_device * tz,int low,int high)172 static int hwmon_thermal_set_trips(struct thermal_zone_device *tz, int low, int high)
173 {
174 	struct hwmon_thermal_data *tdata = thermal_zone_device_priv(tz);
175 	struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
176 	const struct hwmon_chip_info *chip = hwdev->chip;
177 	const struct hwmon_channel_info * const *info = chip->info;
178 	unsigned int i;
179 	int err;
180 
181 	if (!chip->ops->write)
182 		return 0;
183 
184 	for (i = 0; info[i] && info[i]->type != hwmon_temp; i++)
185 		continue;
186 
187 	if (!info[i])
188 		return 0;
189 
190 	if (info[i]->config[tdata->index] & HWMON_T_MIN) {
191 		err = chip->ops->write(tdata->dev, hwmon_temp,
192 				       hwmon_temp_min, tdata->index, low);
193 		if (err && err != -EOPNOTSUPP)
194 			return err;
195 	}
196 
197 	if (info[i]->config[tdata->index] & HWMON_T_MAX) {
198 		err = chip->ops->write(tdata->dev, hwmon_temp,
199 				       hwmon_temp_max, tdata->index, high);
200 		if (err && err != -EOPNOTSUPP)
201 			return err;
202 	}
203 
204 	return 0;
205 }
206 
207 static const struct thermal_zone_device_ops hwmon_thermal_ops = {
208 	.get_temp = hwmon_thermal_get_temp,
209 	.set_trips = hwmon_thermal_set_trips,
210 };
211 
hwmon_thermal_remove_sensor(void * data)212 static void hwmon_thermal_remove_sensor(void *data)
213 {
214 	list_del(data);
215 }
216 
hwmon_thermal_add_sensor(struct device * dev,int index)217 static int hwmon_thermal_add_sensor(struct device *dev, int index)
218 {
219 	struct hwmon_device *hwdev = to_hwmon_device(dev);
220 	struct hwmon_thermal_data *tdata;
221 	struct thermal_zone_device *tzd;
222 	int err;
223 
224 	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
225 	if (!tdata)
226 		return -ENOMEM;
227 
228 	tdata->dev = dev;
229 	tdata->index = index;
230 
231 	tzd = devm_thermal_of_zone_register(dev, index, tdata,
232 					    &hwmon_thermal_ops);
233 	if (IS_ERR(tzd)) {
234 		if (PTR_ERR(tzd) != -ENODEV)
235 			return PTR_ERR(tzd);
236 		dev_info(dev, "temp%d_input not attached to any thermal zone\n",
237 			 index + 1);
238 		devm_kfree(dev, tdata);
239 		return 0;
240 	}
241 
242 	err = devm_add_action(dev, hwmon_thermal_remove_sensor, &tdata->node);
243 	if (err)
244 		return err;
245 
246 	tdata->tzd = tzd;
247 	list_add(&tdata->node, &hwdev->tzdata);
248 
249 	return 0;
250 }
251 
hwmon_thermal_register_sensors(struct device * dev)252 static int hwmon_thermal_register_sensors(struct device *dev)
253 {
254 	struct hwmon_device *hwdev = to_hwmon_device(dev);
255 	const struct hwmon_chip_info *chip = hwdev->chip;
256 	const struct hwmon_channel_info * const *info = chip->info;
257 	void *drvdata = dev_get_drvdata(dev);
258 	int i;
259 
260 	for (i = 1; info[i]; i++) {
261 		int j;
262 
263 		if (info[i]->type != hwmon_temp)
264 			continue;
265 
266 		for (j = 0; info[i]->config[j]; j++) {
267 			int err;
268 
269 			if (!(info[i]->config[j] & HWMON_T_INPUT) ||
270 			    !chip->ops->is_visible(drvdata, hwmon_temp,
271 						   hwmon_temp_input, j))
272 				continue;
273 
274 			err = hwmon_thermal_add_sensor(dev, j);
275 			if (err)
276 				return err;
277 		}
278 	}
279 
280 	return 0;
281 }
282 
hwmon_thermal_notify(struct device * dev,int index)283 static void hwmon_thermal_notify(struct device *dev, int index)
284 {
285 	struct hwmon_device *hwdev = to_hwmon_device(dev);
286 	struct hwmon_thermal_data *tzdata;
287 
288 	list_for_each_entry(tzdata, &hwdev->tzdata, node) {
289 		if (tzdata->index == index) {
290 			thermal_zone_device_update(tzdata->tzd,
291 						   THERMAL_EVENT_UNSPECIFIED);
292 		}
293 	}
294 }
295 
296 #else
hwmon_thermal_register_sensors(struct device * dev)297 static int hwmon_thermal_register_sensors(struct device *dev)
298 {
299 	return 0;
300 }
301 
hwmon_thermal_notify(struct device * dev,int index)302 static void hwmon_thermal_notify(struct device *dev, int index) { }
303 
304 #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
305 
hwmon_attr_base(enum hwmon_sensor_types type)306 static int hwmon_attr_base(enum hwmon_sensor_types type)
307 {
308 	if (type == hwmon_in || type == hwmon_intrusion)
309 		return 0;
310 	return 1;
311 }
312 
313 #if IS_REACHABLE(CONFIG_I2C)
314 
315 /*
316  * PEC support
317  *
318  * The 'pec' attribute is attached to I2C client devices. It is only provided
319  * if the i2c controller supports PEC.
320  *
321  * The mutex ensures that PEC configuration between i2c device and the hardware
322  * is consistent. Use a single mutex because attribute writes are supposed to be
323  * rare, and maintaining a separate mutex for each hardware monitoring device
324  * would add substantial complexity to the driver for little if any gain.
325  *
326  * The hardware monitoring device is identified as child of the i2c client
327  * device. This assumes that only a single hardware monitoring device is
328  * attached to an i2c client device.
329  */
330 
331 static DEFINE_MUTEX(hwmon_pec_mutex);
332 
hwmon_match_device(struct device * dev,void * data)333 static int hwmon_match_device(struct device *dev, void *data)
334 {
335 	return dev->class == &hwmon_class;
336 }
337 
pec_show(struct device * dev,struct device_attribute * dummy,char * buf)338 static ssize_t pec_show(struct device *dev, struct device_attribute *dummy,
339 			char *buf)
340 {
341 	struct i2c_client *client = to_i2c_client(dev);
342 
343 	return sysfs_emit(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
344 }
345 
pec_store(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)346 static ssize_t pec_store(struct device *dev, struct device_attribute *devattr,
347 			 const char *buf, size_t count)
348 {
349 	struct i2c_client *client = to_i2c_client(dev);
350 	struct hwmon_device *hwdev;
351 	struct device *hdev;
352 	bool val;
353 	int err;
354 
355 	err = kstrtobool(buf, &val);
356 	if (err < 0)
357 		return err;
358 
359 	hdev = device_find_child(dev, NULL, hwmon_match_device);
360 	if (!hdev)
361 		return -ENODEV;
362 
363 	mutex_lock(&hwmon_pec_mutex);
364 
365 	/*
366 	 * If there is no write function, we assume that chip specific
367 	 * handling is not required.
368 	 */
369 	hwdev = to_hwmon_device(hdev);
370 	if (hwdev->chip->ops->write) {
371 		err = hwdev->chip->ops->write(hdev, hwmon_chip, hwmon_chip_pec, 0, val);
372 		if (err && err != -EOPNOTSUPP)
373 			goto unlock;
374 	}
375 
376 	if (!val)
377 		client->flags &= ~I2C_CLIENT_PEC;
378 	else
379 		client->flags |= I2C_CLIENT_PEC;
380 
381 	err = count;
382 unlock:
383 	mutex_unlock(&hwmon_pec_mutex);
384 	put_device(hdev);
385 
386 	return err;
387 }
388 
389 static DEVICE_ATTR_RW(pec);
390 
hwmon_remove_pec(void * dev)391 static void hwmon_remove_pec(void *dev)
392 {
393 	device_remove_file(dev, &dev_attr_pec);
394 }
395 
hwmon_pec_register(struct device * hdev)396 static int hwmon_pec_register(struct device *hdev)
397 {
398 	struct i2c_client *client = i2c_verify_client(hdev->parent);
399 	int err;
400 
401 	if (!client)
402 		return -EINVAL;
403 
404 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_PEC))
405 		return 0;
406 
407 	err = device_create_file(&client->dev, &dev_attr_pec);
408 	if (err)
409 		return err;
410 
411 	return devm_add_action_or_reset(hdev, hwmon_remove_pec, &client->dev);
412 }
413 
414 #else /* CONFIG_I2C */
hwmon_pec_register(struct device * hdev)415 static int hwmon_pec_register(struct device *hdev)
416 {
417 	return -EINVAL;
418 }
419 #endif /* CONFIG_I2C */
420 
421 /* sysfs attribute management */
422 
hwmon_attr_show(struct device * dev,struct device_attribute * devattr,char * buf)423 static ssize_t hwmon_attr_show(struct device *dev,
424 			       struct device_attribute *devattr, char *buf)
425 {
426 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
427 	long val;
428 	int ret;
429 
430 	ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
431 			       &val);
432 	if (ret < 0)
433 		return ret;
434 
435 	trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
436 			      hattr->name, val);
437 
438 	return sprintf(buf, "%ld\n", val);
439 }
440 
hwmon_attr_show_string(struct device * dev,struct device_attribute * devattr,char * buf)441 static ssize_t hwmon_attr_show_string(struct device *dev,
442 				      struct device_attribute *devattr,
443 				      char *buf)
444 {
445 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
446 	enum hwmon_sensor_types type = hattr->type;
447 	const char *s;
448 	int ret;
449 
450 	ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
451 				      hattr->index, &s);
452 	if (ret < 0)
453 		return ret;
454 
455 	trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
456 				     hattr->name, s);
457 
458 	return sprintf(buf, "%s\n", s);
459 }
460 
hwmon_attr_store(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)461 static ssize_t hwmon_attr_store(struct device *dev,
462 				struct device_attribute *devattr,
463 				const char *buf, size_t count)
464 {
465 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
466 	long val;
467 	int ret;
468 
469 	ret = kstrtol(buf, 10, &val);
470 	if (ret < 0)
471 		return ret;
472 
473 	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
474 				val);
475 	if (ret < 0)
476 		return ret;
477 
478 	trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
479 			       hattr->name, val);
480 
481 	return count;
482 }
483 
is_string_attr(enum hwmon_sensor_types type,u32 attr)484 static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
485 {
486 	return (type == hwmon_temp && attr == hwmon_temp_label) ||
487 	       (type == hwmon_in && attr == hwmon_in_label) ||
488 	       (type == hwmon_curr && attr == hwmon_curr_label) ||
489 	       (type == hwmon_power && attr == hwmon_power_label) ||
490 	       (type == hwmon_energy && attr == hwmon_energy_label) ||
491 	       (type == hwmon_humidity && attr == hwmon_humidity_label) ||
492 	       (type == hwmon_fan && attr == hwmon_fan_label);
493 }
494 
hwmon_genattr(const void * drvdata,enum hwmon_sensor_types type,u32 attr,int index,const char * template,const struct hwmon_ops * ops)495 static struct attribute *hwmon_genattr(const void *drvdata,
496 				       enum hwmon_sensor_types type,
497 				       u32 attr,
498 				       int index,
499 				       const char *template,
500 				       const struct hwmon_ops *ops)
501 {
502 	struct hwmon_device_attribute *hattr;
503 	struct device_attribute *dattr;
504 	struct attribute *a;
505 	umode_t mode;
506 	const char *name;
507 	bool is_string = is_string_attr(type, attr);
508 
509 	mode = ops->is_visible(drvdata, type, attr, index);
510 	if (!mode)
511 		return ERR_PTR(-ENOENT);
512 
513 	if ((mode & 0444) && ((is_string && !ops->read_string) ||
514 				 (!is_string && !ops->read)))
515 		return ERR_PTR(-EINVAL);
516 	if ((mode & 0222) && !ops->write)
517 		return ERR_PTR(-EINVAL);
518 
519 	hattr = kzalloc(sizeof(*hattr), GFP_KERNEL);
520 	if (!hattr)
521 		return ERR_PTR(-ENOMEM);
522 
523 	if (type == hwmon_chip) {
524 		name = template;
525 	} else {
526 		scnprintf(hattr->name, sizeof(hattr->name), template,
527 			  index + hwmon_attr_base(type));
528 		name = hattr->name;
529 	}
530 
531 	hattr->type = type;
532 	hattr->attr = attr;
533 	hattr->index = index;
534 	hattr->ops = ops;
535 
536 	dattr = &hattr->dev_attr;
537 	dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
538 	dattr->store = hwmon_attr_store;
539 
540 	a = &dattr->attr;
541 	sysfs_attr_init(a);
542 	a->name = name;
543 	a->mode = mode;
544 
545 	return a;
546 }
547 
548 /*
549  * Chip attributes are not attribute templates but actual sysfs attributes.
550  * See hwmon_genattr() for special handling.
551  */
552 static const char * const hwmon_chip_attrs[] = {
553 	[hwmon_chip_temp_reset_history] = "temp_reset_history",
554 	[hwmon_chip_in_reset_history] = "in_reset_history",
555 	[hwmon_chip_curr_reset_history] = "curr_reset_history",
556 	[hwmon_chip_power_reset_history] = "power_reset_history",
557 	[hwmon_chip_update_interval] = "update_interval",
558 	[hwmon_chip_alarms] = "alarms",
559 	[hwmon_chip_samples] = "samples",
560 	[hwmon_chip_curr_samples] = "curr_samples",
561 	[hwmon_chip_in_samples] = "in_samples",
562 	[hwmon_chip_power_samples] = "power_samples",
563 	[hwmon_chip_temp_samples] = "temp_samples",
564 	[hwmon_chip_beep_enable] = "beep_enable",
565 };
566 
567 static const char * const hwmon_temp_attr_templates[] = {
568 	[hwmon_temp_enable] = "temp%d_enable",
569 	[hwmon_temp_input] = "temp%d_input",
570 	[hwmon_temp_type] = "temp%d_type",
571 	[hwmon_temp_lcrit] = "temp%d_lcrit",
572 	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
573 	[hwmon_temp_min] = "temp%d_min",
574 	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
575 	[hwmon_temp_max] = "temp%d_max",
576 	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
577 	[hwmon_temp_crit] = "temp%d_crit",
578 	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
579 	[hwmon_temp_emergency] = "temp%d_emergency",
580 	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
581 	[hwmon_temp_alarm] = "temp%d_alarm",
582 	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
583 	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
584 	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
585 	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
586 	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
587 	[hwmon_temp_fault] = "temp%d_fault",
588 	[hwmon_temp_offset] = "temp%d_offset",
589 	[hwmon_temp_label] = "temp%d_label",
590 	[hwmon_temp_lowest] = "temp%d_lowest",
591 	[hwmon_temp_highest] = "temp%d_highest",
592 	[hwmon_temp_reset_history] = "temp%d_reset_history",
593 	[hwmon_temp_rated_min] = "temp%d_rated_min",
594 	[hwmon_temp_rated_max] = "temp%d_rated_max",
595 	[hwmon_temp_beep] = "temp%d_beep",
596 };
597 
598 static const char * const hwmon_in_attr_templates[] = {
599 	[hwmon_in_enable] = "in%d_enable",
600 	[hwmon_in_input] = "in%d_input",
601 	[hwmon_in_min] = "in%d_min",
602 	[hwmon_in_max] = "in%d_max",
603 	[hwmon_in_lcrit] = "in%d_lcrit",
604 	[hwmon_in_crit] = "in%d_crit",
605 	[hwmon_in_average] = "in%d_average",
606 	[hwmon_in_lowest] = "in%d_lowest",
607 	[hwmon_in_highest] = "in%d_highest",
608 	[hwmon_in_reset_history] = "in%d_reset_history",
609 	[hwmon_in_label] = "in%d_label",
610 	[hwmon_in_alarm] = "in%d_alarm",
611 	[hwmon_in_min_alarm] = "in%d_min_alarm",
612 	[hwmon_in_max_alarm] = "in%d_max_alarm",
613 	[hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
614 	[hwmon_in_crit_alarm] = "in%d_crit_alarm",
615 	[hwmon_in_rated_min] = "in%d_rated_min",
616 	[hwmon_in_rated_max] = "in%d_rated_max",
617 	[hwmon_in_beep] = "in%d_beep",
618 	[hwmon_in_fault] = "in%d_fault",
619 };
620 
621 static const char * const hwmon_curr_attr_templates[] = {
622 	[hwmon_curr_enable] = "curr%d_enable",
623 	[hwmon_curr_input] = "curr%d_input",
624 	[hwmon_curr_min] = "curr%d_min",
625 	[hwmon_curr_max] = "curr%d_max",
626 	[hwmon_curr_lcrit] = "curr%d_lcrit",
627 	[hwmon_curr_crit] = "curr%d_crit",
628 	[hwmon_curr_average] = "curr%d_average",
629 	[hwmon_curr_lowest] = "curr%d_lowest",
630 	[hwmon_curr_highest] = "curr%d_highest",
631 	[hwmon_curr_reset_history] = "curr%d_reset_history",
632 	[hwmon_curr_label] = "curr%d_label",
633 	[hwmon_curr_alarm] = "curr%d_alarm",
634 	[hwmon_curr_min_alarm] = "curr%d_min_alarm",
635 	[hwmon_curr_max_alarm] = "curr%d_max_alarm",
636 	[hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
637 	[hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
638 	[hwmon_curr_rated_min] = "curr%d_rated_min",
639 	[hwmon_curr_rated_max] = "curr%d_rated_max",
640 	[hwmon_curr_beep] = "curr%d_beep",
641 };
642 
643 static const char * const hwmon_power_attr_templates[] = {
644 	[hwmon_power_enable] = "power%d_enable",
645 	[hwmon_power_average] = "power%d_average",
646 	[hwmon_power_average_interval] = "power%d_average_interval",
647 	[hwmon_power_average_interval_max] = "power%d_interval_max",
648 	[hwmon_power_average_interval_min] = "power%d_interval_min",
649 	[hwmon_power_average_highest] = "power%d_average_highest",
650 	[hwmon_power_average_lowest] = "power%d_average_lowest",
651 	[hwmon_power_average_max] = "power%d_average_max",
652 	[hwmon_power_average_min] = "power%d_average_min",
653 	[hwmon_power_input] = "power%d_input",
654 	[hwmon_power_input_highest] = "power%d_input_highest",
655 	[hwmon_power_input_lowest] = "power%d_input_lowest",
656 	[hwmon_power_reset_history] = "power%d_reset_history",
657 	[hwmon_power_accuracy] = "power%d_accuracy",
658 	[hwmon_power_cap] = "power%d_cap",
659 	[hwmon_power_cap_hyst] = "power%d_cap_hyst",
660 	[hwmon_power_cap_max] = "power%d_cap_max",
661 	[hwmon_power_cap_min] = "power%d_cap_min",
662 	[hwmon_power_min] = "power%d_min",
663 	[hwmon_power_max] = "power%d_max",
664 	[hwmon_power_lcrit] = "power%d_lcrit",
665 	[hwmon_power_crit] = "power%d_crit",
666 	[hwmon_power_label] = "power%d_label",
667 	[hwmon_power_alarm] = "power%d_alarm",
668 	[hwmon_power_cap_alarm] = "power%d_cap_alarm",
669 	[hwmon_power_min_alarm] = "power%d_min_alarm",
670 	[hwmon_power_max_alarm] = "power%d_max_alarm",
671 	[hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
672 	[hwmon_power_crit_alarm] = "power%d_crit_alarm",
673 	[hwmon_power_rated_min] = "power%d_rated_min",
674 	[hwmon_power_rated_max] = "power%d_rated_max",
675 };
676 
677 static const char * const hwmon_energy_attr_templates[] = {
678 	[hwmon_energy_enable] = "energy%d_enable",
679 	[hwmon_energy_input] = "energy%d_input",
680 	[hwmon_energy_label] = "energy%d_label",
681 };
682 
683 static const char * const hwmon_humidity_attr_templates[] = {
684 	[hwmon_humidity_enable] = "humidity%d_enable",
685 	[hwmon_humidity_input] = "humidity%d_input",
686 	[hwmon_humidity_label] = "humidity%d_label",
687 	[hwmon_humidity_min] = "humidity%d_min",
688 	[hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
689 	[hwmon_humidity_max] = "humidity%d_max",
690 	[hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
691 	[hwmon_humidity_alarm] = "humidity%d_alarm",
692 	[hwmon_humidity_fault] = "humidity%d_fault",
693 	[hwmon_humidity_rated_min] = "humidity%d_rated_min",
694 	[hwmon_humidity_rated_max] = "humidity%d_rated_max",
695 	[hwmon_humidity_min_alarm] = "humidity%d_min_alarm",
696 	[hwmon_humidity_max_alarm] = "humidity%d_max_alarm",
697 };
698 
699 static const char * const hwmon_fan_attr_templates[] = {
700 	[hwmon_fan_enable] = "fan%d_enable",
701 	[hwmon_fan_input] = "fan%d_input",
702 	[hwmon_fan_label] = "fan%d_label",
703 	[hwmon_fan_min] = "fan%d_min",
704 	[hwmon_fan_max] = "fan%d_max",
705 	[hwmon_fan_div] = "fan%d_div",
706 	[hwmon_fan_pulses] = "fan%d_pulses",
707 	[hwmon_fan_target] = "fan%d_target",
708 	[hwmon_fan_alarm] = "fan%d_alarm",
709 	[hwmon_fan_min_alarm] = "fan%d_min_alarm",
710 	[hwmon_fan_max_alarm] = "fan%d_max_alarm",
711 	[hwmon_fan_fault] = "fan%d_fault",
712 	[hwmon_fan_beep] = "fan%d_beep",
713 };
714 
715 static const char * const hwmon_pwm_attr_templates[] = {
716 	[hwmon_pwm_input] = "pwm%d",
717 	[hwmon_pwm_enable] = "pwm%d_enable",
718 	[hwmon_pwm_mode] = "pwm%d_mode",
719 	[hwmon_pwm_freq] = "pwm%d_freq",
720 	[hwmon_pwm_auto_channels_temp] = "pwm%d_auto_channels_temp",
721 };
722 
723 static const char * const hwmon_intrusion_attr_templates[] = {
724 	[hwmon_intrusion_alarm] = "intrusion%d_alarm",
725 	[hwmon_intrusion_beep]  = "intrusion%d_beep",
726 };
727 
728 static const char * const *__templates[] = {
729 	[hwmon_chip] = hwmon_chip_attrs,
730 	[hwmon_temp] = hwmon_temp_attr_templates,
731 	[hwmon_in] = hwmon_in_attr_templates,
732 	[hwmon_curr] = hwmon_curr_attr_templates,
733 	[hwmon_power] = hwmon_power_attr_templates,
734 	[hwmon_energy] = hwmon_energy_attr_templates,
735 	[hwmon_humidity] = hwmon_humidity_attr_templates,
736 	[hwmon_fan] = hwmon_fan_attr_templates,
737 	[hwmon_pwm] = hwmon_pwm_attr_templates,
738 	[hwmon_intrusion] = hwmon_intrusion_attr_templates,
739 };
740 
741 static const int __templates_size[] = {
742 	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
743 	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
744 	[hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
745 	[hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
746 	[hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
747 	[hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
748 	[hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
749 	[hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
750 	[hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
751 	[hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
752 };
753 
hwmon_notify_event(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel)754 int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
755 		       u32 attr, int channel)
756 {
757 	char event[MAX_SYSFS_ATTR_NAME_LENGTH + 5];
758 	char sattr[MAX_SYSFS_ATTR_NAME_LENGTH];
759 	char *envp[] = { event, NULL };
760 	const char * const *templates;
761 	const char *template;
762 	int base;
763 
764 	if (type >= ARRAY_SIZE(__templates))
765 		return -EINVAL;
766 	if (attr >= __templates_size[type])
767 		return -EINVAL;
768 
769 	templates = __templates[type];
770 	template = templates[attr];
771 
772 	base = hwmon_attr_base(type);
773 
774 	scnprintf(sattr, MAX_SYSFS_ATTR_NAME_LENGTH, template, base + channel);
775 	scnprintf(event, sizeof(event), "NAME=%s", sattr);
776 	sysfs_notify(&dev->kobj, NULL, sattr);
777 	kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
778 
779 	if (type == hwmon_temp)
780 		hwmon_thermal_notify(dev, channel);
781 
782 	return 0;
783 }
784 EXPORT_SYMBOL_GPL(hwmon_notify_event);
785 
hwmon_num_channel_attrs(const struct hwmon_channel_info * info)786 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
787 {
788 	int i, n;
789 
790 	for (i = n = 0; info->config[i]; i++)
791 		n += hweight32(info->config[i]);
792 
793 	return n;
794 }
795 
hwmon_genattrs(const void * drvdata,struct attribute ** attrs,const struct hwmon_ops * ops,const struct hwmon_channel_info * info)796 static int hwmon_genattrs(const void *drvdata,
797 			  struct attribute **attrs,
798 			  const struct hwmon_ops *ops,
799 			  const struct hwmon_channel_info *info)
800 {
801 	const char * const *templates;
802 	int template_size;
803 	int i, aindex = 0;
804 
805 	if (info->type >= ARRAY_SIZE(__templates))
806 		return -EINVAL;
807 
808 	templates = __templates[info->type];
809 	template_size = __templates_size[info->type];
810 
811 	for (i = 0; info->config[i]; i++) {
812 		u32 attr_mask = info->config[i];
813 		u32 attr;
814 
815 		while (attr_mask) {
816 			struct attribute *a;
817 
818 			attr = __ffs(attr_mask);
819 			attr_mask &= ~BIT(attr);
820 			if (attr >= template_size || !templates[attr])
821 				continue;	/* attribute is invisible */
822 			a = hwmon_genattr(drvdata, info->type, attr, i,
823 					  templates[attr], ops);
824 			if (IS_ERR(a)) {
825 				if (PTR_ERR(a) != -ENOENT)
826 					return PTR_ERR(a);
827 				continue;
828 			}
829 			attrs[aindex++] = a;
830 		}
831 	}
832 	return aindex;
833 }
834 
835 static struct attribute **
__hwmon_create_attrs(const void * drvdata,const struct hwmon_chip_info * chip)836 __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
837 {
838 	int ret, i, aindex = 0, nattrs = 0;
839 	struct attribute **attrs;
840 
841 	for (i = 0; chip->info[i]; i++)
842 		nattrs += hwmon_num_channel_attrs(chip->info[i]);
843 
844 	if (nattrs == 0)
845 		return ERR_PTR(-EINVAL);
846 
847 	attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
848 	if (!attrs)
849 		return ERR_PTR(-ENOMEM);
850 
851 	for (i = 0; chip->info[i]; i++) {
852 		ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
853 				     chip->info[i]);
854 		if (ret < 0) {
855 			hwmon_free_attrs(attrs);
856 			return ERR_PTR(ret);
857 		}
858 		aindex += ret;
859 	}
860 
861 	return attrs;
862 }
863 
864 static struct device *
__hwmon_device_register(struct device * dev,const char * name,void * drvdata,const struct hwmon_chip_info * chip,const struct attribute_group ** groups)865 __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
866 			const struct hwmon_chip_info *chip,
867 			const struct attribute_group **groups)
868 {
869 	struct hwmon_device *hwdev;
870 	const char *label;
871 	struct device *hdev;
872 	struct device *tdev = dev;
873 	int i, err, id;
874 
875 	/* Complain about invalid characters in hwmon name attribute */
876 	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
877 		dev_warn(dev,
878 			 "hwmon: '%s' is not a valid name attribute, please fix\n",
879 			 name);
880 
881 	id = ida_alloc(&hwmon_ida, GFP_KERNEL);
882 	if (id < 0)
883 		return ERR_PTR(id);
884 
885 	hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
886 	if (hwdev == NULL) {
887 		err = -ENOMEM;
888 		goto ida_remove;
889 	}
890 
891 	hdev = &hwdev->dev;
892 
893 	if (chip) {
894 		struct attribute **attrs;
895 		int ngroups = 2; /* terminating NULL plus &hwdev->groups */
896 
897 		if (groups)
898 			for (i = 0; groups[i]; i++)
899 				ngroups++;
900 
901 		hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
902 		if (!hwdev->groups) {
903 			err = -ENOMEM;
904 			goto free_hwmon;
905 		}
906 
907 		attrs = __hwmon_create_attrs(drvdata, chip);
908 		if (IS_ERR(attrs)) {
909 			err = PTR_ERR(attrs);
910 			goto free_hwmon;
911 		}
912 
913 		hwdev->group.attrs = attrs;
914 		ngroups = 0;
915 		hwdev->groups[ngroups++] = &hwdev->group;
916 
917 		if (groups) {
918 			for (i = 0; groups[i]; i++)
919 				hwdev->groups[ngroups++] = groups[i];
920 		}
921 
922 		hdev->groups = hwdev->groups;
923 	} else {
924 		hdev->groups = groups;
925 	}
926 
927 	if (dev && device_property_present(dev, "label")) {
928 		err = device_property_read_string(dev, "label", &label);
929 		if (err < 0)
930 			goto free_hwmon;
931 
932 		hwdev->label = kstrdup(label, GFP_KERNEL);
933 		if (hwdev->label == NULL) {
934 			err = -ENOMEM;
935 			goto free_hwmon;
936 		}
937 	}
938 
939 	hwdev->name = name;
940 	hdev->class = &hwmon_class;
941 	hdev->parent = dev;
942 	while (tdev && !tdev->of_node)
943 		tdev = tdev->parent;
944 	hdev->of_node = tdev ? tdev->of_node : NULL;
945 	hwdev->chip = chip;
946 	dev_set_drvdata(hdev, drvdata);
947 	dev_set_name(hdev, HWMON_ID_FORMAT, id);
948 	err = device_register(hdev);
949 	if (err) {
950 		put_device(hdev);
951 		goto ida_remove;
952 	}
953 
954 	INIT_LIST_HEAD(&hwdev->tzdata);
955 
956 	if (hdev->of_node && chip && chip->ops->read &&
957 	    chip->info[0]->type == hwmon_chip) {
958 		u32 config = chip->info[0]->config[0];
959 
960 		if (config & HWMON_C_REGISTER_TZ) {
961 			err = hwmon_thermal_register_sensors(hdev);
962 			if (err) {
963 				device_unregister(hdev);
964 				/*
965 				 * Don't worry about hwdev; hwmon_dev_release(),
966 				 * called from device_unregister(), will free it.
967 				 */
968 				goto ida_remove;
969 			}
970 		}
971 		if (config & HWMON_C_PEC) {
972 			err = hwmon_pec_register(hdev);
973 			if (err) {
974 				device_unregister(hdev);
975 				goto ida_remove;
976 			}
977 		}
978 	}
979 
980 	return hdev;
981 
982 free_hwmon:
983 	hwmon_dev_release(hdev);
984 ida_remove:
985 	ida_free(&hwmon_ida, id);
986 	return ERR_PTR(err);
987 }
988 
989 /**
990  * hwmon_device_register_with_groups - register w/ hwmon
991  * @dev: the parent device
992  * @name: hwmon name attribute
993  * @drvdata: driver data to attach to created device
994  * @groups: List of attribute groups to create
995  *
996  * hwmon_device_unregister() must be called when the device is no
997  * longer needed.
998  *
999  * Returns the pointer to the new device.
1000  */
1001 struct device *
hwmon_device_register_with_groups(struct device * dev,const char * name,void * drvdata,const struct attribute_group ** groups)1002 hwmon_device_register_with_groups(struct device *dev, const char *name,
1003 				  void *drvdata,
1004 				  const struct attribute_group **groups)
1005 {
1006 	if (!name)
1007 		return ERR_PTR(-EINVAL);
1008 
1009 	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
1010 }
1011 EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
1012 
1013 /**
1014  * hwmon_device_register_with_info - register w/ hwmon
1015  * @dev: the parent device (mandatory)
1016  * @name: hwmon name attribute (mandatory)
1017  * @drvdata: driver data to attach to created device (optional)
1018  * @chip: pointer to hwmon chip information (mandatory)
1019  * @extra_groups: pointer to list of additional non-standard attribute groups
1020  *	(optional)
1021  *
1022  * hwmon_device_unregister() must be called when the device is no
1023  * longer needed.
1024  *
1025  * Returns the pointer to the new device.
1026  */
1027 struct device *
hwmon_device_register_with_info(struct device * dev,const char * name,void * drvdata,const struct hwmon_chip_info * chip,const struct attribute_group ** extra_groups)1028 hwmon_device_register_with_info(struct device *dev, const char *name,
1029 				void *drvdata,
1030 				const struct hwmon_chip_info *chip,
1031 				const struct attribute_group **extra_groups)
1032 {
1033 	if (!dev || !name || !chip)
1034 		return ERR_PTR(-EINVAL);
1035 
1036 	if (!chip->ops || !chip->ops->is_visible || !chip->info)
1037 		return ERR_PTR(-EINVAL);
1038 
1039 	return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
1040 }
1041 EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
1042 
1043 /**
1044  * hwmon_device_register_for_thermal - register hwmon device for thermal subsystem
1045  * @dev: the parent device
1046  * @name: hwmon name attribute
1047  * @drvdata: driver data to attach to created device
1048  *
1049  * The use of this function is restricted. It is provided for legacy reasons
1050  * and must only be called from the thermal subsystem.
1051  *
1052  * hwmon_device_unregister() must be called when the device is no
1053  * longer needed.
1054  *
1055  * Returns the pointer to the new device.
1056  */
1057 struct device *
hwmon_device_register_for_thermal(struct device * dev,const char * name,void * drvdata)1058 hwmon_device_register_for_thermal(struct device *dev, const char *name,
1059 				  void *drvdata)
1060 {
1061 	if (!name || !dev)
1062 		return ERR_PTR(-EINVAL);
1063 
1064 	return __hwmon_device_register(dev, name, drvdata, NULL, NULL);
1065 }
1066 EXPORT_SYMBOL_NS_GPL(hwmon_device_register_for_thermal, HWMON_THERMAL);
1067 
1068 /**
1069  * hwmon_device_register - register w/ hwmon
1070  * @dev: the device to register
1071  *
1072  * hwmon_device_unregister() must be called when the device is no
1073  * longer needed.
1074  *
1075  * Returns the pointer to the new device.
1076  */
hwmon_device_register(struct device * dev)1077 struct device *hwmon_device_register(struct device *dev)
1078 {
1079 	dev_warn(dev,
1080 		 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
1081 
1082 	return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
1083 }
1084 EXPORT_SYMBOL_GPL(hwmon_device_register);
1085 
1086 /**
1087  * hwmon_device_unregister - removes the previously registered class device
1088  *
1089  * @dev: the class device to destroy
1090  */
hwmon_device_unregister(struct device * dev)1091 void hwmon_device_unregister(struct device *dev)
1092 {
1093 	int id;
1094 
1095 	if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
1096 		device_unregister(dev);
1097 		ida_free(&hwmon_ida, id);
1098 	} else
1099 		dev_dbg(dev->parent,
1100 			"hwmon_device_unregister() failed: bad class ID!\n");
1101 }
1102 EXPORT_SYMBOL_GPL(hwmon_device_unregister);
1103 
devm_hwmon_release(struct device * dev,void * res)1104 static void devm_hwmon_release(struct device *dev, void *res)
1105 {
1106 	struct device *hwdev = *(struct device **)res;
1107 
1108 	hwmon_device_unregister(hwdev);
1109 }
1110 
1111 /**
1112  * devm_hwmon_device_register_with_groups - register w/ hwmon
1113  * @dev: the parent device
1114  * @name: hwmon name attribute
1115  * @drvdata: driver data to attach to created device
1116  * @groups: List of attribute groups to create
1117  *
1118  * Returns the pointer to the new device. The new device is automatically
1119  * unregistered with the parent device.
1120  */
1121 struct device *
devm_hwmon_device_register_with_groups(struct device * dev,const char * name,void * drvdata,const struct attribute_group ** groups)1122 devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
1123 				       void *drvdata,
1124 				       const struct attribute_group **groups)
1125 {
1126 	struct device **ptr, *hwdev;
1127 
1128 	if (!dev)
1129 		return ERR_PTR(-EINVAL);
1130 
1131 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
1132 	if (!ptr)
1133 		return ERR_PTR(-ENOMEM);
1134 
1135 	hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
1136 	if (IS_ERR(hwdev))
1137 		goto error;
1138 
1139 	*ptr = hwdev;
1140 	devres_add(dev, ptr);
1141 	return hwdev;
1142 
1143 error:
1144 	devres_free(ptr);
1145 	return hwdev;
1146 }
1147 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
1148 
1149 /**
1150  * devm_hwmon_device_register_with_info - register w/ hwmon
1151  * @dev:	the parent device
1152  * @name:	hwmon name attribute
1153  * @drvdata:	driver data to attach to created device
1154  * @chip:	pointer to hwmon chip information
1155  * @extra_groups: pointer to list of driver specific attribute groups
1156  *
1157  * Returns the pointer to the new device. The new device is automatically
1158  * unregistered with the parent device.
1159  */
1160 struct device *
devm_hwmon_device_register_with_info(struct device * dev,const char * name,void * drvdata,const struct hwmon_chip_info * chip,const struct attribute_group ** extra_groups)1161 devm_hwmon_device_register_with_info(struct device *dev, const char *name,
1162 				     void *drvdata,
1163 				     const struct hwmon_chip_info *chip,
1164 				     const struct attribute_group **extra_groups)
1165 {
1166 	struct device **ptr, *hwdev;
1167 
1168 	if (!dev)
1169 		return ERR_PTR(-EINVAL);
1170 
1171 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
1172 	if (!ptr)
1173 		return ERR_PTR(-ENOMEM);
1174 
1175 	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
1176 						extra_groups);
1177 	if (IS_ERR(hwdev))
1178 		goto error;
1179 
1180 	*ptr = hwdev;
1181 	devres_add(dev, ptr);
1182 
1183 	return hwdev;
1184 
1185 error:
1186 	devres_free(ptr);
1187 	return hwdev;
1188 }
1189 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
1190 
__hwmon_sanitize_name(struct device * dev,const char * old_name)1191 static char *__hwmon_sanitize_name(struct device *dev, const char *old_name)
1192 {
1193 	char *name, *p;
1194 
1195 	if (dev)
1196 		name = devm_kstrdup(dev, old_name, GFP_KERNEL);
1197 	else
1198 		name = kstrdup(old_name, GFP_KERNEL);
1199 	if (!name)
1200 		return ERR_PTR(-ENOMEM);
1201 
1202 	for (p = name; *p; p++)
1203 		if (hwmon_is_bad_char(*p))
1204 			*p = '_';
1205 
1206 	return name;
1207 }
1208 
1209 /**
1210  * hwmon_sanitize_name - Replaces invalid characters in a hwmon name
1211  * @name: NUL-terminated name
1212  *
1213  * Allocates a new string where any invalid characters will be replaced
1214  * by an underscore. It is the responsibility of the caller to release
1215  * the memory.
1216  *
1217  * Returns newly allocated name, or ERR_PTR on error.
1218  */
hwmon_sanitize_name(const char * name)1219 char *hwmon_sanitize_name(const char *name)
1220 {
1221 	return __hwmon_sanitize_name(NULL, name);
1222 }
1223 EXPORT_SYMBOL_GPL(hwmon_sanitize_name);
1224 
1225 /**
1226  * devm_hwmon_sanitize_name - resource managed hwmon_sanitize_name()
1227  * @dev: device to allocate memory for
1228  * @name: NUL-terminated name
1229  *
1230  * Allocates a new string where any invalid characters will be replaced
1231  * by an underscore.
1232  *
1233  * Returns newly allocated name, or ERR_PTR on error.
1234  */
devm_hwmon_sanitize_name(struct device * dev,const char * name)1235 char *devm_hwmon_sanitize_name(struct device *dev, const char *name)
1236 {
1237 	if (!dev)
1238 		return ERR_PTR(-EINVAL);
1239 
1240 	return __hwmon_sanitize_name(dev, name);
1241 }
1242 EXPORT_SYMBOL_GPL(devm_hwmon_sanitize_name);
1243 
hwmon_pci_quirks(void)1244 static void __init hwmon_pci_quirks(void)
1245 {
1246 #if defined CONFIG_X86 && defined CONFIG_PCI
1247 	struct pci_dev *sb;
1248 	u16 base;
1249 	u8 enable;
1250 
1251 	/* Open access to 0x295-0x296 on MSI MS-7031 */
1252 	sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
1253 	if (sb) {
1254 		if (sb->subsystem_vendor == 0x1462 &&	/* MSI */
1255 		    sb->subsystem_device == 0x0031) {	/* MS-7031 */
1256 			pci_read_config_byte(sb, 0x48, &enable);
1257 			pci_read_config_word(sb, 0x64, &base);
1258 
1259 			if (base == 0 && !(enable & BIT(2))) {
1260 				dev_info(&sb->dev,
1261 					 "Opening wide generic port at 0x295\n");
1262 				pci_write_config_word(sb, 0x64, 0x295);
1263 				pci_write_config_byte(sb, 0x48,
1264 						      enable | BIT(2));
1265 			}
1266 		}
1267 		pci_dev_put(sb);
1268 	}
1269 #endif
1270 }
1271 
hwmon_init(void)1272 static int __init hwmon_init(void)
1273 {
1274 	int err;
1275 
1276 	hwmon_pci_quirks();
1277 
1278 	err = class_register(&hwmon_class);
1279 	if (err) {
1280 		pr_err("couldn't register hwmon sysfs class\n");
1281 		return err;
1282 	}
1283 	return 0;
1284 }
1285 
hwmon_exit(void)1286 static void __exit hwmon_exit(void)
1287 {
1288 	class_unregister(&hwmon_class);
1289 }
1290 
1291 subsys_initcall(hwmon_init);
1292 module_exit(hwmon_exit);
1293 
1294 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
1295 MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
1296 MODULE_LICENSE("GPL");
1297 
1298