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