c6ca7ce
From 02b823a4d28ffb5fde5192799abd934d9de95630 Mon Sep 17 00:00:00 2001
c6ca7ce
From: Hans de Goede <hdegoede@redhat.com>
c6ca7ce
Date: Fri, 6 Jan 2017 20:08:11 +0100
c6ca7ce
Subject: [PATCH 12/16] Input: gpio_keys - Do not report wake button presses as
c6ca7ce
 evdev events
c6ca7ce
c6ca7ce
If a button is a wake button, it may still be bouncing from the press
c6ca7ce
to wakeup the device by the time the gpio interrupts get enabled again
c6ca7ce
and / or the gpio_keys_report_state call from gpio_keys_resume may
c6ca7ce
find the button still pressed and report this as a new press.
c6ca7ce
c6ca7ce
This is undesirable, esp. since the powerbutton on tablets is typically
c6ca7ce
a wakeup source and uses the gpio_keys driver on some tablets, leading
c6ca7ce
to userspace immediately re-suspending the tablet after the powerbutton
c6ca7ce
is pressed, due to it seeing a powerbutton press.
c6ca7ce
c6ca7ce
This commit ignores wakeup button presses for the first 1 second after
c6ca7ce
resume (and while resumed, as the workqueue may run before the resume
c6ca7ce
function runs), avoiding this problem.
c6ca7ce
c6ca7ce
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
c6ca7ce
---
c6ca7ce
Note: maybe we should make WAKE_DEBOUNCE part of gpio_keys_button and
c6ca7ce
only do this when drivers / platform-data set this to a non-zero value ?
c6ca7ce
---
c6ca7ce
 drivers/input/keyboard/gpio_keys.c | 49 ++++++++++++++++++++++++++++++++++++--
c6ca7ce
 1 file changed, 47 insertions(+), 2 deletions(-)
c6ca7ce
c6ca7ce
diff --git a/drivers/input/keyboard/gpio_keys.c b/drivers/input/keyboard/gpio_keys.c
c6ca7ce
index da3d362f21b1..e1488b534e7d 100644
c6ca7ce
--- a/drivers/input/keyboard/gpio_keys.c
c6ca7ce
+++ b/drivers/input/keyboard/gpio_keys.c
c6ca7ce
@@ -31,6 +31,8 @@
c6ca7ce
 #include <linux/of_irq.h>
c6ca7ce
 #include <linux/spinlock.h>
c6ca7ce
 
