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