diff mbox series

[1/4] serial: Fix _serial_puts using \n\r instead of \r\n

Message ID 20220404181800.2258698-2-sean.anderson@seco.com
State Accepted
Commit 2c777488b6709dea4aadfdadbbfccc0de751a022
Delegated to: Tom Rini
Headers show
Series serial: Finish adding puts support | expand

Commit Message

Sean Anderson April 4, 2022, 6:17 p.m. UTC
A string like "test\n" would be broken up into the following sequence of
prints by _serial_puts:

	puts("test\n")
	putc('\r')

Although functionally this is the same as \r\n, it is not the standard
sequence and caused tests to fail. Fix this by excluding the '\n' from
the initial print. The above string will now be broken up like

	puts("test")
	puts("\r\n")

Since we may now need to call ops->puts twice (with the associated retry
logic), break that part of the function off into a helper.

Fixes: 7a76347189 ("serial: dm: Add support for puts")
Signed-off-by: Sean Anderson <sean.anderson@seco.com>
---

 drivers/serial/serial-uclass.c | 32 ++++++++++++++++++++++----------
 1 file changed, 22 insertions(+), 10 deletions(-)

Comments

Tom Rini April 15, 2022, 12:08 p.m. UTC | #1
On Mon, Apr 04, 2022 at 02:17:57PM -0400, Sean Anderson wrote:

> A string like "test\n" would be broken up into the following sequence of
> prints by _serial_puts:
> 
> 	puts("test\n")
> 	putc('\r')
> 
> Although functionally this is the same as \r\n, it is not the standard
> sequence and caused tests to fail. Fix this by excluding the '\n' from
> the initial print. The above string will now be broken up like
> 
> 	puts("test")
> 	puts("\r\n")
> 
> Since we may now need to call ops->puts twice (with the associated retry
> logic), break that part of the function off into a helper.
> 
> Fixes: 7a76347189 ("serial: dm: Add support for puts")
> Signed-off-by: Sean Anderson <sean.anderson@seco.com>

Applied to u-boot/master, thanks!
diff mbox series

Patch

diff --git a/drivers/serial/serial-uclass.c b/drivers/serial/serial-uclass.c
index 10d6b800e2..30650e37b0 100644
--- a/drivers/serial/serial-uclass.c
+++ b/drivers/serial/serial-uclass.c
@@ -198,6 +198,22 @@  static void _serial_putc(struct udevice *dev, char ch)
 	} while (err == -EAGAIN);
 }
 
+static int __serial_puts(struct udevice *dev, const char *str, size_t len)
+{
+	struct dm_serial_ops *ops = serial_get_ops(dev);
+
+	do {
+		ssize_t written = ops->puts(dev, str, len);
+
+		if (written < 0)
+			return written;
+		str += written;
+		len -= written;
+	} while (len);
+
+	return 0;
+}
+
 static void _serial_puts(struct udevice *dev, const char *str)
 {
 	struct dm_serial_ops *ops = serial_get_ops(dev);
@@ -210,19 +226,15 @@  static void _serial_puts(struct udevice *dev, const char *str)
 
 	do {
 		const char *newline = strchrnul(str, '\n');
-		size_t len = newline - str + !!*newline;
+		size_t len = newline - str;
 
-		do {
-			ssize_t written = ops->puts(dev, str, len);
+		if (__serial_puts(dev, str, len))
+			return;
 
-			if (written < 0)
-				return;
-			str += written;
-			len -= written;
-		} while (len);
+		if (*newline && __serial_puts(dev, "\r\n", 2))
+			return;
 
-		if (*newline)
-			_serial_putc(dev, '\r');
+		str += len + !!*newline;
 	} while (*str);
 }