Merge tag 'u-boot-atmel-fixes-2020.07-a' of https://gitlab.denx.de/u-boot/custodians...
[oweals/u-boot.git] / drivers / serial / usbtty.c
index 75f0ec31bbfb9a6faedeaf9cf137ea1212ceefd5..f1c1a260da5191b7b950e3325be6735e6904b413 100644 (file)
@@ -1,16 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2003
  * Gerry Hamel, geh@ti.com, Texas Instruments
  *
  * (C) Copyright 2006
  * Bryan O'Donoghue, bodonoghue@codehermit.ie
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
 #include <config.h>
 #include <circbuf.h>
+#include <env.h>
+#include <serial.h>
 #include <stdio_dev.h>
 #include <asm/unaligned.h>
 #include "usbtty.h"
@@ -434,11 +435,12 @@ void usbtty_putc(struct stdio_dev *dev, const char c)
        if (!usbtty_configured ())
                return;
 
-       buf_push (&usbtty_output, &c, 1);
        /* If \n, also do \r */
        if (c == '\n')
                buf_push (&usbtty_output, "\r", 1);
 
+       buf_push(&usbtty_output, &c, 1);
+
        /* Poll at end to handle new data... */
        if ((usbtty_output.size + 2) >= usbtty_output.totalsize) {
                usbtty_poll ();
@@ -498,8 +500,8 @@ void usbtty_puts(struct stdio_dev *dev, const char *str)
                n = next_nl_pos (str);
 
                if (str[n] == '\n') {
-                       __usbtty_puts (str, n + 1);
-                       __usbtty_puts ("\r", 1);
+                       __usbtty_puts("\r", 1);
+                       __usbtty_puts(str, n + 1);
                        str += (n + 1);
                        len -= (n + 1);
                } else {
@@ -524,10 +526,10 @@ int drv_usbtty_init (void)
        char * tt;
        int snlen;
 
-       /* Ger seiral number */
-       if (!(sn = getenv("serial#"))) {
+       /* Get serial number */
+       sn = env_get("serial#");
+       if (!sn)
                sn = "000000000000";
-       }
        snlen = strlen(sn);
        if (snlen > sizeof(serial_number) - 1) {
                printf ("Warning: serial number %s is too long (%d > %lu)\n",
@@ -539,10 +541,9 @@ int drv_usbtty_init (void)
 
        /* Decide on which type of UDC device to be.
         */
-
-       if(!(tt = getenv("usbtty"))) {
+       tt = env_get("usbtty");
+       if (!tt)
                tt = "generic";
-       }
        usbtty_init_terminal_type(strcmp(tt,"cdc_acm"));
 
        /* prepare buffers... */
@@ -849,6 +850,13 @@ static int write_buffer (circbuf_t * buf)
        struct urb *current_urb = NULL;
 
        current_urb = next_urb (device_instance, endpoint);
+
+       if (!current_urb) {
+               TTYERR ("current_urb is NULL, buf->size %d\n",
+               buf->size);
+               return 0;
+       }
+
        /* TX data still exists - send it now
         */
        if(endpoint->sent < current_urb->actual_length){
@@ -870,12 +878,6 @@ static int write_buffer (circbuf_t * buf)
                 */
                while (buf->size > 0) {
 
-                       if (!current_urb) {
-                               TTYERR ("current_urb is NULL, buf->size %d\n",
-                                       buf->size);
-                               return total;
-                       }
-
                        dest = (char*)current_urb->buffer +
                                current_urb->actual_length;