2018-12-13 07:07:58 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
2019-09-24 10:48:06 +08:00
|
|
|
* Huawei WMI laptop extras driver
|
2018-12-13 07:07:58 +08:00
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Ayman Bagabas <ayman.bagabas@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/acpi.h>
|
2019-09-24 10:48:07 +08:00
|
|
|
#include <linux/dmi.h>
|
2018-12-13 07:07:58 +08:00
|
|
|
#include <linux/input.h>
|
|
|
|
#include <linux/input/sparse-keymap.h>
|
|
|
|
#include <linux/leds.h>
|
|
|
|
#include <linux/module.h>
|
2019-09-24 10:48:06 +08:00
|
|
|
#include <linux/platform_device.h>
|
2018-12-13 07:07:58 +08:00
|
|
|
#include <linux/wmi.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Huawei WMI GUIDs
|
|
|
|
*/
|
2019-09-24 10:48:06 +08:00
|
|
|
#define HWMI_EVENT_GUID "ABBC0F5C-8EA1-11D1-A000-C90629100000"
|
2018-12-13 07:07:58 +08:00
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
/* Legacy GUIDs */
|
2018-12-13 07:07:58 +08:00
|
|
|
#define WMI0_EXPENSIVE_GUID "39142400-C6A3-40fa-BADB-8A2652834100"
|
2019-09-24 10:48:06 +08:00
|
|
|
#define WMI0_EVENT_GUID "59142400-C6A3-40fa-BADB-8A2652834100"
|
2018-12-13 07:07:58 +08:00
|
|
|
|
2019-09-24 10:48:07 +08:00
|
|
|
struct quirk_entry {
|
|
|
|
bool battery_reset;
|
|
|
|
bool ec_micmute;
|
|
|
|
bool report_brightness;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct quirk_entry *quirks;
|
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
struct huawei_wmi {
|
|
|
|
struct input_dev *idev[2];
|
2018-12-13 07:07:58 +08:00
|
|
|
struct led_classdev cdev;
|
2019-09-24 10:48:06 +08:00
|
|
|
struct platform_device *pdev;
|
2018-12-13 07:07:58 +08:00
|
|
|
acpi_handle handle;
|
|
|
|
char *acpi_method;
|
|
|
|
};
|
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
struct huawei_wmi *huawei_wmi;
|
|
|
|
|
2018-12-13 07:07:58 +08:00
|
|
|
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 } },
|
2019-09-24 10:48:06 +08:00
|
|
|
// Keyboard backlit
|
2018-12-13 07:07:58 +08:00
|
|
|
{ KE_IGNORE, 0x293, { KEY_KBDILLUMTOGGLE } },
|
|
|
|
{ KE_IGNORE, 0x294, { KEY_KBDILLUMUP } },
|
|
|
|
{ KE_IGNORE, 0x295, { KEY_KBDILLUMUP } },
|
|
|
|
{ KE_END, 0 }
|
|
|
|
};
|
|
|
|
|
2019-09-24 10:48:07 +08:00
|
|
|
static int battery_reset = -1;
|
|
|
|
static int report_brightness = -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.");
|
|
|
|
|
|
|
|
/* Quirks */
|
|
|
|
|
|
|
|
static int __init dmi_matched(const struct dmi_system_id *dmi)
|
|
|
|
{
|
|
|
|
quirks = dmi->driver_data;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct quirk_entry quirk_unknown = {
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct quirk_entry quirk_battery_reset = {
|
|
|
|
.battery_reset = true,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct quirk_entry quirk_matebook_x = {
|
|
|
|
.ec_micmute = true,
|
|
|
|
.report_brightness = true,
|
|
|
|
};
|
|
|
|
|
|
|
|
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_battery_reset
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.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
|
|
|
|
},
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2018-12-13 07:07:58 +08:00
|
|
|
static int huawei_wmi_micmute_led_set(struct led_classdev *led_cdev,
|
|
|
|
enum led_brightness brightness)
|
|
|
|
{
|
2019-09-24 10:48:06 +08:00
|
|
|
struct huawei_wmi *huawei = dev_get_drvdata(led_cdev->dev->parent);
|
2018-12-13 07:07:58 +08:00
|
|
|
acpi_status status;
|
|
|
|
union acpi_object args[3];
|
|
|
|
struct acpi_object_list arg_list = {
|
|
|
|
.pointer = args,
|
|
|
|
.count = ARRAY_SIZE(args),
|
|
|
|
};
|
|
|
|
|
|
|
|
args[0].type = args[1].type = args[2].type = ACPI_TYPE_INTEGER;
|
|
|
|
args[1].integer.value = 0x04;
|
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
if (strcmp(huawei->acpi_method, "SPIN") == 0) {
|
2018-12-13 07:07:58 +08:00
|
|
|
args[0].integer.value = 0;
|
|
|
|
args[2].integer.value = brightness ? 1 : 0;
|
2019-09-24 10:48:06 +08:00
|
|
|
} else if (strcmp(huawei->acpi_method, "WPIN") == 0) {
|
2018-12-13 07:07:58 +08:00
|
|
|
args[0].integer.value = 1;
|
|
|
|
args[2].integer.value = brightness ? 0 : 1;
|
|
|
|
} else {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
status = acpi_evaluate_object(huawei->handle, huawei->acpi_method, &arg_list, NULL);
|
2018-12-13 07:07:58 +08:00
|
|
|
if (ACPI_FAILURE(status))
|
|
|
|
return -ENXIO;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
static void huawei_wmi_leds_setup(struct device *dev)
|
2018-12-13 07:07:58 +08:00
|
|
|
{
|
2019-09-24 10:48:06 +08:00
|
|
|
struct huawei_wmi *huawei = dev_get_drvdata(dev);
|
2018-12-13 07:07:58 +08:00
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
huawei->handle = ec_get_handle();
|
|
|
|
if (!huawei->handle)
|
|
|
|
return;
|
2018-12-13 07:07:58 +08:00
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
if (acpi_has_method(huawei->handle, "SPIN"))
|
|
|
|
huawei->acpi_method = "SPIN";
|
|
|
|
else if (acpi_has_method(huawei->handle, "WPIN"))
|
|
|
|
huawei->acpi_method = "WPIN";
|
2018-12-13 07:07:58 +08:00
|
|
|
else
|
2019-09-24 10:48:06 +08:00
|
|
|
return;
|
2018-12-13 07:07:58 +08:00
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
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.brightness = ledtrig_audio_get(LED_AUDIO_MICMUTE);
|
|
|
|
huawei->cdev.dev = dev;
|
|
|
|
huawei->cdev.flags = LED_CORE_SUSPENDRESUME;
|
2018-12-13 07:07:58 +08:00
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
devm_led_classdev_register(dev, &huawei->cdev);
|
2018-12-13 07:07:58 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
/* Input */
|
|
|
|
|
|
|
|
static void huawei_wmi_process_key(struct input_dev *idev, int code)
|
2018-12-13 07:07:58 +08:00
|
|
|
{
|
|
|
|
const struct key_entry *key;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* WMI0 uses code 0x80 to indicate a hotkey event.
|
|
|
|
* The actual key is fetched from the method WQ00
|
|
|
|
* using WMI0_EXPENSIVE_GUID.
|
|
|
|
*/
|
|
|
|
if (code == 0x80) {
|
|
|
|
struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
|
|
|
|
union acpi_object *obj;
|
|
|
|
acpi_status status;
|
|
|
|
|
|
|
|
status = wmi_query_block(WMI0_EXPENSIVE_GUID, 0, &response);
|
|
|
|
if (ACPI_FAILURE(status))
|
|
|
|
return;
|
|
|
|
|
|
|
|
obj = (union acpi_object *)response.pointer;
|
|
|
|
if (obj && obj->type == ACPI_TYPE_INTEGER)
|
|
|
|
code = obj->integer.value;
|
|
|
|
|
|
|
|
kfree(response.pointer);
|
|
|
|
}
|
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
key = sparse_keymap_entry_from_scancode(idev, code);
|
2018-12-13 07:07:58 +08:00
|
|
|
if (!key) {
|
2019-09-24 10:48:06 +08:00
|
|
|
dev_info(&idev->dev, "Unknown key pressed, code: 0x%04x\n", code);
|
2018-12-13 07:07:58 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-24 10:48:07 +08:00
|
|
|
if (quirks && !quirks->report_brightness &&
|
|
|
|
(key->sw.code == KEY_BRIGHTNESSDOWN ||
|
|
|
|
key->sw.code == KEY_BRIGHTNESSUP))
|
|
|
|
return;
|
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
sparse_keymap_report_entry(idev, key, 1, true);
|
2018-12-13 07:07:58 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
static void huawei_wmi_input_notify(u32 value, void *context)
|
2018-12-13 07:07:58 +08:00
|
|
|
{
|
2019-09-24 10:48:06 +08:00
|
|
|
struct input_dev *idev = (struct input_dev *)context;
|
|
|
|
struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
|
|
|
|
union acpi_object *obj;
|
|
|
|
acpi_status status;
|
|
|
|
|
|
|
|
status = wmi_get_event_data(value, &response);
|
|
|
|
if (ACPI_FAILURE(status)) {
|
|
|
|
dev_err(&idev->dev, "Unable to get event data\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj = (union acpi_object *)response.pointer;
|
|
|
|
if (obj && obj->type == ACPI_TYPE_INTEGER)
|
|
|
|
huawei_wmi_process_key(idev, obj->integer.value);
|
2018-12-13 07:07:58 +08:00
|
|
|
else
|
2019-09-24 10:48:06 +08:00
|
|
|
dev_err(&idev->dev, "Bad response type\n");
|
|
|
|
|
|
|
|
kfree(response.pointer);
|
2018-12-13 07:07:58 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
static int huawei_wmi_input_setup(struct device *dev,
|
|
|
|
const char *guid,
|
|
|
|
struct input_dev **idev)
|
2018-12-13 07:07:58 +08:00
|
|
|
{
|
2019-09-24 10:48:06 +08:00
|
|
|
*idev = devm_input_allocate_device(dev);
|
|
|
|
if (!*idev)
|
2018-12-13 07:07:58 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
(*idev)->name = "Huawei WMI hotkeys";
|
|
|
|
(*idev)->phys = "wmi/input0";
|
|
|
|
(*idev)->id.bustype = BUS_HOST;
|
|
|
|
(*idev)->dev.parent = dev;
|
2018-12-13 07:07:58 +08:00
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
return sparse_keymap_setup(*idev, huawei_wmi_keymap, NULL) ||
|
|
|
|
input_register_device(*idev) ||
|
|
|
|
wmi_install_notify_handler(guid, huawei_wmi_input_notify,
|
|
|
|
*idev);
|
|
|
|
}
|
2018-12-13 07:07:58 +08:00
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
static void huawei_wmi_input_exit(struct device *dev, const char *guid)
|
|
|
|
{
|
|
|
|
wmi_remove_notify_handler(guid);
|
2018-12-13 07:07:58 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
/* Huawei driver */
|
|
|
|
|
|
|
|
static const struct wmi_device_id huawei_wmi_events_id_table[] = {
|
|
|
|
{ .guid_string = WMI0_EVENT_GUID },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
static int huawei_wmi_probe(struct platform_device *pdev)
|
2018-12-13 07:07:58 +08:00
|
|
|
{
|
2019-09-24 10:48:06 +08:00
|
|
|
const struct wmi_device_id *guid = huawei_wmi_events_id_table;
|
2018-12-13 07:07:58 +08:00
|
|
|
int err;
|
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
platform_set_drvdata(pdev, huawei_wmi);
|
|
|
|
huawei_wmi->pdev = pdev;
|
2018-12-13 07:07:58 +08:00
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
while (*guid->guid_string) {
|
|
|
|
struct input_dev *idev = *huawei_wmi->idev;
|
2018-12-13 07:07:58 +08:00
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
if (wmi_has_guid(guid->guid_string)) {
|
|
|
|
err = huawei_wmi_input_setup(&pdev->dev, guid->guid_string, &idev);
|
|
|
|
if (err) {
|
|
|
|
dev_err(&pdev->dev, "Failed to setup input on %s\n", guid->guid_string);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
2018-12-13 07:07:58 +08:00
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
idev++;
|
|
|
|
guid++;
|
|
|
|
}
|
|
|
|
|
|
|
|
huawei_wmi_leds_setup(&pdev->dev);
|
|
|
|
return 0;
|
2018-12-13 07:07:58 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
static int huawei_wmi_remove(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
const struct wmi_device_id *guid = huawei_wmi_events_id_table;
|
|
|
|
|
|
|
|
while (*guid->guid_string) {
|
|
|
|
if (wmi_has_guid(guid->guid_string))
|
|
|
|
huawei_wmi_input_exit(&pdev->dev, guid->guid_string);
|
|
|
|
|
|
|
|
guid++;
|
|
|
|
}
|
2018-12-13 07:07:58 +08:00
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct platform_driver huawei_wmi_driver = {
|
2018-12-13 07:07:58 +08:00
|
|
|
.driver = {
|
|
|
|
.name = "huawei-wmi",
|
|
|
|
},
|
|
|
|
.probe = huawei_wmi_probe,
|
2019-09-24 10:48:06 +08:00
|
|
|
.remove = huawei_wmi_remove,
|
2018-12-13 07:07:58 +08:00
|
|
|
};
|
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
static __init int huawei_wmi_init(void)
|
|
|
|
{
|
|
|
|
struct platform_device *pdev;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
huawei_wmi = kzalloc(sizeof(struct huawei_wmi), GFP_KERNEL);
|
|
|
|
if (!huawei_wmi)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2019-09-24 10:48:07 +08:00
|
|
|
quirks = &quirk_unknown;
|
|
|
|
dmi_check_system(huawei_quirks);
|
|
|
|
if (battery_reset != -1)
|
|
|
|
quirks->battery_reset = battery_reset;
|
|
|
|
if (report_brightness != -1)
|
|
|
|
quirks->report_brightness = report_brightness;
|
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
err = platform_driver_register(&huawei_wmi_driver);
|
|
|
|
if (err)
|
|
|
|
goto pdrv_err;
|
|
|
|
|
|
|
|
pdev = platform_device_register_simple("huawei-wmi", -1, NULL, 0);
|
|
|
|
if (IS_ERR(pdev)) {
|
|
|
|
err = PTR_ERR(pdev);
|
|
|
|
goto pdev_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pdev_err:
|
|
|
|
platform_driver_unregister(&huawei_wmi_driver);
|
|
|
|
pdrv_err:
|
|
|
|
kfree(huawei_wmi);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __exit void huawei_wmi_exit(void)
|
|
|
|
{
|
|
|
|
platform_device_unregister(huawei_wmi->pdev);
|
|
|
|
platform_driver_unregister(&huawei_wmi_driver);
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(huawei_wmi_init);
|
|
|
|
module_exit(huawei_wmi_exit);
|
2018-12-13 07:07:58 +08:00
|
|
|
|
2019-09-24 10:48:06 +08:00
|
|
|
MODULE_DEVICE_TABLE(wmi, huawei_wmi_events_id_table);
|
2018-12-13 07:07:58 +08:00
|
|
|
MODULE_AUTHOR("Ayman Bagabas <ayman.bagabas@gmail.com>");
|
2019-09-24 10:48:06 +08:00
|
|
|
MODULE_DESCRIPTION("Huawei WMI laptop extras driver");
|
2018-12-13 07:07:58 +08:00
|
|
|
MODULE_LICENSE("GPL v2");
|