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

Input: adp5589-keys - use guard notation when acquiring mutex

This 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.

Link: https://lore.kernel.org/r/20240825051627.2848495-2-dmitry.torokhov@gmail.com
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

+18 -21
+18 -21
drivers/input/keyboard/adp5589-keys.c
··· 404 404 unsigned int bank = kpad->var->bank(kpad->gpiomap[off]); 405 405 unsigned int bit = kpad->var->bit(kpad->gpiomap[off]); 406 406 407 - mutex_lock(&kpad->gpio_lock); 407 + guard(mutex)(&kpad->gpio_lock); 408 408 409 409 if (val) 410 410 kpad->dat_out[bank] |= bit; ··· 413 413 414 414 adp5589_write(kpad->client, kpad->var->reg(ADP5589_GPO_DATA_OUT_A) + 415 415 bank, kpad->dat_out[bank]); 416 - 417 - mutex_unlock(&kpad->gpio_lock); 418 416 } 419 417 420 418 static int adp5589_gpio_direction_input(struct gpio_chip *chip, unsigned off) ··· 420 422 struct adp5589_kpad *kpad = gpiochip_get_data(chip); 421 423 unsigned int bank = kpad->var->bank(kpad->gpiomap[off]); 422 424 unsigned int bit = kpad->var->bit(kpad->gpiomap[off]); 423 - int ret; 424 425 425 - mutex_lock(&kpad->gpio_lock); 426 + guard(mutex)(&kpad->gpio_lock); 426 427 427 428 kpad->dir[bank] &= ~bit; 428 - ret = adp5589_write(kpad->client, 429 - kpad->var->reg(ADP5589_GPIO_DIRECTION_A) + bank, 430 - kpad->dir[bank]); 431 - 432 - mutex_unlock(&kpad->gpio_lock); 433 - 434 - return ret; 429 + return adp5589_write(kpad->client, 430 + kpad->var->reg(ADP5589_GPIO_DIRECTION_A) + bank, 431 + kpad->dir[bank]); 435 432 } 436 433 437 434 static int adp5589_gpio_direction_output(struct gpio_chip *chip, ··· 435 442 struct adp5589_kpad *kpad = gpiochip_get_data(chip); 436 443 unsigned int bank = kpad->var->bank(kpad->gpiomap[off]); 437 444 unsigned int bit = kpad->var->bit(kpad->gpiomap[off]); 438 - int ret; 445 + int error; 439 446 440 - mutex_lock(&kpad->gpio_lock); 447 + guard(mutex)(&kpad->gpio_lock); 441 448 442 449 kpad->dir[bank] |= bit; 443 450 ··· 446 453 else 447 454 kpad->dat_out[bank] &= ~bit; 448 455 449 - ret = adp5589_write(kpad->client, kpad->var->reg(ADP5589_GPO_DATA_OUT_A) 450 - + bank, kpad->dat_out[bank]); 451 - ret |= adp5589_write(kpad->client, 452 - kpad->var->reg(ADP5589_GPIO_DIRECTION_A) + bank, 453 - kpad->dir[bank]); 456 + error = adp5589_write(kpad->client, 457 + kpad->var->reg(ADP5589_GPO_DATA_OUT_A) + bank, 458 + kpad->dat_out[bank]); 459 + if (error) 460 + return error; 454 461 455 - mutex_unlock(&kpad->gpio_lock); 462 + error = adp5589_write(kpad->client, 463 + kpad->var->reg(ADP5589_GPIO_DIRECTION_A) + bank, 464 + kpad->dir[bank]); 465 + if (error) 466 + return error; 456 467 457 - return ret; 468 + return 0; 458 469 } 459 470 460 471 static int adp5589_build_gpiomap(struct adp5589_kpad *kpad,