diff mbox series

[16/22] Input: max8997_haptic - use guard notation when acquiring mutex

Message ID 20240904044834.1048468-1-dmitry.torokhov@gmail.com (mailing list archive)
State New
Headers show
Series Convert misc input drivers to use new cleanup facilities | expand

Commit Message

Dmitry Torokhov Sept. 4, 2024, 4:48 a.m. UTC
Using guard notation makes the code more compact and error handling
more robust by ensuring that mutexes are released in all code paths
when control leaves critical section.

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 drivers/input/misc/max8997_haptic.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

Comments

Javier Carrasco Sept. 4, 2024, 7:12 p.m. UTC | #1
On 04/09/2024 06:48, Dmitry Torokhov wrote:
> Using guard notation makes the code more compact and error handling
> more robust by ensuring that mutexes are released in all code paths
> when control leaves critical section.
> 
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> ---
>  drivers/input/misc/max8997_haptic.c | 15 +++++----------
>  1 file changed, 5 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/input/misc/max8997_haptic.c b/drivers/input/misc/max8997_haptic.c
> index 11cac4b7dddc..2853455daef2 100644
> --- a/drivers/input/misc/max8997_haptic.c
> +++ b/drivers/input/misc/max8997_haptic.c
> @@ -153,19 +153,19 @@ static void max8997_haptic_enable(struct max8997_haptic *chip)
>  {
>  	int error;
>  
> -	mutex_lock(&chip->mutex);
> +	guard(mutex)(&chip->mutex);
>  
>  	error = max8997_haptic_set_duty_cycle(chip);
>  	if (error) {
>  		dev_err(chip->dev, "set_pwm_cycle failed, error: %d\n", error);
> -		goto out;
> +		return;
>  	}
>  
>  	if (!chip->enabled) {
>  		error = regulator_enable(chip->regulator);
>  		if (error) {
>  			dev_err(chip->dev, "Failed to enable regulator\n");
> -			goto out;
> +			return;
>  		}
>  		max8997_haptic_configure(chip);
>  		if (chip->mode == MAX8997_EXTERNAL_MODE) {
> @@ -173,19 +173,16 @@ static void max8997_haptic_enable(struct max8997_haptic *chip)
>  			if (error) {
>  				dev_err(chip->dev, "Failed to enable PWM\n");
>  				regulator_disable(chip->regulator);
> -				goto out;
> +				return;
>  			}
>  		}
>  		chip->enabled = true;
>  	}
> -
> -out:
> -	mutex_unlock(&chip->mutex);
>  }
>  
>  static void max8997_haptic_disable(struct max8997_haptic *chip)
>  {
> -	mutex_lock(&chip->mutex);
> +	guard(mutex)(&chip->mutex);
>  
>  	if (chip->enabled) {
>  		chip->enabled = false;
> @@ -194,8 +191,6 @@ static void max8997_haptic_disable(struct max8997_haptic *chip)
>  			pwm_disable(chip->pwm);
>  		regulator_disable(chip->regulator);
>  	}
> -
> -	mutex_unlock(&chip->mutex);
>  }
>  
>  static void max8997_haptic_play_effect_work(struct work_struct *work)

Reviewed-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>
diff mbox series

Patch

diff --git a/drivers/input/misc/max8997_haptic.c b/drivers/input/misc/max8997_haptic.c
index 11cac4b7dddc..2853455daef2 100644
--- a/drivers/input/misc/max8997_haptic.c
+++ b/drivers/input/misc/max8997_haptic.c
@@ -153,19 +153,19 @@  static void max8997_haptic_enable(struct max8997_haptic *chip)
 {
 	int error;
 
-	mutex_lock(&chip->mutex);
+	guard(mutex)(&chip->mutex);
 
 	error = max8997_haptic_set_duty_cycle(chip);
 	if (error) {
 		dev_err(chip->dev, "set_pwm_cycle failed, error: %d\n", error);
-		goto out;
+		return;
 	}
 
 	if (!chip->enabled) {
 		error = regulator_enable(chip->regulator);
 		if (error) {
 			dev_err(chip->dev, "Failed to enable regulator\n");
-			goto out;
+			return;
 		}
 		max8997_haptic_configure(chip);
 		if (chip->mode == MAX8997_EXTERNAL_MODE) {
@@ -173,19 +173,16 @@  static void max8997_haptic_enable(struct max8997_haptic *chip)
 			if (error) {
 				dev_err(chip->dev, "Failed to enable PWM\n");
 				regulator_disable(chip->regulator);
-				goto out;
+				return;
 			}
 		}
 		chip->enabled = true;
 	}
-
-out:
-	mutex_unlock(&chip->mutex);
 }
 
 static void max8997_haptic_disable(struct max8997_haptic *chip)
 {
-	mutex_lock(&chip->mutex);
+	guard(mutex)(&chip->mutex);
 
 	if (chip->enabled) {
 		chip->enabled = false;
@@ -194,8 +191,6 @@  static void max8997_haptic_disable(struct max8997_haptic *chip)
 			pwm_disable(chip->pwm);
 		regulator_disable(chip->regulator);
 	}
-
-	mutex_unlock(&chip->mutex);
 }
 
 static void max8997_haptic_play_effect_work(struct work_struct *work)