linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH] Input: gpio_keys: Fix suspend/resume press event lost
@ 2013-01-18 12:51 Ivan Khoronzhuk
  0 siblings, 0 replies; 9+ messages in thread
From: Ivan Khoronzhuk @ 2013-01-18 12:51 UTC (permalink / raw)
  To: linux-input
  Cc: linux-kernel, Dmitry Torokhov, Bill Pemberton, Mark Brown,
	NeilBrown, Bengt Jonsson, Ivan Khoronzhuk

During suspend/resume the key press can be lost if time of resume
sequence is significant.

If press event cannot be remembered then the driver can read the
current button state only in time of interrupt handling. But in some
cases when time between IRQ and IRQ handler is significant we can
read incorrect state. As a particular case, when device is in suspend
we press wakupable key and up it back in a jiffy, the interrupt
handler read the state of up but the interrupt source is press indeed.
As a result, in a OS like android, we resume then suspend right away
because the key state is not changed.

This patch add to gpio_keys framework opportunity to recover lost of
press key event at resuming. The variable "key_pressed" from
gpio_button_data structure is not used for gpio keys, it is only used
for gpio irq keys, so it is logically used to remember press lost
while resuming.

Signed-off-by: Ivan Khoronzhuk <ivan.khoronzhuk@ti.com>
---
 drivers/input/keyboard/gpio_keys.c |   31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/drivers/input/keyboard/gpio_keys.c b/drivers/input/keyboard/gpio_keys.c
index 62bfce4..aa49aef 100644
--- a/drivers/input/keyboard/gpio_keys.c
+++ b/drivers/input/keyboard/gpio_keys.c
@@ -46,6 +46,7 @@ struct gpio_keys_drvdata {
 	struct input_dev *input;
 	struct mutex disable_lock;
 	unsigned int n_buttons;
+	int suspended;
 	int (*enable)(struct device *dev);
 	void (*disable)(struct device *dev);
 	struct gpio_button_data data[0];
@@ -328,14 +329,40 @@ static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
 {
 	const struct gpio_keys_button *button = bdata->button;
 	struct input_dev *input = bdata->input;
+	struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
 	unsigned int type = button->type ?: EV_KEY;
 	int state = (gpio_get_value_cansleep(button->gpio) ? 1 : 0) ^ button->active_low;
 
+	/*
+	 * Don't generate input event while resuming,
+	 * it will be generated at gpio_keys_resume function
+	*/
+	if (ddata->suspended) {
+		/*
+		 * missed press event while resuming so set
+		 * key_pressed flag to generate press and up events
+		 * while gpio_keys_resume function.
+		 */
+		if (button->wakeup && state == 0)
+			bdata->key_pressed = 1;
+		return;
+	}
+
 	if (type == EV_ABS) {
 		if (state)
 			input_event(input, type, button->code, button->value);
 	} else {
-		input_event(input, type, button->code, !!state);
+		/*
+		 * missed press key, so generate press event then up event
+		 */
+		if (bdata->key_pressed) {
+			input_event(bdata->input, EV_KEY, button->code, 1);
+			input_sync(bdata->input);
+			input_event(bdata->input, EV_KEY, button->code, 0);
+			bdata->key_pressed = 0;
+		} else {
+			input_event(input, type, button->code, !!state);
+		}
 	}
 	input_sync(input);
 }
@@ -792,6 +819,7 @@ static int gpio_keys_suspend(struct device *dev)
 		}
 	}
 
+	ddata->suspended = 1;
 	return 0;
 }
 
@@ -800,6 +828,7 @@ static int gpio_keys_resume(struct device *dev)
 	struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
 	int i;
 
