forked from intel/linux-intel-lts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hp-wmi-sensors.c
2087 lines (1723 loc) · 55.7 KB
/
hp-wmi-sensors.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* hwmon driver for HP (and some HP Compaq) business-class computers that
* report numeric sensor data via Windows Management Instrumentation (WMI).
*
* Copyright (C) 2023 James Seo <[email protected]>
*
* References:
* [1] Hewlett-Packard Development Company, L.P.,
* "HP Client Management Interface Technical White Paper", 2005. [Online].
* Available: https://h20331.www2.hp.com/hpsub/downloads/cmi_whitepaper.pdf
* [2] Hewlett-Packard Development Company, L.P.,
* "HP Retail Manageability", 2012. [Online].
* Available: http://h10032.www1.hp.com/ctg/Manual/c03291135.pdf
* [3] Linux Hardware Project, A. Ponomarenko et al.,
* "linuxhw/ACPI - Collect ACPI table dumps", 2018. [Online].
* Available: https://github.com/linuxhw/ACPI
* [4] P. Rohár, "bmfdec - Decompile binary MOF file (BMF) from WMI buffer",
* 2017. [Online]. Available: https://github.com/pali/bmfdec
* [5] Microsoft Corporation, "Driver-Defined WMI Data Items", 2017. [Online].
* Available: https://learn.microsoft.com/en-us/windows-hardware/drivers/kernel/driver-defined-wmi-data-items
*/
#include <linux/acpi.h>
#include <linux/debugfs.h>
#include <linux/hwmon.h>
#include <linux/jiffies.h>
#include <linux/mutex.h>
#include <linux/nls.h>
#include <linux/units.h>
#include <linux/wmi.h>
#define HP_WMI_EVENT_NAMESPACE "root\\WMI"
#define HP_WMI_EVENT_CLASS "HPBIOS_BIOSEvent"
#define HP_WMI_EVENT_GUID "95F24279-4D7B-4334-9387-ACCDC67EF61C"
#define HP_WMI_NUMERIC_SENSOR_GUID "8F1F6435-9F42-42C8-BADC-0E9424F20C9A"
#define HP_WMI_PLATFORM_EVENTS_GUID "41227C2D-80E1-423F-8B8E-87E32755A0EB"
/* Patterns for recognizing sensors and matching events to channels. */
#define HP_WMI_PATTERN_SYS_TEMP "Chassis Thermal Index"
#define HP_WMI_PATTERN_SYS_TEMP2 "System Ambient Temperature"
#define HP_WMI_PATTERN_CPU_TEMP "CPU Thermal Index"
#define HP_WMI_PATTERN_CPU_TEMP2 "CPU Temperature"
#define HP_WMI_PATTERN_TEMP_SENSOR "Thermal Index"
#define HP_WMI_PATTERN_TEMP_ALARM "Thermal Critical"
#define HP_WMI_PATTERN_INTRUSION_ALARM "Hood Intrusion"
#define HP_WMI_PATTERN_FAN_ALARM "Stall"
#define HP_WMI_PATTERN_TEMP "Temperature"
#define HP_WMI_PATTERN_CPU "CPU"
/* These limits are arbitrary. The WMI implementation may vary by system. */
#define HP_WMI_MAX_STR_SIZE 128U
#define HP_WMI_MAX_PROPERTIES 32U
#define HP_WMI_MAX_INSTANCES 32U
enum hp_wmi_type {
HP_WMI_TYPE_OTHER = 1,
HP_WMI_TYPE_TEMPERATURE = 2,
HP_WMI_TYPE_VOLTAGE = 3,
HP_WMI_TYPE_CURRENT = 4,
HP_WMI_TYPE_AIR_FLOW = 12,
HP_WMI_TYPE_INTRUSION = 0xabadb01, /* Custom. */
};
enum hp_wmi_category {
HP_WMI_CATEGORY_SENSOR = 3,
};
enum hp_wmi_severity {
HP_WMI_SEVERITY_UNKNOWN = 0,
HP_WMI_SEVERITY_OK = 5,
HP_WMI_SEVERITY_DEGRADED_WARNING = 10,
HP_WMI_SEVERITY_MINOR_FAILURE = 15,
HP_WMI_SEVERITY_MAJOR_FAILURE = 20,
HP_WMI_SEVERITY_CRITICAL_FAILURE = 25,
HP_WMI_SEVERITY_NON_RECOVERABLE_ERROR = 30,
};
enum hp_wmi_status {
HP_WMI_STATUS_OK = 2,
HP_WMI_STATUS_DEGRADED = 3,
HP_WMI_STATUS_STRESSED = 4,
HP_WMI_STATUS_PREDICTIVE_FAILURE = 5,
HP_WMI_STATUS_ERROR = 6,
HP_WMI_STATUS_NON_RECOVERABLE_ERROR = 7,
HP_WMI_STATUS_NO_CONTACT = 12,
HP_WMI_STATUS_LOST_COMMUNICATION = 13,
HP_WMI_STATUS_ABORTED = 14,
HP_WMI_STATUS_SUPPORTING_ENTITY_IN_ERROR = 16,
/* Occurs combined with one of "OK", "Degraded", and "Error" [1]. */
HP_WMI_STATUS_COMPLETED = 17,
};
enum hp_wmi_units {
HP_WMI_UNITS_OTHER = 1,
HP_WMI_UNITS_DEGREES_C = 2,
HP_WMI_UNITS_DEGREES_F = 3,
HP_WMI_UNITS_DEGREES_K = 4,
HP_WMI_UNITS_VOLTS = 5,
HP_WMI_UNITS_AMPS = 6,
HP_WMI_UNITS_RPM = 19,
};
enum hp_wmi_property {
HP_WMI_PROPERTY_NAME = 0,
HP_WMI_PROPERTY_DESCRIPTION = 1,
HP_WMI_PROPERTY_SENSOR_TYPE = 2,
HP_WMI_PROPERTY_OTHER_SENSOR_TYPE = 3,
HP_WMI_PROPERTY_OPERATIONAL_STATUS = 4,
HP_WMI_PROPERTY_SIZE = 5,
HP_WMI_PROPERTY_POSSIBLE_STATES = 6,
HP_WMI_PROPERTY_CURRENT_STATE = 7,
HP_WMI_PROPERTY_BASE_UNITS = 8,
HP_WMI_PROPERTY_UNIT_MODIFIER = 9,
HP_WMI_PROPERTY_CURRENT_READING = 10,
HP_WMI_PROPERTY_RATE_UNITS = 11,
};
static const acpi_object_type hp_wmi_property_map[] = {
[HP_WMI_PROPERTY_NAME] = ACPI_TYPE_STRING,
[HP_WMI_PROPERTY_DESCRIPTION] = ACPI_TYPE_STRING,
[HP_WMI_PROPERTY_SENSOR_TYPE] = ACPI_TYPE_INTEGER,
[HP_WMI_PROPERTY_OTHER_SENSOR_TYPE] = ACPI_TYPE_STRING,
[HP_WMI_PROPERTY_OPERATIONAL_STATUS] = ACPI_TYPE_INTEGER,
[HP_WMI_PROPERTY_SIZE] = ACPI_TYPE_INTEGER,
[HP_WMI_PROPERTY_POSSIBLE_STATES] = ACPI_TYPE_STRING,
[HP_WMI_PROPERTY_CURRENT_STATE] = ACPI_TYPE_STRING,
[HP_WMI_PROPERTY_BASE_UNITS] = ACPI_TYPE_INTEGER,
[HP_WMI_PROPERTY_UNIT_MODIFIER] = ACPI_TYPE_INTEGER,
[HP_WMI_PROPERTY_CURRENT_READING] = ACPI_TYPE_INTEGER,
[HP_WMI_PROPERTY_RATE_UNITS] = ACPI_TYPE_INTEGER,
};
enum hp_wmi_platform_events_property {
HP_WMI_PLATFORM_EVENTS_PROPERTY_NAME = 0,
HP_WMI_PLATFORM_EVENTS_PROPERTY_DESCRIPTION = 1,
HP_WMI_PLATFORM_EVENTS_PROPERTY_SOURCE_NAMESPACE = 2,
HP_WMI_PLATFORM_EVENTS_PROPERTY_SOURCE_CLASS = 3,
HP_WMI_PLATFORM_EVENTS_PROPERTY_CATEGORY = 4,
HP_WMI_PLATFORM_EVENTS_PROPERTY_POSSIBLE_SEVERITY = 5,
HP_WMI_PLATFORM_EVENTS_PROPERTY_POSSIBLE_STATUS = 6,
};
static const acpi_object_type hp_wmi_platform_events_property_map[] = {
[HP_WMI_PLATFORM_EVENTS_PROPERTY_NAME] = ACPI_TYPE_STRING,
[HP_WMI_PLATFORM_EVENTS_PROPERTY_DESCRIPTION] = ACPI_TYPE_STRING,
[HP_WMI_PLATFORM_EVENTS_PROPERTY_SOURCE_NAMESPACE] = ACPI_TYPE_STRING,
[HP_WMI_PLATFORM_EVENTS_PROPERTY_SOURCE_CLASS] = ACPI_TYPE_STRING,
[HP_WMI_PLATFORM_EVENTS_PROPERTY_CATEGORY] = ACPI_TYPE_INTEGER,
[HP_WMI_PLATFORM_EVENTS_PROPERTY_POSSIBLE_SEVERITY] = ACPI_TYPE_INTEGER,
[HP_WMI_PLATFORM_EVENTS_PROPERTY_POSSIBLE_STATUS] = ACPI_TYPE_INTEGER,
};
enum hp_wmi_event_property {
HP_WMI_EVENT_PROPERTY_NAME = 0,
HP_WMI_EVENT_PROPERTY_DESCRIPTION = 1,
HP_WMI_EVENT_PROPERTY_CATEGORY = 2,
HP_WMI_EVENT_PROPERTY_SEVERITY = 3,
HP_WMI_EVENT_PROPERTY_STATUS = 4,
};
static const acpi_object_type hp_wmi_event_property_map[] = {
[HP_WMI_EVENT_PROPERTY_NAME] = ACPI_TYPE_STRING,
[HP_WMI_EVENT_PROPERTY_DESCRIPTION] = ACPI_TYPE_STRING,
[HP_WMI_EVENT_PROPERTY_CATEGORY] = ACPI_TYPE_INTEGER,
[HP_WMI_EVENT_PROPERTY_SEVERITY] = ACPI_TYPE_INTEGER,
[HP_WMI_EVENT_PROPERTY_STATUS] = ACPI_TYPE_INTEGER,
};
static const enum hwmon_sensor_types hp_wmi_hwmon_type_map[] = {
[HP_WMI_TYPE_TEMPERATURE] = hwmon_temp,
[HP_WMI_TYPE_VOLTAGE] = hwmon_in,
[HP_WMI_TYPE_CURRENT] = hwmon_curr,
[HP_WMI_TYPE_AIR_FLOW] = hwmon_fan,
};
static const u32 hp_wmi_hwmon_attributes[hwmon_max] = {
[hwmon_chip] = HWMON_C_REGISTER_TZ,
[hwmon_temp] = HWMON_T_INPUT | HWMON_T_LABEL | HWMON_T_FAULT,
[hwmon_in] = HWMON_I_INPUT | HWMON_I_LABEL,
[hwmon_curr] = HWMON_C_INPUT | HWMON_C_LABEL,
[hwmon_fan] = HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_FAULT,
[hwmon_intrusion] = HWMON_INTRUSION_ALARM,
};
/*
* struct hp_wmi_numeric_sensor - a HPBIOS_BIOSNumericSensor instance
*
* Two variants of HPBIOS_BIOSNumericSensor are known. The first is specified
* in [1] and appears to be much more widespread. The second was discovered by
* decoding BMOF blobs [4], seems to be found only in some newer ZBook systems
* [3], and has two new properties and a slightly different property order.
*
* These differences don't matter on Windows, where WMI object properties are
* accessed by name. For us, supporting both variants gets ugly and hacky at
* times. The fun begins now; this struct is defined as per the new variant.
*
* Effective MOF definition:
*
* #pragma namespace("\\\\.\\root\\HP\\InstrumentedBIOS");
* class HPBIOS_BIOSNumericSensor {
* [read] string Name;
* [read] string Description;
* [read, ValueMap {"0","1","2","3","4","5","6","7","8","9",
* "10","11","12"}, Values {"Unknown","Other","Temperature",
* "Voltage","Current","Tachometer","Counter","Switch","Lock",
* "Humidity","Smoke Detection","Presence","Air Flow"}]
* uint32 SensorType;
* [read] string OtherSensorType;
* [read, ValueMap {"0","1","2","3","4","5","6","7","8","9",
* "10","11","12","13","14","15","16","17","18","..",
* "0x8000.."}, Values {"Unknown","Other","OK","Degraded",
* "Stressed","Predictive Failure","Error",
* "Non-Recoverable Error","Starting","Stopping","Stopped",
* "In Service","No Contact","Lost Communication","Aborted",
* "Dormant","Supporting Entity in Error","Completed",
* "Power Mode","DMTF Reserved","Vendor Reserved"}]
* uint32 OperationalStatus;
* [read] uint32 Size;
* [read] string PossibleStates[];
* [read] string CurrentState;
* [read, ValueMap {"0","1","2","3","4","5","6","7","8","9",
* "10","11","12","13","14","15","16","17","18","19","20",
* "21","22","23","24","25","26","27","28","29","30","31",
* "32","33","34","35","36","37","38","39","40","41","42",
* "43","44","45","46","47","48","49","50","51","52","53",
* "54","55","56","57","58","59","60","61","62","63","64",
* "65"}, Values {"Unknown","Other","Degrees C","Degrees F",
* "Degrees K","Volts","Amps","Watts","Joules","Coulombs",
* "VA","Nits","Lumens","Lux","Candelas","kPa","PSI",
* "Newtons","CFM","RPM","Hertz","Seconds","Minutes",
* "Hours","Days","Weeks","Mils","Inches","Feet",
* "Cubic Inches","Cubic Feet","Meters","Cubic Centimeters",
* "Cubic Meters","Liters","Fluid Ounces","Radians",
* "Steradians","Revolutions","Cycles","Gravities","Ounces",
* "Pounds","Foot-Pounds","Ounce-Inches","Gauss","Gilberts",
* "Henries","Farads","Ohms","Siemens","Moles","Becquerels",
* "PPM (parts/million)","Decibels","DbA","DbC","Grays",
* "Sieverts","Color Temperature Degrees K","Bits","Bytes",
* "Words (data)","DoubleWords","QuadWords","Percentage"}]
* uint32 BaseUnits;
* [read] sint32 UnitModifier;
* [read] uint32 CurrentReading;
* [read] uint32 RateUnits;
* };
*
* Effective MOF definition of old variant [1] (sans redundant info):
*
* class HPBIOS_BIOSNumericSensor {
* [read] string Name;
* [read] string Description;
* [read] uint32 SensorType;
* [read] string OtherSensorType;
* [read] uint32 OperationalStatus;
* [read] string CurrentState;
* [read] string PossibleStates[];
* [read] uint32 BaseUnits;
* [read] sint32 UnitModifier;
* [read] uint32 CurrentReading;
* };
*/
struct hp_wmi_numeric_sensor {
const char *name;
const char *description;
u32 sensor_type;
const char *other_sensor_type; /* Explains "Other" SensorType. */
u32 operational_status;
u8 size; /* Count of PossibleStates[]. */
const char **possible_states;
const char *current_state;
u32 base_units;
s32 unit_modifier;
u32 current_reading;
u32 rate_units;
};
/*
* struct hp_wmi_platform_events - a HPBIOS_PlatformEvents instance
*
* Instances of this object reveal the set of possible HPBIOS_BIOSEvent
* instances for the current system, but it may not always be present.
*
* Effective MOF definition:
*
* #pragma namespace("\\\\.\\root\\HP\\InstrumentedBIOS");
* class HPBIOS_PlatformEvents {
* [read] string Name;
* [read] string Description;
* [read] string SourceNamespace;
* [read] string SourceClass;
* [read, ValueMap {"0","1","2","3","4",".."}, Values {
* "Unknown","Configuration Change","Button Pressed",
* "Sensor","BIOS Settings","Reserved"}]
* uint32 Category;
* [read, ValueMap{"0","5","10","15","20","25","30",".."},
* Values{"Unknown","OK","Degraded/Warning","Minor Failure",
* "Major Failure","Critical Failure","Non-recoverable Error",
* "DMTF Reserved"}]
* uint32 PossibleSeverity;
* [read, ValueMap {"0","1","2","3","4","5","6","7","8","9",
* "10","11","12","13","14","15","16","17","18","..",
* "0x8000.."}, Values {"Unknown","Other","OK","Degraded",
* "Stressed","Predictive Failure","Error",
* "Non-Recoverable Error","Starting","Stopping","Stopped",
* "In Service","No Contact","Lost Communication","Aborted",
* "Dormant","Supporting Entity in Error","Completed",
* "Power Mode","DMTF Reserved","Vendor Reserved"}]
* uint32 PossibleStatus;
* };
*/
struct hp_wmi_platform_events {
const char *name;
const char *description;
const char *source_namespace;
const char *source_class;
u32 category;
u32 possible_severity;
u32 possible_status;
};
/*
* struct hp_wmi_event - a HPBIOS_BIOSEvent instance
*
* Effective MOF definition [1] (corrected below from original):
*
* #pragma namespace("\\\\.\\root\\WMI");
* class HPBIOS_BIOSEvent : WMIEvent {
* [read] string Name;
* [read] string Description;
* [read ValueMap {"0","1","2","3","4"}, Values {"Unknown",
* "Configuration Change","Button Pressed","Sensor",
* "BIOS Settings"}]
* uint32 Category;
* [read, ValueMap {"0","5","10","15","20","25","30"},
* Values {"Unknown","OK","Degraded/Warning",
* "Minor Failure","Major Failure","Critical Failure",
* "Non-recoverable Error"}]
* uint32 Severity;
* [read, ValueMap {"0","1","2","3","4","5","6","7","8",
* "9","10","11","12","13","14","15","16","17","18","..",
* "0x8000.."}, Values {"Unknown","Other","OK","Degraded",
* "Stressed","Predictive Failure","Error",
* "Non-Recoverable Error","Starting","Stopping","Stopped",
* "In Service","No Contact","Lost Communication","Aborted",
* "Dormant","Supporting Entity in Error","Completed",
* "Power Mode","DMTF Reserved","Vendor Reserved"}]
* uint32 Status;
* };
*/
struct hp_wmi_event {
const char *name;
const char *description;
u32 category;
};
/*
* struct hp_wmi_info - sensor info
* @nsensor: numeric sensor properties
* @instance: its WMI instance number
* @state: pointer to driver state
* @has_alarm: whether sensor has an alarm flag
* @alarm: alarm flag
* @type: its hwmon sensor type
* @cached_val: current sensor reading value, scaled for hwmon
* @last_updated: when these readings were last updated
*/
struct hp_wmi_info {
struct hp_wmi_numeric_sensor nsensor;
u8 instance;
void *state; /* void *: Avoid forward declaration. */
bool has_alarm;
bool alarm;
enum hwmon_sensor_types type;
long cached_val;
unsigned long last_updated; /* In jiffies. */
};
/*
* struct hp_wmi_sensors - driver state
* @wdev: pointer to the parent WMI device
* @info_map: sensor info structs by hwmon type and channel number
* @channel_count: count of hwmon channels by hwmon type
* @has_intrusion: whether an intrusion sensor is present
* @intrusion: intrusion flag
* @lock: mutex to lock polling WMI and changes to driver state
*/
struct hp_wmi_sensors {
struct wmi_device *wdev;
struct hp_wmi_info **info_map[hwmon_max];
u8 channel_count[hwmon_max];
bool has_intrusion;
bool intrusion;
struct mutex lock; /* Lock polling WMI and driver state changes. */
};
static bool is_raw_wmi_string(const u8 *pointer, u32 length)
{
const u16 *ptr;
u16 len;
/* WMI strings are length-prefixed UTF-16 [5]. */
if (length <= sizeof(*ptr))
return false;
length -= sizeof(*ptr);
ptr = (const u16 *)pointer;
len = *ptr;
return len <= length && !(len & 1);
}
static char *convert_raw_wmi_string(const u8 *buf)
{
const wchar_t *src;
unsigned int cps;
unsigned int len;
char *dst;
int i;
src = (const wchar_t *)buf;
/* Count UTF-16 code points. Exclude trailing null padding. */
cps = *src / sizeof(*src);
while (cps && !src[cps])
cps--;
/* Each code point becomes up to 3 UTF-8 characters. */
len = min(cps * 3, HP_WMI_MAX_STR_SIZE - 1);
dst = kmalloc((len + 1) * sizeof(*dst), GFP_KERNEL);
if (!dst)
return NULL;
i = utf16s_to_utf8s(++src, cps, UTF16_LITTLE_ENDIAN, dst, len);
dst[i] = '\0';
return dst;
}
/* hp_wmi_strdup - devm_kstrdup, but length-limited */
static char *hp_wmi_strdup(struct device *dev, const char *src)
{
char *dst;
size_t len;
len = strnlen(src, HP_WMI_MAX_STR_SIZE - 1);
dst = devm_kmalloc(dev, (len + 1) * sizeof(*dst), GFP_KERNEL);
if (!dst)
return NULL;
strscpy(dst, src, len + 1);
return dst;
}
/* hp_wmi_wstrdup - hp_wmi_strdup, but for a raw WMI string */
static char *hp_wmi_wstrdup(struct device *dev, const u8 *buf)
{
char *src;
char *dst;
src = convert_raw_wmi_string(buf);
if (!src)
return NULL;
dst = hp_wmi_strdup(dev, strim(src)); /* Note: Copy is trimmed. */
kfree(src);
return dst;
}
/*
* hp_wmi_get_wobj - poll WMI for a WMI object instance
* @guid: WMI object GUID
* @instance: WMI object instance number
*
* Returns a new WMI object instance on success, or NULL on error.
* Caller must kfree() the result.
*/
static union acpi_object *hp_wmi_get_wobj(const char *guid, u8 instance)
{
struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
acpi_status err;
err = wmi_query_block(guid, instance, &out);
if (ACPI_FAILURE(err))
return NULL;
return out.pointer;
}
/* hp_wmi_wobj_instance_count - find count of WMI object instances */
static u8 hp_wmi_wobj_instance_count(const char *guid)
{
int count;
count = wmi_instance_count(guid);
return clamp(count, 0, (int)HP_WMI_MAX_INSTANCES);
}
static int check_wobj(const union acpi_object *wobj,
const acpi_object_type property_map[], int last_prop)
{
acpi_object_type type = wobj->type;
acpi_object_type valid_type;
union acpi_object *elements;
u32 elem_count;
int prop;
if (type != ACPI_TYPE_PACKAGE)
return -EINVAL;
elem_count = wobj->package.count;
if (elem_count != last_prop + 1)
return -EINVAL;
elements = wobj->package.elements;
for (prop = 0; prop <= last_prop; prop++) {
type = elements[prop].type;
valid_type = property_map[prop];
if (type != valid_type) {
if (type == ACPI_TYPE_BUFFER &&
valid_type == ACPI_TYPE_STRING &&
is_raw_wmi_string(elements[prop].buffer.pointer,
elements[prop].buffer.length))
continue;
return -EINVAL;
}
}
return 0;
}
static int extract_acpi_value(struct device *dev,
union acpi_object *element,
acpi_object_type type,
u32 *out_value, char **out_string)
{
switch (type) {
case ACPI_TYPE_INTEGER:
*out_value = element->integer.value;
break;
case ACPI_TYPE_STRING:
*out_string = element->type == ACPI_TYPE_BUFFER ?
hp_wmi_wstrdup(dev, element->buffer.pointer) :
hp_wmi_strdup(dev, strim(element->string.pointer));
if (!*out_string)
return -ENOMEM;
break;
default:
return -EINVAL;
}
return 0;
}
/*
* check_numeric_sensor_wobj - validate a HPBIOS_BIOSNumericSensor instance
* @wobj: pointer to WMI object instance to check
* @out_size: out pointer to count of possible states
* @out_is_new: out pointer to whether this is a "new" variant object
*
* Returns 0 on success, or a negative error code on error.
*/
static int check_numeric_sensor_wobj(const union acpi_object *wobj,
u8 *out_size, bool *out_is_new)
{
acpi_object_type type = wobj->type;
int prop = HP_WMI_PROPERTY_NAME;
acpi_object_type valid_type;
union acpi_object *elements;
u32 elem_count;
int last_prop;
bool is_new;
u8 count;
u32 j;
u32 i;
if (type != ACPI_TYPE_PACKAGE)
return -EINVAL;
/*
* elements is a variable-length array of ACPI objects, one for
* each property of the WMI object instance, except that the
* strings in PossibleStates[] are flattened into this array
* as if each individual string were a property by itself.
*/
elements = wobj->package.elements;
elem_count = wobj->package.count;
if (elem_count <= HP_WMI_PROPERTY_SIZE ||
elem_count > HP_WMI_MAX_PROPERTIES)
return -EINVAL;
type = elements[HP_WMI_PROPERTY_SIZE].type;
switch (type) {
case ACPI_TYPE_INTEGER:
is_new = true;
last_prop = HP_WMI_PROPERTY_RATE_UNITS;
break;
case ACPI_TYPE_STRING:
is_new = false;
last_prop = HP_WMI_PROPERTY_CURRENT_READING;
break;
default:
return -EINVAL;
}
/*
* In general, the count of PossibleStates[] must be > 0.
* Also, the old variant lacks the Size property, so we may need to
* reduce the value of last_prop by 1 when doing arithmetic with it.
*/
if (elem_count < last_prop - !is_new + 1)
return -EINVAL;
count = elem_count - (last_prop - !is_new);
for (i = 0; i < elem_count && prop <= last_prop; i++, prop++) {
type = elements[i].type;
valid_type = hp_wmi_property_map[prop];
if (type != valid_type)
return -EINVAL;
switch (prop) {
case HP_WMI_PROPERTY_OPERATIONAL_STATUS:
/* Old variant: CurrentState follows OperationalStatus. */
if (!is_new)
prop = HP_WMI_PROPERTY_CURRENT_STATE - 1;
break;
case HP_WMI_PROPERTY_SIZE:
/* New variant: Size == count of PossibleStates[]. */
if (count != elements[i].integer.value)
return -EINVAL;
break;
case HP_WMI_PROPERTY_POSSIBLE_STATES:
/* PossibleStates[0] has already been type-checked. */
for (j = 0; i + 1 < elem_count && j + 1 < count; j++) {
type = elements[++i].type;
if (type != valid_type)
return -EINVAL;
}
/* Old variant: BaseUnits follows PossibleStates[]. */
if (!is_new)
prop = HP_WMI_PROPERTY_BASE_UNITS - 1;
break;
case HP_WMI_PROPERTY_CURRENT_STATE:
/* Old variant: PossibleStates[] follows CurrentState. */
if (!is_new)
prop = HP_WMI_PROPERTY_POSSIBLE_STATES - 1;
break;
}
}
if (prop != last_prop + 1)
return -EINVAL;
*out_size = count;
*out_is_new = is_new;
return 0;
}
static int
numeric_sensor_is_connected(const struct hp_wmi_numeric_sensor *nsensor)
{
u32 operational_status = nsensor->operational_status;
return operational_status != HP_WMI_STATUS_NO_CONTACT;
}
static int numeric_sensor_has_fault(const struct hp_wmi_numeric_sensor *nsensor)
{
u32 operational_status = nsensor->operational_status;
switch (operational_status) {
case HP_WMI_STATUS_DEGRADED:
case HP_WMI_STATUS_STRESSED: /* e.g. Overload, overtemp. */
case HP_WMI_STATUS_PREDICTIVE_FAILURE: /* e.g. Fan removed. */
case HP_WMI_STATUS_ERROR:
case HP_WMI_STATUS_NON_RECOVERABLE_ERROR:
case HP_WMI_STATUS_NO_CONTACT:
case HP_WMI_STATUS_LOST_COMMUNICATION:
case HP_WMI_STATUS_ABORTED:
case HP_WMI_STATUS_SUPPORTING_ENTITY_IN_ERROR:
/* Assume combination by addition; bitwise OR doesn't make sense. */
case HP_WMI_STATUS_COMPLETED + HP_WMI_STATUS_DEGRADED:
case HP_WMI_STATUS_COMPLETED + HP_WMI_STATUS_ERROR:
return true;
}
return false;
}
/* scale_numeric_sensor - scale sensor reading for hwmon */
static long scale_numeric_sensor(const struct hp_wmi_numeric_sensor *nsensor)
{
u32 current_reading = nsensor->current_reading;
s32 unit_modifier = nsensor->unit_modifier;
u32 sensor_type = nsensor->sensor_type;
u32 base_units = nsensor->base_units;
s32 target_modifier;
long val;
/* Fan readings are in RPM units; others are in milliunits. */
target_modifier = sensor_type == HP_WMI_TYPE_AIR_FLOW ? 0 : -3;
val = current_reading;
for (; unit_modifier < target_modifier; unit_modifier++)
val = DIV_ROUND_CLOSEST(val, 10);
for (; unit_modifier > target_modifier; unit_modifier--) {
if (val > LONG_MAX / 10) {
val = LONG_MAX;
break;
}
val *= 10;
}
if (sensor_type == HP_WMI_TYPE_TEMPERATURE) {
switch (base_units) {
case HP_WMI_UNITS_DEGREES_F:
val -= MILLI * 32;
val = val <= LONG_MAX / 5 ?
DIV_ROUND_CLOSEST(val * 5, 9) :
DIV_ROUND_CLOSEST(val, 9) * 5;
break;
case HP_WMI_UNITS_DEGREES_K:
val = milli_kelvin_to_millicelsius(val);
break;
}
}
return val;
}
/*
* classify_numeric_sensor - classify a numeric sensor
* @nsensor: pointer to numeric sensor struct
*
* Returns an enum hp_wmi_type value on success,
* or a negative value if the sensor type is unsupported.
*/
static int classify_numeric_sensor(const struct hp_wmi_numeric_sensor *nsensor)
{
u32 sensor_type = nsensor->sensor_type;
u32 base_units = nsensor->base_units;
const char *name = nsensor->name;
switch (sensor_type) {
case HP_WMI_TYPE_TEMPERATURE:
/*
* Some systems have sensors named "X Thermal Index" in "Other"
* units. Tested CPU sensor examples were found to be in °C,
* albeit perhaps "differently" accurate; e.g. readings were
* reliably -6°C vs. coretemp on a HP Compaq Elite 8300, and
* +8°C on an EliteOne G1 800. But this is still within the
* realm of plausibility for cheaply implemented motherboard
* sensors, and chassis readings were about as expected.
*/
if ((base_units == HP_WMI_UNITS_OTHER &&
strstr(name, HP_WMI_PATTERN_TEMP_SENSOR)) ||
base_units == HP_WMI_UNITS_DEGREES_C ||
base_units == HP_WMI_UNITS_DEGREES_F ||
base_units == HP_WMI_UNITS_DEGREES_K)
return HP_WMI_TYPE_TEMPERATURE;
break;
case HP_WMI_TYPE_VOLTAGE:
if (base_units == HP_WMI_UNITS_VOLTS)
return HP_WMI_TYPE_VOLTAGE;
break;
case HP_WMI_TYPE_CURRENT:
if (base_units == HP_WMI_UNITS_AMPS)
return HP_WMI_TYPE_CURRENT;
break;
case HP_WMI_TYPE_AIR_FLOW:
/*
* Strangely, HP considers fan RPM sensor type to be
* "Air Flow" instead of the more intuitive "Tachometer".
*/
if (base_units == HP_WMI_UNITS_RPM)
return HP_WMI_TYPE_AIR_FLOW;
break;
}
return -EINVAL;
}
static int
populate_numeric_sensor_from_wobj(struct device *dev,
struct hp_wmi_numeric_sensor *nsensor,
union acpi_object *wobj, bool *out_is_new)
{
int last_prop = HP_WMI_PROPERTY_RATE_UNITS;
int prop = HP_WMI_PROPERTY_NAME;
const char **possible_states;
union acpi_object *element;
acpi_object_type type;
char *string;
bool is_new;
u32 value;
u8 size;
int err;
err = check_numeric_sensor_wobj(wobj, &size, &is_new);
if (err)
return err;
possible_states = devm_kcalloc(dev, size, sizeof(*possible_states),
GFP_KERNEL);
if (!possible_states)
return -ENOMEM;
element = wobj->package.elements;
nsensor->possible_states = possible_states;
nsensor->size = size;
if (!is_new)
last_prop = HP_WMI_PROPERTY_CURRENT_READING;
for (; prop <= last_prop; prop++) {
type = hp_wmi_property_map[prop];
err = extract_acpi_value(dev, element, type, &value, &string);
if (err)
return err;
element++;
switch (prop) {
case HP_WMI_PROPERTY_NAME:
nsensor->name = string;
break;
case HP_WMI_PROPERTY_DESCRIPTION:
nsensor->description = string;
break;
case HP_WMI_PROPERTY_SENSOR_TYPE:
if (value > HP_WMI_TYPE_AIR_FLOW)
return -EINVAL;
nsensor->sensor_type = value;
break;
case HP_WMI_PROPERTY_OTHER_SENSOR_TYPE:
nsensor->other_sensor_type = string;
break;
case HP_WMI_PROPERTY_OPERATIONAL_STATUS:
nsensor->operational_status = value;
/* Old variant: CurrentState follows OperationalStatus. */
if (!is_new)
prop = HP_WMI_PROPERTY_CURRENT_STATE - 1;
break;
case HP_WMI_PROPERTY_SIZE:
break; /* Already set. */
case HP_WMI_PROPERTY_POSSIBLE_STATES:
*possible_states++ = string;
if (--size)
prop--;
/* Old variant: BaseUnits follows PossibleStates[]. */
if (!is_new && !size)
prop = HP_WMI_PROPERTY_BASE_UNITS - 1;
break;
case HP_WMI_PROPERTY_CURRENT_STATE:
nsensor->current_state = string;
/* Old variant: PossibleStates[] follows CurrentState. */
if (!is_new)
prop = HP_WMI_PROPERTY_POSSIBLE_STATES - 1;
break;
case HP_WMI_PROPERTY_BASE_UNITS:
nsensor->base_units = value;
break;
case HP_WMI_PROPERTY_UNIT_MODIFIER:
/* UnitModifier is signed. */
nsensor->unit_modifier = (s32)value;
break;
case HP_WMI_PROPERTY_CURRENT_READING:
nsensor->current_reading = value;
break;
case HP_WMI_PROPERTY_RATE_UNITS:
nsensor->rate_units = value;
break;
default:
return -EINVAL;
}
}
*out_is_new = is_new;
return 0;
}
/* update_numeric_sensor_from_wobj - update fungible sensor properties */
static void
update_numeric_sensor_from_wobj(struct device *dev,
struct hp_wmi_numeric_sensor *nsensor,
const union acpi_object *wobj)
{
const union acpi_object *elements;
const union acpi_object *element;
const char *new_string;
char *trimmed;
char *string;
bool is_new;
int offset;
u8 size;
int err;
err = check_numeric_sensor_wobj(wobj, &size, &is_new);
if (err)
return;
elements = wobj->package.elements;
element = &elements[HP_WMI_PROPERTY_OPERATIONAL_STATUS];
nsensor->operational_status = element->integer.value;
/*
* In general, an index offset is needed after PossibleStates[0].
* On a new variant, CurrentState is after PossibleStates[]. This is
* not the case on an old variant, but we still need to offset the
* read because CurrentState is where Size would be on a new variant.
*/
offset = is_new ? size - 1 : -2;
element = &elements[HP_WMI_PROPERTY_CURRENT_STATE + offset];
string = element->type == ACPI_TYPE_BUFFER ?
convert_raw_wmi_string(element->buffer.pointer) :
element->string.pointer;
if (string) {
trimmed = strim(string);
if (strcmp(trimmed, nsensor->current_state)) {
new_string = hp_wmi_strdup(dev, trimmed);
if (new_string) {
devm_kfree(dev, nsensor->current_state);
nsensor->current_state = new_string;
}
}
if (element->type == ACPI_TYPE_BUFFER)
kfree(string);
}
/* Old variant: -2 (not -1) because it lacks the Size property. */
if (!is_new)
offset = (int)size - 2; /* size is > 0, i.e. may be 1. */
element = &elements[HP_WMI_PROPERTY_UNIT_MODIFIER + offset];
nsensor->unit_modifier = (s32)element->integer.value;
element = &elements[HP_WMI_PROPERTY_CURRENT_READING + offset];
nsensor->current_reading = element->integer.value;
}
/*
* check_platform_events_wobj - validate a HPBIOS_PlatformEvents instance
* @wobj: pointer to WMI object instance to check
*
* Returns 0 on success, or a negative error code on error.
*/
static int check_platform_events_wobj(const union acpi_object *wobj)
{
return check_wobj(wobj, hp_wmi_platform_events_property_map,
HP_WMI_PLATFORM_EVENTS_PROPERTY_POSSIBLE_STATUS);
}