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