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