1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * hwmon driver for HP (and some HP Compaq) business-class computers that
4 * report numeric sensor data via Windows Management Instrumentation (WMI).
6 * Copyright (C) 2023 James Seo <james@equiv.tech>
9 * [1] Hewlett-Packard Development Company, L.P.,
10 * "HP Client Management Interface Technical White Paper", 2005. [Online].
11 * Available: https://h20331.www2.hp.com/hpsub/downloads/cmi_whitepaper.pdf
12 * [2] Hewlett-Packard Development Company, L.P.,
13 * "HP Retail Manageability", 2012. [Online].
14 * Available: http://h10032.www1.hp.com/ctg/Manual/c03291135.pdf
15 * [3] Linux Hardware Project, A. Ponomarenko et al.,
16 * "linuxhw/ACPI - Collect ACPI table dumps", 2018. [Online].
17 * Available: https://github.com/linuxhw/ACPI
18 * [4] P. Rohár, "bmfdec - Decompile binary MOF file (BMF) from WMI buffer",
19 * 2017. [Online]. Available: https://github.com/pali/bmfdec
22 #include <linux/acpi.h>
23 #include <linux/debugfs.h>
24 #include <linux/hwmon.h>
25 #include <linux/jiffies.h>
26 #include <linux/mutex.h>
27 #include <linux/units.h>
28 #include <linux/wmi.h>
30 #define HP_WMI_EVENT_NAMESPACE "root\\WMI"
31 #define HP_WMI_EVENT_CLASS "HPBIOS_BIOSEvent"
32 #define HP_WMI_EVENT_GUID "95F24279-4D7B-4334-9387-ACCDC67EF61C"
33 #define HP_WMI_NUMERIC_SENSOR_GUID "8F1F6435-9F42-42C8-BADC-0E9424F20C9A"
34 #define HP_WMI_PLATFORM_EVENTS_GUID "41227C2D-80E1-423F-8B8E-87E32755A0EB"
36 /* Patterns for recognizing sensors and matching events to channels. */
38 #define HP_WMI_PATTERN_SYS_TEMP "Chassis Thermal Index"
39 #define HP_WMI_PATTERN_SYS_TEMP2 "System Ambient Temperature"
40 #define HP_WMI_PATTERN_CPU_TEMP "CPU Thermal Index"
41 #define HP_WMI_PATTERN_CPU_TEMP2 "CPU Temperature"
42 #define HP_WMI_PATTERN_TEMP_SENSOR "Thermal Index"
43 #define HP_WMI_PATTERN_TEMP_ALARM "Thermal Critical"
44 #define HP_WMI_PATTERN_INTRUSION_ALARM "Hood Intrusion"
45 #define HP_WMI_PATTERN_FAN_ALARM "Stall"
46 #define HP_WMI_PATTERN_TEMP "Temperature"
47 #define HP_WMI_PATTERN_CPU "CPU"
49 /* These limits are arbitrary. The WMI implementation may vary by system. */
51 #define HP_WMI_MAX_STR_SIZE 128U
52 #define HP_WMI_MAX_PROPERTIES 32U
53 #define HP_WMI_MAX_INSTANCES 32U
56 HP_WMI_TYPE_OTHER = 1,
57 HP_WMI_TYPE_TEMPERATURE = 2,
58 HP_WMI_TYPE_VOLTAGE = 3,
59 HP_WMI_TYPE_CURRENT = 4,
60 HP_WMI_TYPE_AIR_FLOW = 12,
61 HP_WMI_TYPE_INTRUSION = 0xabadb01, /* Custom. */
64 enum hp_wmi_category {
65 HP_WMI_CATEGORY_SENSOR = 3,
68 enum hp_wmi_severity {
69 HP_WMI_SEVERITY_UNKNOWN = 0,
70 HP_WMI_SEVERITY_OK = 5,
71 HP_WMI_SEVERITY_DEGRADED_WARNING = 10,
72 HP_WMI_SEVERITY_MINOR_FAILURE = 15,
73 HP_WMI_SEVERITY_MAJOR_FAILURE = 20,
74 HP_WMI_SEVERITY_CRITICAL_FAILURE = 25,
75 HP_WMI_SEVERITY_NON_RECOVERABLE_ERROR = 30,
80 HP_WMI_STATUS_DEGRADED = 3,
81 HP_WMI_STATUS_STRESSED = 4,
82 HP_WMI_STATUS_PREDICTIVE_FAILURE = 5,
83 HP_WMI_STATUS_ERROR = 6,
84 HP_WMI_STATUS_NON_RECOVERABLE_ERROR = 7,
85 HP_WMI_STATUS_NO_CONTACT = 12,
86 HP_WMI_STATUS_LOST_COMMUNICATION = 13,
87 HP_WMI_STATUS_ABORTED = 14,
88 HP_WMI_STATUS_SUPPORTING_ENTITY_IN_ERROR = 16,
90 /* Occurs combined with one of "OK", "Degraded", and "Error" [1]. */
91 HP_WMI_STATUS_COMPLETED = 17,
95 HP_WMI_UNITS_OTHER = 1,
96 HP_WMI_UNITS_DEGREES_C = 2,
97 HP_WMI_UNITS_DEGREES_F = 3,
98 HP_WMI_UNITS_DEGREES_K = 4,
99 HP_WMI_UNITS_VOLTS = 5,
100 HP_WMI_UNITS_AMPS = 6,
101 HP_WMI_UNITS_RPM = 19,
104 enum hp_wmi_property {
105 HP_WMI_PROPERTY_NAME = 0,
106 HP_WMI_PROPERTY_DESCRIPTION = 1,
107 HP_WMI_PROPERTY_SENSOR_TYPE = 2,
108 HP_WMI_PROPERTY_OTHER_SENSOR_TYPE = 3,
109 HP_WMI_PROPERTY_OPERATIONAL_STATUS = 4,
110 HP_WMI_PROPERTY_SIZE = 5,
111 HP_WMI_PROPERTY_POSSIBLE_STATES = 6,
112 HP_WMI_PROPERTY_CURRENT_STATE = 7,
113 HP_WMI_PROPERTY_BASE_UNITS = 8,
114 HP_WMI_PROPERTY_UNIT_MODIFIER = 9,
115 HP_WMI_PROPERTY_CURRENT_READING = 10,
116 HP_WMI_PROPERTY_RATE_UNITS = 11,
119 static const acpi_object_type hp_wmi_property_map[] = {
120 [HP_WMI_PROPERTY_NAME] = ACPI_TYPE_STRING,
121 [HP_WMI_PROPERTY_DESCRIPTION] = ACPI_TYPE_STRING,
122 [HP_WMI_PROPERTY_SENSOR_TYPE] = ACPI_TYPE_INTEGER,
123 [HP_WMI_PROPERTY_OTHER_SENSOR_TYPE] = ACPI_TYPE_STRING,
124 [HP_WMI_PROPERTY_OPERATIONAL_STATUS] = ACPI_TYPE_INTEGER,
125 [HP_WMI_PROPERTY_SIZE] = ACPI_TYPE_INTEGER,
126 [HP_WMI_PROPERTY_POSSIBLE_STATES] = ACPI_TYPE_STRING,
127 [HP_WMI_PROPERTY_CURRENT_STATE] = ACPI_TYPE_STRING,
128 [HP_WMI_PROPERTY_BASE_UNITS] = ACPI_TYPE_INTEGER,
129 [HP_WMI_PROPERTY_UNIT_MODIFIER] = ACPI_TYPE_INTEGER,
130 [HP_WMI_PROPERTY_CURRENT_READING] = ACPI_TYPE_INTEGER,
131 [HP_WMI_PROPERTY_RATE_UNITS] = ACPI_TYPE_INTEGER,
134 enum hp_wmi_platform_events_property {
135 HP_WMI_PLATFORM_EVENTS_PROPERTY_NAME = 0,
136 HP_WMI_PLATFORM_EVENTS_PROPERTY_DESCRIPTION = 1,
137 HP_WMI_PLATFORM_EVENTS_PROPERTY_SOURCE_NAMESPACE = 2,
138 HP_WMI_PLATFORM_EVENTS_PROPERTY_SOURCE_CLASS = 3,
139 HP_WMI_PLATFORM_EVENTS_PROPERTY_CATEGORY = 4,
140 HP_WMI_PLATFORM_EVENTS_PROPERTY_POSSIBLE_SEVERITY = 5,
141 HP_WMI_PLATFORM_EVENTS_PROPERTY_POSSIBLE_STATUS = 6,
144 static const acpi_object_type hp_wmi_platform_events_property_map[] = {
145 [HP_WMI_PLATFORM_EVENTS_PROPERTY_NAME] = ACPI_TYPE_STRING,
146 [HP_WMI_PLATFORM_EVENTS_PROPERTY_DESCRIPTION] = ACPI_TYPE_STRING,
147 [HP_WMI_PLATFORM_EVENTS_PROPERTY_SOURCE_NAMESPACE] = ACPI_TYPE_STRING,
148 [HP_WMI_PLATFORM_EVENTS_PROPERTY_SOURCE_CLASS] = ACPI_TYPE_STRING,
149 [HP_WMI_PLATFORM_EVENTS_PROPERTY_CATEGORY] = ACPI_TYPE_INTEGER,
150 [HP_WMI_PLATFORM_EVENTS_PROPERTY_POSSIBLE_SEVERITY] = ACPI_TYPE_INTEGER,
151 [HP_WMI_PLATFORM_EVENTS_PROPERTY_POSSIBLE_STATUS] = ACPI_TYPE_INTEGER,
154 enum hp_wmi_event_property {
155 HP_WMI_EVENT_PROPERTY_NAME = 0,
156 HP_WMI_EVENT_PROPERTY_DESCRIPTION = 1,
157 HP_WMI_EVENT_PROPERTY_CATEGORY = 2,
158 HP_WMI_EVENT_PROPERTY_SEVERITY = 3,
159 HP_WMI_EVENT_PROPERTY_STATUS = 4,
162 static const acpi_object_type hp_wmi_event_property_map[] = {
163 [HP_WMI_EVENT_PROPERTY_NAME] = ACPI_TYPE_STRING,
164 [HP_WMI_EVENT_PROPERTY_DESCRIPTION] = ACPI_TYPE_STRING,
165 [HP_WMI_EVENT_PROPERTY_CATEGORY] = ACPI_TYPE_INTEGER,
166 [HP_WMI_EVENT_PROPERTY_SEVERITY] = ACPI_TYPE_INTEGER,
167 [HP_WMI_EVENT_PROPERTY_STATUS] = ACPI_TYPE_INTEGER,
170 static const enum hwmon_sensor_types hp_wmi_hwmon_type_map[] = {
171 [HP_WMI_TYPE_TEMPERATURE] = hwmon_temp,
172 [HP_WMI_TYPE_VOLTAGE] = hwmon_in,
173 [HP_WMI_TYPE_CURRENT] = hwmon_curr,
174 [HP_WMI_TYPE_AIR_FLOW] = hwmon_fan,
177 static const u32 hp_wmi_hwmon_attributes[hwmon_max] = {
178 [hwmon_chip] = HWMON_C_REGISTER_TZ,
179 [hwmon_temp] = HWMON_T_INPUT | HWMON_T_LABEL | HWMON_T_FAULT,
180 [hwmon_in] = HWMON_I_INPUT | HWMON_I_LABEL,
181 [hwmon_curr] = HWMON_C_INPUT | HWMON_C_LABEL,
182 [hwmon_fan] = HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_FAULT,
183 [hwmon_intrusion] = HWMON_INTRUSION_ALARM,
187 * struct hp_wmi_numeric_sensor - a HPBIOS_BIOSNumericSensor instance
189 * Two variants of HPBIOS_BIOSNumericSensor are known. The first is specified
190 * in [1] and appears to be much more widespread. The second was discovered by
191 * decoding BMOF blobs [4], seems to be found only in some newer ZBook systems
192 * [3], and has two new properties and a slightly different property order.
194 * These differences don't matter on Windows, where WMI object properties are
195 * accessed by name. For us, supporting both variants gets ugly and hacky at
196 * times. The fun begins now; this struct is defined as per the new variant.
198 * Effective MOF definition:
200 * #pragma namespace("\\\\.\\root\\HP\\InstrumentedBIOS");
201 * class HPBIOS_BIOSNumericSensor {
202 * [read] string Name;
203 * [read] string Description;
204 * [read, ValueMap {"0","1","2","3","4","5","6","7","8","9",
205 * "10","11","12"}, Values {"Unknown","Other","Temperature",
206 * "Voltage","Current","Tachometer","Counter","Switch","Lock",
207 * "Humidity","Smoke Detection","Presence","Air Flow"}]
209 * [read] string OtherSensorType;
210 * [read, ValueMap {"0","1","2","3","4","5","6","7","8","9",
211 * "10","11","12","13","14","15","16","17","18","..",
212 * "0x8000.."}, Values {"Unknown","Other","OK","Degraded",
213 * "Stressed","Predictive Failure","Error",
214 * "Non-Recoverable Error","Starting","Stopping","Stopped",
215 * "In Service","No Contact","Lost Communication","Aborted",
216 * "Dormant","Supporting Entity in Error","Completed",
217 * "Power Mode","DMTF Reserved","Vendor Reserved"}]
218 * uint32 OperationalStatus;
219 * [read] uint32 Size;
220 * [read] string PossibleStates[];
221 * [read] string CurrentState;
222 * [read, ValueMap {"0","1","2","3","4","5","6","7","8","9",
223 * "10","11","12","13","14","15","16","17","18","19","20",
224 * "21","22","23","24","25","26","27","28","29","30","31",
225 * "32","33","34","35","36","37","38","39","40","41","42",
226 * "43","44","45","46","47","48","49","50","51","52","53",
227 * "54","55","56","57","58","59","60","61","62","63","64",
228 * "65"}, Values {"Unknown","Other","Degrees C","Degrees F",
229 * "Degrees K","Volts","Amps","Watts","Joules","Coulombs",
230 * "VA","Nits","Lumens","Lux","Candelas","kPa","PSI",
231 * "Newtons","CFM","RPM","Hertz","Seconds","Minutes",
232 * "Hours","Days","Weeks","Mils","Inches","Feet",
233 * "Cubic Inches","Cubic Feet","Meters","Cubic Centimeters",
234 * "Cubic Meters","Liters","Fluid Ounces","Radians",
235 * "Steradians","Revolutions","Cycles","Gravities","Ounces",
236 * "Pounds","Foot-Pounds","Ounce-Inches","Gauss","Gilberts",
237 * "Henries","Farads","Ohms","Siemens","Moles","Becquerels",
238 * "PPM (parts/million)","Decibels","DbA","DbC","Grays",
239 * "Sieverts","Color Temperature Degrees K","Bits","Bytes",
240 * "Words (data)","DoubleWords","QuadWords","Percentage"}]
242 * [read] sint32 UnitModifier;
243 * [read] uint32 CurrentReading;
244 * [read] uint32 RateUnits;
247 * Effective MOF definition of old variant [1] (sans redundant info):
249 * class HPBIOS_BIOSNumericSensor {
250 * [read] string Name;
251 * [read] string Description;
252 * [read] uint32 SensorType;
253 * [read] string OtherSensorType;
254 * [read] uint32 OperationalStatus;
255 * [read] string CurrentState;
256 * [read] string PossibleStates[];
257 * [read] uint32 BaseUnits;
258 * [read] sint32 UnitModifier;
259 * [read] uint32 CurrentReading;
262 struct hp_wmi_numeric_sensor {
264 const char *description;
266 const char *other_sensor_type; /* Explains "Other" SensorType. */
267 u32 operational_status;
268 u8 size; /* Count of PossibleStates[]. */
269 const char **possible_states;
270 const char *current_state;
278 * struct hp_wmi_platform_events - a HPBIOS_PlatformEvents instance
280 * Instances of this object reveal the set of possible HPBIOS_BIOSEvent
281 * instances for the current system, but it may not always be present.
283 * Effective MOF definition:
285 * #pragma namespace("\\\\.\\root\\HP\\InstrumentedBIOS");
286 * class HPBIOS_PlatformEvents {
287 * [read] string Name;
288 * [read] string Description;
289 * [read] string SourceNamespace;
290 * [read] string SourceClass;
291 * [read, ValueMap {"0","1","2","3","4",".."}, Values {
292 * "Unknown","Configuration Change","Button Pressed",
293 * "Sensor","BIOS Settings","Reserved"}]
295 * [read, ValueMap{"0","5","10","15","20","25","30",".."},
296 * Values{"Unknown","OK","Degraded/Warning","Minor Failure",
297 * "Major Failure","Critical Failure","Non-recoverable Error",
299 * uint32 PossibleSeverity;
300 * [read, ValueMap {"0","1","2","3","4","5","6","7","8","9",
301 * "10","11","12","13","14","15","16","17","18","..",
302 * "0x8000.."}, Values {"Unknown","Other","OK","Degraded",
303 * "Stressed","Predictive Failure","Error",
304 * "Non-Recoverable Error","Starting","Stopping","Stopped",
305 * "In Service","No Contact","Lost Communication","Aborted",
306 * "Dormant","Supporting Entity in Error","Completed",
307 * "Power Mode","DMTF Reserved","Vendor Reserved"}]
308 * uint32 PossibleStatus;
311 struct hp_wmi_platform_events {
313 const char *description;
314 const char *source_namespace;
315 const char *source_class;
317 u32 possible_severity;
322 * struct hp_wmi_event - a HPBIOS_BIOSEvent instance
324 * Effective MOF definition [1] (corrected below from original):
326 * #pragma namespace("\\\\.\\root\\WMI");
327 * class HPBIOS_BIOSEvent : WMIEvent {
328 * [read] string Name;
329 * [read] string Description;
330 * [read ValueMap {"0","1","2","3","4"}, Values {"Unknown",
331 * "Configuration Change","Button Pressed","Sensor",
334 * [read, ValueMap {"0","5","10","15","20","25","30"},
335 * Values {"Unknown","OK","Degraded/Warning",
336 * "Minor Failure","Major Failure","Critical Failure",
337 * "Non-recoverable Error"}]
339 * [read, ValueMap {"0","1","2","3","4","5","6","7","8",
340 * "9","10","11","12","13","14","15","16","17","18","..",
341 * "0x8000.."}, Values {"Unknown","Other","OK","Degraded",
342 * "Stressed","Predictive Failure","Error",
343 * "Non-Recoverable Error","Starting","Stopping","Stopped",
344 * "In Service","No Contact","Lost Communication","Aborted",
345 * "Dormant","Supporting Entity in Error","Completed",
346 * "Power Mode","DMTF Reserved","Vendor Reserved"}]
350 struct hp_wmi_event {
352 const char *description;
357 * struct hp_wmi_info - sensor info
358 * @nsensor: numeric sensor properties
359 * @instance: its WMI instance number
360 * @state: pointer to driver state
361 * @has_alarm: whether sensor has an alarm flag
363 * @type: its hwmon sensor type
364 * @cached_val: current sensor reading value, scaled for hwmon
365 * @last_updated: when these readings were last updated
368 struct hp_wmi_numeric_sensor nsensor;
370 void *state; /* void *: Avoid forward declaration. */
373 enum hwmon_sensor_types type;
375 unsigned long last_updated; /* In jiffies. */
380 * struct hp_wmi_sensors - driver state
381 * @wdev: pointer to the parent WMI device
382 * @info_map: sensor info structs by hwmon type and channel number
383 * @channel_count: count of hwmon channels by hwmon type
384 * @has_intrusion: whether an intrusion sensor is present
385 * @intrusion: intrusion flag
386 * @lock: mutex to lock polling WMI and changes to driver state
388 struct hp_wmi_sensors {
389 struct wmi_device *wdev;
390 struct hp_wmi_info **info_map[hwmon_max];
391 u8 channel_count[hwmon_max];
395 struct mutex lock; /* Lock polling WMI and driver state changes. */
398 /* hp_wmi_strdup - devm_kstrdup, but length-limited */
399 static char *hp_wmi_strdup(struct device *dev, const char *src)
404 len = strnlen(src, HP_WMI_MAX_STR_SIZE - 1);
406 dst = devm_kmalloc(dev, (len + 1) * sizeof(*dst), GFP_KERNEL);
410 strscpy(dst, src, len + 1);
416 * hp_wmi_get_wobj - poll WMI for a WMI object instance
417 * @guid: WMI object GUID
418 * @instance: WMI object instance number
420 * Returns a new WMI object instance on success, or NULL on error.
421 * Caller must kfree() the result.
423 static union acpi_object *hp_wmi_get_wobj(const char *guid, u8 instance)
425 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
428 err = wmi_query_block(guid, instance, &out);
429 if (ACPI_FAILURE(err))
435 /* hp_wmi_wobj_instance_count - find count of WMI object instances */
436 static u8 hp_wmi_wobj_instance_count(const char *guid)
440 count = wmi_instance_count(guid);
442 return clamp(count, 0, (int)HP_WMI_MAX_INSTANCES);
445 static int check_wobj(const union acpi_object *wobj,
446 const acpi_object_type property_map[], int last_prop)
448 acpi_object_type type = wobj->type;
449 acpi_object_type valid_type;
450 union acpi_object *elements;
454 if (type != ACPI_TYPE_PACKAGE)
457 elem_count = wobj->package.count;
458 if (elem_count != last_prop + 1)
461 elements = wobj->package.elements;
462 for (prop = 0; prop <= last_prop; prop++) {
463 type = elements[prop].type;
464 valid_type = property_map[prop];
465 if (type != valid_type)
472 static int extract_acpi_value(struct device *dev,
473 union acpi_object *element,
474 acpi_object_type type,
475 u32 *out_value, char **out_string)
478 case ACPI_TYPE_INTEGER:
479 *out_value = element->integer.value;
482 case ACPI_TYPE_STRING:
483 *out_string = hp_wmi_strdup(dev, strim(element->string.pointer));
496 * check_numeric_sensor_wobj - validate a HPBIOS_BIOSNumericSensor instance
497 * @wobj: pointer to WMI object instance to check
498 * @out_size: out pointer to count of possible states
499 * @out_is_new: out pointer to whether this is a "new" variant object
501 * Returns 0 on success, or a negative error code on error.
503 static int check_numeric_sensor_wobj(const union acpi_object *wobj,
504 u8 *out_size, bool *out_is_new)
506 acpi_object_type type = wobj->type;
507 int prop = HP_WMI_PROPERTY_NAME;
508 acpi_object_type valid_type;
509 union acpi_object *elements;
517 if (type != ACPI_TYPE_PACKAGE)
521 * elements is a variable-length array of ACPI objects, one for
522 * each property of the WMI object instance, except that the
523 * strings in PossibleStates[] are flattened into this array
524 * as if each individual string were a property by itself.
526 elements = wobj->package.elements;
528 elem_count = wobj->package.count;
529 if (elem_count <= HP_WMI_PROPERTY_SIZE ||
530 elem_count > HP_WMI_MAX_PROPERTIES)
533 type = elements[HP_WMI_PROPERTY_SIZE].type;
535 case ACPI_TYPE_INTEGER:
537 last_prop = HP_WMI_PROPERTY_RATE_UNITS;
540 case ACPI_TYPE_STRING:
542 last_prop = HP_WMI_PROPERTY_CURRENT_READING;
550 * In general, the count of PossibleStates[] must be > 0.
551 * Also, the old variant lacks the Size property, so we may need to
552 * reduce the value of last_prop by 1 when doing arithmetic with it.
554 if (elem_count < last_prop - !is_new + 1)
557 count = elem_count - (last_prop - !is_new);
559 for (i = 0; i < elem_count && prop <= last_prop; i++, prop++) {
560 type = elements[i].type;
561 valid_type = hp_wmi_property_map[prop];
562 if (type != valid_type)
566 case HP_WMI_PROPERTY_OPERATIONAL_STATUS:
567 /* Old variant: CurrentState follows OperationalStatus. */
569 prop = HP_WMI_PROPERTY_CURRENT_STATE - 1;
572 case HP_WMI_PROPERTY_SIZE:
573 /* New variant: Size == count of PossibleStates[]. */
574 if (count != elements[i].integer.value)
578 case HP_WMI_PROPERTY_POSSIBLE_STATES:
579 /* PossibleStates[0] has already been type-checked. */
580 for (j = 0; i + 1 < elem_count && j + 1 < count; j++) {
581 type = elements[++i].type;
582 if (type != valid_type)
586 /* Old variant: BaseUnits follows PossibleStates[]. */
588 prop = HP_WMI_PROPERTY_BASE_UNITS - 1;
591 case HP_WMI_PROPERTY_CURRENT_STATE:
592 /* Old variant: PossibleStates[] follows CurrentState. */
594 prop = HP_WMI_PROPERTY_POSSIBLE_STATES - 1;
599 if (prop != last_prop + 1)
603 *out_is_new = is_new;
609 numeric_sensor_is_connected(const struct hp_wmi_numeric_sensor *nsensor)
611 u32 operational_status = nsensor->operational_status;
613 return operational_status != HP_WMI_STATUS_NO_CONTACT;
616 static int numeric_sensor_has_fault(const struct hp_wmi_numeric_sensor *nsensor)
618 u32 operational_status = nsensor->operational_status;
620 switch (operational_status) {
621 case HP_WMI_STATUS_DEGRADED:
622 case HP_WMI_STATUS_STRESSED: /* e.g. Overload, overtemp. */
623 case HP_WMI_STATUS_PREDICTIVE_FAILURE: /* e.g. Fan removed. */
624 case HP_WMI_STATUS_ERROR:
625 case HP_WMI_STATUS_NON_RECOVERABLE_ERROR:
626 case HP_WMI_STATUS_NO_CONTACT:
627 case HP_WMI_STATUS_LOST_COMMUNICATION:
628 case HP_WMI_STATUS_ABORTED:
629 case HP_WMI_STATUS_SUPPORTING_ENTITY_IN_ERROR:
631 /* Assume combination by addition; bitwise OR doesn't make sense. */
632 case HP_WMI_STATUS_COMPLETED + HP_WMI_STATUS_DEGRADED:
633 case HP_WMI_STATUS_COMPLETED + HP_WMI_STATUS_ERROR:
640 /* scale_numeric_sensor - scale sensor reading for hwmon */
641 static long scale_numeric_sensor(const struct hp_wmi_numeric_sensor *nsensor)
643 u32 current_reading = nsensor->current_reading;
644 s32 unit_modifier = nsensor->unit_modifier;
645 u32 sensor_type = nsensor->sensor_type;
646 u32 base_units = nsensor->base_units;
650 /* Fan readings are in RPM units; others are in milliunits. */
651 target_modifier = sensor_type == HP_WMI_TYPE_AIR_FLOW ? 0 : -3;
653 val = current_reading;
655 for (; unit_modifier < target_modifier; unit_modifier++)
656 val = DIV_ROUND_CLOSEST(val, 10);
658 for (; unit_modifier > target_modifier; unit_modifier--) {
659 if (val > LONG_MAX / 10) {
666 if (sensor_type == HP_WMI_TYPE_TEMPERATURE) {
667 switch (base_units) {
668 case HP_WMI_UNITS_DEGREES_F:
670 val = val <= LONG_MAX / 5 ?
671 DIV_ROUND_CLOSEST(val * 5, 9) :
672 DIV_ROUND_CLOSEST(val, 9) * 5;
675 case HP_WMI_UNITS_DEGREES_K:
676 val = milli_kelvin_to_millicelsius(val);
685 * classify_numeric_sensor - classify a numeric sensor
686 * @nsensor: pointer to numeric sensor struct
688 * Returns an enum hp_wmi_type value on success,
689 * or a negative value if the sensor type is unsupported.
691 static int classify_numeric_sensor(const struct hp_wmi_numeric_sensor *nsensor)
693 u32 sensor_type = nsensor->sensor_type;
694 u32 base_units = nsensor->base_units;
695 const char *name = nsensor->name;
697 switch (sensor_type) {
698 case HP_WMI_TYPE_TEMPERATURE:
700 * Some systems have sensors named "X Thermal Index" in "Other"
701 * units. Tested CPU sensor examples were found to be in °C,
702 * albeit perhaps "differently" accurate; e.g. readings were
703 * reliably -6°C vs. coretemp on a HP Compaq Elite 8300, and
704 * +8°C on an EliteOne G1 800. But this is still within the
705 * realm of plausibility for cheaply implemented motherboard
706 * sensors, and chassis readings were about as expected.
708 if ((base_units == HP_WMI_UNITS_OTHER &&
709 strstr(name, HP_WMI_PATTERN_TEMP_SENSOR)) ||
710 base_units == HP_WMI_UNITS_DEGREES_C ||
711 base_units == HP_WMI_UNITS_DEGREES_F ||
712 base_units == HP_WMI_UNITS_DEGREES_K)
713 return HP_WMI_TYPE_TEMPERATURE;
716 case HP_WMI_TYPE_VOLTAGE:
717 if (base_units == HP_WMI_UNITS_VOLTS)
718 return HP_WMI_TYPE_VOLTAGE;
721 case HP_WMI_TYPE_CURRENT:
722 if (base_units == HP_WMI_UNITS_AMPS)
723 return HP_WMI_TYPE_CURRENT;
726 case HP_WMI_TYPE_AIR_FLOW:
728 * Strangely, HP considers fan RPM sensor type to be
729 * "Air Flow" instead of the more intuitive "Tachometer".
731 if (base_units == HP_WMI_UNITS_RPM)
732 return HP_WMI_TYPE_AIR_FLOW;
740 populate_numeric_sensor_from_wobj(struct device *dev,
741 struct hp_wmi_numeric_sensor *nsensor,
742 union acpi_object *wobj, bool *out_is_new)
744 int last_prop = HP_WMI_PROPERTY_RATE_UNITS;
745 int prop = HP_WMI_PROPERTY_NAME;
746 const char **possible_states;
747 union acpi_object *element;
748 acpi_object_type type;
755 err = check_numeric_sensor_wobj(wobj, &size, &is_new);
759 possible_states = devm_kcalloc(dev, size, sizeof(*possible_states),
761 if (!possible_states)
764 element = wobj->package.elements;
765 nsensor->possible_states = possible_states;
766 nsensor->size = size;
769 last_prop = HP_WMI_PROPERTY_CURRENT_READING;
771 for (; prop <= last_prop; prop++) {
772 type = hp_wmi_property_map[prop];
774 err = extract_acpi_value(dev, element, type, &value, &string);
781 case HP_WMI_PROPERTY_NAME:
782 nsensor->name = string;
785 case HP_WMI_PROPERTY_DESCRIPTION:
786 nsensor->description = string;
789 case HP_WMI_PROPERTY_SENSOR_TYPE:
790 if (value > HP_WMI_TYPE_AIR_FLOW)
793 nsensor->sensor_type = value;
796 case HP_WMI_PROPERTY_OTHER_SENSOR_TYPE:
797 nsensor->other_sensor_type = string;
800 case HP_WMI_PROPERTY_OPERATIONAL_STATUS:
801 nsensor->operational_status = value;
803 /* Old variant: CurrentState follows OperationalStatus. */
805 prop = HP_WMI_PROPERTY_CURRENT_STATE - 1;
808 case HP_WMI_PROPERTY_SIZE:
809 break; /* Already set. */
811 case HP_WMI_PROPERTY_POSSIBLE_STATES:
812 *possible_states++ = string;
816 /* Old variant: BaseUnits follows PossibleStates[]. */
817 if (!is_new && !size)
818 prop = HP_WMI_PROPERTY_BASE_UNITS - 1;
821 case HP_WMI_PROPERTY_CURRENT_STATE:
822 nsensor->current_state = string;
824 /* Old variant: PossibleStates[] follows CurrentState. */
826 prop = HP_WMI_PROPERTY_POSSIBLE_STATES - 1;
829 case HP_WMI_PROPERTY_BASE_UNITS:
830 nsensor->base_units = value;
833 case HP_WMI_PROPERTY_UNIT_MODIFIER:
834 /* UnitModifier is signed. */
835 nsensor->unit_modifier = (s32)value;
838 case HP_WMI_PROPERTY_CURRENT_READING:
839 nsensor->current_reading = value;
842 case HP_WMI_PROPERTY_RATE_UNITS:
843 nsensor->rate_units = value;
851 *out_is_new = is_new;
856 /* update_numeric_sensor_from_wobj - update fungible sensor properties */
858 update_numeric_sensor_from_wobj(struct device *dev,
859 struct hp_wmi_numeric_sensor *nsensor,
860 const union acpi_object *wobj)
862 const union acpi_object *elements;
863 const union acpi_object *element;
870 err = check_numeric_sensor_wobj(wobj, &size, &is_new);
874 elements = wobj->package.elements;
876 element = &elements[HP_WMI_PROPERTY_OPERATIONAL_STATUS];
877 nsensor->operational_status = element->integer.value;
880 * In general, an index offset is needed after PossibleStates[0].
881 * On a new variant, CurrentState is after PossibleStates[]. This is
882 * not the case on an old variant, but we still need to offset the
883 * read because CurrentState is where Size would be on a new variant.
885 offset = is_new ? size - 1 : -2;
887 element = &elements[HP_WMI_PROPERTY_CURRENT_STATE + offset];
888 string = strim(element->string.pointer);
890 if (strcmp(string, nsensor->current_state)) {
891 devm_kfree(dev, nsensor->current_state);
892 nsensor->current_state = hp_wmi_strdup(dev, string);
895 /* Old variant: -2 (not -1) because it lacks the Size property. */
897 offset = (int)size - 2; /* size is > 0, i.e. may be 1. */
899 element = &elements[HP_WMI_PROPERTY_UNIT_MODIFIER + offset];
900 nsensor->unit_modifier = (s32)element->integer.value;
902 element = &elements[HP_WMI_PROPERTY_CURRENT_READING + offset];
903 nsensor->current_reading = element->integer.value;
907 * check_platform_events_wobj - validate a HPBIOS_PlatformEvents instance
908 * @wobj: pointer to WMI object instance to check
910 * Returns 0 on success, or a negative error code on error.
912 static int check_platform_events_wobj(const union acpi_object *wobj)
914 return check_wobj(wobj, hp_wmi_platform_events_property_map,
915 HP_WMI_PLATFORM_EVENTS_PROPERTY_POSSIBLE_STATUS);
919 populate_platform_events_from_wobj(struct device *dev,
920 struct hp_wmi_platform_events *pevents,
921 union acpi_object *wobj)
923 int last_prop = HP_WMI_PLATFORM_EVENTS_PROPERTY_POSSIBLE_STATUS;
924 int prop = HP_WMI_PLATFORM_EVENTS_PROPERTY_NAME;
925 union acpi_object *element;
926 acpi_object_type type;
931 err = check_platform_events_wobj(wobj);
935 element = wobj->package.elements;
937 for (; prop <= last_prop; prop++, element++) {
938 type = hp_wmi_platform_events_property_map[prop];
940 err = extract_acpi_value(dev, element, type, &value, &string);
945 case HP_WMI_PLATFORM_EVENTS_PROPERTY_NAME:
946 pevents->name = string;
949 case HP_WMI_PLATFORM_EVENTS_PROPERTY_DESCRIPTION:
950 pevents->description = string;
953 case HP_WMI_PLATFORM_EVENTS_PROPERTY_SOURCE_NAMESPACE:
954 if (strcasecmp(HP_WMI_EVENT_NAMESPACE, string))
957 pevents->source_namespace = string;
960 case HP_WMI_PLATFORM_EVENTS_PROPERTY_SOURCE_CLASS:
961 if (strcasecmp(HP_WMI_EVENT_CLASS, string))
964 pevents->source_class = string;
967 case HP_WMI_PLATFORM_EVENTS_PROPERTY_CATEGORY:
968 pevents->category = value;
971 case HP_WMI_PLATFORM_EVENTS_PROPERTY_POSSIBLE_SEVERITY:
972 pevents->possible_severity = value;
975 case HP_WMI_PLATFORM_EVENTS_PROPERTY_POSSIBLE_STATUS:
976 pevents->possible_status = value;
988 * check_event_wobj - validate a HPBIOS_BIOSEvent instance
989 * @wobj: pointer to WMI object instance to check
991 * Returns 0 on success, or a negative error code on error.
993 static int check_event_wobj(const union acpi_object *wobj)
995 return check_wobj(wobj, hp_wmi_event_property_map,
996 HP_WMI_EVENT_PROPERTY_STATUS);
999 static int populate_event_from_wobj(struct hp_wmi_event *event,
1000 union acpi_object *wobj)
1002 int prop = HP_WMI_EVENT_PROPERTY_NAME;
1003 union acpi_object *element;
1006 err = check_event_wobj(wobj);
1010 element = wobj->package.elements;
1012 /* Extracted strings are NOT device-managed copies. */
1014 for (; prop <= HP_WMI_EVENT_PROPERTY_CATEGORY; prop++, element++) {
1016 case HP_WMI_EVENT_PROPERTY_NAME:
1017 event->name = strim(element->string.pointer);
1020 case HP_WMI_EVENT_PROPERTY_DESCRIPTION:
1021 event->description = strim(element->string.pointer);
1024 case HP_WMI_EVENT_PROPERTY_CATEGORY:
1025 event->category = element->integer.value;
1037 * classify_event - classify an event
1039 * @category: event category
1041 * Classify instances of both HPBIOS_PlatformEvents and HPBIOS_BIOSEvent from
1042 * property values. Recognition criteria are based on multiple ACPI dumps [3].
1044 * Returns an enum hp_wmi_type value on success,
1045 * or a negative value if the event type is unsupported.
1047 static int classify_event(const char *event_name, u32 category)
1049 if (category != HP_WMI_CATEGORY_SENSOR)
1052 /* Fan events have Name "X Stall". */
1053 if (strstr(event_name, HP_WMI_PATTERN_FAN_ALARM))
1054 return HP_WMI_TYPE_AIR_FLOW;
1056 /* Intrusion events have Name "Hood Intrusion". */
1057 if (!strcmp(event_name, HP_WMI_PATTERN_INTRUSION_ALARM))
1058 return HP_WMI_TYPE_INTRUSION;
1061 * Temperature events have Name either "Thermal Caution" or
1062 * "Thermal Critical". Deal only with "Thermal Critical" events.
1064 * "Thermal Caution" events have Status "Stressed", informing us that
1065 * the OperationalStatus of the related sensor has become "Stressed".
1066 * However, this is already a fault condition that will clear itself
1067 * when the sensor recovers, so we have no further interest in them.
1069 if (!strcmp(event_name, HP_WMI_PATTERN_TEMP_ALARM))
1070 return HP_WMI_TYPE_TEMPERATURE;
1076 * interpret_info - interpret sensor for hwmon
1077 * @info: pointer to sensor info struct
1079 * Should be called after the numeric sensor member has been updated.
1081 static void interpret_info(struct hp_wmi_info *info)
1083 const struct hp_wmi_numeric_sensor *nsensor = &info->nsensor;
1085 info->cached_val = scale_numeric_sensor(nsensor);
1086 info->last_updated = jiffies;
1090 * hp_wmi_update_info - poll WMI to update sensor info
1091 * @state: pointer to driver state
1092 * @info: pointer to sensor info struct
1094 * Returns 0 on success, or a negative error code on error.
1096 static int hp_wmi_update_info(struct hp_wmi_sensors *state,
1097 struct hp_wmi_info *info)
1099 struct hp_wmi_numeric_sensor *nsensor = &info->nsensor;
1100 struct device *dev = &state->wdev->dev;
1101 const union acpi_object *wobj;
1102 u8 instance = info->instance;
1105 if (time_after(jiffies, info->last_updated + HZ)) {
1106 mutex_lock(&state->lock);
1108 wobj = hp_wmi_get_wobj(HP_WMI_NUMERIC_SENSOR_GUID, instance);
1114 update_numeric_sensor_from_wobj(dev, nsensor, wobj);
1116 interpret_info(info);
1121 mutex_unlock(&state->lock);
1127 static int basic_string_show(struct seq_file *seqf, void *ignored)
1129 const char *str = seqf->private;
1131 seq_printf(seqf, "%s\n", str);
1135 DEFINE_SHOW_ATTRIBUTE(basic_string);
1137 static int fungible_show(struct seq_file *seqf, enum hp_wmi_property prop)
1139 struct hp_wmi_numeric_sensor *nsensor;
1140 struct hp_wmi_sensors *state;
1141 struct hp_wmi_info *info;
1144 info = seqf->private;
1145 state = info->state;
1146 nsensor = &info->nsensor;
1148 err = hp_wmi_update_info(state, info);
1153 case HP_WMI_PROPERTY_OPERATIONAL_STATUS:
1154 seq_printf(seqf, "%u\n", nsensor->operational_status);
1157 case HP_WMI_PROPERTY_CURRENT_STATE:
1158 seq_printf(seqf, "%s\n", nsensor->current_state);
1161 case HP_WMI_PROPERTY_UNIT_MODIFIER:
1162 seq_printf(seqf, "%d\n", nsensor->unit_modifier);
1165 case HP_WMI_PROPERTY_CURRENT_READING:
1166 seq_printf(seqf, "%u\n", nsensor->current_reading);
1176 static int operational_status_show(struct seq_file *seqf, void *ignored)
1178 return fungible_show(seqf, HP_WMI_PROPERTY_OPERATIONAL_STATUS);
1180 DEFINE_SHOW_ATTRIBUTE(operational_status);
1182 static int current_state_show(struct seq_file *seqf, void *ignored)
1184 return fungible_show(seqf, HP_WMI_PROPERTY_CURRENT_STATE);
1186 DEFINE_SHOW_ATTRIBUTE(current_state);
1188 static int possible_states_show(struct seq_file *seqf, void *ignored)
1190 struct hp_wmi_numeric_sensor *nsensor = seqf->private;
1193 for (i = 0; i < nsensor->size; i++)
1194 seq_printf(seqf, "%s%s", i ? "," : "",
1195 nsensor->possible_states[i]);
1197 seq_puts(seqf, "\n");
1201 DEFINE_SHOW_ATTRIBUTE(possible_states);
1203 static int unit_modifier_show(struct seq_file *seqf, void *ignored)
1205 return fungible_show(seqf, HP_WMI_PROPERTY_UNIT_MODIFIER);
1207 DEFINE_SHOW_ATTRIBUTE(unit_modifier);
1209 static int current_reading_show(struct seq_file *seqf, void *ignored)
1211 return fungible_show(seqf, HP_WMI_PROPERTY_CURRENT_READING);
1213 DEFINE_SHOW_ATTRIBUTE(current_reading);
1215 /* hp_wmi_devm_debugfs_remove - devm callback for debugfs cleanup */
1216 static void hp_wmi_devm_debugfs_remove(void *res)
1218 debugfs_remove_recursive(res);
1221 /* hp_wmi_debugfs_init - create and populate debugfs directory tree */
1222 static void hp_wmi_debugfs_init(struct device *dev, struct hp_wmi_info *info,
1223 struct hp_wmi_platform_events *pevents,
1224 u8 icount, u8 pcount, bool is_new)
1226 struct hp_wmi_numeric_sensor *nsensor;
1227 char buf[HP_WMI_MAX_STR_SIZE];
1228 struct dentry *debugfs;
1229 struct dentry *entries;
1234 /* dev_name() gives a not-very-friendly GUID for WMI devices. */
1235 scnprintf(buf, sizeof(buf), "hp-wmi-sensors-%u", dev->id);
1237 debugfs = debugfs_create_dir(buf, NULL);
1238 if (IS_ERR(debugfs))
1241 err = devm_add_action_or_reset(dev, hp_wmi_devm_debugfs_remove,
1246 entries = debugfs_create_dir("sensor", debugfs);
1248 for (i = 0; i < icount; i++, info++) {
1249 nsensor = &info->nsensor;
1251 scnprintf(buf, sizeof(buf), "%u", i);
1252 dir = debugfs_create_dir(buf, entries);
1254 debugfs_create_file("name", 0444, dir,
1255 (void *)nsensor->name,
1256 &basic_string_fops);
1258 debugfs_create_file("description", 0444, dir,
1259 (void *)nsensor->description,
1260 &basic_string_fops);
1262 debugfs_create_u32("sensor_type", 0444, dir,
1263 &nsensor->sensor_type);
1265 debugfs_create_file("other_sensor_type", 0444, dir,
1266 (void *)nsensor->other_sensor_type,
1267 &basic_string_fops);
1269 debugfs_create_file("operational_status", 0444, dir,
1270 info, &operational_status_fops);
1272 debugfs_create_file("possible_states", 0444, dir,
1273 nsensor, &possible_states_fops);
1275 debugfs_create_file("current_state", 0444, dir,
1276 info, ¤t_state_fops);
1278 debugfs_create_u32("base_units", 0444, dir,
1279 &nsensor->base_units);
1281 debugfs_create_file("unit_modifier", 0444, dir,
1282 info, &unit_modifier_fops);
1284 debugfs_create_file("current_reading", 0444, dir,
1285 info, ¤t_reading_fops);
1288 debugfs_create_u32("rate_units", 0444, dir,
1289 &nsensor->rate_units);
1295 entries = debugfs_create_dir("platform_events", debugfs);
1297 for (i = 0; i < pcount; i++, pevents++) {
1298 scnprintf(buf, sizeof(buf), "%u", i);
1299 dir = debugfs_create_dir(buf, entries);
1301 debugfs_create_file("name", 0444, dir,
1302 (void *)pevents->name,
1303 &basic_string_fops);
1305 debugfs_create_file("description", 0444, dir,
1306 (void *)pevents->description,
1307 &basic_string_fops);
1309 debugfs_create_file("source_namespace", 0444, dir,
1310 (void *)pevents->source_namespace,
1311 &basic_string_fops);
1313 debugfs_create_file("source_class", 0444, dir,
1314 (void *)pevents->source_class,
1315 &basic_string_fops);
1317 debugfs_create_u32("category", 0444, dir,
1318 &pevents->category);
1320 debugfs_create_u32("possible_severity", 0444, dir,
1321 &pevents->possible_severity);
1323 debugfs_create_u32("possible_status", 0444, dir,
1324 &pevents->possible_status);
1328 static umode_t hp_wmi_hwmon_is_visible(const void *drvdata,
1329 enum hwmon_sensor_types type,
1330 u32 attr, int channel)
1332 const struct hp_wmi_sensors *state = drvdata;
1333 const struct hp_wmi_info *info;
1335 if (type == hwmon_intrusion)
1336 return state->has_intrusion ? 0644 : 0;
1338 if (!state->info_map[type] || !state->info_map[type][channel])
1341 info = state->info_map[type][channel];
1343 if ((type == hwmon_temp && attr == hwmon_temp_alarm) ||
1344 (type == hwmon_fan && attr == hwmon_fan_alarm))
1345 return info->has_alarm ? 0444 : 0;
1350 static int hp_wmi_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
1351 u32 attr, int channel, long *out_val)
1353 struct hp_wmi_sensors *state = dev_get_drvdata(dev);
1354 const struct hp_wmi_numeric_sensor *nsensor;
1355 struct hp_wmi_info *info;
1358 if (type == hwmon_intrusion) {
1359 *out_val = state->intrusion ? 1 : 0;
1364 info = state->info_map[type][channel];
1366 if ((type == hwmon_temp && attr == hwmon_temp_alarm) ||
1367 (type == hwmon_fan && attr == hwmon_fan_alarm)) {
1368 *out_val = info->alarm ? 1 : 0;
1369 info->alarm = false;
1374 nsensor = &info->nsensor;
1376 err = hp_wmi_update_info(state, info);
1380 if ((type == hwmon_temp && attr == hwmon_temp_fault) ||
1381 (type == hwmon_fan && attr == hwmon_fan_fault))
1382 *out_val = numeric_sensor_has_fault(nsensor);
1384 *out_val = info->cached_val;
1389 static int hp_wmi_hwmon_read_string(struct device *dev,
1390 enum hwmon_sensor_types type, u32 attr,
1391 int channel, const char **out_str)
1393 const struct hp_wmi_sensors *state = dev_get_drvdata(dev);
1394 const struct hp_wmi_info *info;
1396 info = state->info_map[type][channel];
1397 *out_str = info->nsensor.name;
1402 static int hp_wmi_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
1403 u32 attr, int channel, long val)
1405 struct hp_wmi_sensors *state = dev_get_drvdata(dev);
1410 mutex_lock(&state->lock);
1412 state->intrusion = false;
1414 mutex_unlock(&state->lock);
1419 static const struct hwmon_ops hp_wmi_hwmon_ops = {
1420 .is_visible = hp_wmi_hwmon_is_visible,
1421 .read = hp_wmi_hwmon_read,
1422 .read_string = hp_wmi_hwmon_read_string,
1423 .write = hp_wmi_hwmon_write,
1426 static struct hwmon_chip_info hp_wmi_chip_info = {
1427 .ops = &hp_wmi_hwmon_ops,
1431 static struct hp_wmi_info *match_fan_event(struct hp_wmi_sensors *state,
1432 const char *event_description)
1434 struct hp_wmi_info **ptr_info = state->info_map[hwmon_fan];
1435 u8 fan_count = state->channel_count[hwmon_fan];
1436 struct hp_wmi_info *info;
1440 /* Fan event has Description "X Speed". Sensor has Name "X[ Speed]". */
1442 for (i = 0; i < fan_count; i++, ptr_info++) {
1444 name = info->nsensor.name;
1446 if (strstr(event_description, name))
1453 static u8 match_temp_events(struct hp_wmi_sensors *state,
1454 const char *event_description,
1455 struct hp_wmi_info *temp_info[])
1457 struct hp_wmi_info **ptr_info = state->info_map[hwmon_temp];
1458 u8 temp_count = state->channel_count[hwmon_temp];
1459 struct hp_wmi_info *info;
1466 /* Description is either "CPU Thermal Index" or "Chassis Thermal Index". */
1468 is_cpu = !strcmp(event_description, HP_WMI_PATTERN_CPU_TEMP);
1469 is_sys = !strcmp(event_description, HP_WMI_PATTERN_SYS_TEMP);
1470 if (!is_cpu && !is_sys)
1474 * CPU event: Match one sensor with Name either "CPU Thermal Index" or
1475 * "CPU Temperature", or multiple with Name(s) "CPU[#] Temperature".
1477 * Chassis event: Match one sensor with Name either
1478 * "Chassis Thermal Index" or "System Ambient Temperature".
1481 for (i = 0; i < temp_count; i++, ptr_info++) {
1483 name = info->nsensor.name;
1485 if ((is_cpu && (!strcmp(name, HP_WMI_PATTERN_CPU_TEMP) ||
1486 !strcmp(name, HP_WMI_PATTERN_CPU_TEMP2))) ||
1487 (is_sys && (!strcmp(name, HP_WMI_PATTERN_SYS_TEMP) ||
1488 !strcmp(name, HP_WMI_PATTERN_SYS_TEMP2)))) {
1489 temp_info[0] = info;
1493 if (is_cpu && (strstr(name, HP_WMI_PATTERN_CPU) &&
1494 strstr(name, HP_WMI_PATTERN_TEMP)))
1495 temp_info[count++] = info;
1501 /* hp_wmi_devm_debugfs_remove - devm callback for WMI event handler removal */
1502 static void hp_wmi_devm_notify_remove(void *ignored)
1504 wmi_remove_notify_handler(HP_WMI_EVENT_GUID);
1507 /* hp_wmi_notify - WMI event notification handler */
1508 static void hp_wmi_notify(u32 value, void *context)
1510 struct hp_wmi_info *temp_info[HP_WMI_MAX_INSTANCES] = {};
1511 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
1512 struct hp_wmi_sensors *state = context;
1513 struct device *dev = &state->wdev->dev;
1514 struct hp_wmi_info *fan_info;
1515 struct hp_wmi_event event;
1516 union acpi_object *wobj;
1522 * The following warning may occur in the kernel log:
1524 * ACPI Warning: \_SB.WMID._WED: Return type mismatch -
1525 * found Package, expected Integer/String/Buffer
1527 * After using [4] to decode BMOF blobs found in [3], careless copying
1528 * of BIOS code seems the most likely explanation for this warning.
1529 * HP_WMI_EVENT_GUID refers to \\.\root\WMI\HPBIOS_BIOSEvent on
1530 * business-class systems, but it refers to \\.\root\WMI\hpqBEvnt on
1531 * non-business-class systems. Per the existing hp-wmi driver, it
1532 * looks like an instance of hpqBEvnt delivered as event data may
1533 * indeed take the form of a raw ACPI_BUFFER on non-business-class
1534 * systems ("may" because ASL shows some BIOSes do strange things).
1536 * In any case, we can ignore this warning, because we always validate
1537 * the event data to ensure it is an ACPI_PACKAGE containing a
1538 * HPBIOS_BIOSEvent instance.
1541 mutex_lock(&state->lock);
1543 err = wmi_get_event_data(value, &out);
1544 if (ACPI_FAILURE(err))
1549 err = populate_event_from_wobj(&event, wobj);
1551 dev_warn(dev, "Bad event data (ACPI type %d)\n", wobj->type);
1555 event_type = classify_event(event.name, event.category);
1556 switch (event_type) {
1557 case HP_WMI_TYPE_AIR_FLOW:
1558 fan_info = match_fan_event(state, event.description);
1560 fan_info->alarm = true;
1563 case HP_WMI_TYPE_INTRUSION:
1564 state->intrusion = true;
1567 case HP_WMI_TYPE_TEMPERATURE:
1568 count = match_temp_events(state, event.description, temp_info);
1570 temp_info[--count]->alarm = true;
1581 mutex_unlock(&state->lock);
1584 static int init_platform_events(struct device *dev,
1585 struct hp_wmi_platform_events **out_pevents,
1588 struct hp_wmi_platform_events *pevents_arr;
1589 struct hp_wmi_platform_events *pevents;
1590 union acpi_object *wobj;
1595 count = hp_wmi_wobj_instance_count(HP_WMI_PLATFORM_EVENTS_GUID);
1599 dev_dbg(dev, "No platform events\n");
1604 pevents_arr = devm_kcalloc(dev, count, sizeof(*pevents), GFP_KERNEL);
1608 for (i = 0, pevents = pevents_arr; i < count; i++, pevents++) {
1609 wobj = hp_wmi_get_wobj(HP_WMI_PLATFORM_EVENTS_GUID, i);
1613 err = populate_platform_events_from_wobj(dev, pevents, wobj);
1621 *out_pevents = pevents_arr;
1622 *out_pcount = count;
1624 dev_dbg(dev, "Found %u platform events\n", count);
1629 static int init_numeric_sensors(struct hp_wmi_sensors *state,
1630 struct hp_wmi_info *connected[],
1631 struct hp_wmi_info **out_info,
1632 u8 *out_icount, u8 *out_count,
1635 struct hp_wmi_info ***info_map = state->info_map;
1636 u8 *channel_count = state->channel_count;
1637 struct device *dev = &state->wdev->dev;
1638 struct hp_wmi_numeric_sensor *nsensor;
1639 u8 channel_index[hwmon_max] = {};
1640 enum hwmon_sensor_types type;
1641 struct hp_wmi_info *info_arr;
1642 struct hp_wmi_info *info;
1643 union acpi_object *wobj;
1652 icount = hp_wmi_wobj_instance_count(HP_WMI_NUMERIC_SENSOR_GUID);
1656 info_arr = devm_kcalloc(dev, icount, sizeof(*info), GFP_KERNEL);
1660 for (i = 0, info = info_arr; i < icount; i++, info++) {
1661 wobj = hp_wmi_get_wobj(HP_WMI_NUMERIC_SENSOR_GUID, i);
1666 info->state = state;
1667 nsensor = &info->nsensor;
1669 err = populate_numeric_sensor_from_wobj(dev, nsensor, wobj,
1677 if (!numeric_sensor_is_connected(nsensor))
1680 wtype = classify_numeric_sensor(nsensor);
1684 type = hp_wmi_hwmon_type_map[wtype];
1686 channel_count[type]++;
1690 interpret_info(info);
1692 connected[count++] = info;
1695 dev_dbg(dev, "Found %u sensors (%u connected)\n", i, count);
1697 for (i = 0; i < count; i++) {
1698 info = connected[i];
1700 c = channel_index[type]++;
1702 if (!info_map[type]) {
1703 info_map[type] = devm_kcalloc(dev, channel_count[type],
1706 if (!info_map[type])
1710 info_map[type][c] = info;
1713 *out_info = info_arr;
1714 *out_icount = icount;
1716 *out_is_new = is_new;
1721 static bool find_event_attributes(struct hp_wmi_sensors *state,
1722 struct hp_wmi_platform_events *pevents,
1726 * The existence of this HPBIOS_PlatformEvents instance:
1729 * Name = "Rear Chassis Fan0 Stall";
1730 * Description = "Rear Chassis Fan0 Speed";
1731 * Category = 3; // "Sensor"
1732 * PossibleSeverity = 25; // "Critical Failure"
1733 * PossibleStatus = 5; // "Predictive Failure"
1737 * means that this HPBIOS_BIOSEvent instance may occur:
1740 * Name = "Rear Chassis Fan0 Stall";
1741 * Description = "Rear Chassis Fan0 Speed";
1742 * Category = 3; // "Sensor"
1743 * Severity = 25; // "Critical Failure"
1744 * Status = 5; // "Predictive Failure"
1747 * After the event occurs (e.g. because the fan was unplugged),
1748 * polling the related HPBIOS_BIOSNumericSensor instance gives:
1751 * Name = "Rear Chassis Fan0";
1752 * Description = "Reports rear chassis fan0 speed";
1753 * OperationalStatus = 5; // "Predictive Failure", was 3 ("OK")
1754 * CurrentReading = 0;
1758 * In this example, the hwmon fan channel for "Rear Chassis Fan0"
1759 * should support the alarm flag and have it be set if the related
1760 * HPBIOS_BIOSEvent instance occurs.
1762 * In addition to fan events, temperature (CPU/chassis) and intrusion
1763 * events are relevant to hwmon [2]. Note that much information in [2]
1764 * is unreliable; it is referenced in addition to ACPI dumps [3] merely
1765 * to support the conclusion that sensor and event names/descriptions
1766 * are systematic enough to allow this driver to match them.
1768 * Complications and limitations:
1770 * - Strings are freeform and may vary, cf. sensor Name "CPU0 Fan"
1771 * on a Z420 vs. "CPU Fan Speed" on an EliteOne 800 G1.
1772 * - Leading/trailing whitespace is a rare but real possibility [3].
1773 * - The HPBIOS_PlatformEvents object may not exist or its instances
1774 * may show that the system only has e.g. BIOS setting-related
1775 * events (cf. the ProBook 4540s and ProBook 470 G0 [3]).
1778 struct hp_wmi_info *temp_info[HP_WMI_MAX_INSTANCES] = {};
1779 const char *event_description;
1780 struct hp_wmi_info *fan_info;
1781 bool has_events = false;
1782 const char *event_name;
1788 for (i = 0; i < pevents_count; i++, pevents++) {
1789 event_name = pevents->name;
1790 event_description = pevents->description;
1791 event_category = pevents->category;
1793 event_type = classify_event(event_name, event_category);
1794 switch (event_type) {
1795 case HP_WMI_TYPE_AIR_FLOW:
1796 fan_info = match_fan_event(state, event_description);
1800 fan_info->has_alarm = true;
1804 case HP_WMI_TYPE_INTRUSION:
1805 state->has_intrusion = true;
1809 case HP_WMI_TYPE_TEMPERATURE:
1810 count = match_temp_events(state, event_description,
1816 temp_info[--count]->has_alarm = true;
1828 static int make_chip_info(struct hp_wmi_sensors *state, bool has_events)
1830 const struct hwmon_channel_info **ptr_channel_info;
1831 struct hp_wmi_info ***info_map = state->info_map;
1832 u8 *channel_count = state->channel_count;
1833 struct hwmon_channel_info *channel_info;
1834 struct device *dev = &state->wdev->dev;
1835 enum hwmon_sensor_types type;
1842 if (channel_count[hwmon_temp])
1843 channel_count[hwmon_chip] = 1;
1845 if (has_events && state->has_intrusion)
1846 channel_count[hwmon_intrusion] = 1;
1848 for (type = hwmon_chip; type < hwmon_max; type++)
1849 if (channel_count[type])
1852 channel_info = devm_kcalloc(dev, type_count,
1853 sizeof(*channel_info), GFP_KERNEL);
1857 ptr_channel_info = devm_kcalloc(dev, type_count + 1,
1858 sizeof(*ptr_channel_info), GFP_KERNEL);
1859 if (!ptr_channel_info)
1862 hp_wmi_chip_info.info = ptr_channel_info;
1864 for (type = hwmon_chip; type < hwmon_max; type++) {
1865 count = channel_count[type];
1869 config = devm_kcalloc(dev, count + 1,
1870 sizeof(*config), GFP_KERNEL);
1874 attr = hp_wmi_hwmon_attributes[type];
1875 channel_info->type = type;
1876 channel_info->config = config;
1877 memset32(config, attr, count);
1879 *ptr_channel_info++ = channel_info++;
1881 if (!has_events || (type != hwmon_temp && type != hwmon_fan))
1884 attr = type == hwmon_temp ? HWMON_T_ALARM : HWMON_F_ALARM;
1886 for (i = 0; i < count; i++)
1887 if (info_map[type][i]->has_alarm)
1894 static bool add_event_handler(struct hp_wmi_sensors *state)
1896 struct device *dev = &state->wdev->dev;
1899 err = wmi_install_notify_handler(HP_WMI_EVENT_GUID,
1900 hp_wmi_notify, state);
1902 dev_info(dev, "Failed to subscribe to WMI event\n");
1906 err = devm_add_action_or_reset(dev, hp_wmi_devm_notify_remove, NULL);
1913 static int hp_wmi_sensors_init(struct hp_wmi_sensors *state)
1915 struct hp_wmi_info *connected[HP_WMI_MAX_INSTANCES];
1916 struct hp_wmi_platform_events *pevents = NULL;
1917 struct device *dev = &state->wdev->dev;
1918 struct hp_wmi_info *info;
1919 struct device *hwdev;
1927 err = init_platform_events(dev, &pevents, &pcount);
1931 err = init_numeric_sensors(state, connected, &info,
1932 &icount, &count, &is_new);
1936 if (IS_ENABLED(CONFIG_DEBUG_FS))
1937 hp_wmi_debugfs_init(dev, info, pevents, icount, pcount, is_new);
1940 return 0; /* No connected sensors; debugfs only. */
1942 has_events = find_event_attributes(state, pevents, pcount);
1944 /* Survive failure to install WMI event handler. */
1945 if (has_events && !add_event_handler(state))
1948 err = make_chip_info(state, has_events);
1952 hwdev = devm_hwmon_device_register_with_info(dev, "hp_wmi_sensors",
1953 state, &hp_wmi_chip_info,
1955 return PTR_ERR_OR_ZERO(hwdev);
1958 static int hp_wmi_sensors_probe(struct wmi_device *wdev, const void *context)
1960 struct device *dev = &wdev->dev;
1961 struct hp_wmi_sensors *state;
1963 state = devm_kzalloc(dev, sizeof(*state), GFP_KERNEL);
1969 mutex_init(&state->lock);
1971 dev_set_drvdata(dev, state);
1973 return hp_wmi_sensors_init(state);
1976 static const struct wmi_device_id hp_wmi_sensors_id_table[] = {
1977 { HP_WMI_NUMERIC_SENSOR_GUID, NULL },
1981 static struct wmi_driver hp_wmi_sensors_driver = {
1982 .driver = { .name = "hp-wmi-sensors" },
1983 .id_table = hp_wmi_sensors_id_table,
1984 .probe = hp_wmi_sensors_probe,
1986 module_wmi_driver(hp_wmi_sensors_driver);
1988 MODULE_AUTHOR("James Seo <james@equiv.tech>");
1989 MODULE_DESCRIPTION("HP WMI Sensors driver");
1990 MODULE_LICENSE("GPL");