+	ddata->suspended = 0;
 	for (i = 0; i < ddata->n_buttons; i++) {
 		struct gpio_button_data *bdata = &ddata->data[i];
 		if (bdata->button->wakeup && device_may_wakeup(dev))
-- 
1.7.9.5


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [RFC PATCH] Input: gpio_keys: Fix suspend/resume press event lost
@ 2013-01-21 13:15 Ivan Khoronzhuk
  2013-01-21 23:57 ` Dmitry Torokhov
  0 siblings, 1 reply; 9+ messages in thread
From: Ivan Khoronzhuk @ 2013-01-21 13:15 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-input, Bengt Jonsson, NeilBrown, Mark Brown, Bill Pemberton,
	Dmitry Torokhov, Ivan Khoronzhuk

Rebased on linux_omap/master.

During suspend/resume the key press can be lost if time of resume
sequence is significant.

If press event cannot be remembered then the driver can read the
current button state only in time of interrupt handling. But in some
cases when time between IRQ and IRQ handler is significant we can
read incorrect state. As a particular case, when device is in suspend
we press wakupable key and up it back in a jiffy, the interrupt
handler read the state of up but the interrupt source is press indeed.
As a result, in a OS like android, we resume then suspend right away
because the key state is not changed.

This patch add to gpio_keys framework opportunity to recover lost of
press key event at resuming. The variable "key_pressed" from
gpio_button_data structure is not used for gpio keys, it is only used
for gpio irq keys, so it is logically used to remember press lost
while resuming.

Signed-off-by: Ivan Khoronzhuk <ivan.khoronzhuk@ti.com>
---
 drivers/input/keyboard/gpio_keys.c |   31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/drivers/input/keyboard/gpio_keys.c b/drivers/input/keyboard/gpio_keys.c
index b29ca65..33ac8c5 100644
--- a/drivers/input/keyboard/gpio_keys.c
+++ b/drivers/input/keyboard/gpio_keys.c
@@ -45,6 +45,7 @@ struct gpio_button_data {
 struct gpio_keys_drvdata {
 	const struct gpio_keys_platform_data *pdata;
 	struct input_dev *input;
+	int suspended;
 	struct mutex disable_lock;
 	struct gpio_button_data data[0];
 };
@@ -326,14 +327,40 @@ static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
 {
 	const struct gpio_keys_button *button = bdata->button;
 	struct input_dev *input = bdata->input;
+	struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
 	unsigned int type = button->type ?: EV_KEY;
 	int state = (gpio_get_value_cansleep(button->gpio) ? 1 : 0) ^ button->active_low;
 
+	/*
+	 * Don't generate input event while resuming,
+	 * it will be generated at gpio_keys_resume function
+	*/
+	if (ddata->suspended) {
+		/*
+		 * missed press event while resuming so set
+		 * key_pressed flag to generate press and up events
+		 * while gpio_keys_resume function.
+		 */
+		if (button->wakeup && state == 0)
+			bdata->key_pressed = 1;
+		return;
+	}
+
 	if (type == EV_ABS) {
 		if (state)
 			input_event(input, type, button->code, button->value);
 	} else {
-		input_event(input, type, button->code, !!state);
+		/*
+		 * missed press key, so generate press event then up event
+		 */
+		if (bdata->key_pressed) {
+			input_event(bdata->input, EV_KEY, button->code, 1);
+			input_sync(bdata->input);
+			input_event(bdata->input, EV_KEY, button->code, 0);
+			bdata->key_pressed = 0;
+		} else {
+			input_event(input, type, button->code, !!state);
+		}
 	}
 	input_sync(input);
 }
@@ -822,6 +849,7 @@ static int gpio_keys_suspend(struct device *dev)
 		mutex_unlock(&input->mutex);
 	}
 
+	ddata->suspended = 1;
 	return 0;
 }
 
@@ -832,6 +860,7 @@ static int gpio_keys_resume(struct device *dev)
 	int error = 0;
 	int i;
 
+	ddata->suspended = 0;
 	if (device_may_wakeup(dev)) {
 		for (i = 0; i < ddata->pdata->nbuttons; i++) {
 			struct gpio_button_data *bdata = &ddata->data[i];
-- 
1.7.9.5


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [RFC PATCH] Input: gpio_keys: Fix suspend/resume press event lost
  2013-01-21 13:15 [RFC PATCH] Input: gpio_keys: Fix suspend/resume press event lost Ivan Khoronzhuk
@ 2013-01-21 23:57 ` Dmitry Torokhov
  2013-01-22  5:24   ` NeilBrown
  0 siblings, 1 reply; 9+ messages in thread
From: Dmitry Torokhov @ 2013-01-21 23:57 UTC (permalink / raw)
  To: Ivan Khoronzhuk
  Cc: linux-kernel, linux-input, Bengt Jonsson, NeilBrown, Mark Brown,
	Bill Pemberton

Hi Ivan,

On Mon, Jan 21, 2013 at 03:15:14PM +0200, Ivan Khoronzhuk wrote:
> Rebased on linux_omap/master.
> 
> During suspend/resume the key press can be lost if time of resume
> sequence is significant.
> 
> If press event cannot be remembered then the driver can read the
> current button state only in time of interrupt handling. But in some
> cases when time between IRQ and IRQ handler is significant we can
> read incorrect state. As a particular case, when device is in suspend
> we press wakupable key and up it back in a jiffy, the interrupt
> handler read the state of up but the interrupt source is press indeed.
> As a result, in a OS like android, we resume then suspend right away
> because the key state is not changed.
> 
> This patch add to gpio_keys framework opportunity to recover lost of
> press key event at resuming. The variable "key_pressed" from
> gpio_button_data structure is not used for gpio keys, it is only used
> for gpio irq keys, so it is logically used to remember press lost
> while resuming.

The same could happen if you delay processing of interrupt long enough
during normal operation. If key is released by the time you get around
to reading it you will not see a key press.

To me this sounds like you need to speed up your resume process so that
you can start serving interrupts quicker.

Thanks.

-- 
Dmitry

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFC PATCH] Input: gpio_keys: Fix suspend/resume press event lost
  2013-01-21 23:57 ` Dmitry Torokhov
@ 2013-01-22  5:24   ` NeilBrown
  2013-01-28 16:13     ` ivan.khoronzhuk
  0 siblings, 1 reply; 9+ messages in thread
From: NeilBrown @ 2013-01-22  5:24 UTC (permalink / raw)
  To: Dmitry Torokhov
  Cc: Ivan Khoronzhuk, linux-kernel, linux-input, Bengt Jonsson,
	Mark Brown, Bill Pemberton

[-- Attachment #1: Type: text/plain, Size: 2179 bytes --]

On Mon, 21 Jan 2013 15:57:18 -0800 Dmitry Torokhov
<dmitry.torokhov@gmail.com> wrote:

> Hi Ivan,
> 
> On Mon, Jan 21, 2013 at 03:15:14PM +0200, Ivan Khoronzhuk wrote:
> > Rebased on linux_omap/master.
> > 
> > During suspend/resume the key press can be lost if time of resume
> > sequence is significant.
> > 
> > If press event cannot be remembered then the driver can read the
> > current button state only in time of interrupt handling. But in some
> > cases when time between IRQ and IRQ handler is significant we can
> > read incorrect state. As a particular case, when device is in suspend
> > we press wakupable key and up it back in a jiffy, the interrupt
> > handler read the state of up but the interrupt source is press indeed.
> > As a result, in a OS like android, we resume then suspend right away
> > because the key state is not changed.
> > 
> > This patch add to gpio_keys framework opportunity to recover lost of
> > press key event at resuming. The variable "key_pressed" from
> > gpio_button_data structure is not used for gpio keys, it is only used
> > for gpio irq keys, so it is logically used to remember press lost
> > while resuming.
> 
> The same could happen if you delay processing of interrupt long enough
> during normal operation. If key is released by the time you get around
> to reading it you will not see a key press.
> 
> To me this sounds like you need to speed up your resume process so that
> you can start serving interrupts quicker.
> 

Agreed.  When I was looking at this I found that any genuine button press
would have at least 70msec between press and release, while the device could
wake up to the point of being able to handle interrupts in about 14msec.
That is enough of a gap to make it pointless to try to 'fix' the code.

With enough verbose debugging enabled that 14msec can easily grow to
hundreds, but then if  you have debugging enabled to can discipline yourself
to hold the button for longer.

Ivan: What sort of delay are you seeing between the button press and the
interrupt routine running?  And can you measure how long the button is
typically down for?

NeilBrown

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 828 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFC PATCH] Input: gpio_keys: Fix suspend/resume press event lost
  2013-01-22  5:24   ` NeilBrown
@ 2013-01-28 16:13     ` ivan.khoronzhuk
  2013-01-28 18:51       ` NeilBrown
  0 siblings, 1 reply; 9+ messages in thread
From: ivan.khoronzhuk @ 2013-01-28 16:13 UTC (permalink / raw)
  To: NeilBrown
  Cc: Dmitry Torokhov, linux-kernel, linux-input, Bengt Jonsson,
	Mark Brown, Bill Pemberton

On 01/22/2013 07:24 AM, NeilBrown wrote:
> On Mon, 21 Jan 2013 15:57:18 -0800 Dmitry Torokhov
> <dmitry.torokhov@gmail.com> wrote:
>
>> Hi Ivan,
>>
>> On Mon, Jan 21, 2013 at 03:15:14PM +0200, Ivan Khoronzhuk wrote:
>>> Rebased on linux_omap/master.
>>>
>>> During suspend/resume the key press can be lost if time of resume
>>> sequence is significant.
>>>
>>> If press event cannot be remembered then the driver can read the
>>> current button state only in time of interrupt handling. But in some
>>> cases when time between IRQ and IRQ handler is significant we can
>>> read incorrect state. As a particular case, when device is in suspend
>>> we press wakupable key and up it back in a jiffy, the interrupt
>>> handler read the state of up but the interrupt source is press indeed.
>>> As a result, in a OS like android, we resume then suspend right away
>>> because the key state is not changed.
>>>
>>> This patch add to gpio_keys framework opportunity to recover lost of
>>> press key event at resuming. The variable "key_pressed" from
>>> gpio_button_data structure is not used for gpio keys, it is only used
>>> for gpio irq keys, so it is logically used to remember press lost
>>> while resuming.
>> The same could happen if you delay processing of interrupt long enough
>> during normal operation. If key is released by the time you get around
>> to reading it you will not see a key press.
>>
>> To me this sounds like you need to speed up your resume process so that
>> you can start serving interrupts quicker.
>>
> Agreed.  When I was looking at this I found that any genuine button press
> would have at least 70msec between press and release, while the device could
> wake up to the point of being able to handle interrupts in about 14msec.
> That is enough of a gap to make it pointless to try to 'fix' the code.
>
> With enough verbose debugging enabled that 14msec can easily grow to
> hundreds, but then if  you have debugging enabled to can discipline yourself
> to hold the button for longer.
>
> Ivan: What sort of delay are you seeing between the button press and the
> interrupt routine running?  And can you measure how long the button is
> typically down for?
>
> NeilBrown

In my case I have the delay between the button press and the ISR
about 145ms. If the button down for 120ms the IRQ press event is
lost and if 160ms event is captured. I cannot speed up resume
process enough to guarantee correct work, so I wrote this fix.

-- 
Regards,
Ivan Khoronzhuk


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFC PATCH] Input: gpio_keys: Fix suspend/resume press event lost
  2013-01-28 16:13     ` ivan.khoronzhuk
@ 2013-01-28 18:51       ` NeilBrown
  2013-01-31 13:25         ` ivan.khoronzhuk
  0 siblings, 1 reply; 9+ messages in thread
From: NeilBrown @ 2013-01-28 18:51 UTC (permalink / raw)
  To: ivan.khoronzhuk
  Cc: Dmitry Torokhov, linux-kernel, linux-input, Bengt Jonsson,
	Mark Brown, Bill Pemberton

[-- Attachment #1: Type: text/plain, Size: 5612 bytes --]

On Mon, 28 Jan 2013 18:13:14 +0200 "ivan.khoronzhuk" <ivan.khoronzhuk@ti.com>
wrote:

> On 01/22/2013 07:24 AM, NeilBrown wrote:
> > On Mon, 21 Jan 2013 15:57:18 -0800 Dmitry Torokhov
> > <dmitry.torokhov@gmail.com> wrote:
> >
> >> Hi Ivan,
> >>
> >> On Mon, Jan 21, 2013 at 03:15:14PM +0200, Ivan Khoronzhuk wrote:
> >>> Rebased on linux_omap/master.
> >>>
> >>> During suspend/resume the key press can be lost if time of resume
> >>> sequence is significant.
> >>>
> >>> If press event cannot be remembered then the driver can read the
> >>> current button state only in time of interrupt handling. But in some
> >>> cases when time between IRQ and IRQ handler is significant we can
> >>> read incorrect state. As a particular case, when device is in suspend
> >>> we press wakupable key and up it back in a jiffy, the interrupt
> >>> handler read the state of up but the interrupt source is press indeed.
> >>> As a result, in a OS like android, we resume then suspend right away
> >>> because the key state is not changed.
> >>>
> >>> This patch add to gpio_keys framework opportunity to recover lost of
> >>> press key event at resuming. The variable "key_pressed" from
> >>> gpio_button_data structure is not used for gpio keys, it is only used
> >>> for gpio irq keys, so it is logically used to remember press lost
> >>> while resuming.
> >> The same could happen if you delay processing of interrupt long enough
> >> during normal operation. If key is released by the time you get around
> >> to reading it you will not see a key press.
> >>
> >> To me this sounds like you need to speed up your resume process so that
> >> you can start serving interrupts quicker.
> >>
> > Agreed.  When I was looking at this I found that any genuine button press
> > would have at least 70msec between press and release, while the device could
> > wake up to the point of being able to handle interrupts in about 14msec.
> > That is enough of a gap to make it pointless to try to 'fix' the code.
> >
> > With enough verbose debugging enabled that 14msec can easily grow to
> > hundreds, but then if  you have debugging enabled to can discipline yourself
> > to hold the button for longer.
> >
> > Ivan: What sort of delay are you seeing between the button press and the
> > interrupt routine running?  And can you measure how long the button is
> > typically down for?
> >
> > NeilBrown
> 
> In my case I have the delay between the button press and the ISR
> about 145ms. If the button down for 120ms the IRQ press event is
> lost and if 160ms event is captured. I cannot speed up resume
> process enough to guarantee correct work, so I wrote this fix.
> 


145ms does sound like a long time.
You should be able to get precise timings by putting a printk in various
parts of the code and ensuring the kernel logs are getting precise timestamps.

Then you could see if the delay is between resume starting and the ISR
running, or between the ISR and the gpio_work_func getting scheduled.

I assume that you don't have ->debounce_interval set....

If the ISR is running soon enough, it might be possible to read the GPIO from
the ISR - I think that would be a cleaner solution.

If not, then you will need something that interpolates an extra key press.
In that case I would suggest my original patch - it seems simpler than yours
and doesn't make a special case out of suspend.  As Dmitry said, any delay
could cause a problem. 
My patch simply ensures that there is at least one button event for each
interrupt by generating an extra event for the inverse of the current button
position.  If that state had already been noticed, the input subsystem will
filter the extra event out so it won't be visible elsewhere.

For reference, my original patch is below.

NeilBrown

[PATCH] Input: gpio_keys - ensure we don't miss key-presses during resume.

If the latency of resume means we don't poll the key status until
after it has been released, we can lose the keypress which woke the
device.

So on each interrupt, record that a press is pending, and in that
case, report both the up and down event, ordered such that the second
event is that one that reflects the current state.

One event will normally be swallowed by the input layer if there was
no change, but the result will be that every interrupt will produce at
least one event.

Signed-off-by: NeilBrown <neilb@suse.de>

diff --git a/drivers/input/keyboard/gpio_keys.c
b/drivers/input/keyboard/gpio_keys.c index 62bfce4..961e5e1 100644
--- a/drivers/input/keyboard/gpio_keys.c
+++ b/drivers/input/keyboard/gpio_keys.c
@@ -40,6 +40,7 @@ struct gpio_button_data {
 	spinlock_t lock;
 	bool disabled;
 	bool key_pressed;
+	bool pending;
 };
 
 struct gpio_keys_drvdata {
@@ -335,6 +336,14 @@ static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata) if (state)
 			input_event(input, type, button->code, button->value); } else {
+		if (type == EV_KEY && bdata->pending) {
+			/* Before reporting the observed state, report the
+			 * alternate to be sure that a change is seen.
+			 */
+			bdata->pending = 0;
+			input_event(input, type, button->code, !state);
+			input_sync(input);
+		}
 		input_event(input, type, button->code, !!state);
 	}
 	input_sync(input);
@@ -361,6 +370,7 @@ static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id) 
 	BUG_ON(irq != bdata->irq);
 
