This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 66bad554049 drivers/serial: batch TX buffer fills with uart_putxmitbuf
66bad554049 is described below

commit 66bad55404974b732b432d96ff00e4c75f4ccab4
Author: yangsong8 <[email protected]>
AuthorDate: Tue Nov 3 10:37:14 2026 +0800

    drivers/serial: batch TX buffer fills with uart_putxmitbuf
    
    uart_writev() filled the TX buffer one byte at a time via
    uart_putxmitchar(), whose per-byte call and branch cost dominates
    large writes (e.g. USB CDC/ACM block transfers).
    
    Rework uart_putxmitbuf() to copy a whole buffer into the TX ring in
    bulk with memcpy over the contiguous free region, and to do the
    blocking itself when the ring is full: wait for TX space, return
    -EAGAIN for O_NONBLOCK, -EINTR on a signal and -ENOTCONN on a removed
    device, or the number of bytes already copied.
    
    Rewrite uart_writev() and uart_irqwrite() around it: locate the next
    character that needs output post-processing (CR under OCRNL, NL under
    ONLCR/ONLRET) with memchr, copy the run of ordinary bytes before it in
    one call, then handle that one character.  A short count from
    uart_putxmitbuf() ends the write with the partial count, so a signal
    still ends a blocking write().
    
    The bulk copy is now always used, so CONFIG_SERIAL_TXBULK is removed,
    also from the ab32vg1-generic nsh and ostest defconfigs that set it.
    
    Assisted-by: Kiro:claude-opus-5-5
    Signed-off-by: yangsong8 <[email protected]>
---
 .../ab32vg1/ab32vg1-generic/configs/nsh/defconfig  |   1 -
 .../ab32vg1-generic/configs/ostest/defconfig       |   1 -
 drivers/serial/Kconfig                             |  11 -
 drivers/serial/serial.c                            | 534 ++++++++++++---------
 4 files changed, 303 insertions(+), 244 deletions(-)

diff --git a/boards/risc-v/ab32vg1/ab32vg1-generic/configs/nsh/defconfig 
b/boards/risc-v/ab32vg1/ab32vg1-generic/configs/nsh/defconfig
index 50e427ed519..2eee7dbfb73 100644
--- a/boards/risc-v/ab32vg1/ab32vg1-generic/configs/nsh/defconfig
+++ b/boards/risc-v/ab32vg1/ab32vg1-generic/configs/nsh/defconfig
@@ -130,7 +130,6 @@ CONFIG_SCHED_DUMP_STACK=y
 CONFIG_SCHED_DUMP_TASKS=y
 CONFIG_SCHED_INLINE_ADDPRIORITIZED=y
 CONFIG_SCHED_WAITPID=y
-CONFIG_SERIAL_TXBULK=y
 CONFIG_SIG_PREALLOC_IRQ_ACTIONS=8
 CONFIG_STACK_COLORATION=y
 CONFIG_START_DAY=24
diff --git a/boards/risc-v/ab32vg1/ab32vg1-generic/configs/ostest/defconfig 
b/boards/risc-v/ab32vg1/ab32vg1-generic/configs/ostest/defconfig
index a655d2ea718..d4ca1bd83e6 100644
--- a/boards/risc-v/ab32vg1/ab32vg1-generic/configs/ostest/defconfig
+++ b/boards/risc-v/ab32vg1/ab32vg1-generic/configs/ostest/defconfig
@@ -79,7 +79,6 @@ CONFIG_SCHED_DUMP_STACK=y
 CONFIG_SCHED_DUMP_TASKS=y
 CONFIG_SCHED_INLINE_ADDPRIORITIZED=y
 CONFIG_SCHED_WAITPID=y
-CONFIG_SERIAL_TXBULK=y
 CONFIG_SIG_PREALLOC_IRQ_ACTIONS=8
 CONFIG_STACK_COLORATION=y
 CONFIG_START_DAY=24