c6ca7ce
+#define WAKE_DEBOUNCE msecs_to_jiffies(1000)
c6ca7ce
+
c6ca7ce
 struct gpio_button_data {
c6ca7ce
 	const struct gpio_keys_button *button;
c6ca7ce
 	struct input_dev *input;
c6ca7ce
@@ -44,10 +46,14 @@ struct gpio_button_data {
c6ca7ce
 	struct delayed_work work;
c6ca7ce
 	unsigned int software_debounce;	/* in msecs, for GPIO-driven buttons */
c6ca7ce
 
c6ca7ce
+	unsigned long resume_time;	/* in jiffies, for wakeup buttons */
c6ca7ce
+
c6ca7ce
 	unsigned int irq;
c6ca7ce
 	spinlock_t lock;
c6ca7ce
 	bool disabled;
c6ca7ce
 	bool key_pressed;
c6ca7ce
+	bool suspended;
c6ca7ce
+	bool resume_time_valid;
c6ca7ce
 };
c6ca7ce
 
c6ca7ce
 struct gpio_keys_drvdata {
c6ca7ce
@@ -356,6 +362,27 @@ static struct attribute_group gpio_keys_attr_group = {
c6ca7ce
 	.attrs = gpio_keys_attrs,
c6ca7ce
 };
c6ca7ce
 
c6ca7ce
+static bool gpio_keys_ignore_wakeup_button_press(struct gpio_button_data *bdata)
c6ca7ce
+{
c6ca7ce
+	unsigned long flags;
c6ca7ce
+	bool ret = false;
c6ca7ce
+
c6ca7ce
+	if (!bdata->button->wakeup)
c6ca7ce
+		return ret;
c6ca7ce
+
c6ca7ce
+	spin_lock_irqsave(&bdata->lock, flags);
c6ca7ce
+
c6ca7ce
+	if (bdata->suspended)
c6ca7ce
+		ret = true; /* Our resume method did not run yet */
c6ca7ce
+	else if (bdata->resume_time_valid &&
c6ca7ce
+		 time_before(jiffies, bdata->resume_time + WAKE_DEBOUNCE))
c6ca7ce
+		ret = true; /* Assume this is a wakeup press and ignore */
c6ca7ce
+
c6ca7ce
+	spin_unlock_irqrestore(&bdata->lock, flags);
c6ca7ce
+
c6ca7ce
+	return ret;
c6ca7ce
+}
c6ca7ce
+
c6ca7ce
 static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
c6ca7ce
 {
c6ca7ce
 	const struct gpio_keys_button *button = bdata->button;
c6ca7ce
@@ -370,6 +397,9 @@ static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
c6ca7ce
 		return;
c6ca7ce
 	}
c6ca7ce
 
c6ca7ce
+	if (state && gpio_keys_ignore_wakeup_button_press(bdata))
c6ca7ce
+		return;
c6ca7ce
+
c6ca7ce
 	if (type == EV_ABS) {
c6ca7ce
 		if (state)
c6ca7ce
 			input_event(input, type, button->code, button->value);
c6ca7ce
@@ -429,6 +459,9 @@ static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
c6ca7ce
 
c6ca7ce
 	BUG_ON(irq != bdata->irq);
c6ca7ce
 
c6ca7ce
+	if (gpio_keys_ignore_wakeup_button_press(bdata))
c6ca7ce
+		return IRQ_HANDLED;
c6ca7ce
+
c6ca7ce
 	spin_lock_irqsave(&bdata->lock, flags);
c6ca7ce
 
c6ca7ce
 	if (!bdata->key_pressed) {
c6ca7ce
@@ -848,13 +881,18 @@ static int __maybe_unused gpio_keys_suspend(struct device *dev)
c6ca7ce
 {
c6ca7ce
 	struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
c6ca7ce
 	struct input_dev *input = ddata->input;
c6ca7ce
+	unsigned long flags;
c6ca7ce
 	int i;
c6ca7ce
 
c6ca7ce
 	if (device_may_wakeup(dev)) {
c6ca7ce
 		for (i = 0; i < ddata->pdata->nbuttons; i++) {
c6ca7ce
 			struct gpio_button_data *bdata = &ddata->data[i];
c6ca7ce
-			if (bdata->button->wakeup)
c6ca7ce
+			if (bdata->button->wakeup) {
c6ca7ce
+				spin_lock_irqsave(&bdata->lock, flags);
c6ca7ce
+				bdata->suspended = true;
c6ca7ce
+				spin_unlock_irqrestore(&bdata->lock, flags);
c6ca7ce
 				enable_irq_wake(bdata->irq);
c6ca7ce
+			}
c6ca7ce
 		}
c6ca7ce
 	} else {
c6ca7ce
 		mutex_lock(&input->mutex);
c6ca7ce
@@ -870,14 +908,21 @@ static int __maybe_unused gpio_keys_resume(struct device *dev)
c6ca7ce
 {
c6ca7ce
 	struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
c6ca7ce
 	struct input_dev *input = ddata->input;
c6ca7ce
+	unsigned long flags;
c6ca7ce
 	int error = 0;
c6ca7ce
 	int i;
c6ca7ce
 
c6ca7ce
 	if (device_may_wakeup(dev)) {
c6ca7ce
 		for (i = 0; i < ddata->pdata->nbuttons; i++) {
c6ca7ce
 			struct gpio_button_data *bdata = &ddata->data[i];
c6ca7ce
-			if (bdata->button->wakeup)
c6ca7ce
+			if (bdata->button->wakeup) {
c6ca7ce
 				disable_irq_wake(bdata->irq);
c6ca7ce
+				spin_lock_irqsave(&bdata->lock, flags);
c6ca7ce
+				bdata->resume_time = jiffies;
c6ca7ce
+				bdata->resume_time_valid = true;
c6ca7ce
+				bdata->suspended = false;
c6ca7ce
+				spin_unlock_irqrestore(&bdata->lock, flags);
c6ca7ce
+			}
c6ca7ce
 		}
c6ca7ce
 	} else {
c6ca7ce
 		mutex_lock(&input->mutex);
c6ca7ce
-- 
c6ca7ce
2.13.0
c6ca7ce