f5dae5a
From 25bb14c1e78e641049fd1ee0c404a9ccd2755e44 Mon Sep 17 00:00:00 2001
f5dae5a
From: Hans de Goede <hdegoede@redhat.com>
f5dae5a
Date: Sat, 22 Jul 2017 13:00:05 +0200
f5dae5a
Subject: [PATCH 1/2] Input: gpio_keys - Allow suppression of input events for
f5dae5a
 wakeup button presses
f5dae5a
f5dae5a
In some cases it is undesirable for a wakeup button to send input events
f5dae5a
to userspace if pressed to wakeup the system (if pressed during suspend).
f5dae5a
f5dae5a
A typical example of this is the power-button on laptops / tablets,
f5dae5a
sending a KEY_POWER event to userspace when woken up with the power-button
f5dae5a
will cause userspace to immediately suspend the system again which is
f5dae5a
undesirable.
f5dae5a
f5dae5a
For power-buttons attached to a PMIC, or handled by e.g. ACPI, not sending
f5dae5a
an input event in this case is take care of by the PMIC / ACPI hardware /
f5dae5a
code. But in the case of a GPIO button we need to explicitly suppress the
f5dae5a
sending of the input event.
f5dae5a
f5dae5a
This commit adds support for this by adding a no_wakeup_events bool to
f5dae5a
struct gpio_keys_button, which platform code can set to suppress the
f5dae5a
input events for presses of wakeup keys during suspend.
f5dae5a
f5dae5a
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
f5dae5a
---
f5dae5a
Changes in v2:
f5dae5a
-This is a rewrite if my "Input: gpio_keys - Do not report wake button
f5dae5a
 presses as evdev events" patch.
f5dae5a
-Instead of unconditionally ignoring presses of all wake-up buttons during
f5dae5a
 suspend, this rewrite makes this configurable per button
f5dae5a
-This version uses a timer to delay clearing the suspended flag for software
f5dae5a
 debouncing, rather then jiffy compare magic
f5dae5a
---
f5dae5a
 drivers/input/keyboard/gpio_keys.c | 33 +++++++++++++++++++++++++++++++--
f5dae5a
 include/linux/gpio_keys.h          |  3 +++
f5dae5a
 2 files changed, 34 insertions(+), 2 deletions(-)
f5dae5a
f5dae5a
diff --git a/drivers/input/keyboard/gpio_keys.c b/drivers/input/keyboard/gpio_keys.c
f5dae5a
index a047b9af8369..fa3a58620407 100644
f5dae5a
--- a/drivers/input/keyboard/gpio_keys.c
f5dae5a
+++ b/drivers/input/keyboard/gpio_keys.c
f5dae5a
@@ -38,6 +38,7 @@ struct gpio_button_data {
f5dae5a
 
f5dae5a
 	unsigned short *code;
f5dae5a
 
f5dae5a
+	struct timer_list unsuspend_timer;
f5dae5a
 	struct timer_list release_timer;
f5dae5a
 	unsigned int release_delay;	/* in msecs, for IRQ-only buttons */
f5dae5a
 
f5dae5a
@@ -371,6 +372,9 @@ static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
f5dae5a
 		return;
f5dae5a
 	}
f5dae5a
 