+	bdata->pending = true;
 	if (bdata->timer_debounce)
 		mod_timer(&bdata->timer,
 			jiffies + msecs_to_jiffies(bdata->timer_debounce));

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 828 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFC PATCH] Input: gpio_keys: Fix suspend/resume press event lost
  2013-01-28 18:51       ` NeilBrown
@ 2013-01-31 13:25         ` ivan.khoronzhuk
  2013-02-04 22:40           ` NeilBrown
  0 siblings, 1 reply; 9+ messages in thread
From: ivan.khoronzhuk @ 2013-01-31 13:25 UTC (permalink / raw)
  To: NeilBrown
  Cc: Dmitry Torokhov, linux-kernel, linux-input, Bengt Jonsson,
	Mark Brown, Bill Pemberton

On 01/28/2013 08:51 PM, NeilBrown wrote:
> On Mon, 28 Jan 2013 18:13:14 +0200 "ivan.khoronzhuk" <ivan.khoronzhuk@ti.com>
> wrote:
>
>> On 01/22/2013 07:24 AM, NeilBrown wrote:
>>> On Mon, 21 Jan 2013 15:57:18 -0800 Dmitry Torokhov
>>> <dmitry.torokhov@gmail.com> wrote:
>>>
>>>> Hi Ivan,
>>>>
>>>> On Mon, Jan 21, 2013 at 03:15:14PM +0200, Ivan Khoronzhuk wrote:
>>>>> Rebased on linux_omap/master.
>>>>>
>>>>> During suspend/resume the key press can be lost if time of resume
>>>>> sequence is significant.
>>>>>
>>>>> If press event cannot be remembered then the driver can read the
>>>>> current button state only in time of interrupt handling. But in some
>>>>> cases when time between IRQ and IRQ handler is significant we can
>>>>> read incorrect state. As a particular case, when device is in suspend
>>>>> we press wakupable key and up it back in a jiffy, the interrupt
>>>>> handler read the state of up but the interrupt source is press indeed.
>>>>> As a result, in a OS like android, we resume then suspend right away
>>>>> because the key state is not changed.
>>>>>
>>>>> This patch add to gpio_keys framework opportunity to recover lost of
>>>>> press key event at resuming. The variable "key_pressed" from
>>>>> gpio_button_data structure is not used for gpio keys, it is only used
>>>>> for gpio irq keys, so it is logically used to remember press lost
>>>>> while resuming.
>>>> The same could happen if you delay processing of interrupt long enough
>>>> during normal operation. If key is released by the time you get around
>>>> to reading it you will not see a key press.
>>>>
>>>> To me this sounds like you need to speed up your resume process so that
>>>> you can start serving interrupts quicker.
>>>>
>>> Agreed.  When I was looking at this I found that any genuine button press
>>> would have at least 70msec between press and release, while the device could
>>> wake up to the point of being able to handle interrupts in about 14msec.
>>> That is enough of a gap to make it pointless to try to 'fix' the code.
>>>
>>> With enough verbose debugging enabled that 14msec can easily grow to
>>> hundreds, but then if  you have debugging enabled to can discipline yourself
>>> to hold the button for longer.
>>>
>>> Ivan: What sort of delay are you seeing between the button press and the
>>> interrupt routine running?  And can you measure how long the button is
>>> typically down for?
>>>
>>> NeilBrown
>> In my case I have the delay between the button press and the ISR
>> about 145ms. If the button down for 120ms the IRQ press event is
>> lost and if 160ms event is captured. I cannot speed up resume
>> process enough to guarantee correct work, so I wrote this fix.
>>
>
> 145ms does sound like a long time.
> You should be able to get precise timings by putting a printk in various
> parts of the code and ensuring the kernel logs are getting precise timestamps.
>
> Then you could see if the delay is between resume starting and the ISR
> running, or between the ISR and the gpio_work_func getting scheduled.
>
> I assume that you don't have ->debounce_interval set....
>
> If the ISR is running soon enough, it might be possible to read the GPIO from
> the ISR - I think that would be a cleaner solution.
>
> If not, then you will need something that interpolates an extra key press.
> In that case I would suggest my original patch - it seems simpler than yours
> and doesn't make a special case out of suspend.  As Dmitry said, any delay
> could cause a problem.
> My patch simply ensures that there is at least one button event for each
> interrupt by generating an extra event for the inverse of the current button
> position.  If that state had already been noticed, the input subsystem will
> filter the extra event out so it won't be visible elsewhere.
>
> For reference, my original patch is below.
>
> NeilBrown
>
> [PATCH] Input: gpio_keys - ensure we don't miss key-presses during resume.
>
> If the latency of resume means we don't poll the key status until
> after it has been released, we can lose the keypress which woke the
> device.
>
> So on each interrupt, record that a press is pending, and in that
> case, report both the up and down event, ordered such that the second
> event is that one that reflects the current state.
>
> One event will normally be swallowed by the input layer if there was
> no change, but the result will be that every interrupt will produce at
> least one event.
>
> Signed-off-by: NeilBrown <neilb@suse.de>
>
> diff --git a/drivers/input/keyboard/gpio_keys.c
> b/drivers/input/keyboard/gpio_keys.c index 62bfce4..961e5e1 100644
> --- a/drivers/input/keyboard/gpio_keys.c
> +++ b/drivers/input/keyboard/gpio_keys.c
> @@ -40,6 +40,7 @@ struct gpio_button_data {
>   	spinlock_t lock;
>   	bool disabled;
>   	bool key_pressed;
> +	bool pending;
>   };
>   
>   struct gpio_keys_drvdata {
> @@ -335,6 +336,14 @@ static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata) if (state)
>   			input_event(input, type, button->code, button->value); } else {
> +		if (type == EV_KEY && bdata->pending) {
> +			/* Before reporting the observed state, report the
> +			 * alternate to be sure that a change is seen.
> +			 */
> +			bdata->pending = 0;
> +			input_event(input, type, button->code, !state);
> +			input_sync(input);
> +		}
>   		input_event(input, type, button->code, !!state);
>   	}Yes it si
>   	input_sync(input);
> @@ -361,6 +370,7 @@ static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id)
>   	BUG_ON(irq != bdata->irq);
>   
> +	bdata->pending = true;
>   	if (bdata->timer_debounce)
>   		mod_timer(&bdata->timer,
>   			jiffies + msecs_to_jiffies(bdata->timer_debounce));

The delay is between resume starting and the ISR running,
It is measured with oscilloscope between signal on button pin and
before irq enabling. And I doubly checked it by printk() right in
the ISR. So I cannot read the button state right in the ISR.

Your patch is simpler but it doesn't take into account one situation.
It is unlikely but what if the event is lost at 16:00 and reproduced
at 17:30, in some cases it is better to lose the event than recover it
out of time.

My patch works only for suspend/resume and ensure that reproduced
event is issued at time of lost.


-- 
Regards,
Ivan Khoronzhuk


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFC PATCH] Input: gpio_keys: Fix suspend/resume press event lost
  2013-01-31 13:25         ` ivan.khoronzhuk