diff --git a/drivers/serial/Kconfig b/drivers/serial/Kconfig
index 5df44cf3e8f..6afd4223cbd 100644
--- a/drivers/serial/Kconfig
+++ b/drivers/serial/Kconfig
@@ -315,17 +315,6 @@ config SERIAL_TERMIOS
                Note: other software settings (echo, \r\n<->\n, break, tcflush)
                are always supported.
 
-config SERIAL_TXBULK
-       bool "Bulk TX buffer copy"
-       default !DEFAULT_SMALL
-       ---help---
-               Copy whole runs of raw output into the TX buffer with memcpy()
-               instead of one byte at a time.  Roughly doubles the write()
-               throughput on slow cores at the cost of about 150 bytes of
-               code.  Only used on non-console ports with no output
-               post-processing and no echo; every other case keeps the
-               byte-wise path.
-
 config TTY_LAUNCH
        bool "Enable feature TTY launch program"
        depends on SCHED_HPWORK
diff --git a/drivers/serial/serial.c b/drivers/serial/serial.c
index 1a6225dda6b..383d0256e6e 100644
--- a/drivers/serial/serial.c
+++ b/drivers/serial/serial.c
@@ -120,10 +120,8 @@ static void    uart_poll_notify(FAR uart_dev_t *dev, 
unsigned int min,
 
 /* Write support */
 
-#ifdef CONFIG_SERIAL_TXBULK
-static size_t  uart_putxmitbuf(FAR uart_dev_t *dev, FAR const char *buf,
-                               size_t len);
-#endif
+static ssize_t uart_putxmitbuf(FAR uart_dev_t *dev, FAR const char *buf,
+                               size_t len, bool oktoblock);
 static int     uart_putxmitchar(FAR uart_dev_t *dev, int ch,
                                 bool oktoblock);
 static inline ssize_t uart_irqwrite(FAR uart_dev_t *dev,
@@ -253,209 +251,218 @@ static void uart_poll_notify(FAR uart_dev_t *dev, 
unsigned int min,
   leave_critical_section(flags);
 }
 
-#ifdef CONFIG_SERIAL_TXBULK
 /****************************************************************************
  * Name: uart_putxmitbuf
  *
  * Description:
- *   Copy as many bytes as currently fit into the TX buffer, without
- *   blocking.  Returns the number of bytes copied, which may be zero if
- *   the TX buffer is full.  The caller handles a full buffer with
- *   uart_putxmitchar().
+ *   Copy up to 'len' bytes from 'buf' into the TX ring buffer,
+ *   blocking (when 'oktoblock' is true) to wait for free space.  Data is
+ *   moved in bulk with memcpy over the contiguous free region, removing the
+ *   per-byte call/branch overhead of adding one character at a time.  This
+ *   dominates large raw-mode writes (e.g. block transfers to USB CDC/ACM).
+ *
+ *   Concurrency: dev->xmit.head is owned by the writer under xmit.lock,
+ *   which the caller must hold.  dev->xmit.tail only advances on the drain
+ *   side, so reading a stale tail only underestimates the free space (safe,
+ *   never overwrites unread data).
+ *
+ * Returned Value:
+ *   The number of bytes copied into the TX buffer.  If no byte at all could
+ *   be copied, a negated errno value is returned instead (-EINTR if a signal
+ *   was received, -ENOTCONN if a removable device was disconnected, or
+ *   -EAGAIN if non-blocking and the buffer is full).
  *
  ****************************************************************************/
 
-static size_t uart_putxmitbuf(FAR uart_dev_t *dev, FAR const char *buf,
-                              size_t len)
+static ssize_t uart_putxmitbuf(FAR uart_dev_t *dev, FAR const char *buf,
+                               size_t len, bool oktoblock)
 {
-  size_t total = 0;
-  size_t nfree;
-  size_t ncopy;
-  int head = dev->xmit.head;
-  int tail = dev->xmit.tail;  /* Snapshot: the drain side only frees space */
+  size_t nwritten = 0;
+  irqstate_t flags;
+  int nexthead;
+  int ret;
 
-  while (total < len)
+  /* Loop until all data has been added to the TX buffer (or we stop early
+   * because of an error or a full buffer in non-blocking mode).
+   */
+
+  while (nwritten < len)
     {
-      /* Contiguous free space at the head, keeping one byte unused to
-       * distinguish a full buffer from an empty one.
+      int head = dev->xmit.head;
+      int tail = dev->xmit.tail;
+      size_t space;
+      size_t chunk;
+
+      /* Determine the contiguous free space at the head of the ring
+       * buffer.
        */
 
-      if (head < tail)
+      if (dev->xmit.buffer == NULL)
         {
-          nfree = tail - head - 1;
+          space = 0;
         }
-      else if (tail > 0)
+      else if (head >= tail)
         {
-          nfree = dev->xmit.size - head;
+          /* Contiguous space up to the physical end of the buffer.  If tail
+           * is at 0, leave the final slot free so that head == tail keeps
+           * meaning "empty".
+           */
+
+          space = dev->xmit.size - head;
+          if (tail == 0)
+            {
+              space -= 1;
+            }
         }
       else
         {
-          nfree = dev->xmit.size - head - 1;
+          space = tail - head - 1;
         }
 
-      if (nfree == 0)
+      if (space > 0)
         {
-          break;
-        }
+          /* Copy as much as fits into the contiguous region, then advance
+           * (and wrap) the head pointer.
+           */
 
-      ncopy = MIN(len - total, nfree);
-      memcpy(&dev->xmit.buffer[head], buf + total, ncopy);
-      total += ncopy;
+          chunk = MIN(space, len - nwritten);
+          memcpy(&dev->xmit.buffer[head], &buf[nwritten], chunk);
 
-      /* Publish the new head only after the data is in place */
+          head += chunk;
+          if (head >= dev->xmit.size)
+            {
+              head = 0;
+            }
 
-      head += ncopy;
-      if (head >= dev->xmit.size)
-        {
-          head = 0;
+          dev->xmit.head = head;
+          nwritten      += chunk;
+          continue;
         }
 
-      dev->xmit.head = head;
-    }
-
-  return total;
-}
+      /* The TX buffer is full (or not yet available).  Should we block,
+       * waiting for the hardware to remove some data from the TX buffer?
+       */
 
-#endif /* CONFIG_SERIAL_TXBULK */
+      if (!oktoblock)
+        {
+          /* The caller has requested that we not block for data.  Return the
+           * count copied so far, or -EAGAIN if nothing was copied.
+           */
 
-/****************************************************************************
- * Name: uart_putxmitchar
- ****************************************************************************/
+          return nwritten > 0 ? nwritten : -EAGAIN;
+        }
 
-static int uart_putxmitchar(FAR uart_dev_t *dev, int ch, bool oktoblock)
-{
-  irqstate_t flags;
-  int nexthead;
-  int ret;
+      /* The following steps must be atomic with respect to serial interrupt
+       * handling.
+       *
+       * This critical section is also used for the serialization with the
+       * up_putc-based syslog channels.
+       * See https://github.com/apache/nuttx/issues/14662
+       */
 
-  /* Loop until we are able to add the character to the TX buffer. */
+      flags = enter_critical_section();
 
-  for (; ; )
-    {
-      /* Increment to see what the next head pointer will be.
-       * We need to use the "next" head pointer to determine when the
-       * circular buffer would overrun
+      /* Check again...  In certain race conditions an interrupt may have
+       * occurred between the test at the top of the loop and entering the
+       * critical section and the TX buffer may no longer be full.
+       *
+       * NOTE: On certain devices, such as USB CDC/ACM, the entire TX buffer
+       * may have been emptied in this race condition.  In that case, the
+       * logic would hang below waiting for space in the TX buffer without
+       * this test.
        */
 
-      nexthead = dev->xmit.head + 1;
-      if (nexthead >= dev->xmit.size)
+      if (dev->xmit.buffer == NULL)
         {
-          nexthead = 0;
+          nexthead = dev->xmit.tail;
+        }
+      else
+        {
+          nexthead = dev->xmit.head + 1;
+          if (nexthead >= dev->xmit.size)
+            {
+              nexthead = 0;
+            }
         }
-
-      /* Check if the TX buffer is full */
 
       if (nexthead != dev->xmit.tail)
         {
-          /* No.. not full.  Add the character to the TX buffer and return. */
+          /* Space is available again; retry the copy. */
 
-          dev->xmit.buffer[dev->xmit.head] = ch;
-          dev->xmit.head = nexthead;
-          break;
+          leave_critical_section(flags);
+          continue;
         }
 
-      /* The TX buffer is full.  Should be block, waiting for the hardware
-       * to remove some data from the TX buffer?
+#ifdef CONFIG_SERIAL_REMOVABLE
+      /* Check if the removable device is no longer connected while we have
+       * interrupts off.  We do not want the transition to occur as a race
+       * condition before we begin the wait.
        */
 
-      else if (oktoblock)
+      if (dev->disconnected)
         {
-          /* The following steps must be atomic with respect to serial
-           * interrupt handling.
-           *
-           * This critical section is also used for the serialization
-           * with the up_putc-based syslog channels.
-           * See https://github.com/apache/nuttx/issues/14662
-           */
-
-          flags = enter_critical_section();
-
-          /* Check again...  In certain race conditions an interrupt may
-           * have occurred between the test at the top of the loop and
-           * entering the critical section and the TX buffer may no longer
-           * be full.
-           *
-           * NOTE: On certain devices, such as USB CDC/ACM, the entire TX
-           * buffer may have been emptied in this race condition.  In that
-           * case, the logic would hang below waiting for space in the TX
-           * buffer without this test.
-           */
-
-          if (nexthead != dev->xmit.tail)
-            {
-              ret = OK;
-            }
-
-#ifdef CONFIG_SERIAL_REMOVABLE
-          /* Check if the removable device is no longer connected while we
-           * have interrupts off.  We do not want the transition to occur
-           * as a race condition before we begin the wait.
-           */
-
-          else if (dev->disconnected)
-            {
-              ret = -ENOTCONN;
-            }
+          leave_critical_section(flags);
+          return nwritten > 0 ? nwritten : -ENOTCONN;
+        }
 #endif
-          else
-            {
-              /* Wait for some characters to be sent from the buffer with
-               * the TX interrupt enabled.  When the TX interrupt is enabled,
-               * uart_xmitchars() should execute and remove some of the data
-               * from the TX buffer.
-               *
-               * NOTE that interrupts will be re-enabled while we wait for
-               * the semaphore.
-               */
+
+      /* Wait for some characters to be sent from the buffer with the TX
+       * interrupt enabled.  When the TX interrupt is enabled,
+       * uart_xmitchars() should execute and remove some of the data from
+       * the TX buffer.
+       *
+       * NOTE that interrupts will be re-enabled while we wait for the
+       * semaphore.
+       */
 
 #ifdef CONFIG_SERIAL_TXDMA
-              uart_dmatxavail(dev);
+      uart_dmatxavail(dev);
 #endif
-              uart_enabletxint(dev);
-              ret = nxsem_wait(&dev->xmitsem);
-              uart_disabletxint(dev);
-            }
-
-          leave_critical_section(flags);
+      uart_enabletxint(dev);
+      ret = nxsem_wait(&dev->xmitsem);
+      uart_disabletxint(dev);
+      leave_critical_section(flags);
 
 #ifdef CONFIG_SERIAL_REMOVABLE
-          /* Check if the removable device was disconnected while we were
-           * waiting.
-           */
+      /* Check if the removable device was disconnected while we were
+       * waiting.
+       */
 
-          if (dev->disconnected)
-            {
-              return -ENOTCONN;
-            }
+      if (dev->disconnected)
+        {
+          return nwritten > 0 ? nwritten : -ENOTCONN;
+        }
 #endif
 
-          /* Check if we were awakened by signal. */
+      /* Check if we were awakened by signal. */
 
-          if (ret < 0)
-            {
-              /* A signal received while waiting for the xmit buffer to
-               * become non-full will abort the transfer.
-               */
+      if (ret < 0)
+        {
+          /* A signal received while waiting for the xmit buffer to become
+           * non-full will abort the transfer.
+           */
 
-              return -EINTR;
-            }
+          return nwritten > 0 ? nwritten : ret;
         }
+    }
 
-      /* The caller has request that we not block for data.  So return the
-       * EAGAIN error to signal this situation.
-       */
+  return nwritten;
+}
 
-      else
-        {
-          return -EAGAIN;
-        }
-    }
+/****************************************************************************
+ * Name: uart_putxmitchar
+ ****************************************************************************/
 
-  /* We won't get here.  Some compilers may complain that this code is
-   * unreachable.
-   */
+static inline
+int uart_putxmitchar(FAR uart_dev_t *dev, int ch, bool oktoblock)
+{
+  ssize_t nsent;
+  char c = ch;
 
-  return OK;
+  /* Forward the single character to the batch helper. */
+
+  nsent = uart_putxmitbuf(dev, &c, 1, oktoblock);
+  return nsent < 0 ? nsent : OK;
 }
 
 /****************************************************************************
@@ -499,40 +506,64 @@ static inline ssize_t uart_irqwrite(FAR uart_dev_t *dev,
                                     FAR const char *buffer,
                                     size_t buflen)
 {
-  size_t tail = 0;
-  size_t head = buflen;
+  bool opost = (dev->tc_oflag & OPOST) != 0;
+  size_t offset = 0;
 
-  /* Do output post-processing */
+  /* Do output post-processing: locate the next character that requires
+   * translation (CR under OCRNL, NL under ONLCR/ONLRET), emit the run of
+   * ordinary bytes before it in one go, then emit the translation.  When no
+   * translation is enabled the whole buffer is written in a single call.
+   */
 
-  if ((dev->tc_oflag & OPOST) != 0)
+  while (offset < buflen)
     {
-      for (head = 0; head < buflen; head++)
+      FAR const char *special = NULL;
+      size_t nsent = buflen - offset;
+
+      if (opost && (dev->tc_oflag & OCRNL) != 0)
         {
-          int ch = buffer[head];
+          special = memchr(&buffer[offset], '\r', nsent);
+        }
 
-          /* Mapping CR to NL? */
+      if (opost && (dev->tc_oflag & (ONLCR | ONLRET)) != 0)
+        {
+          FAR const char *nl = memchr(&buffer[offset], '\n', nsent);
 
-          if ((ch == '\r') && (dev->tc_oflag & OCRNL) != 0)
+          if (nl != NULL && (special == NULL || nl < special))
             {
-              uart_putchars(dev, &buffer[tail], head - tail);
-              uart_putchars(dev, "\n", 1);
-              tail = head + 1;
+              special = nl;
             }
+        }
 
-          /* Are we interested in newline processing? */
+      if (special != NULL)
+        {
+          nsent = special - &buffer[offset];
+        }
 
-          if ((ch == '\n') && (dev->tc_oflag & (ONLCR | ONLRET)) != 0)
-            {
-              uart_putchars(dev, &buffer[tail], head - tail);
-              uart_putchars(dev, "\r", 1);
-              tail = head;
-            }
+      uart_putchars(dev, &buffer[offset], nsent);
+      offset += nsent;
+
+      if (special == NULL)
+        {
+          break;
         }
-    }
 
-  /* Output the character, using the low-level direct UART interfaces */
+      if (buffer[offset] == '\r')
+        {
+          /* OCRNL: map CR to NL (the CR itself is dropped). */
+
+          uart_putchars(dev, "\n", 1);
+        }
+      else
+        {
+          /* ONLCR/ONLRET: prepend CR before the NL. */
+
+          uart_putchars(dev, "\r\n", 2);
+        }
+
+      offset += 1;
+    }
 
