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)
438 u8 hi = HP_WMI_MAX_INSTANCES;
439 union acpi_object *wobj;
446 wobj = hp_wmi_get_wobj(guid, mid);
459 static int check_wobj(const union acpi_object *wobj,
460 const acpi_object_type property_map[], int last_prop)
462 acpi_object_type type = wobj->type;
463 acpi_object_type valid_type;
464 union acpi_object *elements;
468 if (type != ACPI_TYPE_PACKAGE)
471 elem_count = wobj->package.count;
472 if (elem_count != last_prop + 1)
475 elements = wobj->package.elements;
476 for (prop = 0; prop <= last_prop; prop++) {
477 type = elements[prop].type;
478 valid_type = property_map[prop];
479 if (type != valid_type)
486 static int extract_acpi_value(struct device *dev,
487 union acpi_object *element,
488 acpi_object_type type,
489 u32 *out_value, char **out_string)
492 case ACPI_TYPE_INTEGER:
493 *out_value = element->integer.value;
496 case ACPI_TYPE_STRING:
497 *out_string = hp_wmi_strdup(dev, strim(element->string.pointer));
510 * check_numeric_sensor_wobj - validate a HPBIOS_BIOSNumericSensor instance
511 * @wobj: pointer to WMI object instance to check
512 * @out_size: out pointer to count of possible states
513 * @out_is_new: out pointer to whether this is a "new" variant object
515 * Returns 0 on success, or a negative error code on error.
517 static int check_numeric_sensor_wobj(const union acpi_object *wobj,
518 u8 *out_size, bool *out_is_new)
520 acpi_object_type type = wobj->type;
521 int prop = HP_WMI_PROPERTY_NAME;
522 acpi_object_type valid_type;
523 union acpi_object *elements;
531 if (type != ACPI_TYPE_PACKAGE)
535 * elements is a variable-length array of ACPI objects, one for
536 * each property of the WMI object instance, except that the
537 * strings in PossibleStates[] are flattened into this array
538 * as if each individual string were a property by itself.
540 elements = wobj->package.elements;
542 elem_count = wobj->package.count;
543 if (elem_count <= HP_WMI_PROPERTY_SIZE ||
544 elem_count > HP_WMI_MAX_PROPERTIES)
547 type = elements[HP_WMI_PROPERTY_SIZE].type;
549 case ACPI_TYPE_INTEGER:
551 last_prop = HP_WMI_PROPERTY_RATE_UNITS;
554 case ACPI_TYPE_STRING:
556 last_prop = HP_WMI_PROPERTY_CURRENT_READING;
564 * In general, the count of PossibleStates[] must be > 0.
565 * Also, the old variant lacks the Size property, so we may need to
566 * reduce the value of last_prop by 1 when doing arithmetic with it.
568 if (elem_count < last_prop - !is_new + 1)
571 count = elem_count - (last_prop - !is_new);
573 for (i = 0; i < elem_count && prop <= last_prop; i++, prop++) {
574 type = elements[i].type;
575 valid_type = hp_wmi_property_map[prop];
576 if (type != valid_type)
580 case HP_WMI_PROPERTY_OPERATIONAL_STATUS:
581 /* Old variant: CurrentState follows OperationalStatus. */
583 prop = HP_WMI_PROPERTY_CURRENT_STATE - 1;
586 case HP_WMI_PROPERTY_SIZE:
587 /* New variant: Size == count of PossibleStates[]. */
588 if (count != elements[i].integer.value)
592 case HP_WMI_PROPERTY_POSSIBLE_STATES:
593 /* PossibleStates[0] has already been type-checked. */
594 for (j = 0; i + 1 < elem_count && j + 1 < count; j++) {
595 type = elements[++i].type;
596 if (type != valid_type)
600 /* Old variant: BaseUnits follows PossibleStates[]. */
602 prop = HP_WMI_PROPERTY_BASE_UNITS - 1;
605 case HP_WMI_PROPERTY_CURRENT_STATE:
606 /* Old variant: PossibleStates[] follows CurrentState. */
608 prop = HP_WMI_PROPERTY_POSSIBLE_STATES - 1;
613 if (prop != last_prop + 1)
617 *out_is_new = is_new;
623 numeric_sensor_is_connected(const struct hp_wmi_numeric_sensor *nsensor)
625 u32 operational_status = nsensor->operational_status;
627 return operational_status != HP_WMI_STATUS_NO_CONTACT;
630 static int numeric_sensor_has_fault(const struct hp_wmi_numeric_sensor *nsensor)
632 u32 operational_status = nsensor->operational_status;
634 switch (operational_status) {
635 case HP_WMI_STATUS_DEGRADED:
636 case HP_WMI_STATUS_STRESSED: /* e.g. Overload, overtemp. */
637 case HP_WMI_STATUS_PREDICTIVE_FAILURE: /* e.g. Fan removed. */
638 case HP_WMI_STATUS_ERROR:
639 case HP_WMI_STATUS_NON_RECOVERABLE_ERROR:
640 case HP_WMI_STATUS_NO_CONTACT:
641 case HP_WMI_STATUS_LOST_COMMUNICATION:
642 case HP_WMI_STATUS_ABORTED:
643 case HP_WMI_STATUS_SUPPORTING_ENTITY_IN_ERROR:
645 /* Assume combination by addition; bitwise OR doesn't make sense. */
646 case HP_WMI_STATUS_COMPLETED + HP_WMI_STATUS_DEGRADED:
647 case HP_WMI_STATUS_COMPLETED + HP_WMI_STATUS_ERROR:
654 /* scale_numeric_sensor - scale sensor reading for hwmon */
655 static long scale_numeric_sensor(const struct hp_wmi_numeric_sensor *nsensor)
657 u32 current_reading = nsensor->current_reading;
658 s32 unit_modifier = nsensor->unit_modifier;
659 u32 sensor_type = nsensor->sensor_type;
660 u32 base_units = nsensor->base_units;
664 /* Fan readings are in RPM units; others are in milliunits. */
665 target_modifier = sensor_type == HP_WMI_TYPE_AIR_FLOW ? 0 : -3;
667 val = current_reading;
669 for (; unit_modifier < target_modifier; unit_modifier++)
670 val = DIV_ROUND_CLOSEST(val, 10);
672 for (; unit_modifier > target_modifier; unit_modifier--) {
673 if (val > LONG_MAX / 10) {
680 if (sensor_type == HP_WMI_TYPE_TEMPERATURE) {
681 switch (base_units) {
682 case HP_WMI_UNITS_DEGREES_F:
684 val = val <= LONG_MAX / 5 ?
685 DIV_ROUND_CLOSEST(val * 5, 9) :
686 DIV_ROUND_CLOSEST(val, 9) * 5;
689 case HP_WMI_UNITS_DEGREES_K:
690 val = milli_kelvin_to_millicelsius(val);
699 * classify_numeric_sensor - classify a numeric sensor
700 * @nsensor: pointer to numeric sensor struct
702 * Returns an enum hp_wmi_type value on success,
703 * or a negative value if the sensor type is unsupported.
705 static int classify_numeric_sensor(const struct hp_wmi_numeric_sensor *nsensor)
707 u32 sensor_type = nsensor->sensor_type;
708 u32 base_units = nsensor->base_units;
709 const char *name = nsensor->name;
711 switch (sensor_type) {
712 case HP_WMI_TYPE_TEMPERATURE:
714 * Some systems have sensors named "X Thermal Index" in "Other"
715 * units. Tested CPU sensor examples were found to be in °C,
716 * albeit perhaps "differently" accurate; e.g. readings were
717 * reliably -6°C vs. coretemp on a HP Compaq Elite 8300, and
718 * +8°C on an EliteOne G1 800. But this is still within the
719 * realm of plausibility for cheaply implemented motherboard
720 * sensors, and chassis readings were about as expected.
722 if ((base_units == HP_WMI_UNITS_OTHER &&
723 strstr(name, HP_WMI_PATTERN_TEMP_SENSOR)) ||
724 base_units == HP_WMI_UNITS_DEGREES_C ||
725 base_units == HP_WMI_UNITS_DEGREES_F ||
726 base_units == HP_WMI_UNITS_DEGREES_K)
727 return HP_WMI_TYPE_TEMPERATURE;
730 case HP_WMI_TYPE_VOLTAGE:
731 if (base_units == HP_WMI_UNITS_VOLTS)
732 return HP_WMI_TYPE_VOLTAGE;
735 case HP_WMI_TYPE_CURRENT:
736 if (base_units == HP_WMI_UNITS_AMPS)
737 return HP_WMI_TYPE_CURRENT;
740 case HP_WMI_TYPE_AIR_FLOW:
742 * Strangely, HP considers fan RPM sensor type to be
743 * "Air Flow" instead of the more intuitive "Tachometer".
745 if (base_units == HP_WMI_UNITS_RPM)
746 return HP_WMI_TYPE_AIR_FLOW;
754 populate_numeric_sensor_from_wobj(struct device *dev,
755 struct hp_wmi_numeric_sensor *nsensor,
756 union acpi_object *wobj, bool *out_is_new)
758 int last_prop = HP_WMI_PROPERTY_RATE_UNITS;
759 int prop = HP_WMI_PROPERTY_NAME;
760 const char **possible_states;
761 union acpi_object *element;
762 acpi_object_type type;
769 err = check_numeric_sensor_wobj(wobj, &size, &is_new);
773 possible_states = devm_kcalloc(dev, size, sizeof(*possible_states),
775 if (!possible_states)
778 element = wobj->package.elements;
779 nsensor->possible_states = possible_states;
780 nsensor->size = size;
783 last_prop = HP_WMI_PROPERTY_CURRENT_READING;
785 for (; prop <= last_prop; prop++) {
786 type = hp_wmi_property_map[prop];
788 err = extract_acpi_value(dev, element, type, &value, &string);
795 case HP_WMI_PROPERTY_NAME:
796 nsensor->name = string;
799 case HP_WMI_PROPERTY_DESCRIPTION:
800 nsensor->description = string;
803 case HP_WMI_PROPERTY_SENSOR_TYPE:
804 if (value > HP_WMI_TYPE_AIR_FLOW)
807 nsensor->sensor_type = value;
810 case HP_WMI_PROPERTY_OTHER_SENSOR_TYPE:
811 nsensor->other_sensor_type = string;
814 case HP_WMI_PROPERTY_OPERATIONAL_STATUS:
815 nsensor->operational_status = value;
817 /* Old variant: CurrentState follows OperationalStatus. */
819 prop = HP_WMI_PROPERTY_CURRENT_STATE - 1;
822 case HP_WMI_PROPERTY_SIZE:
823 break; /* Already set. */
825 case HP_WMI_PROPERTY_POSSIBLE_STATES:
826 *possible_states++ = string;
830 /* Old variant: BaseUnits follows PossibleStates[]. */
831 if (!is_new && !size)
832 prop = HP_WMI_PROPERTY_BASE_UNITS - 1;
835 case HP_WMI_PROPERTY_CURRENT_STATE:
836 nsensor->current_state = string;
838 /* Old variant: PossibleStates[] follows CurrentState. */
840 prop = HP_WMI_PROPERTY_POSSIBLE_STATES - 1;
843 case HP_WMI_PROPERTY_BASE_UNITS:
844 nsensor->base_units = value;
847 case HP_WMI_PROPERTY_UNIT_MODIFIER:
848 /* UnitModifier is signed. */
849 nsensor->unit_modifier = (s32)value;
852 case HP_WMI_PROPERTY_CURRENT_READING:
853 nsensor->current_reading = value;
856 case HP_WMI_PROPERTY_RATE_UNITS:
857 nsensor->rate_units = value;
865 *out_is_new = is_new;
870 /* update_numeric_sensor_from_wobj - update fungible sensor properties */
872 update_numeric_sensor_from_wobj(struct device *dev,
873 struct hp_wmi_numeric_sensor *nsensor,
874 const union acpi_object *wobj)
876 const union acpi_object *elements;
877 const union acpi_object *element;
884 err = check_numeric_sensor_wobj(wobj, &size, &is_new);
888 elements = wobj->package.elements;
890 element = &elements[HP_WMI_PROPERTY_OPERATIONAL_STATUS];
891 nsensor->operational_status = element->integer.value;
894 * In general, an index offset is needed after PossibleStates[0].
895 * On a new variant, CurrentState is after PossibleStates[]. This is
896 * not the case on an old variant, but we still need to offset the
897 * read because CurrentState is where Size would be on a new variant.
899 offset = is_new ? size - 1 : -2;
901 element = &elements[HP_WMI_PROPERTY_CURRENT_STATE + offset];
902 string = strim(element->string.pointer);
904 if (strcmp(string, nsensor->current_state)) {
905 devm_kfree(dev, nsensor->current_state);
906 nsensor->current_state = hp_wmi_strdup(dev, string);
909 /* Old variant: -2 (not -1) because it lacks the Size property. */
911 offset = (int)size - 2; /* size is > 0, i.e. may be 1. */
913 element = &elements[HP_WMI_PROPERTY_UNIT_MODIFIER + offset];
914 nsensor->unit_modifier = (s32)element->integer.value;
916 element = &elements[HP_WMI_PROPERTY_CURRENT_READING + offset];
917 nsensor->current_reading = element->integer.value;
921 * check_platform_events_wobj - validate a HPBIOS_PlatformEvents instance
922 * @wobj: pointer to WMI object instance to check
924 * Returns 0 on success, or a negative error code on error.
926 static int check_platform_events_wobj(const union acpi_object *wobj)
928 return check_wobj(wobj, hp_wmi_platform_events_property_map,
929 HP_WMI_PLATFORM_EVENTS_PROPERTY_POSSIBLE_STATUS);
933 populate_platform_events_from_wobj(struct device *dev,
934 struct hp_wmi_platform_events *pevents,
935 union acpi_object *wobj)
937 int last_prop = HP_WMI_PLATFORM_EVENTS_PROPERTY_POSSIBLE_STATUS;
938 int prop = HP_WMI_PLATFORM_EVENTS_PROPERTY_NAME;
939 union acpi_object *element;
940 acpi_object_type type;
945 err = check_platform_events_wobj(wobj);
949 element = wobj->package.elements;
951 for (; prop <= last_prop; prop++, element++) {
952 type = hp_wmi_platform_events_property_map[prop];
954 err = extract_acpi_value(dev, element, type, &value, &string);
959 case HP_WMI_PLATFORM_EVENTS_PROPERTY_NAME:
960 pevents->name = string;
963 case HP_WMI_PLATFORM_EVENTS_PROPERTY_DESCRIPTION:
964 pevents->description = string;
967 case HP_WMI_PLATFORM_EVENTS_PROPERTY_SOURCE_NAMESPACE:
968 if (strcasecmp(HP_WMI_EVENT_NAMESPACE, string))
971 pevents->source_namespace = string;
974 case HP_WMI_PLATFORM_EVENTS_PROPERTY_SOURCE_CLASS:
975 if (strcasecmp(HP_WMI_EVENT_CLASS, string))
978 pevents->source_class = string;
981 case HP_WMI_PLATFORM_EVENTS_PROPERTY_CATEGORY:
982 pevents->category = value;
985 case HP_WMI_PLATFORM_EVENTS_PROPERTY_POSSIBLE_SEVERITY:
986 pevents->possible_severity = value;
989 case HP_WMI_PLATFORM_EVENTS_PROPERTY_POSSIBLE_STATUS:
990 pevents->possible_status = value;
1002 * check_event_wobj - validate a HPBIOS_BIOSEvent instance
1003 * @wobj: pointer to WMI object instance to check
1005 * Returns 0 on success, or a negative error code on error.
1007 static int check_event_wobj(const union acpi_object *wobj)
1009 return check_wobj(wobj, hp_wmi_event_property_map,
1010 HP_WMI_EVENT_PROPERTY_STATUS);
1013 static int populate_event_from_wobj(struct hp_wmi_event *event,
1014 union acpi_object *wobj)
1016 int prop = HP_WMI_EVENT_PROPERTY_NAME;
1017 union acpi_object *element;
1020 err = check_event_wobj(wobj);
1024 element = wobj->package.elements;
1026 /* Extracted strings are NOT device-managed copies. */
1028 for (; prop <= HP_WMI_EVENT_PROPERTY_CATEGORY; prop++, element++) {
1030 case HP_WMI_EVENT_PROPERTY_NAME:
1031 event->name = strim(element->string.pointer);
1034 case HP_WMI_EVENT_PROPERTY_DESCRIPTION:
1035 event->description = strim(element->string.pointer);
1038 case HP_WMI_EVENT_PROPERTY_CATEGORY:
1039 event->category = element->integer.value;
1051 * classify_event - classify an event
1053 * @category: event category
1055 * Classify instances of both HPBIOS_PlatformEvents and HPBIOS_BIOSEvent from
1056 * property values. Recognition criteria are based on multiple ACPI dumps [3].
1058 * Returns an enum hp_wmi_type value on success,
1059 * or a negative value if the event type is unsupported.
1061 static int classify_event(const char *event_name, u32 category)
1063 if (category != HP_WMI_CATEGORY_SENSOR)
1066 /* Fan events have Name "X Stall". */
1067 if (strstr(event_name, HP_WMI_PATTERN_FAN_ALARM))
1068 return HP_WMI_TYPE_AIR_FLOW;
1070 /* Intrusion events have Name "Hood Intrusion". */
1071 if (!strcmp(event_name, HP_WMI_PATTERN_INTRUSION_ALARM))
1072 return HP_WMI_TYPE_INTRUSION;
1075 * Temperature events have Name either "Thermal Caution" or
1076 * "Thermal Critical". Deal only with "Thermal Critical" events.
1078 * "Thermal Caution" events have Status "Stressed", informing us that
1079 * the OperationalStatus of the related sensor has become "Stressed".
1080 * However, this is already a fault condition that will clear itself
1081 * when the sensor recovers, so we have no further interest in them.
1083 if (!strcmp(event_name, HP_WMI_PATTERN_TEMP_ALARM))
1084 return HP_WMI_TYPE_TEMPERATURE;
1090 * interpret_info - interpret sensor for hwmon
1091 * @info: pointer to sensor info struct
1093 * Should be called after the numeric sensor member has been updated.
1095 static void interpret_info(struct hp_wmi_info *info)
1097 const struct hp_wmi_numeric_sensor *nsensor = &info->nsensor;
1099 info->cached_val = scale_numeric_sensor(nsensor);
1100 info->last_updated = jiffies;
1104 * hp_wmi_update_info - poll WMI to update sensor info
1105 * @state: pointer to driver state
1106 * @info: pointer to sensor info struct
1108 * Returns 0 on success, or a negative error code on error.
1110 static int hp_wmi_update_info(struct hp_wmi_sensors *state,
1111 struct hp_wmi_info *info)
1113 struct hp_wmi_numeric_sensor *nsensor = &info->nsensor;
1114 struct device *dev = &state->wdev->dev;
1115 const union acpi_object *wobj;
1116 u8 instance = info->instance;
1119 if (time_after(jiffies, info->last_updated + HZ)) {
1120 mutex_lock(&state->lock);
1122 wobj = hp_wmi_get_wobj(HP_WMI_NUMERIC_SENSOR_GUID, instance);
1128 update_numeric_sensor_from_wobj(dev, nsensor, wobj);
1130 interpret_info(info);
1135 mutex_unlock(&state->lock);
1141 static int basic_string_show(struct seq_file *seqf, void *ignored)
1143 const char *str = seqf->private;
1145 seq_printf(seqf, "%s\n", str);
1149 DEFINE_SHOW_ATTRIBUTE(basic_string);
1151 static int fungible_show(struct seq_file *seqf, enum hp_wmi_property prop)
1153 struct hp_wmi_numeric_sensor *nsensor;
1154 struct hp_wmi_sensors *state;
1155 struct hp_wmi_info *info;
1158 info = seqf->private;
1159 state = info->state;
1160 nsensor = &info->nsensor;
1162 err = hp_wmi_update_info(state, info);
1167 case HP_WMI_PROPERTY_OPERATIONAL_STATUS:
1168 seq_printf(seqf, "%u\n", nsensor->operational_status);
1171 case HP_WMI_PROPERTY_CURRENT_STATE:
1172 seq_printf(seqf, "%s\n", nsensor->current_state);
1175 case HP_WMI_PROPERTY_UNIT_MODIFIER:
1176 seq_printf(seqf, "%d\n", nsensor->unit_modifier);
1179 case HP_WMI_PROPERTY_CURRENT_READING:
1180 seq_printf(seqf, "%u\n", nsensor->current_reading);
1190 static int operational_status_show(struct seq_file *seqf, void *ignored)
1192 return fungible_show(seqf, HP_WMI_PROPERTY_OPERATIONAL_STATUS);
1194 DEFINE_SHOW_ATTRIBUTE(operational_status);
1196 static int current_state_show(struct seq_file *seqf, void *ignored)
1198 return fungible_show(seqf, HP_WMI_PROPERTY_CURRENT_STATE);
1200 DEFINE_SHOW_ATTRIBUTE(current_state);
1202 static int possible_states_show(struct seq_file *seqf, void *ignored)
1204 struct hp_wmi_numeric_sensor *nsensor = seqf->private;
1207 for (i = 0; i < nsensor->size; i++)
1208 seq_printf(seqf, "%s%s", i ? "," : "",
1209 nsensor->possible_states[i]);
1211 seq_puts(seqf, "\n");
1215 DEFINE_SHOW_ATTRIBUTE(possible_states);
1217 static int unit_modifier_show(struct seq_file *seqf, void *ignored)
1219 return fungible_show(seqf, HP_WMI_PROPERTY_UNIT_MODIFIER);
1221 DEFINE_SHOW_ATTRIBUTE(unit_modifier);
1223 static int current_reading_show(struct seq_file *seqf, void *ignored)
1225 return fungible_show(seqf, HP_WMI_PROPERTY_CURRENT_READING);
1227 DEFINE_SHOW_ATTRIBUTE(current_reading);
1229 /* hp_wmi_devm_debugfs_remove - devm callback for debugfs cleanup */
1230 static void hp_wmi_devm_debugfs_remove(void *res)
1232 debugfs_remove_recursive(res);
1235 /* hp_wmi_debugfs_init - create and populate debugfs directory tree */
1236 static void hp_wmi_debugfs_init(struct device *dev, struct hp_wmi_info *info,
1237 struct hp_wmi_platform_events *pevents,
1238 u8 icount, u8 pcount, bool is_new)
1240 struct hp_wmi_numeric_sensor *nsensor;
1241 char buf[HP_WMI_MAX_STR_SIZE];
1242 struct dentry *debugfs;
1243 struct dentry *entries;
1248 /* dev_name() gives a not-very-friendly GUID for WMI devices. */
1249 scnprintf(buf, sizeof(buf), "hp-wmi-sensors-%u", dev->id);
1251 debugfs = debugfs_create_dir(buf, NULL);
1252 if (IS_ERR(debugfs))
1255 err = devm_add_action_or_reset(dev, hp_wmi_devm_debugfs_remove,
1260 entries = debugfs_create_dir("sensor", debugfs);
1262 for (i = 0; i < icount; i++, info++) {
1263 nsensor = &info->nsensor;
1265 scnprintf(buf, sizeof(buf), "%u", i);
1266 dir = debugfs_create_dir(buf, entries);
1268 debugfs_create_file("name", 0444, dir,
1269 (void *)nsensor->name,
1270 &basic_string_fops);
1272 debugfs_create_file("description", 0444, dir,
1273 (void *)nsensor->description,
1274 &basic_string_fops);
1276 debugfs_create_u32("sensor_type", 0444, dir,
1277 &nsensor->sensor_type);
1279 debugfs_create_file("other_sensor_type", 0444, dir,
1280 (void *)nsensor->other_sensor_type,
1281 &basic_string_fops);
1283 debugfs_create_file("operational_status", 0444, dir,
1284 info, &operational_status_fops);
1286 debugfs_create_file("possible_states", 0444, dir,
1287 nsensor, &possible_states_fops);
1289 debugfs_create_file("current_state", 0444, dir,
1290 info, ¤t_state_fops);
1292 debugfs_create_u32("base_units", 0444, dir,
1293 &nsensor->base_units);
1295 debugfs_create_file("unit_modifier", 0444, dir,
1296 info, &unit_modifier_fops);
1298 debugfs_create_file("current_reading", 0444, dir,
1299 info, ¤t_reading_fops);
1302 debugfs_create_u32("rate_units", 0444, dir,
1303 &nsensor->rate_units);
1309 entries = debugfs_create_dir("platform_events", debugfs);
1311 for (i = 0; i < pcount; i++, pevents++) {
1312 scnprintf(buf, sizeof(buf), "%u", i);
1313 dir = debugfs_create_dir(buf, entries);
1315 debugfs_create_file("name", 0444, dir,
1316 (void *)pevents->name,
1317 &basic_string_fops);
1319 debugfs_create_file("description", 0444, dir,
1320 (void *)pevents->description,
1321 &basic_string_fops);
1323 debugfs_create_file("source_namespace", 0444, dir,
1324 (void *)pevents->source_namespace,
1325 &basic_string_fops);
1327 debugfs_create_file("source_class", 0444, dir,
1328 (void *)pevents->source_class,
1329 &basic_string_fops);
1331 debugfs_create_u32("category", 0444, dir,
1332 &pevents->category);
1334 debugfs_create_u32("possible_severity", 0444, dir,
1335 &pevents->possible_severity);
1337 debugfs_create_u32("possible_status", 0444, dir,
1338 &pevents->possible_status);
1342 static umode_t hp_wmi_hwmon_is_visible(const void *drvdata,
1343 enum hwmon_sensor_types type,
1344 u32 attr, int channel)
1346 const struct hp_wmi_sensors *state = drvdata;
1347 const struct hp_wmi_info *info;
1349 if (type == hwmon_intrusion)
1350 return state->has_intrusion ? 0644 : 0;
1352 if (!state->info_map[type] || !state->info_map[type][channel])
1355 info = state->info_map[type][channel];
1357 if ((type == hwmon_temp && attr == hwmon_temp_alarm) ||
1358 (type == hwmon_fan && attr == hwmon_fan_alarm))
1359 return info->has_alarm ? 0444 : 0;
1364 static int hp_wmi_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
1365 u32 attr, int channel, long *out_val)
1367 struct hp_wmi_sensors *state = dev_get_drvdata(dev);
1368 const struct hp_wmi_numeric_sensor *nsensor;
1369 struct hp_wmi_info *info;
1372 if (type == hwmon_intrusion) {
1373 *out_val = state->intrusion ? 1 : 0;
1378 info = state->info_map[type][channel];
1380 if ((type == hwmon_temp && attr == hwmon_temp_alarm) ||
1381 (type == hwmon_fan && attr == hwmon_fan_alarm)) {
1382 *out_val = info->alarm ? 1 : 0;
1383 info->alarm = false;
1388 nsensor = &info->nsensor;
1390 err = hp_wmi_update_info(state, info);
1394 if ((type == hwmon_temp && attr == hwmon_temp_fault) ||
1395 (type == hwmon_fan && attr == hwmon_fan_fault))
1396 *out_val = numeric_sensor_has_fault(nsensor);
1398 *out_val = info->cached_val;
1403 static int hp_wmi_hwmon_read_string(struct device *dev,
1404 enum hwmon_sensor_types type, u32 attr,
1405 int channel, const char **out_str)
1407 const struct hp_wmi_sensors *state = dev_get_drvdata(dev);
1408 const struct hp_wmi_info *info;
1410 info = state->info_map[type][channel];
1411 *out_str = info->nsensor.name;
1416 static int hp_wmi_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
1417 u32 attr, int channel, long val)
1419 struct hp_wmi_sensors *state = dev_get_drvdata(dev);
1424 mutex_lock(&state->lock);
1426 state->intrusion = false;
1428 mutex_unlock(&state->lock);
1433 static const struct hwmon_ops hp_wmi_hwmon_ops = {
1434 .is_visible = hp_wmi_hwmon_is_visible,
1435 .read = hp_wmi_hwmon_read,
1436 .read_string = hp_wmi_hwmon_read_string,
1437 .write = hp_wmi_hwmon_write,
1440 static struct hwmon_chip_info hp_wmi_chip_info = {
1441 .ops = &hp_wmi_hwmon_ops,
1445 static struct hp_wmi_info *match_fan_event(struct hp_wmi_sensors *state,
1446 const char *event_description)
1448 struct hp_wmi_info **ptr_info = state->info_map[hwmon_fan];
1449 u8 fan_count = state->channel_count[hwmon_fan];
1450 struct hp_wmi_info *info;
1454 /* Fan event has Description "X Speed". Sensor has Name "X[ Speed]". */
1456 for (i = 0; i < fan_count; i++, ptr_info++) {
1458 name = info->nsensor.name;
1460 if (strstr(event_description, name))
1467 static u8 match_temp_events(struct hp_wmi_sensors *state,
1468 const char *event_description,
1469 struct hp_wmi_info *temp_info[])
1471 struct hp_wmi_info **ptr_info = state->info_map[hwmon_temp];
1472 u8 temp_count = state->channel_count[hwmon_temp];
1473 struct hp_wmi_info *info;
1480 /* Description is either "CPU Thermal Index" or "Chassis Thermal Index". */
1482 is_cpu = !strcmp(event_description, HP_WMI_PATTERN_CPU_TEMP);
1483 is_sys = !strcmp(event_description, HP_WMI_PATTERN_SYS_TEMP);
1484 if (!is_cpu && !is_sys)
1488 * CPU event: Match one sensor with Name either "CPU Thermal Index" or
1489 * "CPU Temperature", or multiple with Name(s) "CPU[#] Temperature".
1491 * Chassis event: Match one sensor with Name either
1492 * "Chassis Thermal Index" or "System Ambient Temperature".
1495 for (i = 0; i < temp_count; i++, ptr_info++) {
1497 name = info->nsensor.name;
1499 if ((is_cpu && (!strcmp(name, HP_WMI_PATTERN_CPU_TEMP) ||
1500 !strcmp(name, HP_WMI_PATTERN_CPU_TEMP2))) ||
1501 (is_sys && (!strcmp(name, HP_WMI_PATTERN_SYS_TEMP) ||
1502 !strcmp(name, HP_WMI_PATTERN_SYS_TEMP2)))) {
1503 temp_info[0] = info;
1507 if (is_cpu && (strstr(name, HP_WMI_PATTERN_CPU) &&
1508 strstr(name, HP_WMI_PATTERN_TEMP)))
1509 temp_info[count++] = info;
1515 /* hp_wmi_devm_debugfs_remove - devm callback for WMI event handler removal */
1516 static void hp_wmi_devm_notify_remove(void *ignored)
1518 wmi_remove_notify_handler(HP_WMI_EVENT_GUID);
1521 /* hp_wmi_notify - WMI event notification handler */
1522 static void hp_wmi_notify(u32 value, void *context)
1524 struct hp_wmi_info *temp_info[HP_WMI_MAX_INSTANCES] = {};
1525 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
1526 struct hp_wmi_sensors *state = context;
1527 struct device *dev = &state->wdev->dev;
1528 struct hp_wmi_info *fan_info;
1529 struct hp_wmi_event event;
1530 union acpi_object *wobj;
1536 * The following warning may occur in the kernel log:
1538 * ACPI Warning: \_SB.WMID._WED: Return type mismatch -
1539 * found Package, expected Integer/String/Buffer
1541 * After using [4] to decode BMOF blobs found in [3], careless copying
1542 * of BIOS code seems the most likely explanation for this warning.
1543 * HP_WMI_EVENT_GUID refers to \\.\root\WMI\HPBIOS_BIOSEvent on
1544 * business-class systems, but it refers to \\.\root\WMI\hpqBEvnt on
1545 * non-business-class systems. Per the existing hp-wmi driver, it
1546 * looks like an instance of hpqBEvnt delivered as event data may
1547 * indeed take the form of a raw ACPI_BUFFER on non-business-class
1548 * systems ("may" because ASL shows some BIOSes do strange things).
1550 * In any case, we can ignore this warning, because we always validate
1551 * the event data to ensure it is an ACPI_PACKAGE containing a
1552 * HPBIOS_BIOSEvent instance.
1555 mutex_lock(&state->lock);
1557 err = wmi_get_event_data(value, &out);
1558 if (ACPI_FAILURE(err))
1563 err = populate_event_from_wobj(&event, wobj);
1565 dev_warn(dev, "Bad event data (ACPI type %d)\n", wobj->type);
1569 event_type = classify_event(event.name, event.category);
1570 switch (event_type) {
1571 case HP_WMI_TYPE_AIR_FLOW:
1572 fan_info = match_fan_event(state, event.description);
1574 fan_info->alarm = true;
1577 case HP_WMI_TYPE_INTRUSION:
1578 state->intrusion = true;
1581 case HP_WMI_TYPE_TEMPERATURE:
1582 count = match_temp_events(state, event.description, temp_info);
1584 temp_info[--count]->alarm = true;
1595 mutex_unlock(&state->lock);
1598 static int init_platform_events(struct device *dev,
1599 struct hp_wmi_platform_events **out_pevents,
1602 struct hp_wmi_platform_events *pevents_arr;
1603 struct hp_wmi_platform_events *pevents;
1604 union acpi_object *wobj;
1609 count = hp_wmi_wobj_instance_count(HP_WMI_PLATFORM_EVENTS_GUID);
1613 dev_dbg(dev, "No platform events\n");
1618 pevents_arr = devm_kcalloc(dev, count, sizeof(*pevents), GFP_KERNEL);
1622 for (i = 0, pevents = pevents_arr; i < count; i++, pevents++) {
1623 wobj = hp_wmi_get_wobj(HP_WMI_PLATFORM_EVENTS_GUID, i);
1627 err = populate_platform_events_from_wobj(dev, pevents, wobj);
1635 *out_pevents = pevents_arr;
1636 *out_pcount = count;
1638 dev_dbg(dev, "Found %u platform events\n", count);
1643 static int init_numeric_sensors(struct hp_wmi_sensors *state,
1644 struct hp_wmi_info *connected[],
1645 struct hp_wmi_info **out_info,
1646 u8 *out_icount, u8 *out_count,
1649 struct hp_wmi_info ***info_map = state->info_map;
1650 u8 *channel_count = state->channel_count;
1651 struct device *dev = &state->wdev->dev;
1652 struct hp_wmi_numeric_sensor *nsensor;
1653 u8 channel_index[hwmon_max] = {};
1654 enum hwmon_sensor_types type;
1655 struct hp_wmi_info *info_arr;
1656 struct hp_wmi_info *info;
1657 union acpi_object *wobj;
1666 icount = hp_wmi_wobj_instance_count(HP_WMI_NUMERIC_SENSOR_GUID);
1670 info_arr = devm_kcalloc(dev, icount, sizeof(*info), GFP_KERNEL);
1674 for (i = 0, info = info_arr; i < icount; i++, info++) {
1675 wobj = hp_wmi_get_wobj(HP_WMI_NUMERIC_SENSOR_GUID, i);
1680 info->state = state;
1681 nsensor = &info->nsensor;
1683 err = populate_numeric_sensor_from_wobj(dev, nsensor, wobj,
1691 if (!numeric_sensor_is_connected(nsensor))
1694 wtype = classify_numeric_sensor(nsensor);
1698 type = hp_wmi_hwmon_type_map[wtype];
1700 channel_count[type]++;
1704 interpret_info(info);
1706 connected[count++] = info;
1709 dev_dbg(dev, "Found %u sensors (%u connected)\n", i, count);
1711 for (i = 0; i < count; i++) {
1712 info = connected[i];
1714 c = channel_index[type]++;
1716 if (!info_map[type]) {
1717 info_map[type] = devm_kcalloc(dev, channel_count[type],
1720 if (!info_map[type])
1724 info_map[type][c] = info;
1727 *out_info = info_arr;
1728 *out_icount = icount;
1730 *out_is_new = is_new;
1735 static bool find_event_attributes(struct hp_wmi_sensors *state,
1736 struct hp_wmi_platform_events *pevents,
1740 * The existence of this HPBIOS_PlatformEvents instance:
1743 * Name = "Rear Chassis Fan0 Stall";
1744 * Description = "Rear Chassis Fan0 Speed";
1745 * Category = 3; // "Sensor"
1746 * PossibleSeverity = 25; // "Critical Failure"
1747 * PossibleStatus = 5; // "Predictive Failure"
1751 * means that this HPBIOS_BIOSEvent instance may occur:
1754 * Name = "Rear Chassis Fan0 Stall";
1755 * Description = "Rear Chassis Fan0 Speed";
1756 * Category = 3; // "Sensor"
1757 * Severity = 25; // "Critical Failure"
1758 * Status = 5; // "Predictive Failure"
1761 * After the event occurs (e.g. because the fan was unplugged),
1762 * polling the related HPBIOS_BIOSNumericSensor instance gives:
1765 * Name = "Rear Chassis Fan0";
1766 * Description = "Reports rear chassis fan0 speed";
1767 * OperationalStatus = 5; // "Predictive Failure", was 3 ("OK")
1768 * CurrentReading = 0;
1772 * In this example, the hwmon fan channel for "Rear Chassis Fan0"
1773 * should support the alarm flag and have it be set if the related
1774 * HPBIOS_BIOSEvent instance occurs.
1776 * In addition to fan events, temperature (CPU/chassis) and intrusion
1777 * events are relevant to hwmon [2]. Note that much information in [2]
1778 * is unreliable; it is referenced in addition to ACPI dumps [3] merely
1779 * to support the conclusion that sensor and event names/descriptions
1780 * are systematic enough to allow this driver to match them.
1782 * Complications and limitations:
1784 * - Strings are freeform and may vary, cf. sensor Name "CPU0 Fan"
1785 * on a Z420 vs. "CPU Fan Speed" on an EliteOne 800 G1.
1786 * - Leading/trailing whitespace is a rare but real possibility [3].
1787 * - The HPBIOS_PlatformEvents object may not exist or its instances
1788 * may show that the system only has e.g. BIOS setting-related
1789 * events (cf. the ProBook 4540s and ProBook 470 G0 [3]).
1792 struct hp_wmi_info *temp_info[HP_WMI_MAX_INSTANCES] = {};
1793 const char *event_description;
1794 struct hp_wmi_info *fan_info;
1795 bool has_events = false;
1796 const char *event_name;
1802 for (i = 0; i < pevents_count; i++, pevents++) {
1803 event_name = pevents->name;
1804 event_description = pevents->description;
1805 event_category = pevents->category;
1807 event_type = classify_event(event_name, event_category);
1808 switch (event_type) {
1809 case HP_WMI_TYPE_AIR_FLOW:
1810 fan_info = match_fan_event(state, event_description);
1814 fan_info->has_alarm = true;
1818 case HP_WMI_TYPE_INTRUSION:
1819 state->has_intrusion = true;
1823 case HP_WMI_TYPE_TEMPERATURE:
1824 count = match_temp_events(state, event_description,
1830 temp_info[--count]->has_alarm = true;
1842 static int make_chip_info(struct hp_wmi_sensors *state, bool has_events)
1844 const struct hwmon_channel_info **ptr_channel_info;
1845 struct hp_wmi_info ***info_map = state->info_map;
1846 u8 *channel_count = state->channel_count;
1847 struct hwmon_channel_info *channel_info;
1848 struct device *dev = &state->wdev->dev;
1849 enum hwmon_sensor_types type;
1856 if (channel_count[hwmon_temp])
1857 channel_count[hwmon_chip] = 1;
1859 if (has_events && state->has_intrusion)
1860 channel_count[hwmon_intrusion] = 1;
1862 for (type = hwmon_chip; type < hwmon_max; type++)
1863 if (channel_count[type])
1866 channel_info = devm_kcalloc(dev, type_count,
1867 sizeof(*channel_info), GFP_KERNEL);
1871 ptr_channel_info = devm_kcalloc(dev, type_count + 1,
1872 sizeof(*ptr_channel_info), GFP_KERNEL);
1873 if (!ptr_channel_info)
1876 hp_wmi_chip_info.info = ptr_channel_info;
1878 for (type = hwmon_chip; type < hwmon_max; type++) {
1879 count = channel_count[type];
1883 config = devm_kcalloc(dev, count + 1,
1884 sizeof(*config), GFP_KERNEL);
1888 attr = hp_wmi_hwmon_attributes[type];
1889 channel_info->type = type;
1890 channel_info->config = config;
1891 memset32(config, attr, count);
1893 *ptr_channel_info++ = channel_info++;
1895 if (!has_events || (type != hwmon_temp && type != hwmon_fan))
1898 attr = type == hwmon_temp ? HWMON_T_ALARM : HWMON_F_ALARM;
1900 for (i = 0; i < count; i++)
1901 if (info_map[type][i]->has_alarm)
1908 static bool add_event_handler(struct hp_wmi_sensors *state)
1910 struct device *dev = &state->wdev->dev;
1913 err = wmi_install_notify_handler(HP_WMI_EVENT_GUID,
1914 hp_wmi_notify, state);
1916 dev_info(dev, "Failed to subscribe to WMI event\n");
1920 err = devm_add_action_or_reset(dev, hp_wmi_devm_notify_remove, NULL);
1927 static int hp_wmi_sensors_init(struct hp_wmi_sensors *state)
1929 struct hp_wmi_info *connected[HP_WMI_MAX_INSTANCES];
1930 struct hp_wmi_platform_events *pevents;
1931 struct device *dev = &state->wdev->dev;
1932 struct hp_wmi_info *info;
1933 struct device *hwdev;
1941 err = init_platform_events(dev, &pevents, &pcount);
1945 err = init_numeric_sensors(state, connected, &info,
1946 &icount, &count, &is_new);
1950 if (IS_ENABLED(CONFIG_DEBUG_FS))
1951 hp_wmi_debugfs_init(dev, info, pevents, icount, pcount, is_new);
1954 return 0; /* No connected sensors; debugfs only. */
1956 has_events = find_event_attributes(state, pevents, pcount);
1958 /* Survive failure to install WMI event handler. */
1959 if (has_events && !add_event_handler(state))
1962 err = make_chip_info(state, has_events);
1966 hwdev = devm_hwmon_device_register_with_info(dev, "hp_wmi_sensors",
1967 state, &hp_wmi_chip_info,
1969 return PTR_ERR_OR_ZERO(hwdev);
1972 static int hp_wmi_sensors_probe(struct wmi_device *wdev, const void *context)
1974 struct device *dev = &wdev->dev;
1975 struct hp_wmi_sensors *state;
1977 state = devm_kzalloc(dev, sizeof(*state), GFP_KERNEL);
1983 mutex_init(&state->lock);
1985 dev_set_drvdata(dev, state);
1987 return hp_wmi_sensors_init(state);
1990 static const struct wmi_device_id hp_wmi_sensors_id_table[] = {
1991 { HP_WMI_NUMERIC_SENSOR_GUID, NULL },
1995 static struct wmi_driver hp_wmi_sensors_driver = {
1996 .driver = { .name = "hp-wmi-sensors" },
1997 .id_table = hp_wmi_sensors_id_table,
1998 .probe = hp_wmi_sensors_probe,
2000 module_wmi_driver(hp_wmi_sensors_driver);
2002 MODULE_AUTHOR("James Seo <james@equiv.tech>");
2003 MODULE_DESCRIPTION("HP WMI Sensors driver");
2004 MODULE_LICENSE("GPL");