lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Wed, 18 Mar 2020 15:26:35 +0100
From:   Heiko Stuebner <heiko@...ech.de>
To:     gregkh@...uxfoundation.org
Cc:     jslaby@...e.com, andriy.shevchenko@...ux.intel.com,
        matwey.kornilov@...il.com, linux-serial@...r.kernel.org,
        linux-kernel@...r.kernel.org, heiko@...ech.de,
        Heiko Stuebner <heiko.stuebner@...obroma-systems.com>
Subject: [PATCH 2/7] serial: 8250: add serial_in_poll_timeout helper

From: Heiko Stuebner <heiko.stuebner@...obroma-systems.com>

In cases where a serial register needs to be polled until a specific
state, this should have a timeout as noted in the thread bringing em485
support to 8250_dw.

To not re-implement timeout handling in each case, add a helper modelled
after readx_poll_timeout / regmap_read_poll_timeout to facilitate this.

Signed-off-by: Heiko Stuebner <heiko.stuebner@...obroma-systems.com>
---
 drivers/tty/serial/8250/8250.h | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/drivers/tty/serial/8250/8250.h b/drivers/tty/serial/8250/8250.h
index 33ad9d6de532..50a4c174410d 100644
--- a/drivers/tty/serial/8250/8250.h
+++ b/drivers/tty/serial/8250/8250.h
@@ -118,6 +118,40 @@ static inline void serial_out(struct uart_8250_port *up, int offset, int value)
 	up->port.serial_out(&up->port, offset, value);
 }
 
+/**
+ * serial_in_poll_timeout - Poll until a condition is met or a timeout occurs
+ *
+ * @port: uart_8250_port to read from
+ * @offs: Offset to poll
+ * @val: Integer variable to read the value into
+ * @cond: Break condition (usually involving @val)
+ * @timeout_us: Timeout in us, 0 means never timeout
+ *
+ * Returns 0 on success and -ETIMEDOUT upon a timeout or the serial_in
+ * error return value in case of a error read.
+ *
+ * This is modelled after the readx_poll_timeout macros in linux/iopoll.h.
+ */
+#define serial_in_poll_timeout(port, offs, val, cond, timeout_us) \
+({ \
+	u64 __timeout_us = (timeout_us); \
+	ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
+	for (;;) { \
+		val = serial_in((port), (offs)); \
+		if (val < 0) \
+			break; \
+		if (cond) \
+			break; \
+		if ((__timeout_us) && \
+		    ktime_compare(ktime_get(), __timeout) > 0) { \
+			val = serial_in((port), (offs)); \
+			break; \
+		} \
+		cpu_relax(); \
+	} \
+	(val < 0) ? val : ((cond) ? 0 : -ETIMEDOUT); \
+})
+
 void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p);
 
 static inline int serial_dl_read(struct uart_8250_port *up)
-- 
2.24.1

Powered by blists - more mailing lists