-  uart_putchars(dev, &buffer[tail], head - tail);
   return buflen;
 }
 
@@ -1592,12 +1623,10 @@ static ssize_t uart_writev(FAR struct file *filep, FAR 
struct uio *uio)
   FAR const char   *segbuf   = NULL;
   size_t            seglen   = 0;
   size_t            nseg     = 0;
-#ifdef CONFIG_SERIAL_TXBULK
-  size_t            ncopy;
-#endif
   ssize_t           nwritten;
   ssize_t           buflen;
   bool              oktoblock;
+  bool              opost;
   int               ret;
   char              ch;
 
@@ -1669,8 +1698,23 @@ static ssize_t uart_writev(FAR struct file *filep, FAR 
struct uio *uio)
    */
 
   uart_disabletxint(dev);
+
+  /* Copy the payload into the TX buffer.  Output post-processing only needs
+   * to translate CR (when OCRNL is set) and NL (when ONLCR/ONLRET is set);
+   * every other byte is copied verbatim.  So within each iovec segment,
+   * locate the next byte that needs translation, bulk-copy everything before
+   * it with uart_putxmitbuf(), then handle that one byte.  When no
+   * translation is enabled (the common raw-mode case, e.g. block transfers
+   * to USB CDC/ACM) each segment is copied in a single bulk operation.
+   */
+
+  opost = (dev->tc_oflag & OPOST) != 0;
+
   while (buflen > 0)
     {
+      FAR const char *special = NULL;
+      size_t          nsent;
+
       if (nseg >= seglen)
         {
           /* Consume the current segment and take a pointer to the next.
@@ -1684,105 +1728,133 @@ static ssize_t uart_writev(FAR struct file *filep, 
FAR struct uio *uio)
           nseg   = 0;
         }
 
-      /* With no output processing, copy whole runs into the TX buffer at
-       * once.  The bulk copy caches the head index, so it is only used
-       * when this thread is provably the sole producer: ECHO makes
-       * uart_readv() echo into the same buffer, and on a console
-       * uart_irqwrite() produces into it from interrupt context.  A full
-       * TX buffer falls through to uart_putxmitchar() below, which keeps
-       * the canonical blocking, disconnect and O_NONBLOCK handling.
+      /* Number of contiguous bytes remaining in the current segment. */
+
+      nsent = seglen - nseg;
+
+      /* Locate the next character in this segment that requires output
+       * post-processing.
        */
 
-#ifdef CONFIG_SERIAL_TXBULK
-      if ((dev->tc_oflag & OPOST) == 0 && (dev->tc_lflag & ECHO) == 0 &&
-          !dev->isconsole)
+      if (opost && (dev->tc_oflag & OCRNL) != 0)
         {
-          ncopy = uart_putxmitbuf(dev, segbuf + nseg, seglen - nseg);
-          if (ncopy > 0)
+          special = memchr(&segbuf[nseg], '\r', nsent);
+        }
+
+      if (opost && (dev->tc_oflag & (ONLCR | ONLRET)) != 0)
+        {
+          FAR const char *nl = memchr(&segbuf[nseg], '\n', nsent);
+
+          if (nl != NULL && (special == NULL || nl < special))
             {
-              nseg   += ncopy;
-              buflen -= ncopy;
-              continue;
+              special = nl;
             }
         }
-#endif
 
-      ch  = segbuf[nseg];
-      ret = OK;
+      if (special != NULL)
+        {
+          nsent = special - &segbuf[nseg];
+        }
 
-      /* Do output post-processing */
+      /* Bulk-copy the run of ordinary bytes that precede the special
+       * character (or the rest of the segment when there is none).
+       */
 
-      if ((dev->tc_oflag & OPOST) != 0)
+      if (nsent > 0)
         {
-          /* Mapping CR to NL? */
-
-          if ((ch == '\r') && (dev->tc_oflag & OCRNL) != 0)
+          ret = uart_putxmitbuf(dev, &segbuf[nseg], nsent, oktoblock);
+          if (ret < 0)
             {
-              ch = '\n';
+              break;
             }
 
-          /* Are we interested in newline processing? */
-
-          if ((ch == '\n') && (dev->tc_oflag & (ONLCR | ONLRET)) != 0)
+          nseg   += ret;
+          buflen -= ret;
+          if (ret < nsent)
             {
-              ret = uart_putxmitchar(dev, '\r', oktoblock);
+              break;
             }
+        }
 
-          /* Specifically not handled:
-           *
-           * OXTABS - primarily a full-screen terminal optimization
-           * ONOEOT - Unix interoperability hack
-           * OLCUC  - Not specified by POSIX
-           * ONOCR  - low-speed interactive optimization
-           */
+      /* Copy entire segment, no special characters, goto next segment */
+
+      if (special == NULL)
+        {
+          continue;
+        }
+
+      /* 'special' points at the current byte.  Translate and
+       * emit that single character.
+       *
+       * Specifically not handled:
+       *   OXTABS - primarily a full-screen terminal optimization
+       *   ONOEOT - Unix interoperability hack
+       *   OLCUC  - Not specified by POSIX
+       *   ONOCR  - low-speed interactive optimization
+       */
+
+      ch = segbuf[nseg];
+
+      /* Mapping CR to NL? */
+
+      if ((ch == '\r') && (dev->tc_oflag & OCRNL) != 0)
+        {
+          ch = '\n';
+        }
+
+      /* Are we interested in newline processing? */
+
+      if ((ch == '\n') && (dev->tc_oflag & (ONLCR | ONLRET)) != 0)
+        {
+          ret = uart_putxmitchar(dev, '\r', oktoblock);
         }
 
-      /* Put the character into the transmit buffer */
+      /* Put the (translated) character into the transmit buffer. */
 
       if (ret >= 0)
         {
           ret = uart_putxmitchar(dev, ch, oktoblock);
         }
 
-      /* uart_putxmitchar() might return an error under one of two
+      /* uart_putxmitchar() might return an error under one of three
        * conditions:  (1) The wait for buffer space might have been
        * interrupted by a signal (ret should be -EINTR), (2) if
        * CONFIG_SERIAL_REMOVABLE is defined, then uart_putxmitchar()
        * might also return if the serial device was disconnected
        * (with -ENOTCONN), or (3) if O_NONBLOCK is specified, then
-       * then uart_putxmitchar() might return -EAGAIN if the output
+       * uart_putxmitchar() might return -EAGAIN if the output
        * TX buffer is full.
        */
 
       if (ret < 0)
         {
-          /* POSIX requires that we return -1 and errno set if no data was
-           * transferred.  Otherwise, we return the number of bytes in the
-           * interrupted transfer.
-           */
+          break;
+        }
 
-          if (buflen < (size_t)nwritten)
-            {
-              /* Some data was transferred.  Return the number of bytes that
-               * were successfully transferred.
-               */
+      nseg++;
+      buflen--;
+    }
 
-              nwritten -= buflen;
-            }
-          else
-            {
-              /* No data was transferred. Return the negated errno value.
-               * The VFS layer will set the errno value appropriately).
-               */
+  /* POSIX requires that we return -1 and errno set if no data was
+   * transferred.  Otherwise, we return the number of bytes in the
+   * interrupted transfer.
+   */
 
-              nwritten = ret;
-            }
+  if (nwritten > buflen)
+    {
+      /* Some data was transferred. Return the number of bytes that
+       * were successfully transferred.
+       */
 
-          break;
-        }
+      nwritten -= buflen;
+    }
+  else
+    {
+      /* No data was transferred.  Return the negated errno value.  The VFS
+       * layer will set the errno value appropriately.
+       */
 
-      buflen--;
-      nseg++;
+      nwritten = ret;
     }
 
   /* Consume the bytes that were successfully queued */

Reply via email to