diff mbox

spi: Add a timeout when waiting for transfers

Message ID 1391121536-10937-1-git-send-email-broonie@kernel.org (mailing list archive)
State Accepted
Commit 16a0ce4e10edfe376b3071f161c71c4160c89d6b
Headers show

Commit Message

Mark Brown Jan. 30, 2014, 10:38 p.m. UTC
From: Mark Brown <broonie@linaro.org>

Don't wait indefinitely for transfers to complete but time out after 10ms
more than we expect the transfer to take on the wire.

Signed-off-by: Mark Brown <broonie@linaro.org>
---
 drivers/spi/spi.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
diff mbox

Patch

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 7f23cf9afa79..1826a50c2aaf 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -710,6 +710,7 @@  static int spi_transfer_one_message(struct spi_master *master,
 	bool cur_cs = true;
 	bool keep_cs = false;
 	int ret = 0;
+	int ms = 1;
 
 	spi_set_cs(msg->spi, true);
 
@@ -727,7 +728,16 @@  static int spi_transfer_one_message(struct spi_master *master,
 
 		if (ret > 0) {
 			ret = 0;
-			wait_for_completion(&master->xfer_completion);
+			ms = xfer->len * 8 * 1000 / xfer->speed_hz;
+			ms += 10; /* some tolerance */
+
+			ms = wait_for_completion_timeout(&master->xfer_completion,
+							 msecs_to_jiffies(ms));
+		}
+
+		if (ms == 0) {
+			dev_err(&msg->spi->dev, "SPI transfer timed out\n");
+			msg->status = -ETIMEDOUT;
 		}
 
 		trace_spi_transfer_stop(msg, xfer);