Skip to content

Commit 78af00d

Browse files
committed
Input: tegra-kbc - use guard notation when acquiring mutex and spinlock
This makes the code more compact and error handling more robust by ensuring that locks are released in all code paths when control leaves critical section. Acked-by: Thierry Reding <treding@nvidia.com> Link: https://lore.kernel.org/r/20240825051627.2848495-18-dmitry.torokhov@gmail.com Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
1 parent 22df245 commit 78af00d

1 file changed

Lines changed: 19 additions & 26 deletions

File tree

drivers/input/keyboard/tegra-kbc.c

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,10 @@ static void tegra_kbc_set_fifo_interrupt(struct tegra_kbc *kbc, bool enable)
241241
static void tegra_kbc_keypress_timer(struct timer_list *t)
242242
{
243243
struct tegra_kbc *kbc = from_timer(kbc, t, timer);
244-
unsigned long flags;
245244
u32 val;
246245
unsigned int i;
247246

248-
spin_lock_irqsave(&kbc->lock, flags);
247+
guard(spinlock_irqsave)(&kbc->lock);
249248

250249
val = (readl(kbc->mmio + KBC_INT_0) >> 4) & 0xf;
251250
if (val) {
@@ -270,17 +269,14 @@ static void tegra_kbc_keypress_timer(struct timer_list *t)
270269
/* All keys are released so enable the keypress interrupt */
271270
tegra_kbc_set_fifo_interrupt(kbc, true);
272271
}
273-
274-
spin_unlock_irqrestore(&kbc->lock, flags);
275272
}
276273

277274
static irqreturn_t tegra_kbc_isr(int irq, void *args)
278275
{
279276
struct tegra_kbc *kbc = args;
280-
unsigned long flags;
281277
u32 val;
282278

283-
spin_lock_irqsave(&kbc->lock, flags);
279+
guard(spinlock_irqsave)(&kbc->lock);
284280

285281
/*
286282
* Quickly bail out & reenable interrupts if the fifo threshold
@@ -301,8 +297,6 @@ static irqreturn_t tegra_kbc_isr(int irq, void *args)
301297
kbc->keypress_caused_wake = true;
302298
}
303299

304-
spin_unlock_irqrestore(&kbc->lock, flags);
305-
306300
return IRQ_HANDLED;
307301
}
308302

@@ -413,14 +407,13 @@ static int tegra_kbc_start(struct tegra_kbc *kbc)
413407

414408
static void tegra_kbc_stop(struct tegra_kbc *kbc)
415409
{
416-
unsigned long flags;
417410
u32 val;
418411

419-
spin_lock_irqsave(&kbc->lock, flags);
420-
val = readl(kbc->mmio + KBC_CONTROL_0);
421-
val &= ~1;
422-
writel(val, kbc->mmio + KBC_CONTROL_0);
423-
spin_unlock_irqrestore(&kbc->lock, flags);
412+
scoped_guard(spinlock_irqsave, &kbc->lock) {
413+
val = readl(kbc->mmio + KBC_CONTROL_0);
414+
val &= ~1;
415+
writel(val, kbc->mmio + KBC_CONTROL_0);
416+
}
424417

425418
disable_irq(kbc->irq);
426419
del_timer_sync(&kbc->timer);
@@ -724,7 +717,8 @@ static int tegra_kbc_suspend(struct device *dev)
724717
struct platform_device *pdev = to_platform_device(dev);
725718
struct tegra_kbc *kbc = platform_get_drvdata(pdev);
726719

727-
mutex_lock(&kbc->idev->mutex);
720+
guard(mutex)(&kbc->idev->mutex);
721+
728722
if (device_may_wakeup(&pdev->dev)) {
729723
disable_irq(kbc->irq);
730724
del_timer_sync(&kbc->timer);
@@ -747,11 +741,9 @@ static int tegra_kbc_suspend(struct device *dev)
747741
tegra_kbc_set_keypress_interrupt(kbc, true);
748742
enable_irq(kbc->irq);
749743
enable_irq_wake(kbc->irq);
750-
} else {
751-
if (input_device_enabled(kbc->idev))
752-
tegra_kbc_stop(kbc);
744+
} else if (input_device_enabled(kbc->idev)) {
745+
tegra_kbc_stop(kbc);
753746
}
754-
mutex_unlock(&kbc->idev->mutex);
755747

756748
return 0;
757749
}
@@ -760,9 +752,10 @@ static int tegra_kbc_resume(struct device *dev)
760752
{
761753
struct platform_device *pdev = to_platform_device(dev);
762754
struct tegra_kbc *kbc = platform_get_drvdata(pdev);
763-
int err = 0;
755+
int err;
756+
757+
guard(mutex)(&kbc->idev->mutex);
764758

765-
mutex_lock(&kbc->idev->mutex);
766759
if (device_may_wakeup(&pdev->dev)) {
767760
disable_irq_wake(kbc->irq);
768761
tegra_kbc_setup_wakekeys(kbc, false);
@@ -787,13 +780,13 @@ static int tegra_kbc_resume(struct device *dev)
787780
input_report_key(kbc->idev, kbc->wakeup_key, 0);
788781
input_sync(kbc->idev);
789782
}
790-
} else {
791-
if (input_device_enabled(kbc->idev))
792-
err = tegra_kbc_start(kbc);
783+
} else if (input_device_enabled(kbc->idev)) {
784+
err = tegra_kbc_start(kbc);
785+
if (err)
786+
return err;
793787
}
794-
mutex_unlock(&kbc->idev->mutex);
795788

796-
return err;
789+
return 0;
797790
}
798791

799792
static DEFINE_SIMPLE_DEV_PM_OPS(tegra_kbc_pm_ops,

0 commit comments

Comments
 (0)