[PATCH 2/2] Input: tca8418_keypad - fix race condition in interrupt handler
From: Zhian Liang
Date: Thu Sep 03 2026 - 15:44:38 EST
There is a race condition in tca8418_irq_handler() where a new key event
can be lost if it arrives after the FIFO is drained but before the
interrupt status register is cleared.
Fix this by re-reading INT_STAT after draining the FIFO and repeating
the process if K_INT is still asserted. This ensures the FIFO is truly
empty before clearing interrupts.
The FIFO depth is 10 events, so a loop limit of 16 provides sufficient
margin while preventing infinite loops in case of hardware misbehavior.
Signed-off-by: Zhian Liang <liangzhan5dev@xxxxxxxxx>
---
drivers/input/keyboard/tca8418_keypad.c | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/drivers/input/keyboard/tca8418_keypad.c b/drivers/input/keyboard/tca8418_keypad.c
index 9f0aace6bd21..0952370c0636 100644
--- a/drivers/input/keyboard/tca8418_keypad.c
+++ b/drivers/input/keyboard/tca8418_keypad.c
@@ -212,6 +212,7 @@ static irqreturn_t tca8418_irq_handler(int irq, void *dev_id)
struct tca8418_keypad *keypad_data = dev_id;
u8 reg;
int error;
+ int max_loops = 16;
error = tca8418_read_byte(keypad_data, REG_INT_STAT, ®);
if (error) {
@@ -225,9 +226,19 @@ static irqreturn_t tca8418_irq_handler(int irq, void *dev_id)
if (reg & INT_STAT_OVR_FLOW_INT)
dev_warn(&keypad_data->client->dev, "overflow occurred\n");
+ do {
+ if (reg & INT_STAT_K_INT)
+ tca8418_read_keypad(keypad_data);
+
+ /* Re-read interrupt status to check for new events */
+ error = tca8418_read_byte(keypad_data, REG_INT_STAT, ®);
+ if (error) {
+ dev_err(&keypad_data->client->dev,
+ "unable to re-read REG_INT_STAT\n");
+ return IRQ_HANDLED;
+ }
- if (reg & INT_STAT_K_INT)
- tca8418_read_keypad(keypad_data);
+ } while ((reg & INT_STAT_K_INT) && --max_loops);
/* Clear all interrupts, even IRQs we didn't check (GPI, CAD, LCK) */
reg = 0xff;
--
2.34.1