@ 2013-02-04 22:40           ` NeilBrown
  2013-02-06 16:57             ` ivan.khoronzhuk
  0 siblings, 1 reply; 9+ messages in thread
From: NeilBrown @ 2013-02-04 22:40 UTC (permalink / raw)
  To: ivan.khoronzhuk
  Cc: Dmitry Torokhov, linux-kernel, linux-input, Bengt Jonsson,
	Mark Brown, Bill Pemberton

[-- Attachment #1: Type: text/plain, Size: 7062 bytes --]

On Thu, 31 Jan 2013 15:25:43 +0200 "ivan.khoronzhuk" <ivan.khoronzhuk@ti.com>
wrote:

> On 01/28/2013 08:51 PM, NeilBrown wrote:
> > On Mon, 28 Jan 2013 18:13:14 +0200 "ivan.khoronzhuk" <ivan.khoronzhuk@ti.com>
> > wrote:
> >
> >> On 01/22/2013 07:24 AM, NeilBrown wrote:
> >>> On Mon, 21 Jan 2013 15:57:18 -0800 Dmitry Torokhov
> >>> <dmitry.torokhov@gmail.com> wrote:
> >>>
> >>>> Hi Ivan,
> >>>>
> >>>> On Mon, Jan 21, 2013 at 03:15:14PM +0200, Ivan Khoronzhuk wrote:
> >>>>> Rebased on linux_omap/master.
> >>>>>
> >>>>> During suspend/resume the key press can be lost if time of resume
> >>>>> sequence is significant.
> >>>>>
> >>>>> If press event cannot be remembered then the driver can read the
> >>>>> current button state only in time of interrupt handling. But in some
> >>>>> cases when time between IRQ and IRQ handler is significant we can
> >>>>> read incorrect state. As a particular case, when device is in suspend
> >>>>> we press wakupable key and up it back in a jiffy, the interrupt
> >>>>> handler read the state of up but the interrupt source is press indeed.
> >>>>> As a result, in a OS like android, we resume then suspend right away
> >>>>> because the key state is not changed.
> >>>>>
> >>>>> This patch add to gpio_keys framework opportunity to recover lost of
> >>>>> press key event at resuming. The variable "key_pressed" from
> >>>>> gpio_button_data structure is not used for gpio keys, it is only used
> >>>>> for gpio irq keys, so it is logically used to remember press lost
> >>>>> while resuming.
> >>>> The same could happen if you delay processing of interrupt long enough
> >>>> during normal operation. If key is released by the time you get around
> >>>> to reading it you will not see a key press.
> >>>>
> >>>> To me this sounds like you need to speed up your resume process so that
> >>>> you can start serving interrupts quicker.
> >>>>
> >>> Agreed.  When I was looking at this I found that any genuine button press
> >>> would have at least 70msec between press and release, while the device could
> >>> wake up to the point of being able to handle interrupts in about 14msec.
> >>> That is enough of a gap to make it pointless to try to 'fix' the code.
> >>>
> >>> With enough verbose debugging enabled that 14msec can easily grow to
> >>> hundreds, but then if  you have debugging enabled to can discipline yourself
> >>> to hold the button for longer.
> >>>
> >>> Ivan: What sort of delay are you seeing between the button press and the
> >>> interrupt routine running?  And can you measure how long the button is
> >>> typically down for?
> >>>
> >>> NeilBrown
> >> In my case I have the delay between the button press and the ISR
> >> about 145ms. If the button down for 120ms the IRQ press event is
> >> lost and if 160ms event is captured. I cannot speed up resume
> >> process enough to guarantee correct work, so I wrote this fix.
> >>
> >
> > 145ms does sound like a long time.
> > You should be able to get precise timings by putting a printk in various
> > parts of the code and ensuring the kernel logs are getting precise timestamps.
> >
> > Then you could see if the delay is between resume starting and the ISR
> > running, or between the ISR and the gpio_work_func getting scheduled.
> >
> > I assume that you don't have ->debounce_interval set....
> >
> > If the ISR is running soon enough, it might be possible to read the GPIO from
> > the ISR - I think that would be a cleaner solution.
> >
> > If not, then you will need something that interpolates an extra key press.
> > In that case I would suggest my original patch - it seems simpler than yours
> > and doesn't make a special case out of suspend.  As Dmitry said, any delay
> > could cause a problem.
> > My patch simply ensures that there is at least one button event for each
> > interrupt by generating an extra event for the inverse of the current button
> > position.  If that state had already been noticed, the input subsystem will
> > filter the extra event out so it won't be visible elsewhere.
> >
> > For reference, my original patch is below.
> >
> > NeilBrown
> >
> > [PATCH] Input: gpio_keys - ensure we don't miss key-presses during resume.
> >
> > If the latency of resume means we don't poll the key status until
> > after it has been released, we can lose the keypress which woke the
> > device.
> >
> > So on each interrupt, record that a press is pending, and in that
> > case, report both the up and down event, ordered such that the second
> > event is that one that reflects the current state.
> >
> > One event will normally be swallowed by the input layer if there was
> > no change, but the result will be that every interrupt will produce at
> > least one event.
> >
> > Signed-off-by: NeilBrown <neilb@suse.de>
> >
> > diff --git a/drivers/input/keyboard/gpio_keys.c
> > b/drivers/input/keyboard/gpio_keys.c index 62bfce4..961e5e1 100644
> > --- a/drivers/input/keyboard/gpio_keys.c
> > +++ b/drivers/input/keyboard/gpio_keys.c
> > @@ -40,6 +40,7 @@ struct gpio_button_data {
> >   	spinlock_t lock;
> >   	bool disabled;
> >   	bool key_pressed;
> > +	bool pending;
> >   };
> >   
> >   struct gpio_keys_drvdata {
> > @@ -335,6 +336,14 @@ static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata) if (state)
> >   			input_event(input, type, button->code, button->value); } else {
> > +		if (type == EV_KEY && bdata->pending) {
> > +			/* Before reporting the observed state, report the
> > +			 * alternate to be sure that a change is seen.
> > +			 */
> > +			bdata->pending = 0;
> > +			input_event(input, type, button->code, !state);
> > +			input_sync(input);
> > +		}
> >   		input_event(input, type, button->code, !!state);
> >   	}Yes it si
> >   	input_sync(input);
> > @@ -361,6 +370,7 @@ static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id)
> >   	BUG_ON(irq != bdata->irq);
> >   
> > +	bdata->pending = true;
> >   	if (bdata->timer_debounce)
> >   		mod_timer(&bdata->timer,
> >   			jiffies + msecs_to_jiffies(bdata->timer_debounce));
> 
> The delay is between resume starting and the ISR running,
> It is measured with oscilloscope between signal on button pin and
> before irq enabling. And I doubly checked it by printk() right in
> the ISR. So I cannot read the button state right in the ISR.

