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 e47cb9a9e724bbb560a3b0f42f0c844dd0bcb867..f1c1a260da5191b7b950e3325be6735e6904b413 100644 (file)
@@ -1,29 +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
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307         USA
- *
  */
 
 #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"
@@ -63,7 +51,7 @@
 /*
  * Buffers to hold input and output data
  */
-#define USBTTY_BUFFER_SIZE 256
+#define USBTTY_BUFFER_SIZE 2048
 static circbuf_t usbtty_input;
 static circbuf_t usbtty_output;
 
@@ -402,7 +390,7 @@ static void str2wide (char *str, u16 * wide)
  * Test whether a character is in the RX buffer
  */
 
-int usbtty_tstc (void)
+int usbtty_tstc(struct stdio_dev *dev)
 {
        struct usb_endpoint_instance *endpoint =
                &endpoint_instance[rx_endpoint];
@@ -422,7 +410,7 @@ int usbtty_tstc (void)
  * written into its argument c.
  */
 
-int usbtty_getc (void)
+int usbtty_getc(struct stdio_dev *dev)
 {
        char c;
        struct usb_endpoint_instance *endpoint =
@@ -442,16 +430,17 @@ int usbtty_getc (void)
 /*
  * Output a single byte to the usb client port.
  */
-void usbtty_putc (const char c)
+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 ();
@@ -488,7 +477,7 @@ static void __usbtty_puts (const char *str, int len)
                if (space) {
                        write_buffer (&usbtty_output);
 
-                       n = MIN (space, MIN (len, maxlen));
+                       n = min(space, min(len, maxlen));
                        buf_push (&usbtty_output, str, n);
 
                        str += n;
@@ -497,7 +486,7 @@ static void __usbtty_puts (const char *str, int len)
        }
 }
 
-void usbtty_puts (const char *str)
+void usbtty_puts(struct stdio_dev *dev, const char *str)
 {
        int n;
        int len;
@@ -511,8 +500,8 @@ void usbtty_puts (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 {
@@ -537,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",
@@ -552,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... */
@@ -862,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){
@@ -883,19 +878,13 @@ 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;
 
                        space_avail =
                                current_urb->buffer_length -
                                current_urb->actual_length;
-                       popnum = MIN (space_avail, buf->size);
+                       popnum = min(space_avail, (int)buf->size);
                        if (popnum == 0)
                                break;
 
@@ -971,8 +960,8 @@ static void usbtty_event_handler (struct usb_device_instance *device,
                /*
                 * is_usbd_high_speed routine needs to be defined by
                 * specific gadget driver
-                * It returns TRUE if device enumerates at High speed
-                * Retuns FALSE otherwise
+                * It returns true if device enumerates at High speed
+                * Retuns false otherwise
                 */
                for (i = 0; i < NUM_ENDPOINTS; i++) {
                        if (((ep_descriptor_ptrs[i]->bmAttributes &