diff --git a/drivers/input/input.c b/drivers/input/input.c --- a/drivers/input/input.c +++ b/drivers/input/input.c @@ -25,6 +25,8 @@ #include #include #include +#include +#include #include "input-compat.h" #include "input-core-private.h" #include "input-poller.h" @@ -48,6 +50,155 @@ */ static DEFINE_MUTEX(input_mutex); +static unsigned int ba_key_debounce_ms = 45; +module_param_named(key_debounce_ms, ba_key_debounce_ms, uint, 0644); +MODULE_PARM_DESC(key_debounce_ms, + "Post-release chatter debounce window in ms for built-in physical keys (0=off)"); + +#define BA_KEY_DEBOUNCE_SLOTS 64 + +struct ba_key_debounce_slot { + const struct input_dev *dev; + unsigned int code; + ktime_t last_press; + ktime_t last_release; + ktime_t settle_until; + bool suppressed_press; +}; + +static struct ba_key_debounce_slot ba_key_slots[BA_KEY_DEBOUNCE_SLOTS]; +static DEFINE_SPINLOCK(ba_key_debounce_lock); + +static bool ba_should_debounce_key(const struct input_dev *dev, + unsigned int code) +{ + if (!READ_ONCE(ba_key_debounce_ms)) + return false; + + if (dev->id.bustype == BUS_VIRTUAL || + dev->id.bustype == BUS_BLUETOOTH || + dev->id.bustype == BUS_USB) + return false; + + if (code >= BTN_MISC && code < KEY_OK) + return false; + + return true; +} + +static void ba_pop_trailing_msc_scan(struct input_dev *dev) +{ + if (dev->num_vals > 0 && + dev->vals[dev->num_vals - 1].type == EV_MSC && + dev->vals[dev->num_vals - 1].code == MSC_SCAN) + dev->num_vals--; +} + +static bool ba_filter_key_bounce(struct input_dev *dev, unsigned int code, + int value) +{ + unsigned int db_ms = min(READ_ONCE(ba_key_debounce_ms), 250U); + struct ba_key_debounce_slot *slot = NULL; + struct ba_key_debounce_slot *empty = NULL; + struct ba_key_debounce_slot *oldest = NULL; + ktime_t oldest_ts = 0; + ktime_t now; + s64 db_ns; + unsigned long flags; + bool was_down; + int i; + + if (!db_ms || !ba_should_debounce_key(dev, code)) + return false; + + now = ktime_get(); + db_ns = (s64)db_ms * NSEC_PER_MSEC; + was_down = test_bit(code, dev->key); + + spin_lock_irqsave(&ba_key_debounce_lock, flags); + + for (i = 0; i < BA_KEY_DEBOUNCE_SLOTS; i++) { + struct ba_key_debounce_slot *s = &ba_key_slots[i]; + + if (s->dev == dev && s->code == code) { + slot = s; + break; + } + if (!s->dev && !empty) + empty = s; + if (s->dev) { + ktime_t ts = ktime_after(s->last_release, s->last_press) ? + s->last_release : s->last_press; + if (!oldest || ktime_before(ts, oldest_ts)) { + oldest = s; + oldest_ts = ts; + } + } + } + + if (!slot) { + slot = empty ? empty : oldest; + memset(slot, 0, sizeof(*slot)); + slot->dev = dev; + slot->code = code; + } + + if (value) { + if (was_down) { + spin_unlock_irqrestore(&ba_key_debounce_lock, flags); + return false; + } + if ((ktime_to_ns(slot->settle_until) != 0 && + ktime_before(now, slot->settle_until)) || + (ktime_to_ns(slot->last_press) != 0 && + ktime_sub(now, slot->last_press) < db_ns)) { + slot->suppressed_press = true; + spin_unlock_irqrestore(&ba_key_debounce_lock, flags); + ba_pop_trailing_msc_scan(dev); + return true; + } + slot->last_press = now; + slot->suppressed_press = false; + spin_unlock_irqrestore(&ba_key_debounce_lock, flags); + return false; + } + + if (was_down) { + slot->last_release = now; + slot->settle_until = ktime_add_ns(now, db_ns); + slot->suppressed_press = false; + spin_unlock_irqrestore(&ba_key_debounce_lock, flags); + return false; + } + + if (slot->suppressed_press) { + ktime_t ext = ktime_add_ns(now, db_ns / 2); + ktime_t cap = ktime_add_ns(slot->last_release, db_ns + db_ns / 2); + + if (ktime_after(ext, cap)) + ext = cap; + if (ktime_after(ext, slot->settle_until)) + slot->settle_until = ext; + slot->suppressed_press = false; + } + spin_unlock_irqrestore(&ba_key_debounce_lock, flags); + ba_pop_trailing_msc_scan(dev); + return true; +} + +static void ba_clear_key_debounce_dev(const struct input_dev *dev) +{ + unsigned long flags; + int i; + + spin_lock_irqsave(&ba_key_debounce_lock, flags); + for (i = 0; i < BA_KEY_DEBOUNCE_SLOTS; i++) { + if (ba_key_slots[i].dev == dev) + memset(&ba_key_slots[i], 0, sizeof(ba_key_slots[i])); + } + spin_unlock_irqrestore(&ba_key_debounce_lock, flags); +} + static const struct input_value input_value_sync = { EV_SYN, SYN_REPORT, 1 }; static const unsigned int input_max_code[EV_CNT] = { @@ -240,6 +391,9 @@ break; } + if (ba_filter_key_bounce(dev, code, value)) + break; + if (!!test_bit(code, dev->key) != !!value) { __change_bit(code, dev->key); @@ -2267,6 +2421,7 @@ { struct input_handle *handle, *next; + ba_clear_key_debounce_dev(dev); input_disconnect_device(dev); mutex_lock(&input_mutex); diff --git a/drivers/input/misc/pm8941-pwrkey.c b/drivers/input/misc/pm8941-pwrkey.c --- a/drivers/input/misc/pm8941-pwrkey.c +++ b/drivers/input/misc/pm8941-pwrkey.c @@ -151,7 +151,7 @@ unsigned int sts; int err; - if (pwrkey->sw_debounce_time_us) { + if (ktime_to_ns(pwrkey->sw_debounce_end_time)) { if (ktime_before(ktime_get(), pwrkey->sw_debounce_end_time)) { dev_dbg(pwrkey->dev, "ignoring key event received before debounce end %llu us\n", @@ -166,9 +166,12 @@ sts &= pwrkey->data->status_bit; - if (pwrkey->sw_debounce_time_us && !sts) + if (!sts) pwrkey->sw_debounce_end_time = ktime_add_us(ktime_get(), - pwrkey->sw_debounce_time_us); + max_t(u32, pwrkey->sw_debounce_time_us, 45000U)); + else + pwrkey->sw_debounce_end_time = ktime_add_us(ktime_get(), + min_t(u32, max_t(u32, pwrkey->sw_debounce_time_us, 15000U), 25000U)); /* * Simulate a press event in case a release event occurred without a