1 // SPDX-License-Identifier: GPL-2.0
3 * Huawei WMI laptop extras driver
8 #include <linux/acpi.h>
9 #include <linux/debugfs.h>
10 #include <linux/delay.h>
11 #include <linux/dmi.h>
12 #include <linux/input.h>
13 #include <linux/input/sparse-keymap.h>
14 #include <linux/leds.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/platform_device.h>
18 #include <linux/power_supply.h>
19 #include <linux/sysfs.h>
20 #include <linux/wmi.h>
21 #include <acpi/battery.h>
26 #define HWMI_METHOD_GUID "ABBC0F5B-8EA1-11D1-A000-C90629100000"
27 #define HWMI_EVENT_GUID "ABBC0F5C-8EA1-11D1-A000-C90629100000"
30 #define WMI0_EXPENSIVE_GUID "39142400-C6A3-40fa-BADB-8A2652834100"
31 #define WMI0_EVENT_GUID "59142400-C6A3-40fa-BADB-8A2652834100"
36 BATTERY_THRESH_GET = 0x00001103, /* \GBTT */
37 BATTERY_THRESH_SET = 0x00001003, /* \SBTT */
38 FN_LOCK_GET = 0x00000604, /* \GFRS */
39 FN_LOCK_SET = 0x00000704, /* \SFRS */
40 MICMUTE_LED_SET = 0x00000b04, /* \SMLS */
51 bool report_brightness;
54 static struct quirk_entry *quirks;
56 struct huawei_wmi_debug {
62 bool battery_available;
63 bool fn_lock_available;
65 struct huawei_wmi_debug debug;
66 struct led_classdev cdev;
69 struct mutex wmi_lock;
72 static struct huawei_wmi *huawei_wmi;
74 static const struct key_entry huawei_wmi_keymap[] = {
75 { KE_KEY, 0x281, { KEY_BRIGHTNESSDOWN } },
76 { KE_KEY, 0x282, { KEY_BRIGHTNESSUP } },
77 { KE_KEY, 0x284, { KEY_MUTE } },
78 { KE_KEY, 0x285, { KEY_VOLUMEDOWN } },
79 { KE_KEY, 0x286, { KEY_VOLUMEUP } },
80 { KE_KEY, 0x287, { KEY_MICMUTE } },
81 { KE_KEY, 0x289, { KEY_WLAN } },
83 { KE_KEY, 0x28a, { KEY_CONFIG } },
85 { KE_IGNORE, 0x293, { KEY_KBDILLUMTOGGLE } },
86 { KE_IGNORE, 0x294, { KEY_KBDILLUMUP } },
87 { KE_IGNORE, 0x295, { KEY_KBDILLUMUP } },
91 static int battery_reset = -1;
92 static int report_brightness = -1;
94 module_param(battery_reset, bint, 0444);
95 MODULE_PARM_DESC(battery_reset,
96 "Reset battery charge values to (0-0) before disabling it using (0-100)");
97 module_param(report_brightness, bint, 0444);
98 MODULE_PARM_DESC(report_brightness,
99 "Report brightness keys.");
103 static int __init dmi_matched(const struct dmi_system_id *dmi)
105 quirks = dmi->driver_data;
109 static struct quirk_entry quirk_unknown = {
112 static struct quirk_entry quirk_battery_reset = {
113 .battery_reset = true,
116 static struct quirk_entry quirk_matebook_x = {
118 .report_brightness = true,
121 static const struct dmi_system_id huawei_quirks[] = {
123 .callback = dmi_matched,
124 .ident = "Huawei MACH-WX9",
126 DMI_MATCH(DMI_SYS_VENDOR, "HUAWEI"),
127 DMI_MATCH(DMI_PRODUCT_NAME, "MACH-WX9"),
129 .driver_data = &quirk_battery_reset
132 .callback = dmi_matched,
133 .ident = "Huawei MateBook X",
135 DMI_MATCH(DMI_SYS_VENDOR, "HUAWEI"),
136 DMI_MATCH(DMI_PRODUCT_NAME, "HUAWEI MateBook X")
138 .driver_data = &quirk_matebook_x
145 static int huawei_wmi_call(struct huawei_wmi *huawei,
146 struct acpi_buffer *in, struct acpi_buffer *out)
150 mutex_lock(&huawei->wmi_lock);
151 status = wmi_evaluate_method(HWMI_METHOD_GUID, 0, 1, in, out);
152 mutex_unlock(&huawei->wmi_lock);
153 if (ACPI_FAILURE(status)) {
154 dev_err(huawei->dev, "Failed to evaluate wmi method\n");
161 /* HWMI takes a 64 bit input and returns either a package with 2 buffers, one of
162 * 4 bytes and the other of 256 bytes, or one buffer of size 0x104 (260) bytes.
163 * The first 4 bytes are ignored, we ignore the first 4 bytes buffer if we got a
164 * package, or skip the first 4 if a buffer of 0x104 is used. The first byte of
165 * the remaining 0x100 sized buffer has the return status of every call. In case
166 * the return status is non-zero, we return -ENODEV but still copy the returned
167 * buffer to the given buffer parameter (buf).
169 static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen)
171 struct huawei_wmi *huawei = huawei_wmi;
172 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
173 struct acpi_buffer in;
174 union acpi_object *obj;
178 in.length = sizeof(arg);
181 /* Some models require calling HWMI twice to execute a command. We evaluate
182 * HWMI and if we get a non-zero return status we evaluate it again.
184 for (i = 0; i < 2; i++) {
185 err = huawei_wmi_call(huawei, &in, &out);
196 /* Models that implement both "legacy" and HWMI tend to return a 0x104
197 * sized buffer instead of a package of 0x4 and 0x100 buffers.
199 case ACPI_TYPE_BUFFER:
200 if (obj->buffer.length == 0x104) {
201 // Skip the first 4 bytes.
202 obj->buffer.pointer += 4;
205 dev_err(huawei->dev, "Bad buffer length, got %d\n", obj->buffer.length);
211 /* HWMI returns a package with 2 buffer elements, one of 4 bytes and the
212 * other is 256 bytes.
214 case ACPI_TYPE_PACKAGE:
215 if (obj->package.count != 2) {
216 dev_err(huawei->dev, "Bad package count, got %d\n", obj->package.count);
221 obj = &obj->package.elements[1];
222 if (obj->type != ACPI_TYPE_BUFFER) {
223 dev_err(huawei->dev, "Bad package element type, got %d\n", obj->type);
227 len = obj->buffer.length;
230 /* Shouldn't get here! */
232 dev_err(huawei->dev, "Unexpected obj type, got: %d\n", obj->type);
237 if (!*obj->buffer.pointer)
241 err = (*obj->buffer.pointer) ? -ENODEV : 0;
244 len = min(buflen, len);
245 memcpy(buf, obj->buffer.pointer, len);
255 static int huawei_wmi_micmute_led_set(struct led_classdev *led_cdev,
256 enum led_brightness brightness)
258 /* This is a workaround until the "legacy" interface is implemented. */
259 if (quirks && quirks->ec_micmute) {
263 union acpi_object args[3];
264 struct acpi_object_list arg_list = {
266 .count = ARRAY_SIZE(args),
269 handle = ec_get_handle();
273 args[0].type = args[1].type = args[2].type = ACPI_TYPE_INTEGER;
274 args[1].integer.value = 0x04;
276 if (acpi_has_method(handle, "SPIN")) {
277 acpi_method = "SPIN";
278 args[0].integer.value = 0;
279 args[2].integer.value = brightness ? 1 : 0;
280 } else if (acpi_has_method(handle, "WPIN")) {
281 acpi_method = "WPIN";
282 args[0].integer.value = 1;
283 args[2].integer.value = brightness ? 0 : 1;
288 status = acpi_evaluate_object(handle, acpi_method, &arg_list, NULL);
289 if (ACPI_FAILURE(status))
296 arg.cmd = MICMUTE_LED_SET;
297 arg.args[2] = brightness;
299 return huawei_wmi_cmd(arg.cmd, NULL, 0);
303 static void huawei_wmi_leds_setup(struct device *dev)
305 struct huawei_wmi *huawei = dev_get_drvdata(dev);
307 huawei->cdev.name = "platform::micmute";
308 huawei->cdev.max_brightness = 1;
309 huawei->cdev.brightness_set_blocking = &huawei_wmi_micmute_led_set;
310 huawei->cdev.default_trigger = "audio-micmute";
311 huawei->cdev.brightness = ledtrig_audio_get(LED_AUDIO_MICMUTE);
312 huawei->cdev.dev = dev;
313 huawei->cdev.flags = LED_CORE_SUSPENDRESUME;
315 devm_led_classdev_register(dev, &huawei->cdev);
318 /* Battery protection */
320 static int huawei_wmi_battery_get(int *start, int *end)
325 err = huawei_wmi_cmd(BATTERY_THRESH_GET, ret, sizeof(ret));
329 /* Find the last two non-zero values. Return status is ignored. */
330 i = ARRAY_SIZE(ret) - 1;
336 } while (i > 2 && !ret[i--]);
341 static int huawei_wmi_battery_set(int start, int end)
346 if (start < 0 || end < 0 || start > 100 || end > 100)
349 arg.cmd = BATTERY_THRESH_SET;
353 /* This is an edge case were some models turn battery protection
354 * off without changing their thresholds values. We clear the
355 * values before turning off protection. Sometimes we need a sleep delay to
356 * make sure these values make their way to EC memory.
358 if (quirks && quirks->battery_reset && start == 0 && end == 100) {
359 err = huawei_wmi_battery_set(0, 0);
366 err = huawei_wmi_cmd(arg.cmd, NULL, 0);
371 static ssize_t charge_control_start_threshold_show(struct device *dev,
372 struct device_attribute *attr,
377 err = huawei_wmi_battery_get(&start, NULL);
381 return sprintf(buf, "%d\n", start);
384 static ssize_t charge_control_end_threshold_show(struct device *dev,
385 struct device_attribute *attr,
390 err = huawei_wmi_battery_get(NULL, &end);
394 return sprintf(buf, "%d\n", end);
397 static ssize_t charge_control_thresholds_show(struct device *dev,
398 struct device_attribute *attr,
403 err = huawei_wmi_battery_get(&start, &end);
407 return sprintf(buf, "%d %d\n", start, end);
410 static ssize_t charge_control_start_threshold_store(struct device *dev,
411 struct device_attribute *attr,
412 const char *buf, size_t size)
416 err = huawei_wmi_battery_get(NULL, &end);
420 if (sscanf(buf, "%d", &start) != 1)
423 err = huawei_wmi_battery_set(start, end);
430 static ssize_t charge_control_end_threshold_store(struct device *dev,
431 struct device_attribute *attr,
432 const char *buf, size_t size)
436 err = huawei_wmi_battery_get(&start, NULL);
440 if (sscanf(buf, "%d", &end) != 1)
443 err = huawei_wmi_battery_set(start, end);
450 static ssize_t charge_control_thresholds_store(struct device *dev,
451 struct device_attribute *attr,
452 const char *buf, size_t size)
456 if (sscanf(buf, "%d %d", &start, &end) != 2)
459 err = huawei_wmi_battery_set(start, end);
466 static DEVICE_ATTR_RW(charge_control_start_threshold);
467 static DEVICE_ATTR_RW(charge_control_end_threshold);
468 static DEVICE_ATTR_RW(charge_control_thresholds);
470 static int huawei_wmi_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook)
474 err = device_create_file(&battery->dev, &dev_attr_charge_control_start_threshold);
478 err = device_create_file(&battery->dev, &dev_attr_charge_control_end_threshold);
480 device_remove_file(&battery->dev, &dev_attr_charge_control_start_threshold);
485 static int huawei_wmi_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook)
487 device_remove_file(&battery->dev, &dev_attr_charge_control_start_threshold);
488 device_remove_file(&battery->dev, &dev_attr_charge_control_end_threshold);
493 static struct acpi_battery_hook huawei_wmi_battery_hook = {
494 .add_battery = huawei_wmi_battery_add,
495 .remove_battery = huawei_wmi_battery_remove,
496 .name = "Huawei Battery Extension"
499 static void huawei_wmi_battery_setup(struct device *dev)
501 struct huawei_wmi *huawei = dev_get_drvdata(dev);
503 huawei->battery_available = true;
504 if (huawei_wmi_battery_get(NULL, NULL)) {
505 huawei->battery_available = false;
509 battery_hook_register(&huawei_wmi_battery_hook);
510 device_create_file(dev, &dev_attr_charge_control_thresholds);
513 static void huawei_wmi_battery_exit(struct device *dev)
515 struct huawei_wmi *huawei = dev_get_drvdata(dev);
517 if (huawei->battery_available) {
518 battery_hook_unregister(&huawei_wmi_battery_hook);
519 device_remove_file(dev, &dev_attr_charge_control_thresholds);
525 static int huawei_wmi_fn_lock_get(int *on)
527 u8 ret[0x100] = { 0 };
530 err = huawei_wmi_cmd(FN_LOCK_GET, ret, 0x100);
534 /* Find the first non-zero value. Return status is ignored. */
538 *on = ret[i] - 1; // -1 undefined, 0 off, 1 on.
539 } while (i < 0xff && !ret[i++]);
544 static int huawei_wmi_fn_lock_set(int on)
548 arg.cmd = FN_LOCK_SET;
549 arg.args[2] = on + 1; // 0 undefined, 1 off, 2 on.
551 return huawei_wmi_cmd(arg.cmd, NULL, 0);
554 static ssize_t fn_lock_state_show(struct device *dev,
555 struct device_attribute *attr,
560 err = huawei_wmi_fn_lock_get(&on);
564 return sprintf(buf, "%d\n", on);
567 static ssize_t fn_lock_state_store(struct device *dev,
568 struct device_attribute *attr,
569 const char *buf, size_t size)
573 if (kstrtoint(buf, 10, &on) ||
577 err = huawei_wmi_fn_lock_set(on);
584 static DEVICE_ATTR_RW(fn_lock_state);
586 static void huawei_wmi_fn_lock_setup(struct device *dev)
588 struct huawei_wmi *huawei = dev_get_drvdata(dev);
590 huawei->fn_lock_available = true;
591 if (huawei_wmi_fn_lock_get(NULL)) {
592 huawei->fn_lock_available = false;
596 device_create_file(dev, &dev_attr_fn_lock_state);
599 static void huawei_wmi_fn_lock_exit(struct device *dev)
601 struct huawei_wmi *huawei = dev_get_drvdata(dev);
603 if (huawei->fn_lock_available)
604 device_remove_file(dev, &dev_attr_fn_lock_state);
609 static void huawei_wmi_debugfs_call_dump(struct seq_file *m, void *data,
610 union acpi_object *obj)
612 struct huawei_wmi *huawei = m->private;
616 case ACPI_TYPE_INTEGER:
617 seq_printf(m, "0x%llx", obj->integer.value);
619 case ACPI_TYPE_STRING:
620 seq_printf(m, "\"%.*s\"", obj->string.length, obj->string.pointer);
622 case ACPI_TYPE_BUFFER:
624 for (i = 0; i < obj->buffer.length; i++) {
625 seq_printf(m, "0x%02x", obj->buffer.pointer[i]);
626 if (i < obj->buffer.length - 1)
631 case ACPI_TYPE_PACKAGE:
633 for (i = 0; i < obj->package.count; i++) {
634 huawei_wmi_debugfs_call_dump(m, huawei, &obj->package.elements[i]);
635 if (i < obj->package.count - 1)
641 dev_err(huawei->dev, "Unexpected obj type, got %d\n", obj->type);
646 static int huawei_wmi_debugfs_call_show(struct seq_file *m, void *data)
648 struct huawei_wmi *huawei = m->private;
649 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
650 struct acpi_buffer in;
651 union acpi_object *obj;
654 in.length = sizeof(u64);
655 in.pointer = &huawei->debug.arg;
657 err = huawei_wmi_call(huawei, &in, &out);
664 goto fail_debugfs_call;
667 huawei_wmi_debugfs_call_dump(m, huawei, obj);
674 DEFINE_SHOW_ATTRIBUTE(huawei_wmi_debugfs_call);
676 static void huawei_wmi_debugfs_setup(struct device *dev)
678 struct huawei_wmi *huawei = dev_get_drvdata(dev);
680 huawei->debug.root = debugfs_create_dir("huawei-wmi", NULL);
682 debugfs_create_x64("arg", 0644, huawei->debug.root,
684 debugfs_create_file("call", 0400,
685 huawei->debug.root, huawei, &huawei_wmi_debugfs_call_fops);
688 static void huawei_wmi_debugfs_exit(struct device *dev)
690 struct huawei_wmi *huawei = dev_get_drvdata(dev);
692 debugfs_remove_recursive(huawei->debug.root);
697 static void huawei_wmi_process_key(struct input_dev *idev, int code)
699 const struct key_entry *key;
702 * WMI0 uses code 0x80 to indicate a hotkey event.
703 * The actual key is fetched from the method WQ00
704 * using WMI0_EXPENSIVE_GUID.
707 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
708 union acpi_object *obj;
711 status = wmi_query_block(WMI0_EXPENSIVE_GUID, 0, &response);
712 if (ACPI_FAILURE(status))
715 obj = (union acpi_object *)response.pointer;
716 if (obj && obj->type == ACPI_TYPE_INTEGER)
717 code = obj->integer.value;
719 kfree(response.pointer);
722 key = sparse_keymap_entry_from_scancode(idev, code);
724 dev_info(&idev->dev, "Unknown key pressed, code: 0x%04x\n", code);
728 if (quirks && !quirks->report_brightness &&
729 (key->sw.code == KEY_BRIGHTNESSDOWN ||
730 key->sw.code == KEY_BRIGHTNESSUP))
733 sparse_keymap_report_entry(idev, key, 1, true);
736 static void huawei_wmi_input_notify(u32 value, void *context)
738 struct input_dev *idev = (struct input_dev *)context;
739 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
740 union acpi_object *obj;
743 status = wmi_get_event_data(value, &response);
744 if (ACPI_FAILURE(status)) {
745 dev_err(&idev->dev, "Unable to get event data\n");
749 obj = (union acpi_object *)response.pointer;
750 if (obj && obj->type == ACPI_TYPE_INTEGER)
751 huawei_wmi_process_key(idev, obj->integer.value);
753 dev_err(&idev->dev, "Bad response type\n");
755 kfree(response.pointer);
758 static int huawei_wmi_input_setup(struct device *dev, const char *guid)
760 struct input_dev *idev;
764 idev = devm_input_allocate_device(dev);
768 idev->name = "Huawei WMI hotkeys";
769 idev->phys = "wmi/input0";
770 idev->id.bustype = BUS_HOST;
771 idev->dev.parent = dev;
773 err = sparse_keymap_setup(idev, huawei_wmi_keymap, NULL);
777 err = input_register_device(idev);
781 status = wmi_install_notify_handler(guid, huawei_wmi_input_notify, idev);
782 if (ACPI_FAILURE(status))
788 static void huawei_wmi_input_exit(struct device *dev, const char *guid)
790 wmi_remove_notify_handler(guid);
795 static const struct wmi_device_id huawei_wmi_events_id_table[] = {
796 { .guid_string = WMI0_EVENT_GUID },
797 { .guid_string = HWMI_EVENT_GUID },
801 static int huawei_wmi_probe(struct platform_device *pdev)
803 const struct wmi_device_id *guid = huawei_wmi_events_id_table;
806 platform_set_drvdata(pdev, huawei_wmi);
807 huawei_wmi->dev = &pdev->dev;
809 while (*guid->guid_string) {
810 if (wmi_has_guid(guid->guid_string)) {
811 err = huawei_wmi_input_setup(&pdev->dev, guid->guid_string);
813 dev_err(&pdev->dev, "Failed to setup input on %s\n", guid->guid_string);
821 if (wmi_has_guid(HWMI_METHOD_GUID)) {
822 mutex_init(&huawei_wmi->wmi_lock);
824 huawei_wmi_leds_setup(&pdev->dev);
825 huawei_wmi_fn_lock_setup(&pdev->dev);
826 huawei_wmi_battery_setup(&pdev->dev);
827 huawei_wmi_debugfs_setup(&pdev->dev);
833 static int huawei_wmi_remove(struct platform_device *pdev)
835 const struct wmi_device_id *guid = huawei_wmi_events_id_table;
837 while (*guid->guid_string) {
838 if (wmi_has_guid(guid->guid_string))
839 huawei_wmi_input_exit(&pdev->dev, guid->guid_string);
844 if (wmi_has_guid(HWMI_METHOD_GUID)) {
845 huawei_wmi_debugfs_exit(&pdev->dev);
846 huawei_wmi_battery_exit(&pdev->dev);
847 huawei_wmi_fn_lock_exit(&pdev->dev);
853 static struct platform_driver huawei_wmi_driver = {
855 .name = "huawei-wmi",
857 .probe = huawei_wmi_probe,
858 .remove = huawei_wmi_remove,
861 static __init int huawei_wmi_init(void)
863 struct platform_device *pdev;
866 huawei_wmi = kzalloc(sizeof(struct huawei_wmi), GFP_KERNEL);
870 quirks = &quirk_unknown;
871 dmi_check_system(huawei_quirks);
872 if (battery_reset != -1)
873 quirks->battery_reset = battery_reset;
874 if (report_brightness != -1)
875 quirks->report_brightness = report_brightness;
877 err = platform_driver_register(&huawei_wmi_driver);
881 pdev = platform_device_register_simple("huawei-wmi", PLATFORM_DEVID_NONE, NULL, 0);
890 platform_driver_unregister(&huawei_wmi_driver);
896 static __exit void huawei_wmi_exit(void)
898 struct platform_device *pdev = to_platform_device(huawei_wmi->dev);
900 platform_device_unregister(pdev);
901 platform_driver_unregister(&huawei_wmi_driver);
906 module_init(huawei_wmi_init);
907 module_exit(huawei_wmi_exit);
909 MODULE_ALIAS("wmi:"HWMI_METHOD_GUID);
910 MODULE_DEVICE_TABLE(wmi, huawei_wmi_events_id_table);
912 MODULE_DESCRIPTION("Huawei WMI laptop extras driver");
913 MODULE_LICENSE("GPL v2");