platform/x86: Huawei WMI laptop extras driver
v4 cover platform/x86: Huawei WMI laptop extras driver
Changes in v4:
* Use int and bint for module params.
Changes in v3:
* Kconfig changes
* Fix NULL cast to int warning.
* Add ACPI_BATTERY as a dependency.
Changes in v2:
* Use battery charge control API.
This patch series introduce changes to huawei-wmi driver that includes:
* Move to platform driver
* Implement driver quirks and parameters
* Implement WMI management interface
* Add micmute LED support through WMI
* Add battery charging protection support through WMI
* Add fn-lock support through WMI
* Add a debugfs interface to WMI
# Move to platform driver
The current driver offers hotkeys and micmute led support only. With these changes, a platform driver makes more sense since it handles these changes pretty nicely.
# Implement WMI management interface
Huawei Matebook laptops come with two WMI interfaces. The first being WMI0 which is considered "legacy" and AFAIK only found on the Matebook X released in 2017. The second has a UID of "HWMI" and is found in pretty much all models with a slight difference in implementation except for the Matebook X (2017). Since this model has two interfaces, some aspects are controlled through the legacy interface and some through the other interface. Currently, the legacy interface is not fully implemented and is only used for hotkeys and further debugging has to be done.
The WMI interface takes a 64 bit integer, although uses 32 bits most of the time, and returns a 256-260 bytes buffer consists of either one ACPI buffer of 260 bytes, in the case of Matebook X (2017), or one ACPI package of two buffers, one with 4 bytes, and the other with 256 bytes. We only care about the latter 256 buffer in both cases since the 4 bytes always return zeros. The first byte of this 256 buffer always has the return status where 1 indicated error. Some models require calling the WMI interface twice to execute a command.
# Add micmute LED support through WMI
After implementing the WMI interface, micmute LED can be controlled easily. Models with the legacy interface fall back to ACPI EC method control since the legacy interface is not implemented.
# Add battery charging protection support through WMI
Most models, that has the WMI interface, are capable of battery protection where it can control battery charging thresholds and limits charging the battery to certain values.
# Add fn-lock support through WMI
The behavior of hotkeys is not the same among all models. Some models require fn-lock to do things like Ctrl-Ins or Alt-PrtSc. By default, hotkeys behave as special keys (media keys, Ins, etc), but if a modifier is used (ctrl, alt, shift) these keys behave as F1-F12 keys. If the Fn key is toggled on, the hotkeys with or without a modifier, behave as F1-F12 keys. This makes it impossible to use a modifier and PrtSc or Ins.
Now, some models fix this by excluding PrtSc and Ins keys from being treated as F11 and F12 keys with the use of a modifier. However, some models do not, and fixes this by the so called fn-lock.
Fn-lock inverts the behavior of the top row from special keys to F1-F12 keys. So a modifier and a special key would be possible which make things like Alt-Ins possible. Now, with fn-lock we would have 4 modes:
* Fn-key off & fn-lock off - hotkeys treated as special keys using a modifier gives F1-F12 keys.
* Fn-key on & fn-lock off - hotkeys treated as F1-F12 keys and using a modifier gives F1-F12.
* Fn-key off & fn-lock on - hotkeys are treated as F1-F12 keys and using a modifier gives special keys.
* Fn-key on & fn-lock on - hotkeys are treated as special keys and using a modifier gives special keys.
# Implement driver quirks and parameters
The driver introduces 3 quirks and 2 parameters that can change the driver's behavior. These quirks being as:
1. Fixes reporting brightness keys twice since it's already handled by acpi-video.
2. Some models need a short delay when setting battery thresholds to prevent a race condition when two processes read/write.
3. Matebook X (2017) handles micmute led through the "legacy" interface which is not currently implemented. Use ACPI EC method to control this led.
and the 2 parameters can enforce the behavior of quirk 1 & 2.
# Add a debugfs interface to WMI
An interface to the WMI management interface that allows easier debugging.
Ayman Bagabas (6):
platform/x86: huawei-wmi: Move to platform driver platform/x86: huawei-wmi: Add quirks and module parameters platform/x86: huawei-wmi: Implement huawei wmi management platform/x86: huawei-wmi: Add battery charging thresholds platform/x86: huawei-wmi: Add fn-lock support platform/x86: huawei-wmi: Add debugfs support
drivers/platform/x86/Kconfig | 8 +- drivers/platform/x86/huawei-wmi.c | 874 ++++++++++++++++++++++++++---- 2 files changed, 786 insertions(+), 96 deletions(-)
base-commit: 288b9117de5cc1b7fb80f54b7c17deed6f018641
Signature · 1 line
2.21.0
PATCH v4 1/6 platform/x86: huawei-wmi: Move to platform driver
Move from WMI driver to platform driver. This move is necessary since the driver is no longer a hotkeys driver only. Platform driver makes it easier for users to access sysfs attributes under (i.e. /sys/devices/platform/huawei-wmi) compared to wmi driver.
Use WMI device UID, AMW0 has a UID of HWMI. WMI0 is the device name and doesn't have a UID so keep it as it is.
--- drivers/platform/x86/Kconfig | 7 +- drivers/platform/x86/huawei-wmi.c | 226 ++++++++++++++++++++---------- 2 files changed, 156 insertions(+), 77 deletions(-)
drivers/platform/x86/Kconfig +3 -4
@@ -1304,7 +1304,7 @@ config INTEL_ATOMISP2_PM will be called intel_atomisp2_pm. config HUAWEI_WMI - tristate "Huawei WMI hotkeys driver" + tristate "Huawei WMI laptop extras driver" depends on ACPI_WMI depends on INPUT select INPUT_SPARSEKMAP
@@ -1313,9 +1313,8 @@ config HUAWEI_WMI select LEDS_TRIGGER_AUDIO select NEW_LEDS help - This driver provides support for Huawei WMI hotkeys. - It enables the missing keys and adds support to the micmute - LED found on some of these laptops. + This driver provides support for Huawei WMI hotkeys, battery charge + control, fn-lock, mic-mute LED, and other extra features. To compile this driver as a module, choose M here: the module will be called huawei-wmi.
drivers/platform/x86/huawei-wmi.c +153 -73
329 lines not on this page · raw message
Signature · 1 line
2.21.0
PATCH v4 2/6 platform/x86: huawei-wmi: Add quirks and module parameters
In reply to Ayman BagabasIntroduce quirks and module parameters. 3 quirks are added:
1. Fixes reporting brightness keys twice since it's already handled by acpi-video.
2. Some models need a short delay when setting battery thresholds to prevent a race condition when two processes read/write. (will be used later)
3. Matebook X (2017) handles micmute led through the "legacy" interface which is not currently implemented. Use ACPI EC method to control this led. (will be used later)
2 module parameters are added to enable this short delay and/or report
brightness keys through this driver.
--- drivers/platform/x86/huawei-wmi.c | 73 +++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+)
drivers/platform/x86/huawei-wmi.c +73 -0
@@ -6,6 +6,7 @@*/ #include <linux/acpi.h> +#include <linux/dmi.h> #include <linux/input.h> #include <linux/input/sparse-keymap.h> #include <linux/leds.h>
@@ -22,6 +23,14 @@#define WMI0_EXPENSIVE_GUID "39142400-C6A3-40fa-BADB-8A2652834100"#define WMI0_EVENT_GUID "59142400-C6A3-40fa-BADB-8A2652834100" +struct quirk_entry { +bool battery_reset; +bool ec_micmute; +bool report_brightness; +}; + +static struct quirk_entry *quirks; +struct huawei_wmi {struct input_dev *idev[2 ];struct led_classdev cdev;
@@ -49,6 +58,58 @@ static const struct key_entry huawei_wmi_keymap[] = { { KE_END, 0 } }; +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 + }, + { } +}; +static int huawei_wmi_micmute_led_set (struct led_classdev *led_cdev,enum led_brightness brightness) {
@@ -139,6 +200,11 @@ static void huawei_wmi_process_key(struct input_dev *idev, int code)return ; } +if (quirks && !quirks->report_brightness && + (key->sw.code ==KEY_BRIGHTNESSDOWN || + key->sw.code ==KEY_BRIGHTNESSUP )) +return ; +sparse_keymap_report_entry (idev, key,1 , true); }
@@ -253,6 +319,13 @@ static __init int huawei_wmi_init(void)if (!huawei_wmi)return -ENOMEM ; + 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; + err =platform_driver_register (&huawei_wmi_driver);if (err) goto pdrv_err;
Signature · 1 line
2.21.0
PATCH v4 3/6 platform/x86: huawei-wmi: Implement huawei wmi management
In reply to Ayman BagabasHuawei Matebook laptops come with a WMI management interface that can control various aspects of the device. This interface is also found on the old Matebook X released in 2017.
Use that to control the mic mute LED.
--- drivers/platform/x86/huawei-wmi.c | 217 +++++++++++++++++++++++++----- 1 file changed, 180 insertions(+), 37 deletions(-)
drivers/platform/x86/huawei-wmi.c +180 -37
@@ -11,18 +11,35 @@#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/wmi.h> /* * 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 */ +MICMUTE_LED_SET =0x00000b04 ,/* \SMLS */ +}; + +union hwmi_arg { +u64 cmd; +u8 args[8 ]; +}; +struct quirk_entry {bool battery_reset;bool ec_micmute;
@@ -35,8 +52,8 @@ struct huawei_wmi {struct input_dev *idev[2 ];struct led_classdev cdev;struct platform_device *pdev; -acpi_handle handle; -char *acpi_method; + +struct mutex wmi_lock; };struct huawei_wmi *huawei_wmi;
@@ -110,52 +127,168 @@ static const struct dmi_system_id huawei_quirks[] = { { } }; +/* Utils */ + +static int huawei_wmi_call (struct acpi_buffer *in,struct acpi_buffer *out) +{ +acpi_status status; + +mutex_lock (&huawei_wmi->wmi_lock); + status =wmi_evaluate_method (HWMI_METHOD_GUID ,0 ,1 , in, out); +mutex_unlock (&huawei_wmi->wmi_lock); +if (ACPI_FAILURE (status)) { +dev_err (&huawei_wmi->pdev->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 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 (&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 =0x100 ; + }else { +dev_err (&huawei_wmi->pdev->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_wmi->pdev->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_wmi->pdev->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_wmi->pdev->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) { -struct huawei_wmi *huawei =dev_get_drvdata (led_cdev->dev->parent); -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 ; - -if (strcmp (huawei->acpi_method,"SPIN" ) ==0 ) { - args[0 ].integer.value =0 ; - args[2 ].integer.value = brightness ?1 :0 ; - }else if (strcmp (huawei->acpi_method,"WPIN" ) ==0 ) { - args[0 ].integer.value =1 ; - args[2 ].integer.value = brightness ?0 :1 ; +/* 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 { -return -EINVAL ; - } +union hwmi_arg arg; - status =acpi_evaluate_object (huawei->handle, huawei->acpi_method, &arg_list,NULL ); -if (ACPI_FAILURE (status)) -return -ENXIO ; + arg.cmd =MICMUTE_LED_SET ; + arg.args[2 ] = brightness; -return 0 ; +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->handle =ec_get_handle (); -if (!huawei->handle) -return ; - -if (acpi_has_method (huawei->handle,"SPIN" )) - huawei->acpi_method ="SPIN" ; -else if (acpi_has_method (huawei->handle,"WPIN" )) - huawei->acpi_method ="WPIN" ; -else -return ; - huawei->cdev.name ="platform::micmute" ; huawei->cdev.max_brightness =1 ; huawei->cdev.brightness_set_blocking = &huawei_wmi_micmute_led_set;
@@ -258,6 +391,7 @@ static void huawei_wmi_input_exit(struct device *dev, const char *guid)static const struct wmi_device_id huawei_wmi_events_id_table[] = { { .guid_string =WMI0_EVENT_GUID }, + { .guid_string =HWMI_EVENT_GUID }, { } };
@@ -284,7 +418,12 @@ static int huawei_wmi_probe(struct platform_device *pdev) guid++; } -huawei_wmi_leds_setup (&pdev->dev); +if (wmi_has_guid (HWMI_METHOD_GUID )) { +mutex_init (&huawei_wmi->wmi_lock); + +huawei_wmi_leds_setup (&pdev->dev); + } +return 0 ; }
@@ -299,6 +438,9 @@ static int huawei_wmi_remove(struct platform_device *pdev) guid++; } +if (wmi_has_guid (HWMI_METHOD_GUID )) { + } +return 0 ; }
@@ -354,6 +496,7 @@ static __exit void huawei_wmi_exit(void)module_init (huawei_wmi_init);module_exit (huawei_wmi_exit); +MODULE_ALIAS ("wmi:" HWMI_METHOD_GUID );MODULE_DEVICE_TABLE (wmi, huawei_wmi_events_id_table);MODULE_AUTHOR ("Ayman Bagabas <[email protected]>" );MODULE_DESCRIPTION ("Huawei WMI laptop extras driver" );
Signature · 1 line
2.21.0
PATCH v4 4/6 platform/x86: huawei-wmi: Add battery charging thresholds
In reply to Ayman BagabasControl battery charge thresholds through the battery API and driver's attributes.
Setting battery charging thresholds can introduce a race condition with MACH-WX9 where two or more threads are trying to read/write values from/to EC memory.
--- drivers/platform/x86/Kconfig | 1 + drivers/platform/x86/huawei-wmi.c | 212 ++++++++++++++++++++++++++++++ 2 files changed, 213 insertions(+)
drivers/platform/x86/Kconfig +1 -0
@@ -1305,6 +1305,7 @@ config INTEL_ATOMISP2_PM config HUAWEI_WMI tristate "Huawei WMI laptop extras driver" + depends on ACPI_BATTERY depends on ACPI_WMI depends on INPUT select INPUT_SPARSEKMAP
drivers/platform/x86/huawei-wmi.c +212 -0
255 lines not on this page · raw message
Signature · 1 line
2.21.0
kbuild test robot
build botRe: [PATCH v4 4/6] platform/x86: huawei-wmi: Add battery charging thresholds
Thank you for the patch! Perhaps something to improve:
Hi Ayman,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on next-20190918]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Ayman-Bagabas/platform-x86-Huawei-WMI-laptop-extras-driver/20190921-072831 base: next-20190918
:::::: branch date: 31 hours ago
:::::: commit date: 31 hours ago
If you fix the issue, kindly add following tag
smatch warnings:drivers/platform/x86/huawei-wmi.c:327 huawei_wmi_battery_get() error: buffer overflow 'ret' 256 <= 256
# https://github.com/0day-ci/linux/commit/2b04f79aef9a86ecb9483dd27a82498fa56bc0c9
git remote add linux-review https://github.com/0day-ci/linux
git remote update linux-review
git checkout 2b04f79aef9a86ecb9483dd27a82498fa56bc0c9
vim +/ret +327 drivers/platform/x86/huawei-wmi.c
2b04f79aef9a86 Ayman Bagabas 2019-09-20 311
2b04f79aef9a86 Ayman Bagabas 2019-09-20 312 static int huawei_wmi_battery_get(int *start, int *end)
2b04f79aef9a86 Ayman Bagabas 2019-09-20 313 {
2b04f79aef9a86 Ayman Bagabas 2019-09-20 314 u8 ret[0x100];
2b04f79aef9a86 Ayman Bagabas 2019-09-20 315 int err, i;
2b04f79aef9a86 Ayman Bagabas 2019-09-20 316
2b04f79aef9a86 Ayman Bagabas 2019-09-20 317 err = huawei_wmi_cmd(BATTERY_THRESH_GET, ret, 0x100);
2b04f79aef9a86 Ayman Bagabas 2019-09-20 318 if (err)
2b04f79aef9a86 Ayman Bagabas 2019-09-20 319 return err;
2b04f79aef9a86 Ayman Bagabas 2019-09-20 320
2b04f79aef9a86 Ayman Bagabas 2019-09-20 321 /* Find the last two non-zero values. Return status is ignored. */
2b04f79aef9a86 Ayman Bagabas 2019-09-20 322 i = 0x100;
2b04f79aef9a86 Ayman Bagabas 2019-09-20 323 do {
2b04f79aef9a86 Ayman Bagabas 2019-09-20 324 if (start)
2b04f79aef9a86 Ayman Bagabas 2019-09-20 325 *start = ret[i-1];
2b04f79aef9a86 Ayman Bagabas 2019-09-20 326 if (end)
2b04f79aef9a86 Ayman Bagabas 2019-09-20 @327 *end = ret[i];
2b04f79aef9a86 Ayman Bagabas 2019-09-20 328 } while (i > 2 && !ret[i--]);
2b04f79aef9a86 Ayman Bagabas 2019-09-20 329
2b04f79aef9a86 Ayman Bagabas 2019-09-20 330 return 0;
2b04f79aef9a86 Ayman Bagabas 2019-09-20 331 }
2b04f79aef9a86 Ayman Bagabas 2019-09-20 332 ---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
PATCH v4 5/6 platform/x86: huawei-wmi: Add fn-lock support
In reply to Ayman BagabasHuawei Matebook laptops uses Fn key and toggle to access F1-F12 keys. Along with that, there is this feature called fn-lock that inverts the behavior of this Fn key and the F1-F12 row.
--- drivers/platform/x86/huawei-wmi.c | 85 +++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+)
drivers/platform/x86/huawei-wmi.c +85 -0
@@ -54,6 +54,7 @@ static struct quirk_entry *quirks;struct huawei_wmi {bool battery_available; +bool fn_lock_available;struct input_dev *idev[2 ];struct led_classdev cdev;
@@ -509,6 +510,88 @@ static void huawei_wmi_battery_exit(struct device *dev) } } +/* Fn lock */ + +static int huawei_wmi_fn_lock_get (int *on) +{ +u8 ret[0x100 ] = {0 }; +int err, i; + + err =huawei_wmi_cmd (FN_LOCK_GET , ret,0x100 ); +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 <0x100 && !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) +{ +if (huawei_wmi->fn_lock_available) +device_remove_file (dev, &dev_attr_fn_lock_state); +} +/* Input */ static void huawei_wmi_process_key (struct input_dev *idev,int code)
@@ -632,6 +715,7 @@ static int huawei_wmi_probe(struct platform_device *pdev)mutex_init (&huawei_wmi->battery_lock);huawei_wmi_leds_setup (&pdev->dev); +huawei_wmi_fn_lock_setup (&pdev->dev);huawei_wmi_battery_setup (&pdev->dev); }
@@ -651,6 +735,7 @@ static int huawei_wmi_remove(struct platform_device *pdev)if (wmi_has_guid (HWMI_METHOD_GUID )) {huawei_wmi_battery_exit (&pdev->dev); +huawei_wmi_fn_lock_exit (&pdev->dev); }return 0 ;
Signature · 1 line
2.21.0
PATCH v4 6/6 platform/x86: huawei-wmi: Add debugfs support
In reply to Ayman BagabasAdd a debugfs interface that can be used to call the WMI management interface function if available.
--- drivers/platform/x86/huawei-wmi.c | 97 +++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+)
drivers/platform/x86/huawei-wmi.c +97 -0
@@ -6,6 +6,7 @@*/ #include <linux/acpi.h> +#include <linux/debugfs.h> #include <linux/delay.h> #include <linux/dmi.h> #include <linux/input.h>
@@ -52,10 +53,16 @@ struct quirk_entry {static struct quirk_entry *quirks; +struct huawei_wmi_debug { +struct dentry *root; +u64 arg; +}; +struct huawei_wmi {bool battery_available;bool fn_lock_available; +struct huawei_wmi_debug debug;struct input_dev *idev[2 ];struct led_classdev cdev;struct platform_device *pdev;
@@ -592,6 +599,94 @@ static void huawei_wmi_fn_lock_exit(struct device *dev)device_remove_file (dev, &dev_attr_fn_lock_state); } +/* debugfs */ + +static void huawei_wmi_debugfs_call_dump (struct seq_file *m,void *data, +union acpi_object *obj) +{ +struct huawei_wmi *huawei = m->private; +int i; + +switch (obj->type) { +case ACPI_TYPE_INTEGER : +seq_printf (m,"0x%llx" , obj->integer.value); +break ; +case ACPI_TYPE_STRING : +seq_printf (m,"\"%*s\"" , obj->string.length, obj->string.pointer); +break ; +case ACPI_TYPE_BUFFER : +seq_puts (m,"{" ); +for (i =0 ; i < obj->buffer.length; i++) { +seq_printf (m,"0x%02x" , obj->buffer.pointer[i]); +if (i < obj->buffer.length -1 ) +seq_puts (m,"," ); + } +seq_puts (m,"}" ); +break ; +case ACPI_TYPE_PACKAGE : +seq_puts (m,"[" ); +for (i =0 ; i < obj->package.count; i++) { +huawei_wmi_debugfs_call_dump (m, huawei, &obj->package.elements[i]); +if (i < obj->package.count -1 ) +seq_puts (m,"," ); + } +seq_puts (m,"]" ); +break ; +default : +dev_err (&huawei->pdev->dev,"Unexpected obj type, got %d\n" , obj->type); +return ; + } +} + +static int huawei_wmi_debugfs_call_show (struct seq_file *m,void *data) +{ +struct huawei_wmi *huawei = m->private; +struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER ,NULL }; +struct acpi_buffer in; +union acpi_object *obj; +int err; + + in.length =sizeof (u64); + in.pointer = &huawei->debug.arg; + + err =huawei_wmi_call (&in, &out); +if (err) +return err; + + obj = out.pointer; +if (!obj) { + err = -EIO ; + goto fail_debugfs_call; + } + +huawei_wmi_debugfs_call_dump (m, huawei, obj); + +fail_debugfs_call: +kfree (out.pointer); +return err; +} + +DEFINE_SHOW_ATTRIBUTE (huawei_wmi_debugfs_call); + +static void huawei_wmi_debugfs_setup (struct device *dev) +{ +struct huawei_wmi *huawei =dev_get_drvdata (dev); + + huawei->debug.root =debugfs_create_dir ("huawei-wmi" ,NULL ); + +debugfs_create_x64 ("arg" ,0644 , huawei->debug.root, + &huawei->debug.arg); +debugfs_create_file ("call" ,0400 , + huawei->debug.root, huawei, &huawei_wmi_debugfs_call_fops); +} + +static void huawei_wmi_debugfs_exit (struct device *dev) +{ +struct huawei_wmi *huawei =dev_get_drvdata (dev); + +debugfs_remove_recursive (huawei->debug.root); +} +/* Input */ static void huawei_wmi_process_key (struct input_dev *idev,int code)
@@ -717,6 +812,7 @@ static int huawei_wmi_probe(struct platform_device *pdev)huawei_wmi_leds_setup (&pdev->dev);huawei_wmi_fn_lock_setup (&pdev->dev);huawei_wmi_battery_setup (&pdev->dev); +huawei_wmi_debugfs_setup (&pdev->dev); }return 0 ;
@@ -734,6 +830,7 @@ static int huawei_wmi_remove(struct platform_device *pdev) }if (wmi_has_guid (HWMI_METHOD_GUID )) { +huawei_wmi_debugfs_exit (&pdev->dev);huawei_wmi_battery_exit (&pdev->dev);huawei_wmi_fn_lock_exit (&pdev->dev); }
Signature · 1 line
2.21.0