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