Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Input: drv2667 - use guard notation when acquiring mutex

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.

Reviewed-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>
Link: https://lore.kernel.org/r/20240904044244.1042174-10-dmitry.torokhov@gmail.com
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

+21 -23
+21 -23
drivers/input/misc/drv2667.c
··· 402 402 static int drv2667_suspend(struct device *dev) 403 403 { 404 404 struct drv2667_data *haptics = dev_get_drvdata(dev); 405 - int ret = 0; 405 + int error; 406 406 407 - mutex_lock(&haptics->input_dev->mutex); 407 + guard(mutex)(&haptics->input_dev->mutex); 408 408 409 409 if (input_device_enabled(haptics->input_dev)) { 410 - ret = regmap_update_bits(haptics->regmap, DRV2667_CTRL_2, 411 - DRV2667_STANDBY, DRV2667_STANDBY); 412 - if (ret) { 410 + error = regmap_update_bits(haptics->regmap, DRV2667_CTRL_2, 411 + DRV2667_STANDBY, DRV2667_STANDBY); 412 + if (error) { 413 413 dev_err(dev, "Failed to set standby mode\n"); 414 414 regulator_disable(haptics->regulator); 415 - goto out; 415 + return error; 416 416 } 417 417 418 - ret = regulator_disable(haptics->regulator); 419 - if (ret) { 418 + error = regulator_disable(haptics->regulator); 419 + if (error) { 420 420 dev_err(dev, "Failed to disable regulator\n"); 421 421 regmap_update_bits(haptics->regmap, 422 422 DRV2667_CTRL_2, 423 423 DRV2667_STANDBY, 0); 424 + return error; 424 425 } 425 426 } 426 - out: 427 - mutex_unlock(&haptics->input_dev->mutex); 428 - return ret; 427 + 428 + return 0; 429 429 } 430 430 431 431 static int drv2667_resume(struct device *dev) 432 432 { 433 433 struct drv2667_data *haptics = dev_get_drvdata(dev); 434 - int ret = 0; 434 + int error; 435 435 436 - mutex_lock(&haptics->input_dev->mutex); 436 + guard(mutex)(&haptics->input_dev->mutex); 437 437 438 438 if (input_device_enabled(haptics->input_dev)) { 439 - ret = regulator_enable(haptics->regulator); 440 - if (ret) { 439 + error = regulator_enable(haptics->regulator); 440 + if (error) { 441 441 dev_err(dev, "Failed to enable regulator\n"); 442 - goto out; 442 + return error; 443 443 } 444 444 445 - ret = regmap_update_bits(haptics->regmap, DRV2667_CTRL_2, 446 - DRV2667_STANDBY, 0); 447 - if (ret) { 445 + error = regmap_update_bits(haptics->regmap, DRV2667_CTRL_2, 446 + DRV2667_STANDBY, 0); 447 + if (error) { 448 448 dev_err(dev, "Failed to unset standby mode\n"); 449 449 regulator_disable(haptics->regulator); 450 - goto out; 450 + return error; 451 451 } 452 452 453 453 } 454 454 455 - out: 456 - mutex_unlock(&haptics->input_dev->mutex); 457 - return ret; 455 + return 0; 458 456 } 459 457 460 458 static DEFINE_SIMPLE_DEV_PM_OPS(drv2667_pm_ops, drv2667_suspend, drv2667_resume);