Linux Audio

Check our new training course

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