-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
huawei-wmi.c
1669 lines (1347 loc) · 38 KB
/
huawei-wmi.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
/*
* Huawei WMI laptop extras driver
*
* Copyright (C) 2018 Ayman Bagabas <[email protected]>
*/
#include <linux/acpi.h>
#include <linux/debugfs.h>
#include <linux/delay.h>
#include <linux/dmi.h>
#include <linux/input.h>
#include <linux/input/sparse-keymap.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/sysfs.h>
#include <linux/wmi.h>
#include <linux/hwmon.h>
#include <linux/version.h>
#include <acpi/battery.h>
#include <linux/version.h>
#define HWMI_BUFF_SIZE 0x100
/*
* Huawei WMI GUIDs
*/
#define HWMI_METHOD_GUID "ABBC0F5B-8EA1-11D1-A000-C90629100000"
#define HWMI_EVENT_GUID "ABBC0F5C-8EA1-11D1-A000-C90629100000"
/* Legacy GUIDs */
#define WMI0_EXPENSIVE_GUID "39142400-C6A3-40fa-BADB-8A2652834100"
#define WMI0_EVENT_GUID "59142400-C6A3-40fa-BADB-8A2652834100"
/* HWMI commands */
enum {
BATTERY_THRESH_GET = 0x00001103, /* \GBTT */
BATTERY_THRESH_SET = 0x00001003, /* \SBTT */
FN_LOCK_GET = 0x00000604, /* \GFRS */
FN_LOCK_SET = 0x00000704, /* \SFRS */
KBDLIGHT_GET = 0x00000602, /* \GLIV */
KBDLIGHT_SET = 0x00000702, /* \SLIV */
MICMUTE_LED_SET = 0x00000b04, /* \SMLS */
KBDLIGHT_TIMEOUT_SET = 0x00001106, /* \SKBT */
KBDLIGHT_TIMEOUT_GET = 0x00001206, /* \GKBT */
POWER_UNLOCK_SET = 0x00000F04, /* \STUB */
POWER_UNLOCK_GET = 0x00000E04, /* \STUB */
FAN_SPEED_GET = 0x00000802, /* \GFNS */
TEMP_GET = 0x00000202, /* \GTMP */
BATTERY_CHARGE_MODE_GET = 0x00001603, /* \GBCM */
BATTERY_CHARGE_MODE_SET = 0x00001503, /* \SBCM */
BATTERY_CHARGE_MODE_PARAM_GET = 0x00001303, /* \GBAC */
BATTERY_CHARGE_MODE_PARAM_SET = 0x00001203, /* \SBAC */
};
union hwmi_arg {
u64 cmd;
u8 args[8];
};
struct quirk_entry {
bool battery_reset;
bool ec_micmute;
bool report_brightness;
bool handle_kbdlight;
};
static struct quirk_entry *quirks;
struct huawei_wmi_debug {
struct dentry *root;
u64 arg;
};
struct huawei_wmi {
bool battery_available;
bool fn_lock_available;
bool kbdlight_available;
bool kbdlight_quirk_input;
bool kbdlight_timeout_available;
bool power_unlock_available;
bool fan_speed_available;
bool temp_available;
bool smart_charge_available;
bool smart_charge_param_available;
struct huawei_wmi_debug debug;
struct input_dev *idev[2];
struct led_classdev cdev;
struct led_classdev fn_cdev;
struct led_classdev KCMS_cdev;
struct device *dev;
struct device *hwmon;
struct mutex wmi_lock;
};
static struct huawei_wmi *huawei_wmi;
enum {
KBDLIGHT_KEY_0 = 0x293,
KBDLIGHT_KEY_1 = 0x294,
KBDLIGHT_KEY_2 = 0x295
};
static const struct key_entry huawei_wmi_keymap[] = {
{ KE_KEY, 0x281, { KEY_BRIGHTNESSDOWN } },
{ KE_KEY, 0x282, { KEY_BRIGHTNESSUP } },
{ KE_KEY, 0x284, { KEY_MUTE } },
{ KE_KEY, 0x285, { KEY_VOLUMEDOWN } },
{ KE_KEY, 0x286, { KEY_VOLUMEUP } },
{ KE_KEY, 0x287, { KEY_MICMUTE } },
{ KE_KEY, 0x289, { KEY_WLAN } },
// Huawei |M| key
{ KE_KEY, 0x28a, { KEY_CONFIG } },
// Power unlock (Fn+P)
{ KE_KEY, 0x2a0, { KEY_PROG1 } },
{ KE_KEY, 0x2a1, { KEY_PROG1 } },
{ KE_KEY, 0x2a6, { KEY_PROG1 } },
// Keyboard backlight
{ KE_IGNORE, KBDLIGHT_KEY_0, { KEY_KBDILLUMTOGGLE } },
{ KE_IGNORE, KBDLIGHT_KEY_1, { KEY_KBDILLUMDOWN } },
{ KE_IGNORE, KBDLIGHT_KEY_2, { KEY_KBDILLUMUP } },
// Ignore Ambient Light Sensoring
{ KE_KEY, 0x2c1, { KEY_RESERVED } },
{ KE_END, 0 }
};
static int battery_reset = -1;
static int report_brightness = -1;
static int handle_kbdlight = -1;
module_param(battery_reset, bint, 0444);
MODULE_PARM_DESC(battery_reset,
"Reset battery charge values to (0-0) before disabling it using (0-100)");
module_param(report_brightness, bint, 0444);
MODULE_PARM_DESC(report_brightness,
"Report brightness keys.");
module_param(handle_kbdlight, bint, 0444);
MODULE_PARM_DESC(handle_kbdlight,
"Handle keyboard backlight events.");
/* Quirks */
static int __init dmi_matched(const struct dmi_system_id *dmi)
{
quirks = dmi->driver_data;
return 1;
}
static struct quirk_entry quirk_unknown = {
.handle_kbdlight = true,
};
static struct quirk_entry quirk_skip_kbdlight = {
.handle_kbdlight = false,
};
static struct quirk_entry quirk_mach_wx9 = {
.battery_reset = true,
.handle_kbdlight = false,
};
static struct quirk_entry quirk_matebook_x = {
.ec_micmute = true,
.report_brightness = true,
.handle_kbdlight = false,
};
static const struct dmi_system_id huawei_quirks[] = {
{
.callback = dmi_matched,
.ident = "Huawei MACH-WX9",
.matches = {
DMI_MATCH(DMI_SYS_VENDOR, "HUAWEI"),
DMI_MATCH(DMI_PRODUCT_NAME, "MACH-WX9"),
},
.driver_data = &quirk_mach_wx9
},
{
.callback = dmi_matched,
.ident = "Huawei MateBook X",
.matches = {
DMI_MATCH(DMI_SYS_VENDOR, "HUAWEI"),
DMI_MATCH(DMI_PRODUCT_NAME, "HUAWEI MateBook X")
},
.driver_data = &quirk_matebook_x
},
{
.callback = dmi_matched,
.ident = "Huawei KPL-W0X",
.matches = {
DMI_MATCH(DMI_SYS_VENDOR, "HUAWEI"),
DMI_MATCH(DMI_PRODUCT_NAME, "KPL-W0X")
},
.driver_data = &quirk_skip_kbdlight
},
{
.callback = dmi_matched,
.ident = "Huawei MACHC-WAX9",
.matches = {
DMI_MATCH(DMI_SYS_VENDOR, "HUAWEI"),
DMI_MATCH(DMI_PRODUCT_NAME, "MACHC-WAX9")
},
.driver_data = &quirk_unknown
},
{
.callback = dmi_matched,
.ident = "Huawei NBLK-WAX9X",
.matches = {
DMI_MATCH(DMI_SYS_VENDOR, "HUAWEI"),
DMI_MATCH(DMI_PRODUCT_NAME, "NBLK-WAX9X")
},
.driver_data = &quirk_unknown
},
{
.callback = dmi_matched,
.ident = "Huawei HLYL-WXX9",
.matches = {
DMI_MATCH(DMI_SYS_VENDOR, "HUAWEI"),
DMI_MATCH(DMI_PRODUCT_NAME, "HLYL-WXX9")
},
.driver_data = &quirk_skip_kbdlight
},
{ }
};
/* Utils */
static int huawei_wmi_call(struct huawei_wmi *huawei,
struct acpi_buffer *in, struct acpi_buffer *out)
{
acpi_status status;
mutex_lock(&huawei->wmi_lock);
status = wmi_evaluate_method(HWMI_METHOD_GUID, 0, 1, in, out);
mutex_unlock(&huawei->wmi_lock);
if (ACPI_FAILURE(status)) {
dev_err(huawei->dev, "Failed to evaluate wmi method\n");
return -ENODEV;
}
return 0;
}
/* HWMI takes a 64 bit input and returns either a package with 2 buffers, one of
* 4 bytes and the other of 256 bytes, or one buffer of size 0x104 (260) bytes.
* The first 4 bytes are ignored, we ignore the first 4 bytes buffer if we got a
* package, or skip the first 4 if a buffer of 0x104 is used. The first byte of
* the remaining 0x100 sized buffer has the return status of every call. In case
* the return status is non-zero, we return -ENODEV but still copy the returned
* buffer to the given buffer parameter (buf).
*/
static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen)
{
struct huawei_wmi *huawei = huawei_wmi;
struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
struct acpi_buffer in;
union acpi_object *obj;
size_t len;
int err, i;
in.length = sizeof(arg);
in.pointer = &arg;
/* Some models require calling HWMI twice to execute a command. We evaluate
* HWMI and if we get a non-zero return status we evaluate it again.
*/
for (i = 0; i < 2; i++) {
err = huawei_wmi_call(huawei, &in, &out);
if (err)
goto fail_cmd;
obj = out.pointer;
if (!obj) {
err = -EIO;
goto fail_cmd;
}
switch (obj->type) {
/* Models that implement both "legacy" and HWMI tend to return a 0x104
* sized buffer instead of a package of 0x4 and 0x100 buffers.
*/
case ACPI_TYPE_BUFFER:
if (obj->buffer.length == 0x104) {
// Skip the first 4 bytes.
obj->buffer.pointer += 4;
len = HWMI_BUFF_SIZE;
} else {
dev_err(huawei->dev, "Bad buffer length, got %d\n", obj->buffer.length);
err = -EIO;
goto fail_cmd;
}
break;
/* HWMI returns a package with 2 buffer elements, one of 4 bytes and the
* other is 256 bytes.
*/
case ACPI_TYPE_PACKAGE:
if (obj->package.count != 2) {
dev_err(huawei->dev, "Bad package count, got %d\n", obj->package.count);
err = -EIO;
goto fail_cmd;
}
obj = &obj->package.elements[1];
if (obj->type != ACPI_TYPE_BUFFER) {
dev_err(huawei->dev, "Bad package element type, got %d\n", obj->type);
err = -EIO;
goto fail_cmd;
}
len = obj->buffer.length;
break;
/* Shouldn't get here! */
default:
dev_err(huawei->dev, "Unexpected obj type, got: %d\n", obj->type);
err = -EIO;
goto fail_cmd;
}
if (!*obj->buffer.pointer)
break;
}
err = (*obj->buffer.pointer) ? -ENODEV : 0;
if (buf) {
len = min(buflen, len);
memcpy(buf, obj->buffer.pointer, len);
}
fail_cmd:
kfree(out.pointer);
return err;
}
/* LEDs */
static int huawei_wmi_micmute_led_set(struct led_classdev *led_cdev,
enum led_brightness brightness)
{
/* This is a workaround until the "legacy" interface is implemented. */
if (quirks && quirks->ec_micmute) {
char *acpi_method;
acpi_handle handle;
acpi_status status;
union acpi_object args[3];
struct acpi_object_list arg_list = {
.pointer = args,
.count = ARRAY_SIZE(args),
};
handle = ec_get_handle();
if (!handle)
return -ENODEV;
args[0].type = args[1].type = args[2].type = ACPI_TYPE_INTEGER;
args[1].integer.value = 0x04;
if (acpi_has_method(handle, "SPIN")) {
acpi_method = "SPIN";
args[0].integer.value = 0;
args[2].integer.value = brightness ? 1 : 0;
} else if (acpi_has_method(handle, "WPIN")) {
acpi_method = "WPIN";
args[0].integer.value = 1;
args[2].integer.value = brightness ? 0 : 1;
} else {
return -ENODEV;
}
status = acpi_evaluate_object(handle, acpi_method, &arg_list, NULL);
if (ACPI_FAILURE(status))
return -ENODEV;
return 0;
} else {
union hwmi_arg arg;
arg.cmd = MICMUTE_LED_SET;
arg.args[2] = brightness;
return huawei_wmi_cmd(arg.cmd, NULL, 0);
}
}
static void huawei_wmi_leds_setup(struct device *dev)
{
struct huawei_wmi *huawei = dev_get_drvdata(dev);
huawei->cdev.name = "platform::micmute";
huawei->cdev.max_brightness = 1;
huawei->cdev.brightness_set_blocking = &huawei_wmi_micmute_led_set;
huawei->cdev.default_trigger = "audio-micmute";
huawei->cdev.dev = dev;
huawei->cdev.flags = LED_CORE_SUSPENDRESUME;
if (acpi_has_method(NULL, "\\SMLS") || (quirks && quirks->ec_micmute))
devm_led_classdev_register(dev, &huawei->cdev);
}
/* Battery protection */
static int huawei_wmi_battery_get(int *start, int *end)
{
u8 ret[HWMI_BUFF_SIZE];
int err, i;
err = huawei_wmi_cmd(BATTERY_THRESH_GET, ret, HWMI_BUFF_SIZE);
if (err)
return err;
/* Find the last two non-zero values. Return status is ignored. */
i = 0xff;
do {
if (start)
*start = ret[i-1];
if (end)
*end = ret[i];
} while (i > 2 && !ret[i--]);
return 0;
}
static int huawei_wmi_battery_set(int start, int end)
{
union hwmi_arg arg;
int err;
if (start < 0 || end < 0 || start > 100 || end > 100)
return -EINVAL;
arg.cmd = BATTERY_THRESH_SET;
arg.args[2] = start;
arg.args[3] = end;
/* This is an edge case were some models turn battery protection
* off without changing their thresholds values. We clear the
* values before turning off protection. Sometimes we need a sleep delay to
* make sure these values make their way to EC memory.
*/
if (quirks && quirks->battery_reset && start == 0 && end == 100) {
err = huawei_wmi_battery_set(0, 0);
if (err)
return err;
msleep(1000);
}
err = huawei_wmi_cmd(arg.cmd, NULL, 0);
return err;
}
static ssize_t charge_control_start_threshold_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
int err, start;
err = huawei_wmi_battery_get(&start, NULL);
if (err)
return err;
return sprintf(buf, "%d\n", start);
}
static ssize_t charge_control_end_threshold_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
int err, end;
err = huawei_wmi_battery_get(NULL, &end);
if (err)
return err;
return sprintf(buf, "%d\n", end);
}
static ssize_t charge_control_thresholds_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
int err, start, end;
err = huawei_wmi_battery_get(&start, &end);
if (err)
return err;
return sprintf(buf, "%d %d\n", start, end);
}
static ssize_t charge_control_start_threshold_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
int err, start, end;
err = huawei_wmi_battery_get(NULL, &end);
if (err)
return err;
if (sscanf(buf, "%d", &start) != 1)
return -EINVAL;
err = huawei_wmi_battery_set(start, end);
if (err)
return err;
return size;
}
static ssize_t charge_control_end_threshold_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
int err, start, end;
err = huawei_wmi_battery_get(&start, NULL);
if (err)
return err;
if (sscanf(buf, "%d", &end) != 1)
return -EINVAL;
err = huawei_wmi_battery_set(start, end);
if (err)
return err;
return size;
}
static ssize_t charge_control_thresholds_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
int err, start, end;
if (sscanf(buf, "%d %d", &start, &end) != 2)
return -EINVAL;
err = huawei_wmi_battery_set(start, end);
if (err)
return err;
return size;
}
static DEVICE_ATTR_RW(charge_control_start_threshold);
static DEVICE_ATTR_RW(charge_control_end_threshold);
static DEVICE_ATTR_RW(charge_control_thresholds);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 2, 0)
static int huawei_wmi_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook)
#else
static int huawei_wmi_battery_add(struct power_supply *battery)
#endif
{
device_create_file(&battery->dev, &dev_attr_charge_control_start_threshold);
device_create_file(&battery->dev, &dev_attr_charge_control_end_threshold);
return 0;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 2, 0)
static int huawei_wmi_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook)
#else
static int huawei_wmi_battery_remove(struct power_supply *battery)
#endif
{
device_remove_file(&battery->dev, &dev_attr_charge_control_start_threshold);
device_remove_file(&battery->dev, &dev_attr_charge_control_end_threshold);
return 0;
}
static struct acpi_battery_hook huawei_wmi_battery_hook = {
.add_battery = huawei_wmi_battery_add,
.remove_battery = huawei_wmi_battery_remove,
.name = "Huawei Battery Extension"
};
static void huawei_wmi_battery_setup(struct device *dev)
{
struct huawei_wmi *huawei = dev_get_drvdata(dev);
huawei->battery_available = true;
if (huawei_wmi_battery_get(NULL, NULL)) {
huawei->battery_available = false;
return;
}
battery_hook_register(&huawei_wmi_battery_hook);
device_create_file(dev, &dev_attr_charge_control_thresholds);
}
static void huawei_wmi_battery_exit(struct device *dev)
{
struct huawei_wmi *huawei = dev_get_drvdata(dev);
if (huawei->battery_available) {
battery_hook_unregister(&huawei_wmi_battery_hook);
device_remove_file(dev, &dev_attr_charge_control_thresholds);
}
}
/* Smart charge param*/
static int huawei_wmi_smart_charge_param_get(int *value)
{
u8 ret[HWMI_BUFF_SIZE];
int err;
err = huawei_wmi_cmd(BATTERY_CHARGE_MODE_PARAM_GET, ret, HWMI_BUFF_SIZE);
if (err)
return err;
if (value)
*value = ret[1];
return 0;
}
static int huawei_wmi_smart_charge_param_set(int value)
{
union hwmi_arg arg;
int err;
if (value < 0 || value > 2)
return -EINVAL;
arg.cmd = BATTERY_CHARGE_MODE_PARAM_SET;
arg.args[2] = (u8) value;
err = huawei_wmi_cmd(arg.cmd, NULL, 0);
return err;
}
static ssize_t smart_charge_param_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
int err, value;
err = huawei_wmi_smart_charge_param_get(&value);
if (err)
return err;
return sprintf(buf, "%d\n", value);
}
static ssize_t smart_charge_param_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
int err, value;
if (sscanf(buf, "%d", &value) != 1)
return -EINVAL;
err = huawei_wmi_smart_charge_param_set(value);
if (err)
return err;
return size;
}
static DEVICE_ATTR_RW(smart_charge_param);
static void huawei_wmi_smart_charge_param_setup(struct device *dev)
{
struct huawei_wmi *huawei = dev_get_drvdata(dev);
huawei->smart_charge_param_available = true;
if (huawei_wmi_smart_charge_param_get(NULL)) {
huawei->smart_charge_param_available = false;
return;
}
device_create_file(dev, &dev_attr_smart_charge_param);
}
static void huawei_wmi_smart_charge_param_exit(struct device *dev)
{
struct huawei_wmi *huawei = dev_get_drvdata(dev);
if (huawei->smart_charge_param_available) {
device_remove_file(dev, &dev_attr_smart_charge_param);
}
}
/* Smart charge */
static int huawei_wmi_smart_charge_get(int *mode, int *unknow, int *start, int *end)
{
u8 ret[HWMI_BUFF_SIZE];
int err;
err = huawei_wmi_cmd(BATTERY_CHARGE_MODE_GET, ret, HWMI_BUFF_SIZE);
if (err)
return err;
if (mode)
*mode = ret[1];
if (unknow)
*unknow = ret[2];
if (start)
*start = ret[3];
if (end)
*end = ret[4];
return 0;
}
static int huawei_wmi_smart_charge_set(int mode, int unknow, int start, int end)
{
union hwmi_arg arg;
int err;
if (start < 0 || end < 0 || start > 100 || end > 100)
return -EINVAL;
arg.cmd = BATTERY_CHARGE_MODE_SET;
arg.args[2] = (u8) mode;
arg.args[3] = (u8) unknow;
arg.args[4] = (u8) start;
arg.args[5] = (u8) end;
err = huawei_wmi_cmd(arg.cmd, NULL, 0);
return err;
}
static ssize_t smart_charge_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
int err, start, end, mode, unknow;
err = huawei_wmi_smart_charge_get(&mode, &unknow, &start, &end);
if (err)
return err;
return sprintf(buf, "%d %d %d %d\n", mode, unknow, start, end);
}
static ssize_t smart_charge_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
int err, start, end, mode, unknow;
if (sscanf(buf, "%d %d %d %d", &mode, &unknow, &start, &end) != 4)
return -EINVAL;
err = huawei_wmi_smart_charge_set(mode, unknow, start, end);
if (err)
return err;
return size;
}
static DEVICE_ATTR_RW(smart_charge);
static void huawei_wmi_smart_charge_setup(struct device *dev)
{
struct huawei_wmi *huawei = dev_get_drvdata(dev);
huawei->smart_charge_available = true;
if (huawei_wmi_smart_charge_get(NULL, NULL, NULL, NULL)) {
huawei->smart_charge_available = false;
return;
}
device_create_file(dev, &dev_attr_smart_charge);
}
static void huawei_wmi_smart_charge_exit(struct device *dev)
{
struct huawei_wmi *huawei = dev_get_drvdata(dev);
if (huawei->smart_charge_available) {
device_remove_file(dev, &dev_attr_smart_charge);
}
}
/* Fn lock */
static int huawei_wmi_fn_lock_get(int *on)
{
u8 ret[HWMI_BUFF_SIZE] = { 0 };
int err, i;
err = huawei_wmi_cmd(FN_LOCK_GET, ret, HWMI_BUFF_SIZE);
if (err)
return err;
/* Find the first non-zero value. Return status is ignored. */
i = 1;
do {
if (on)
*on = ret[i] - 1; // -1 undefined, 0 off, 1 on.
} while (i < 0xff && !ret[i++]);
return 0;
}
static int huawei_wmi_fn_lock_set(int on)
{
union hwmi_arg arg;
arg.cmd = FN_LOCK_SET;
arg.args[2] = on + 1; // 0 undefined, 1 off, 2 on.
return huawei_wmi_cmd(arg.cmd, NULL, 0);
}
static ssize_t fn_lock_state_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
int err, on;
err = huawei_wmi_fn_lock_get(&on);
if (err)
return err;
return sprintf(buf, "%d\n", on);
}
static ssize_t fn_lock_state_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
int on, err;
if (kstrtoint(buf, 10, &on) ||
on < 0 || on > 1)
return -EINVAL;
err = huawei_wmi_fn_lock_set(on);
if (err)
return err;
return size;
}
static DEVICE_ATTR_RW(fn_lock_state);
static void huawei_wmi_fn_lock_setup(struct device *dev)
{
struct huawei_wmi *huawei = dev_get_drvdata(dev);
huawei->fn_lock_available = true;
if (huawei_wmi_fn_lock_get(NULL)) {
huawei->fn_lock_available = false;
return;
}
device_create_file(dev, &dev_attr_fn_lock_state);
}
static void huawei_wmi_fn_lock_exit(struct device *dev)
{
struct huawei_wmi *huawei = dev_get_drvdata(dev);
if (huawei->fn_lock_available)
device_remove_file(dev, &dev_attr_fn_lock_state);
}
/* Keyboard backlight */
static int huawei_wmi_kbdlight_get(int *level)
{
u8 ret[HWMI_BUFF_SIZE] = { 0 };
int err;
err = huawei_wmi_cmd(KBDLIGHT_GET, ret, HWMI_BUFF_SIZE);
if (err)
return err;
if (!ret[2])
return -ENODEV;
/* Some models like the MACH-WX9 use 0x01, 0x02, and 0x04 for off, level 1,
* and level 2 respectively rather than 0x04, 0x08, and 0x10.
*/
huawei_wmi->kbdlight_quirk_input = ret[1] == 0xff;
if (level) {
*level = 0;
while (ret[2] >>= 1) {
*level += 1;
}
if (ret[1] != 0xff)
*level -= 2;
}
return 0;
}
static int huawei_wmi_kbdlight_set(int level)
{
union hwmi_arg arg;
// Huawei laptops only support 3 kbdlight levels
if (level < 0 || level > 2)
return -EINVAL;
if (!huawei_wmi->kbdlight_quirk_input)
level += 2;
arg.cmd = KBDLIGHT_SET;
arg.args[2] = 1 << level;
return huawei_wmi_cmd(arg.cmd, NULL, 0);
}
static ssize_t kbdlight_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
int err, level;
err = huawei_wmi_kbdlight_get(&level);
if (err)
return err;
return sprintf(buf, "%d\n", level);
}
static ssize_t kbdlight_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
int level, err;
if (kstrtoint(buf, 10, &level))
return -EINVAL;
err = huawei_wmi_kbdlight_set(level);
if (err)
return err;
return size;
}
static DEVICE_ATTR_RW(kbdlight);
static void huawei_wmi_kbdlight_setup(struct device *dev)
{
struct huawei_wmi *huawei = dev_get_drvdata(dev);
huawei->kbdlight_available = true;
if (huawei_wmi_kbdlight_get(NULL)) {
huawei->kbdlight_available = false;
return;
}
device_create_file(dev, &dev_attr_kbdlight);
}
static void huawei_wmi_kbdlight_exit(struct device *dev)
{
struct huawei_wmi *huawei = dev_get_drvdata(dev);
if (huawei->kbdlight_available)
device_remove_file(dev, &dev_attr_kbdlight);
}
/* Keyboard backlight timeout */
static int huawei_wmi_kbdlight_timeout_get(int *seconds)
{
u8 ret[HWMI_BUFF_SIZE] = { 0 };
int err;
err = huawei_wmi_cmd(KBDLIGHT_TIMEOUT_GET, ret, HWMI_BUFF_SIZE);
if (err)
return err;
if (seconds)
*seconds = ret[1] | (ret[2] << 8);
return 0;
}
static int huawei_wmi_kbdlight_timeout_set(int seconds)
{
union hwmi_arg arg;
arg.cmd = KBDLIGHT_TIMEOUT_SET;
arg.args[2] = (seconds & 0xff);
arg.args[3] = (seconds >> 8);
return huawei_wmi_cmd(arg.cmd, NULL, 0);