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