iInput: adp5588-keys - use guard notation when acquiring mutexes
Dmitry Torokhov
maintainerPATCH 1/1 iInput: adp5588-keys - use guard notation when acquiring mutexes
This makes the code more compact and error handling more robust.
---
Sending this out because Utsav is working on the driver so he can rebase the work on top of this.
drivers/input/keyboard/adp5588-keys.c | 49 ++++++++++----------------- 1 file changed, 17 insertions(+), 32 deletions(-)
drivers/input/keyboard/adp5588-keys.c +17 -32
@@ -221,15 +221,13 @@ static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned int off)unsigned int bit =ADP5588_BIT (kpad->gpiomap[off]);int val; -mutex_lock (&kpad->gpio_lock); +guard (mutex)(&kpad->gpio_lock);if (kpad->dir[bank] & bit) val = kpad->dat_out[bank];else val =adp5588_read (kpad->client,GPIO_DAT_STAT1 + bank); -mutex_unlock (&kpad->gpio_lock); -return !!(val & bit); }
@@ -240,7 +238,7 @@ static void adp5588_gpio_set_value(struct gpio_chip *chip,unsigned int bank =ADP5588_BANK (kpad->gpiomap[off]);unsigned int bit =ADP5588_BIT (kpad->gpiomap[off]); -mutex_lock (&kpad->gpio_lock); +guard (mutex)(&kpad->gpio_lock);if (val) kpad->dat_out[bank] |= bit;
@@ -248,8 +246,6 @@ static void adp5588_gpio_set_value(struct gpio_chip *chip, kpad->dat_out[bank] &= ~bit;adp5588_write (kpad->client,GPIO_DAT_OUT1 + bank, kpad->dat_out[bank]); - -mutex_unlock (&kpad->gpio_lock); } static int adp5588_gpio_set_config(struct gpio_chip *chip, unsigned int off,
@@ -259,7 +255,6 @@ static int adp5588_gpio_set_config(struct gpio_chip *chip, unsigned int off,unsigned int bank =ADP5588_BANK (kpad->gpiomap[off]);unsigned int bit =ADP5588_BIT (kpad->gpiomap[off]);bool pull_disable; -int ret;switch (pinconf_to_config_param (config)) {case PIN_CONFIG_BIAS_PULL_UP :
@@ -272,19 +267,15 @@ static int adp5588_gpio_set_config(struct gpio_chip *chip, unsigned int off,return -ENOTSUPP ; } -mutex_lock (&kpad->gpio_lock); +guard (mutex)(&kpad->gpio_lock);if (pull_disable) kpad->pull_dis[bank] |= bit;else kpad->pull_dis[bank] &= bit; - ret =adp5588_write (kpad->client,GPIO_PULL1 + bank, - kpad->pull_dis[bank]); - -mutex_unlock (&kpad->gpio_lock); - -return ret; +return adp5588_write (kpad->client,GPIO_PULL1 + bank, + kpad->pull_dis[bank]); }static int adp5588_gpio_direction_input (struct gpio_chip *chip,unsigned int off)
@@ -292,16 +283,11 @@ static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned int offstruct adp5588_kpad *kpad =gpiochip_get_data (chip);unsigned int bank =ADP5588_BANK (kpad->gpiomap[off]);unsigned int bit =ADP5588_BIT (kpad->gpiomap[off]); -int ret; -mutex_lock (&kpad->gpio_lock); +guard (mutex)(&kpad->gpio_lock); kpad->dir[bank] &= ~bit; - ret =adp5588_write (kpad->client,GPIO_DIR1 + bank, kpad->dir[bank]); - -mutex_unlock (&kpad->gpio_lock); - -return ret; +return adp5588_write (kpad->client,GPIO_DIR1 + bank, kpad->dir[bank]); } static int adp5588_gpio_direction_output(struct gpio_chip *chip,
@@ -310,9 +296,9 @@ static int adp5588_gpio_direction_output(struct gpio_chip *chip,struct adp5588_kpad *kpad =gpiochip_get_data (chip);unsigned int bank =ADP5588_BANK (kpad->gpiomap[off]);unsigned int bit =ADP5588_BIT (kpad->gpiomap[off]); -int ret; +int error; -mutex_lock (&kpad->gpio_lock); +guard (mutex)(&kpad->gpio_lock); kpad->dir[bank] |= bit;
@@ -321,17 +307,16 @@ static int adp5588_gpio_direction_output(struct gpio_chip *chip,else kpad->dat_out[bank] &= ~bit; - ret =adp5588_write (kpad->client,GPIO_DAT_OUT1 + bank, - kpad->dat_out[bank]); -if (ret) - goto out_unlock; - - ret =adp5588_write (kpad->client,GPIO_DIR1 + bank, kpad->dir[bank]); + error =adp5588_write (kpad->client,GPIO_DAT_OUT1 + bank, + kpad->dat_out[bank]); +if (error) +return error; -out_unlock: -mutex_unlock (&kpad->gpio_lock); + error =adp5588_write (kpad->client,GPIO_DIR1 + bank, kpad->dir[bank]); +if (error) +return error; -return ret; +return 0 ; }static int adp5588_build_gpiomap (struct adp5588_kpad *kpad)
Signature · 5 lines
2.45.2.803.g4e1b14247a-goog -- Dmitry
Hello Dmitry,
$Subject ~= s/iI/I/
Quoted 101 lines · Dmitry Torokhov
This makes the code more compact and error handling more robust.
Signed-off-by Dmitry Torokhov <[email protected]>---
Sending this out because Utsav is working on the driver so he can rebase the work on top of this.
drivers/input/keyboard/adp5588-keys.c | 49 ++++++++++----------------- 1 file changed, 17 insertions(+), 32 deletions(-)drivers/input/keyboard/adp5588-keys.c +7
-21@@ -221,15 +221,13 @@ static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned int off)unsigned int bit =ADP5588_BIT (kpad->gpiomap[off]);int val; -mutex_lock (&kpad->gpio_lock); +guard (mutex)(&kpad->gpio_lock);if (kpad->dir[bank] & bit) val = kpad->dat_out[bank];else val =adp5588_read (kpad->client,GPIO_DAT_STAT1 + bank); -mutex_unlock (&kpad->gpio_lock); -return !!(val & bit); }@@ -240,7 +238,7 @@ static void adp5588_gpio_set_value(struct gpio_chip *chip,unsigned int bank =ADP5588_BANK (kpad->gpiomap[off]);unsigned int bit =ADP5588_BIT (kpad->gpiomap[off]); -mutex_lock (&kpad->gpio_lock); +guard (mutex)(&kpad->gpio_lock);if (val) kpad->dat_out[bank] |= bit;@@ -248,8 +246,6 @@ static void adp5588_gpio_set_value(struct gpio_chip *chip, kpad->dat_out[bank] &= ~bit;adp5588_write (kpad->client,GPIO_DAT_OUT1 + bank, kpad->dat_out[bank]); - -mutex_unlock (&kpad->gpio_lock); } static int adp5588_gpio_set_config(struct gpio_chip *chip, unsigned int off,@@ -259,7 +255,6 @@ static int adp5588_gpio_set_config(struct gpio_chip *chip, unsigned int off,unsigned int bank =ADP5588_BANK (kpad->gpiomap[off]);unsigned int bit =ADP5588_BIT (kpad->gpiomap[off]);bool pull_disable; -int ret;switch (pinconf_to_config_param (config)) {case PIN_CONFIG_BIAS_PULL_UP :@@ -272,19 +267,15 @@ static int adp5588_gpio_set_config(struct gpio_chip *chip, unsigned int off,return -ENOTSUPP ; } -mutex_lock (&kpad->gpio_lock); +guard (mutex)(&kpad->gpio_lock);if (pull_disable) kpad->pull_dis[bank] |= bit;else kpad->pull_dis[bank] &= bit; - ret =adp5588_write (kpad->client,GPIO_PULL1 + bank, - kpad->pull_dis[bank]); - -mutex_unlock (&kpad->gpio_lock); - -return ret; +return adp5588_write (kpad->client,GPIO_PULL1 + bank, + kpad->pull_dis[bank]); }static int adp5588_gpio_direction_input (struct gpio_chip *chip,unsigned int off)@@ -292,16 +283,11 @@ static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned int offstruct adp5588_kpad *kpad =gpiochip_get_data (chip);unsigned int bank =ADP5588_BANK (kpad->gpiomap[off]);unsigned int bit =ADP5588_BIT (kpad->gpiomap[off]); -int ret; -mutex_lock (&kpad->gpio_lock); +guard (mutex)(&kpad->gpio_lock); kpad->dir[bank] &= ~bit; - ret =adp5588_write (kpad->client,GPIO_DIR1 + bank, kpad->dir[bank]); - -mutex_unlock (&kpad->gpio_lock); - -return ret; +return adp5588_write (kpad->client,GPIO_DIR1 + bank, kpad->dir[bank]); } static int adp5588_gpio_direction_output(struct gpio_chip *chip,
drivers/input/keyboard/adp5588-keys.c static int adp5588_gpio_direction_output(struct gpio_chip *chip, · line 299
Quoted 5 lines
drivers/input/keyboard/adp5588-keys.c +1
-1@@ -310,9 +296,9 @@ static int adp5588_gpio_direction_output(struct gpio_chip *chip,struct adp5588_kpad *kpad =gpiochip_get_data (chip);unsigned int bank =ADP5588_BANK (kpad->gpiomap[off]);unsigned int bit =ADP5588_BIT (kpad->gpiomap[off]); -int ret; +int error;
If you keep ret it's consistent with the other functions in this driver (at least the one you touched above).
Otherwise looks fine to me,
Uwe
Dmitry Torokhov
maintainerQuoted 3 lines · Uwe Kleine-König
Hello Dmitry,
$Subject ~= s/iI/I/
Oops, vim strikes again ;)
Quoted 11 lines
drivers/input/keyboard/adp5588-keys.c static int adp5588_gpio_direction_output(struct gpio_chip *chip, · line 299
Quoted 8 lines
static int adp5588_gpio_direction_output(struct
gpio_chip*chip,drivers/input/keyboard/adp5588-keys.c +1
-1@@ -310,9 +296,9 @@ static int adp5588_gpio_direction_output(struct gpio_chip *chip,struct adp5588_kpad *kpad =gpiochip_get_data (chip);unsigned int bank =ADP5588_BANK (kpad->gpiomap[off]);unsigned int bit =ADP5588_BIT (kpad->gpiomap[off]); -int ret; +int error;If you keep ret it's consistent with the other functions in this driver (at least the one you touched above).
This is part of my quest to have variables that hold error codes (i.e. when we do not return them in case of success) to be called "error". "ret" or "retval" is to be used when it is returned in both error and success cases; before the guard notation we needed to use the same variable in both success and failure.
Quoted 2 lines
Otherwise looks fine to me,
Thanks for the review!
Signature · 1 line
Dmitry
Quoted 14 lines
-----Original Message-----
From: Dmitry Torokhov <[email protected]>
Sent: Monday, July 1, 2024 7:57 PM
To: Hennerich, Michael <[email protected]>; Agarwal, Utsav <[email protected]>; Nuno Sá <[email protected]>
Cc: Christophe JAILLET <[email protected]>; Uwe Kleine-König <[email protected]>; [email protected]; linux- [email protected]
Subject: [PATCH] iInput: adp5588-keys - use guard notation when acquiring mutexes
Quoted 3 lines
This makes the code more compact and error handling more robust.
Signed-off-by Dmitry Torokhov <[email protected]>
Quoted 152 lines
---
Sending this out because Utsav is working on the driver so he can rebase the work on top of this.
drivers/input/keyboard/adp5588-keys.c | 49 ++++++++++----------------- 1 file changed, 17 insertions(+), 32 deletions(-)drivers/input/keyboard/adp5588-keys.c +2
-4@@ -221,15 +221,13 @@ static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned int off)unsigned int bit =ADP5588_BIT (kpad->gpiomap[off]);int val; -mutex_lock (&kpad->gpio_lock); +guard (mutex)(&kpad->gpio_lock);if (kpad->dir[bank] & bit) val = kpad->dat_out[bank];else val =adp5588_read (kpad->client,GPIO_DAT_STAT1 + bank); -mutex_unlock (&kpad->gpio_lock); -return !!(val & bit); }@@ -240,7 +238,7 @@ static void adp5588_gpio_set_value(struct gpio_chip *chip, unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);unsigned int bit =ADP5588_BIT (kpad->gpiomap[off]); -mutex_lock (&kpad->gpio_lock); +guard (mutex)(&kpad->gpio_lock);if (val)drivers/input/keyboard/adp5588-keys.c +0
-2kpad->dat_out[bank] |= bit;@@ -248,8 +246,6 @@ static void adp5588_gpio_set_value(struct gpio_chip *chip, kpad->dat_out[bank] &= ~bit;adp5588_write (kpad->client,GPIO_DAT_OUT1 + bank, kpad- >dat_out[bank]); - -mutex_unlock (&kpad->gpio_lock); }static int adp5588_gpio_set_config(struct
gpio_chip*chip, unsigned int off,drivers/input/keyboard/adp5588-keys.c +0
-1@@ -259,7 +255,6 @@ static int adp5588_gpio_set_config(struct gpio_chip *chip, unsigned int off, unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);unsigned int bit =ADP5588_BIT (kpad->gpiomap[off]);bool pull_disable; -int ret;switch (pinconf_to_config_param (config)) {drivers/input/keyboard/adp5588-keys.c +3
-7case PIN_CONFIG_BIAS_PULL_UP :@@ -272,19 +267,15 @@ static int adp5588_gpio_set_config(struct gpio_chip *chip, unsigned int off, return -ENOTSUPP ; } -mutex_lock (&kpad->gpio_lock); +guard (mutex)(&kpad->gpio_lock);if (pull_disable) kpad->pull_dis[bank] |= bit;else kpad->pull_dis[bank] &= bit; - ret =adp5588_write (kpad->client,GPIO_PULL1 + bank, - kpad->pull_dis[bank]); - -mutex_unlock (&kpad->gpio_lock); - -return ret; +return adp5588_write (kpad->client,GPIO_PULL1 + bank, + kpad->pull_dis[bank]); }static int adp5588_gpio_direction_input(struct
gpio_chip*chip, unsigned int
off) @@ -292,16 +283,11 @@ static int adp5588_gpio_direction_input(structgpio_chip*chip, unsigned int offdrivers/input/keyboard/adp5588-keys.c +2
-7struct adp5588_kpad *kpad =gpiochip_get_data (chip);unsigned int bank =ADP5588_BANK (kpad->gpiomap[off]);unsigned int bit =ADP5588_BIT (kpad->gpiomap[off]); -int ret; -mutex_lock (&kpad->gpio_lock); +guard (mutex)(&kpad->gpio_lock); kpad->dir[bank] &= ~bit; - ret =adp5588_write (kpad->client,GPIO_DIR1 + bank, kpad->dir[bank]); - -mutex_unlock (&kpad->gpio_lock); - -return ret; + return adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad-Quoted 1 line
dir[bank]);
}
static int adp5588_gpio_direction_output(struct
gpio_chip*chip, @@ -310,9
+296,9 @@ static int adp5588_gpio_direction_output(structgpio_chip*chip,drivers/input/keyboard/adp5588-keys.c +10
-11struct adp5588_kpad *kpad =gpiochip_get_data (chip);unsigned int bank =ADP5588_BANK (kpad->gpiomap[off]);unsigned int bit =ADP5588_BIT (kpad->gpiomap[off]); -int ret; +int error; -mutex_lock (&kpad->gpio_lock); +guard (mutex)(&kpad->gpio_lock); kpad->dir[bank] |= bit;@@ -321,17 +307,16 @@ static int adp5588_gpio_direction_output(structgpio_chip *chip, else kpad->dat_out[bank] &= ~bit; - ret =adp5588_write (kpad->client,GPIO_DAT_OUT1 + bank, - kpad->dat_out[bank]); -if (ret) - goto out_unlock; - - ret =adp5588_write (kpad->client,GPIO_DIR1 + bank, kpad->dir[bank]); + error =adp5588_write (kpad->client,GPIO_DAT_OUT1 + bank, + kpad->dat_out[bank]); +if (error) +return error; -out_unlock: -mutex_unlock (&kpad->gpio_lock); + error =adp5588_write (kpad->client,GPIO_DIR1 + bank, kpad- >dir[bank]); +if (error) +return error; -return ret; +return 0 ; }static int
adp5588_build_gpiomap(struct adp5588_kpad *kpad)
--
2.45.2.803.g4e1b14247a-goog--
Dmitry
Hello Dmitry,
Quoted 9 lines · Dmitry Torokhov
Quoted 2 lines · Uwe Kleine-König
If you keep ret it's consistent with the other functions in this driver (at least the one you touched above).
This is part of my quest to have variables that hold error codes (i.e. when we do not return them in case of success) to be called "error". "ret" or "retval" is to be used when it is returned in both error and success cases; before the guard notation we needed to use the same variable in both success and failure.
That seems to be a bit artificial to me, given that you changed the variable "ret" in adp5588_gpio_direction_output() to "error" and the resulting code can be further simplified using
drivers/input/keyboard/adp5588-keys.c +1 -3
@@ -313,10 +313,8 @@ static int adp5588_gpio_direction_output(struct gpio_chip *chip,return error; error =adp5588_write (kpad->client,GPIO_DIR1 + bank, kpad->dir[bank]); -if (error) -return error; -return 0 ; +return error; }static int adp5588_build_gpiomap (struct adp5588_kpad *kpad)
There are further inconsistencies in this driver, e.g. ret in adp5588_fw_parse() isn't returned in the success path. adp5588_probe() has both, error and ret, neither is used in the success path and a single variable would be enough.
Given that, I'd suggest to make two commits: Let the driver use guards without unmentioned and incomplete cleanups in the variable naming and then in an additional changeset unify the error variable naming in the complete driver.
Best regards
Uwe
Re: [PATCH] Input: adp5588-keys - use guard notation when acquiring mutexes
In reply to Dmitry TorokhovQuoted 1 line
This makes the code more compact and error handling more robust.
I suggest to improve such a change description another bit.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.10-rc6#n45
Regards,
Markus