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

Input: drv2665 - 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-9-dmitry.torokhov@gmail.com
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

+21 -23
+21 -23
drivers/input/misc/drv2665.c
··· 225 225 static int drv2665_suspend(struct device *dev) 226 226 { 227 227 struct drv2665_data *haptics = dev_get_drvdata(dev); 228 - int ret = 0; 228 + int error; 229 229 230 - mutex_lock(&haptics->input_dev->mutex); 230 + guard(mutex)(&haptics->input_dev->mutex); 231 231 232 232 if (input_device_enabled(haptics->input_dev)) { 233 - ret = regmap_update_bits(haptics->regmap, DRV2665_CTRL_2, 234 - DRV2665_STANDBY, DRV2665_STANDBY); 235 - if (ret) { 233 + error = regmap_update_bits(haptics->regmap, DRV2665_CTRL_2, 234 + DRV2665_STANDBY, DRV2665_STANDBY); 235 + if (error) { 236 236 dev_err(dev, "Failed to set standby mode\n"); 237 237 regulator_disable(haptics->regulator); 238 - goto out; 238 + return error; 239 239 } 240 240 241 - ret = regulator_disable(haptics->regulator); 242 - if (ret) { 241 + error = regulator_disable(haptics->regulator); 242 + if (error) { 243 243 dev_err(dev, "Failed to disable regulator\n"); 244 244 regmap_update_bits(haptics->regmap, 245 245 DRV2665_CTRL_2, 246 246 DRV2665_STANDBY, 0); 247 + return error; 247 248 } 248 249 } 249 - out: 250 - mutex_unlock(&haptics->input_dev->mutex); 251 - return ret; 250 + 251 + return 0; 252 252 } 253 253 254 254 static int drv2665_resume(struct device *dev) 255 255 { 256 256 struct drv2665_data *haptics = dev_get_drvdata(dev); 257 - int ret = 0; 257 + int error; 258 258 259 - mutex_lock(&haptics->input_dev->mutex); 259 + guard(mutex)(&haptics->input_dev->mutex); 260 260 261 261 if (input_device_enabled(haptics->input_dev)) { 262 - ret = regulator_enable(haptics->regulator); 263 - if (ret) { 262 + error = regulator_enable(haptics->regulator); 263 + if (error) { 264 264 dev_err(dev, "Failed to enable regulator\n"); 265 - goto out; 265 + return error; 266 266 } 267 267 268 - ret = regmap_update_bits(haptics->regmap, DRV2665_CTRL_2, 269 - DRV2665_STANDBY, 0); 270 - if (ret) { 268 + error = regmap_update_bits(haptics->regmap, DRV2665_CTRL_2, 269 + DRV2665_STANDBY, 0); 270 + if (error) { 271 271 dev_err(dev, "Failed to unset standby mode\n"); 272 272 regulator_disable(haptics->regulator); 273 - goto out; 273 + return error; 274 274 } 275 275 276 276 } 277 277 278 - out: 279 - mutex_unlock(&haptics->input_dev->mutex); 280 - return ret; 278 + return 0; 281 279 } 282 280 283 281 static DEFINE_SIMPLE_DEV_PM_OPS(drv2665_pm_ops, drv2665_suspend, drv2665_resume);