Wow.  That is a big delay then.

> 
> Your patch is simpler but it doesn't take into account one situation.
> It is unlikely but what if the event is lost at 16:00 and reproduced
> at 17:30, in some cases it is better to lose the event than recover it
> out of time.

Is that at all a realistic scenario?  Interrupts disabled for 90 minutes!  If
that happened you would have more to worry about than one stray button press
(which is still a valid button press, but is a bit late).

I think you are over-complicating things.  Just keep it simple:  Generate at
least one button event on every interrupt.

NeilBrown

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 828 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFC PATCH] Input: gpio_keys: Fix suspend/resume press event lost
  2013-02-04 22:40           ` NeilBrown
@ 2013-02-06 16:57             ` ivan.khoronzhuk
  0 siblings, 0 replies; 9+ messages in thread
From: ivan.khoronzhuk @ 2013-02-06 16:57 UTC (permalink / raw)
  To: NeilBrown
  Cc: Dmitry Torokhov, linux-kernel, linux-input, Bengt Jonsson,
	Mark Brown, Bill Pemberton

On 02/05/2013 12:40 AM, NeilBrown wrote:
> On Thu, 31 Jan 2013 15:25:43 +0200 "ivan.khoronzhuk" <ivan.khoronzhuk@ti.com>
> wrote:
>
>> On 01/28/2013 08:51 PM, NeilBrown wrote:
>>> On Mon, 28 Jan 2013 18:13:14 +0200 "ivan.khoronzhuk" <ivan.khoronzhuk@ti.com>
>>> wrote:
>>>
>>>> On 01/22/2013 07:24 AM, NeilBrown wrote:
>>>>> On Mon, 21 Jan 2013 15:57:18 -0800 Dmitry Torokhov
>>>>> <dmitry.torokhov@gmail.com> wrote:
>>>>>
>>>>>> Hi Ivan,
>>>>>>
>>>>>> On Mon, Jan 21, 2013 at 03:15:14PM +0200, Ivan Khoronzhuk wrote:
>>>>>>> Rebased on linux_omap/master.
>>>>>>>
>>>>>>> During suspend/resume the key press can be lost if time of resume
>>>>>>> sequence is significant.
>>>>>>>
>>>>>>> If press event cannot be remembered then the driver can read the
>>>>>>> current button state only in time of interrupt handling. But in some
>>>>>>> cases when time between IRQ and IRQ handler is significant we can
>>>>>>> read incorrect state. As a particular case, when device is in suspend
>>>>>>> we press wakupable key and up it back in a jiffy, the interrupt
>>>>>>> handler read the state of up but the interrupt source is press indeed.
>>>>>>> As a result, in a OS like android, we resume then suspend right away
>>>>>>> because the key state is not changed.
>>>>>>>
>>>>>>> This patch add to gpio_keys framework opportunity to recover lost of
>>>>>>> press key event at resuming. The variable "key_pressed" from
>>>>>>> gpio_button_data structure is not used for gpio keys, it is only used
>>>>>>> for gpio irq keys, so it is logically used to remember press lost
>>>>>>> while resuming.
>>>>>> The same could happen if you delay processing of interrupt long enough
>>>>>> during normal operation. If key is released by the time you get around
>>>>>> to reading it you will not see a key press.
>>>>>>
>>>>>> To me this sounds like you need to speed up your resume process so that
>>>>>> you can start serving interrupts quicker.
>>>>>>
>>>>> Agreed.  When I was looking at this I found that any genuine button press
>>>>> would have at least 70msec between press and release, while the device could
>>>>> wake up to the point of being able to handle interrupts in about 14msec.
>>>>> That is enough of a gap to make it pointless to try to 'fix' the code.
>>>>>
>>>>> With enough verbose debugging enabled that 14msec can easily grow to
>>>>> hundreds, but then if  you have debugging enabled to can discipline yourself
>>>>> to hold the button for longer.
>>>>>
>>>>> Ivan: What sort of delay are you seeing between the button press and the
>>>>> interrupt routine running?  And can you measure how long the button is
>>>>> typically down for?
>>>>>
>>>>> NeilBrown
>>>> In my case I have the delay between the button press and the ISR
>>>> about 145ms. If the button down for 120ms the IRQ press event is
>>>> lost and if 160ms event is captured. I cannot speed up resume
>>>> process enough to guarantee correct work, so I wrote this fix.
>>>>
>>> 145ms does sound like a long time.
>>> You should be able to get precise timings by putting a printk in various
>>> parts of the code and ensuring the kernel logs are getting precise timestamps.
>>>
>>> Then you could see if the delay is between resume starting and the ISR
>>> running, or between the ISR and the gpio_work_func getting scheduled.
>>>
>>> I assume that you don't have ->debounce_interval set....
>>>
>>> If the ISR is running soon enough, it might be possible to read the GPIO from
>>> the ISR - I think that would be a cleaner solution.
>>>
>>> If not, then you will need something that interpolates an extra key press.
>>> In that case I would suggest my original patch - it seems simpler than yours
>>> and doesn't make a special case out of suspend.  As Dmitry said, any delay
>>> could cause a problem.
>>> My patch simply ensures that there is at least one button event for each
>>> interrupt by generating an extra event for the inverse of the current button
>>> position.  If that state had already been noticed, the input subsystem will
>>> filter the extra event out so it won't be visible elsewhere.
>>>
>>> For reference, my original patch is below.
>>>
>>> NeilBrown
>>>
>>> [PATCH] Input: gpio_keys - ensure we don't miss key-presses during resume.
>>>
>>> If the latency of resume means we don't poll the key status until
>>> after it has been released, we can lose the keypress which woke the
>>> device.
>>>
>>> So on each interrupt, record that a press is pending, and in that
>>> case, report both the up and down event, ordered such that the second
>>> event is that one that reflects the current state.
>>>
>>> One event will normally be swallowed by the input layer if there was
>>> no change, but the result will be that every interrupt will produce at
>>> least one event.
>>>
>>> Signed-off-by: NeilBrown <neilb@suse.de>
>>>
>>> diff --git a/drivers/input/keyboard/gpio_keys.c
>>> b/drivers/input/keyboard/gpio_keys.c index 62bfce4..961e5e1 100644
>>> --- a/drivers/input/keyboard/gpio_keys.c
>>> +++ b/drivers/input/keyboard/gpio_keys.c
>>> @@ -40,6 +40,7 @@ struct gpio_button_data {
>>>    	spinlock_t lock;
>>>    	bool disabled;
>>>    	bool key_pressed;
>>> +	bool pending;
>>>    };
>>>    
>>>    struct gpio_keys_drvdata {
>>> @@ -335,6 +336,14 @@ static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata) if (state)
>>>    			input_event(input, type, button->code, button->value); } else {
>>> +		if (type == EV_KEY && bdata->pending) {
>>> +			/* Before reporting the observed state, report the
>>> +			 * alternate to be sure that a change is seen.
>>> +			 */
>>> +			bdata->pending = 0;
>>> +			input_event(input, type, button->code, !state);
>>> +			input_sync(input);
>>> +		}
>>>    		input_event(input, type, button->code, !!state);
>>>    	}Yes it si
>>>    	input_sync(input);
>>> @@ -361,6 +370,7 @@ static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id)
>>>    	BUG_ON(irq != bdata->irq);
>>>    
>>> +	bdata->pending = true;
>>>    	if (bdata->timer_debounce)
>>>    		mod_timer(&bdata->timer,
>>>    			jiffies + msecs_to_jiffies(bdata->timer_debounce));
>> The delay is between resume starting and the ISR running,
>> It is measured with oscilloscope between signal on button pin and
>> before irq enabling. And I doubly checked it by printk() right in
>> the ISR. So I cannot read the button state right in the ISR.
> Wow.  That is a big delay then.
>
>> Your patch is simpler but it doesn't take into account one situation.
>> It is unlikely but what if the event is lost at 16:00 and reproduced
>> at 17:30, in some cases it is better to lose the event than recover it
>> out of time.
> Is that at all a realistic scenario?  Interrupts disabled for 90 minutes!  If
> that happened you would have more to worry about than one stray button press
> (which is still a valid button press, but is a bit late).
>
> I think you are over-complicating things.  Just keep it simple:  Generate at
> least one button event on every interrupt.
>
> NeilBrown
You are right I over-complicated a little. But this is slightly not
I mean. The interrupts are enabled always, we don't suspend, we just
lost interrupt completely for some reason (for instance, up event).
And then we press key later on, as an example in 1 hour. In that
case we reproduce the lost (up) event in 1 hour that is not needed any
more.

Any way the problem is more in resume latency than in gpio-keys.
When latency is normal the situation is very rarely and is not so
major.

In my case it is enough to use your patch.

Thanks for your comments and the interesting approach.

-- 
Regards,
Ivan Khoronzhuk


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2013-02-06 16:58 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-01-21 13:15 [RFC PATCH] Input: gpio_keys: Fix suspend/resume press event lost Ivan Khoronzhuk
2013-01-21 23:57 ` Dmitry Torokhov
2013-01-22  5:24   ` NeilBrown
2013-01-28 16:13     ` ivan.khoronzhuk
2013-01-28 18:51       ` NeilBrown
2013-01-31 13:25         ` ivan.khoronzhuk
2013-02-04 22:40           ` NeilBrown
2013-02-06 16:57             ` ivan.khoronzhuk
  -- strict thread matches above, loose matches on Subject: below --
2013-01-18 12:51 Ivan Khoronzhuk

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).