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