f5dae5a
+	if (state && bdata->button->no_wakeup_events && bdata->suspended)
f5dae5a
+		return;
f5dae5a
+
f5dae5a
 	if (type == EV_ABS) {
f5dae5a
 		if (state)
f5dae5a
 			input_event(input, type, button->code, button->value);
f5dae5a
@@ -400,6 +404,9 @@ static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id)
f5dae5a
 	if (bdata->button->wakeup) {
f5dae5a
 		const struct gpio_keys_button *button = bdata->button;
f5dae5a
 
f5dae5a
+		if (bdata->button->no_wakeup_events && bdata->suspended)
f5dae5a
+			return IRQ_HANDLED;
f5dae5a
+
f5dae5a
 		pm_stay_awake(bdata->input->dev.parent);
f5dae5a
 		if (bdata->suspended  &&
f5dae5a
 		    (button->type == 0 || button->type == EV_KEY)) {
f5dae5a
@@ -445,9 +452,13 @@ static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
f5dae5a
 	spin_lock_irqsave(&bdata->lock, flags);
f5dae5a
 
f5dae5a
 	if (!bdata->key_pressed) {
f5dae5a
-		if (bdata->button->wakeup)
f5dae5a
+		if (bdata->button->wakeup) {
f5dae5a
 			pm_wakeup_event(bdata->input->dev.parent, 0);
f5dae5a
 
f5dae5a
+			if (bdata->button->no_wakeup_events && bdata->suspended)
f5dae5a
+				goto out;
f5dae5a
+		}
f5dae5a
+
f5dae5a
 		input_event(input, EV_KEY, *bdata->code, 1);
f5dae5a
 		input_sync(input);
f5dae5a
 
f5dae5a
@@ -468,6 +479,13 @@ static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
f5dae5a
 	return IRQ_HANDLED;
f5dae5a
 }
f5dae5a
 
f5dae5a
+static void gpio_keys_unsuspend_timer(unsigned long _data)
f5dae5a
+{
f5dae5a
+	struct gpio_button_data *bdata = (struct gpio_button_data *)_data;
f5dae5a
+
f5dae5a
+	bdata->suspended = false;
f5dae5a
+}
f5dae5a
+
f5dae5a
 static void gpio_keys_quiesce_key(void *data)
f5dae5a
 {
f5dae5a
 	struct gpio_button_data *bdata = data;
f5dae5a
@@ -476,6 +494,8 @@ static void gpio_keys_quiesce_key(void *data)
f5dae5a
 		cancel_delayed_work_sync(&bdata->work);
f5dae5a
 	else
f5dae5a
 		del_timer_sync(&bdata->release_timer);
f5dae5a
+
f5dae5a
+	del_timer_sync(&bdata->unsuspend_timer);
f5dae5a
 }
f5dae5a
 
f5dae5a
 static int gpio_keys_setup_key(struct platform_device *pdev,
f5dae5a
@@ -496,6 +516,8 @@ static int gpio_keys_setup_key(struct platform_device *pdev,
f5dae5a
 	bdata->input = input;
f5dae5a
 	bdata->button = button;
f5dae5a
 	spin_lock_init(&bdata->lock);
f5dae5a
+	setup_timer(&bdata->unsuspend_timer, gpio_keys_unsuspend_timer,
f5dae5a
+		    (unsigned long)bdata);
f5dae5a
 
f5dae5a
 	if (child) {
f5dae5a
 		bdata->gpiod = devm_fwnode_get_gpiod_from_child(dev, NULL,
f5dae5a
@@ -868,6 +890,7 @@ static int __maybe_unused gpio_keys_suspend(struct device *dev)
f5dae5a
 			struct gpio_button_data *bdata = &ddata->data[i];
f5dae5a
 			if (bdata->button->wakeup)
f5dae5a
 				enable_irq_wake(bdata->irq);
f5dae5a
+			del_timer_sync(&bdata->unsuspend_timer);
f5dae5a
 			bdata->suspended = true;
f5dae5a
 		}
f5dae5a
 	} else {
f5dae5a
@@ -892,7 +915,13 @@ static int __maybe_unused gpio_keys_resume(struct device *dev)
f5dae5a
 			struct gpio_button_data *bdata = &ddata->data[i];
f5dae5a
 			if (bdata->button->wakeup)
f5dae5a
 				disable_irq_wake(bdata->irq);
f5dae5a
-			bdata->suspended = false;
f5dae5a
+			if (bdata->button->no_wakeup_events) {
f5dae5a
+				mod_timer(&bdata->unsuspend_timer, jiffies +
f5dae5a
+					  msecs_to_jiffies(
f5dae5a
+						    bdata->software_debounce));
f5dae5a
+			} else {
f5dae5a
+				bdata->suspended = false;
f5dae5a
+			}
f5dae5a
 		}
f5dae5a
 	} else {
f5dae5a
 		mutex_lock(&input->mutex);
f5dae5a
diff --git a/include/linux/gpio_keys.h b/include/linux/gpio_keys.h
f5dae5a
index 0b71024c082c..d8a85e52b6bb 100644
f5dae5a
--- a/include/linux/gpio_keys.h
f5dae5a
+++ b/include/linux/gpio_keys.h
f5dae5a
@@ -15,6 +15,8 @@ struct device;
f5dae5a
  * @debounce_interval:	debounce ticks interval in msecs
f5dae5a
  * @can_disable:	%true indicates that userspace is allowed to
f5dae5a
  *			disable button via sysfs
f5dae5a
+ * @no_wakeup_events:	For wake-up source buttons only, if %true then no input
f5dae5a
+ *			events will be generated if pressed while suspended
f5dae5a
  * @value:		axis value for %EV_ABS
f5dae5a
  * @irq:		Irq number in case of interrupt keys
f5dae5a
  */
f5dae5a
@@ -27,6 +29,7 @@ struct gpio_keys_button {
f5dae5a
 	int wakeup;
f5dae5a
 	int debounce_interval;
f5dae5a
 	bool can_disable;
f5dae5a
+	bool no_wakeup_events;
f5dae5a
 	int value;
f5dae5a
 	unsigned int irq;
f5dae5a
 };
f5dae5a
-- 
f5dae5a
2.13.4
f5dae5a