diff mbox series

[1/3] Input: applespi: Don't wait for responses to commands indefinitely.

Message ID 20210217190718.11035-1-ronald@innovation.ch
State Accepted
Commit 0ce1ac23149c6da939a5926c098c270c58c317a0
Headers show
Series [1/3] Input: applespi: Don't wait for responses to commands indefinitely. | expand

Commit Message

Ronald Tschalär Feb. 17, 2021, 7:07 p.m. UTC
The response to a command may never arrive or it may be corrupted (and
hence dropped) for some reason. While exceedingly rare, when it did
happen it blocked all further commands. One way to fix this was to
do a suspend/resume. However, recovering automatically seems like a
nicer option. Hence this puts a time limit (1 sec) on how long we're
willing to wait for a response, after which we assume it got lost.

Signed-off-by: Ronald Tschalär <ronald@innovation.ch>
---
 drivers/input/keyboard/applespi.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

Comments

Dmitry Torokhov Feb. 17, 2021, 8:23 p.m. UTC | #1
Hi Ronald,

On Wed, Feb 17, 2021 at 11:07:16AM -0800, Ronald Tschalär wrote:
> @@ -869,7 +878,7 @@ static int applespi_send_cmd_msg(struct applespi_data *applespi)
>  		return sts;
>  	}
>  
> -	applespi->cmd_msg_queued = true;
> +	applespi->cmd_msg_queued = ktime_get();

Will it be OK if I change this to ktime_get_coarse()? I do not think we
need exact time here, and the coarse variant is cheaper I believe.

Thanks.
Dmitry Torokhov Feb. 19, 2021, 7:12 p.m. UTC | #2
On Wed, Feb 17, 2021 at 12:45:51PM -0800, Life is hard, and then you die wrote:
> 

>   Hi Dmitry,

> 

> On Wed, Feb 17, 2021 at 12:23:23PM -0800, Dmitry Torokhov wrote:

> > 

> > On Wed, Feb 17, 2021 at 11:07:16AM -0800, Ronald Tschalär wrote:

> > > @@ -869,7 +878,7 @@ static int applespi_send_cmd_msg(struct applespi_data *applespi)

> > >  		return sts;

> > >  	}

> > >  

> > > -	applespi->cmd_msg_queued = true;

> > > +	applespi->cmd_msg_queued = ktime_get();

> > 

> > Will it be OK if I change this to ktime_get_coarse()? I do not think we

> > need exact time here, and the coarse variant is cheaper I believe.

> 

> Certainly - I just wasn't aware of the coarse variant.


Applied, thank you.

-- 
Dmitry
Dmitry Torokhov Feb. 19, 2021, 7:12 p.m. UTC | #3
On Wed, Feb 17, 2021 at 11:07:17AM -0800, Ronald Tschalär wrote:
> For some reason, when the system is under heavy CPU load, the read

> following the write sometimes occurs unusually quickly, resulting in

> the read data not being quite ready and hence a bad packet getting read.

> Adding another delay after reading the status message appears to fix

> this.

> 

> Signed-off-by: Ronald Tschalär <ronald@innovation.ch>


Applied, thank you.

-- 
Dmitry
diff mbox series

Patch

diff --git a/drivers/input/keyboard/applespi.c b/drivers/input/keyboard/applespi.c
index d22223154177f..8494bf610fd70 100644
--- a/drivers/input/keyboard/applespi.c
+++ b/drivers/input/keyboard/applespi.c
@@ -48,6 +48,7 @@ 
 #include <linux/efi.h>
 #include <linux/input.h>
 #include <linux/input/mt.h>
+#include <linux/ktime.h>
 #include <linux/leds.h>
 #include <linux/module.h>
 #include <linux/spinlock.h>
@@ -409,7 +410,7 @@  struct applespi_data {
 	unsigned int			cmd_msg_cntr;
 	/* lock to protect the above parameters and flags below */
 	spinlock_t			cmd_msg_lock;
-	bool				cmd_msg_queued;
+	ktime_t				cmd_msg_queued;
 	enum applespi_evt_type		cmd_evt_type;
 
 	struct led_classdev		backlight_info;
@@ -729,7 +730,7 @@  static void applespi_msg_complete(struct applespi_data *applespi,
 		wake_up_all(&applespi->drain_complete);
 
 	if (is_write_msg) {
-		applespi->cmd_msg_queued = false;
+		applespi->cmd_msg_queued = 0;
 		applespi_send_cmd_msg(applespi);
 	}
 
@@ -771,8 +772,16 @@  static int applespi_send_cmd_msg(struct applespi_data *applespi)
 		return 0;
 
 	/* check whether send is in progress */
-	if (applespi->cmd_msg_queued)
-		return 0;
+	if (applespi->cmd_msg_queued) {
+		if (ktime_ms_delta(ktime_get(), applespi->cmd_msg_queued) < 1000)
+			return 0;
+
+		dev_warn(&applespi->spi->dev, "Command %d timed out\n",
+			 applespi->cmd_evt_type);
+
+		applespi->cmd_msg_queued = 0;
+		applespi->write_active = false;
+	}
 
 	/* set up packet */
 	memset(packet, 0, APPLESPI_PACKET_SIZE);
@@ -869,7 +878,7 @@  static int applespi_send_cmd_msg(struct applespi_data *applespi)
 		return sts;
 	}
 
-	applespi->cmd_msg_queued = true;
+	applespi->cmd_msg_queued = ktime_get();
 	applespi->write_active = true;
 
 	return 0;
@@ -1921,7 +1930,7 @@  static int __maybe_unused applespi_resume(struct device *dev)
 	applespi->drain = false;
 	applespi->have_cl_led_on = false;
 	applespi->have_bl_level = 0;
-	applespi->cmd_msg_queued = false;
+	applespi->cmd_msg_queued = 0;
 	applespi->read_active = false;
 	applespi->write_active = false;