platform/x86: ideapad-laptop: synchronize VPC commands
v3 cover platform/x86: ideapad-laptop: synchronize VPC commands
Hi,
Sometimes the Yoga mode control switch did not work properly on my laptop, and sometimes even caused a platform profile switch. It turned out that it was caused by a race situation, the lenovo-ymc wmi notify handler was running at the same time as the ideapad-laptop acpi notify handler, and the partial results of the VPC calls in the two were mixed up.
This series introduces a notification chain to receive YMC or other events, moves the EC triggering into ideapad-laptop, and then introduces a mutex to eliminate the problem.
Best regards,
Gergo Koteles
---
Changes in v3:
- use devm_mutex_init for vpc_mutex
- Link to v2: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Skip the already merged "platform/x86: ideapad-laptop: use cleanup.h" commit
- Based on the WMI driver development guide and the dell drivers, introduce a generic notification chain
- Use the notification chain to send the YMC event from lenovo-ymc to the ideapad-laptop module
- Move the ec_trigger module parameter to the ideapad-laptop as well
- Mutex changes go into one patch
- Link to v1: https://lore.kernel.org/all/[email protected]/
---
Gergo Koteles (4):
platform/x86: ideapad-laptop: introduce a generic notification chain platform/x86: ideapad-laptop: move ymc_trigger_ec from lenovo-ymc platform/x86: ideapad-laptop: move ACPI helpers from header to source
file
platform/x86: ideapad-laptop: add a mutex to synchronize VPC commands
drivers/platform/x86/Kconfig | 1 + drivers/platform/x86/ideapad-laptop.c | 282 ++++++++++++++++++++++++-- drivers/platform/x86/ideapad-laptop.h | 142 +------------ drivers/platform/x86/lenovo-ymc.c | 60 +----- 4 files changed, 275 insertions(+), 210 deletions(-)
Signature · 1 line
2.45.2
PATCH v3 1/4 platform/x86: ideapad-laptop: introduce a generic notification chain
There are several cases where a notification chain can simplify Lenovo WMI drivers.
Add a generic notification chain into ideapad-laptop.
--- drivers/platform/x86/ideapad-laptop.c | 37 +++++++++++++++++++++++++++ drivers/platform/x86/ideapad-laptop.h | 5 ++++ 2 files changed, 42 insertions(+)
drivers/platform/x86/ideapad-laptop.c +37 -0
@@ -1592,6 +1592,39 @@ static void ideapad_sync_touchpad_state(struct ideapad_private *priv, bool send_ priv->r_touchpad_val = value; } +static int ideapad_laptop_nb_notify (struct notifier_block *nb, +unsigned long action,void *data) +{ +switch (action) { + } + +return 0 ; +} + +static struct notifier_block ideapad_laptop_notifier = { + .notifier_call = ideapad_laptop_nb_notify, +}; + +static BLOCKING_NOTIFIER_HEAD (ideapad_laptop_chain_head); + +int ideapad_laptop_register_notifier (struct notifier_block *nb) +{ +return blocking_notifier_chain_register (&ideapad_laptop_chain_head, nb); +} +EXPORT_SYMBOL_NS_GPL (ideapad_laptop_register_notifier,IDEAPAD_LAPTOP ); + +int ideapad_laptop_unregister_notifier (struct notifier_block *nb) +{ +return blocking_notifier_chain_unregister (&ideapad_laptop_chain_head, nb); +} +EXPORT_SYMBOL_NS_GPL (ideapad_laptop_unregister_notifier,IDEAPAD_LAPTOP ); + +void ideapad_laptop_call_notifier (unsigned long action,void *data) +{ +blocking_notifier_call_chain (&ideapad_laptop_chain_head, action, data); +} +EXPORT_SYMBOL_NS_GPL (ideapad_laptop_call_notifier,IDEAPAD_LAPTOP ); +static void ideapad_acpi_notify (acpi_handle handle,u32 event,void *data) {struct ideapad_private *priv = data;
@@ -1974,6 +2007,8 @@ static int ideapad_acpi_add(struct platform_device *pdev)if (err) goto shared_init_failed; +ideapad_laptop_register_notifier (&ideapad_laptop_notifier); +return 0 ; shared_init_failed:
@@ -2006,6 +2041,8 @@ static void ideapad_acpi_remove(struct platform_device *pdev)struct ideapad_private *priv =dev_get_drvdata (&pdev->dev);int i; +ideapad_laptop_unregister_notifier (&ideapad_laptop_notifier); +ideapad_shared_exit (priv); acpi_remove_notify_handler(priv->adev->handle,
drivers/platform/x86/ideapad-laptop.h +5 -0
@@ -12,6 +12,11 @@#include <linux/acpi.h> #include <linux/jiffies.h> #include <linux/errno.h> +#include <linux/notifier.h> + +int ideapad_laptop_register_notifier (struct notifier_block *nb); +int ideapad_laptop_unregister_notifier (struct notifier_block *nb); +void ideapad_laptop_call_notifier (unsigned long action,void *data);enum {VPCCMD_R_VPC1 =0x10 ,
Signature · 1 line
2.45.2
PATCH v3 2/4 platform/x86: ideapad-laptop: move ymc_trigger_ec from lenovo-ymc
In reply to Gergo KotelesSome models need to trigger the EC after each YMC event for the yoga mode control to work properly. EC triggering consist of a VPC call from the lenovo-ymc module. Except for this, all VPC calls are in the ideapad-laptop module.
Since ideapad-laptop has a notification chain, a new YMC_EVENT action can be added and triggered from the lenovo-ymc module. Then the ideapad-laptop can trigger the EC.
If the triggering is in the ideapad-laptop module, then the ec_trigger module parameter should be there as well.
Move the ymc_trigger_ec functionality and the ec_trigger module parameter to the ideapad-laptop module.
--- drivers/platform/x86/Kconfig | 1 + drivers/platform/x86/ideapad-laptop.c | 49 ++++++++++++++++++++++ drivers/platform/x86/ideapad-laptop.h | 4 ++ drivers/platform/x86/lenovo-ymc.c | 60 +-------------------------- 4 files changed, 56 insertions(+), 58 deletions(-)
drivers/platform/x86/Kconfig +1 -0
@@ -476,6 +476,7 @@ config LENOVO_YMC tristate "Lenovo Yoga Tablet Mode Control" depends on ACPI_WMI depends on INPUT + depends on IDEAPAD_LAPTOP select INPUT_SPARSEKMAP help This driver maps the Tablet Mode Control switch to SW_TABLET_MODE input
drivers/platform/x86/ideapad-laptop.c +49 -0
77 lines not on this page · raw message
drivers/platform/x86/ideapad-laptop.h +4 -0
@@ -14,6 +14,10 @@#include <linux/errno.h> #include <linux/notifier.h> +enum ideapad_laptop_notifier_actions { +IDEAPAD_LAPTOP_YMC_EVENT , +}; +int ideapad_laptop_register_notifier (struct notifier_block *nb);int ideapad_laptop_unregister_notifier (struct notifier_block *nb);void ideapad_laptop_call_notifier (unsigned long action,void *data);
drivers/platform/x86/lenovo-ymc.c +2 -58
105 lines not on this page · raw message
Signature · 1 line
2.45.2
PATCH v3 3/4 platform/x86: ideapad-laptop: move ACPI helpers from header to source file
In reply to Gergo KotelesSince moving ymc_trigger_ec from lenovo-ymc to ideapad-laptop, only the latter uses these definitions and functions.
Move them back to source file.
--- drivers/platform/x86/ideapad-laptop.c | 136 +++++++++++++++++++++++++ drivers/platform/x86/ideapad-laptop.h | 139 -------------------------- 2 files changed, 136 insertions(+), 139 deletions(-)
drivers/platform/x86/ideapad-laptop.c +136 -0
@@ -22,6 +22,7 @@#include <linux/init.h> #include <linux/input.h> #include <linux/input/sparse-keymap.h> +#include <linux/jiffies.h> #include <linux/kernel.h> #include <linux/leds.h> #include <linux/module.h>
@@ -87,6 +88,34 @@ enum { SALS_FNLOCK_OFF = 0xf, }; +enum { +VPCCMD_R_VPC1 =0x10 , +VPCCMD_R_BL_MAX , +VPCCMD_R_BL , +VPCCMD_W_BL , +VPCCMD_R_WIFI , +VPCCMD_W_WIFI , +VPCCMD_R_BT , +VPCCMD_W_BT , +VPCCMD_R_BL_POWER , +VPCCMD_R_NOVO , +VPCCMD_R_VPC2 , +VPCCMD_R_TOUCHPAD , +VPCCMD_W_TOUCHPAD , +VPCCMD_R_CAMERA , +VPCCMD_W_CAMERA , +VPCCMD_R_3G , +VPCCMD_W_3G , +VPCCMD_R_ODD ,/* 0x21 */ +VPCCMD_W_FAN , +VPCCMD_R_RF , +VPCCMD_W_RF , +VPCCMD_W_YMC =0x2A , +VPCCMD_R_FAN =0x2B , +VPCCMD_R_SPECIAL_BUTTONS =0x31 , +VPCCMD_W_BL_POWER =0x33 , +}; +/* * These correspond to the number of supported states - 1 * Future keyboard types may need a new system, if there's a collision
@@ -236,6 +265,7 @@ static void ideapad_shared_exit(struct ideapad_private *priv)/* * ACPI Helpers */ +#define IDEAPAD_EC_TIMEOUT 200/* in ms */ static int eval_int (acpi_handle handle,const char *name,unsigned long *res) {
@@ -251,6 +281,29 @@ static int eval_int(acpi_handle handle, const char *name, unsigned long *res)return 0 ; } +static int eval_int_with_arg (acpi_handle handle,const char *name,unsigned long arg, +unsigned long *res) +{ +struct acpi_object_list params; +unsigned long long result; +union acpi_object in_obj; +acpi_status status; + + params.count =1 ; + params.pointer = &in_obj; + in_obj.type =ACPI_TYPE_INTEGER ; + in_obj.integer.value = arg; + + status =acpi_evaluate_integer (handle, (char *)name, ¶ms, &result); +if (ACPI_FAILURE (status)) +return -EIO ; + +if (res) + *res = result; + +return 0 ; +} +static int exec_simple_method (acpi_handle handle,const char *name,unsigned long arg) {acpi_status status =acpi_execute_simple_method (handle, (char *)name, arg);
@@ -293,6 +346,89 @@ static int eval_dytc(acpi_handle handle, unsigned long cmd, unsigned long *res)return eval_int_with_arg (handle,"DYTC" , cmd, res); } +static int eval_vpcr (acpi_handle handle,unsigned long cmd,unsigned long *res) +{ +return eval_int_with_arg (handle,"VPCR" , cmd, res); +} + +static int eval_vpcw (acpi_handle handle,unsigned long cmd,unsigned long data) +{ +struct acpi_object_list params; +union acpi_object in_obj[2 ]; +acpi_status status; + + params.count =2 ; + params.pointer = in_obj; + in_obj[0 ].type =ACPI_TYPE_INTEGER ; + in_obj[0 ].integer.value = cmd; + in_obj[1 ].type =ACPI_TYPE_INTEGER ; + in_obj[1 ].integer.value = data; + + status =acpi_evaluate_object (handle,"VPCW" , ¶ms,NULL ); +if (ACPI_FAILURE (status)) +return -EIO ; + +return 0 ; +} + +static int read_ec_data (acpi_handle handle,unsigned long cmd,unsigned long *data) +{ +unsigned long end_jiffies, val; +int err; + + err =eval_vpcw (handle,1 , cmd); +if (err) +return err; + + end_jiffies = jiffies +msecs_to_jiffies (IDEAPAD_EC_TIMEOUT ) +1 ; + +while (time_before (jiffies, end_jiffies)) { +schedule (); + + err =eval_vpcr (handle,1 , &val); +if (err) +return err; + +if (val ==0 ) +return eval_vpcr (handle,0 , data); + } + +acpi_handle_err (handle,"timeout in %s\n" , __func__); + +return -ETIMEDOUT ; +} + +static int write_ec_cmd (acpi_handle handle,unsigned long cmd,unsigned long data) +{ +unsigned long end_jiffies, val; +int err; + + err =eval_vpcw (handle,0 , data); +if (err) +return err; + + err =eval_vpcw (handle,1 , cmd); +if (err) +return err; + + end_jiffies = jiffies +msecs_to_jiffies (IDEAPAD_EC_TIMEOUT ) +1 ; + +while (time_before (jiffies, end_jiffies)) { +schedule (); + + err =eval_vpcr (handle,1 , &val); +if (err) +return err; + +if (val ==0 ) +return 0 ; + } + +acpi_handle_err (handle,"timeout in %s\n" , __func__); + +return -ETIMEDOUT ; +} +/* * debugfs */
drivers/platform/x86/ideapad-laptop.h +0 -139
149 lines not on this page · raw message
Signature · 1 line
2.45.2
PATCH v3 4/4 platform/x86: ideapad-laptop: add a mutex to synchronize VPC commands
In reply to Gergo KotelesCalling VPC commands consists of several VPCW and VPCR ACPI calls. These calls and their results can get mixed up if they are called simultaneously from different threads, like acpi notify handler, sysfs, debugfs, notification chain.
Add a mutex to synchronize VPC commands.
--- drivers/platform/x86/ideapad-laptop.c | 62 +++++++++++++++++++-------- 1 file changed, 45 insertions(+), 17 deletions(-)
drivers/platform/x86/ideapad-laptop.c +45 -17
@@ -155,6 +155,7 @@ struct ideapad_rfk_priv {struct ideapad_private {struct acpi_device *adev; +struct mutex vpc_mutex;/* protects the VPC calls */ struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM ];struct ideapad_rfk_priv rfk_priv[IDEAPAD_RFKILL_DEV_NUM ];struct platform_device *platform_device;
@@ -437,6 +438,8 @@ static int debugfs_status_show(struct seq_file *s, void *data)struct ideapad_private *priv = s->private;unsigned long value; +guard (mutex)(&priv->vpc_mutex); +if (!read_ec_data (priv->adev->handle,VPCCMD_R_BL_MAX , &value))seq_printf (s,"Backlight max: %lu\n" , value);if (!read_ec_data (priv->adev->handle,VPCCMD_R_BL , &value))
@@ -555,7 +558,8 @@ static ssize_t camera_power_show(struct device *dev,unsigned long result;int err; - err =read_ec_data (priv->adev->handle,VPCCMD_R_CAMERA , &result); +scoped_guard (mutex, &priv->vpc_mutex) + err = read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result);if (err)return err;
@@ -574,7 +578,8 @@ static ssize_t camera_power_store(struct device *dev,if (err)return err; - err =write_ec_cmd (priv->adev->handle,VPCCMD_W_CAMERA , state); +scoped_guard (mutex, &priv->vpc_mutex) + err = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state);if (err)return err;
@@ -627,7 +632,8 @@ static ssize_t fan_mode_show(struct device *dev,unsigned long result;int err; - err =read_ec_data (priv->adev->handle,VPCCMD_R_FAN , &result); +scoped_guard (mutex, &priv->vpc_mutex) + err = read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result);if (err)return err;
@@ -649,7 +655,8 @@ static ssize_t fan_mode_store(struct device *dev,if (state >4 || state ==3 )return -EINVAL ; - err =write_ec_cmd (priv->adev->handle,VPCCMD_W_FAN , state); +scoped_guard (mutex, &priv->vpc_mutex) + err = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state);if (err)return err;
@@ -734,7 +741,8 @@ static ssize_t touchpad_show(struct device *dev,unsigned long result;int err; - err =read_ec_data (priv->adev->handle,VPCCMD_R_TOUCHPAD , &result); +scoped_guard (mutex, &priv->vpc_mutex) + err = read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &result);if (err)return err;
@@ -755,7 +763,8 @@ static ssize_t touchpad_store(struct device *dev,if (err)return err; - err =write_ec_cmd (priv->adev->handle,VPCCMD_W_TOUCHPAD , state); +scoped_guard (mutex, &priv->vpc_mutex) + err = write_ec_cmd(priv->adev->handle, VPCCMD_W_TOUCHPAD, state);if (err)return err;
@@ -1148,6 +1157,8 @@ static int ideapad_rfk_set(void *data, bool blocked)struct ideapad_rfk_priv *priv = data;int opcode = ideapad_rfk_data[priv->dev].opcode; +guard (mutex)(&priv->priv->vpc_mutex); +return write_ec_cmd (priv->priv->adev->handle, opcode, !blocked); }
@@ -1161,6 +1172,8 @@ static void ideapad_sync_rfk_state(struct ideapad_private *priv)int i;if (priv->features.hw_rfkill_switch) { +guard (mutex)(&priv->vpc_mutex); +if (read_ec_data (priv->adev->handle,VPCCMD_R_RF , &hw_blocked))return ; hw_blocked = !hw_blocked;
@@ -1334,8 +1347,9 @@ static void ideapad_input_novokey(struct ideapad_private *priv) {unsigned long long_pressed; -if (read_ec_data (priv->adev->handle,VPCCMD_R_NOVO , &long_pressed)) -return ; + scoped_guard(mutex, &priv->vpc_mutex) +if (read_ec_data (priv->adev->handle,VPCCMD_R_NOVO , &long_pressed)) +return ;if (long_pressed)ideapad_input_report (priv,17 );
@@ -1347,8 +1361,9 @@ static void ideapad_check_special_buttons(struct ideapad_private *priv) {unsigned long bit, value; -if (read_ec_data (priv->adev->handle,VPCCMD_R_SPECIAL_BUTTONS , &value)) -return ; + scoped_guard(mutex, &priv->vpc_mutex) +if (read_ec_data (priv->adev->handle,VPCCMD_R_SPECIAL_BUTTONS , &value)) +return ; for_each_set_bit (bit, &value, 16) {switch (bit) {
@@ -1381,6 +1396,8 @@ static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)unsigned long now;int err; +guard (mutex)(&priv->vpc_mutex); + err =read_ec_data (priv->adev->handle,VPCCMD_R_BL , &now);if (err)return err;
@@ -1393,6 +1410,8 @@ static int ideapad_backlight_update_status(struct backlight_device *blightdev)struct ideapad_private *priv =bl_get_data (blightdev);int err; +guard (mutex)(&priv->vpc_mutex); + err =write_ec_cmd (priv->adev->handle,VPCCMD_W_BL , blightdev->props.brightness);if (err)
@@ -1470,6 +1489,8 @@ static void ideapad_backlight_notify_power(struct ideapad_private *priv)if (!blightdev)return ; +guard (mutex)(&priv->vpc_mutex); +if (read_ec_data (priv->adev->handle,VPCCMD_R_BL_POWER , &power))return ;
@@ -1482,7 +1503,8 @@ static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)/* if we control brightness via acpi video driver */ if (!priv->blightdev) -read_ec_data (priv->adev->handle,VPCCMD_R_BL , &now); + scoped_guard(mutex, &priv->vpc_mutex) +read_ec_data (priv->adev->handle,VPCCMD_R_BL , &now);else backlight_force_update (priv->blightdev,BACKLIGHT_UPDATE_HOTKEY ); }
@@ -1707,7 +1729,8 @@ static void ideapad_sync_touchpad_state(struct ideapad_private *priv, bool send_int ret;/* Without reading from EC touchpad LED doesn't switch state */ - ret =read_ec_data (priv->adev->handle,VPCCMD_R_TOUCHPAD , &value); +scoped_guard (mutex, &priv->vpc_mutex) + ret = read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value);if (ret)return ;
@@ -1767,7 +1790,8 @@ static void ideapad_laptop_trigger_ec(void)if (!priv->features.ymc_ec_trigger)return ; - ret =write_ec_cmd (priv->adev->handle,VPCCMD_W_YMC ,1 ); +scoped_guard (mutex, &priv->vpc_mutex) + ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_YMC, 1);if (ret)dev_warn (&priv->platform_device->dev,"Could not write YMC: %d\n" , ret); }
@@ -1813,11 +1837,13 @@ static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)struct ideapad_private *priv = data;unsigned long vpc1, vpc2, bit; -if (read_ec_data (handle,VPCCMD_R_VPC1 , &vpc1)) -return ; + scoped_guard(mutex, &priv->vpc_mutex) { +if (read_ec_data (handle,VPCCMD_R_VPC1 , &vpc1)) +return ; -if (read_ec_data (handle,VPCCMD_R_VPC2 , &vpc2)) -return ; +if (read_ec_data (handle,VPCCMD_R_VPC2 , &vpc2)) +return ; + } vpc1 = (vpc2 <<8 ) | vpc1;
@@ -2124,6 +2150,8 @@ static int ideapad_acpi_add(struct platform_device *pdev) priv->adev = adev; priv->platform_device = pdev; +devm_mutex_init (&pdev->dev, &priv->vpc_mutex); +ideapad_check_features (priv); err =ideapad_sysfs_init (priv);
Signature · 1 line
2.45.2