[PATCH v7] usb-serial:cp210x: add support to software flow control

2021-01-04 Thread Sheng Long Wang
From: Wang Sheng Long 

When data is transmitted between two serial ports,
the phenomenon of data loss often occurs. The two kinds
of flow control commonly used in serial communication
are hardware flow control and software flow control.

In serial communication, If you only use RX/TX/GND Pins, you
can't do hardware flow. So we often used software flow control
and prevent data loss. The user sets the software flow control
through the application program, and the application program
sets the software flow control mode for the serial port
chip through the driver.

For the cp210 serial port chip, its driver lacks the
software flow control setting code, so the user cannot set
the software flow control function through the application
program. This adds the missing software flow control.

Signed-off-by: Wang Sheng Long 

Changes in v3:
- fixed code style, It mainly adjusts the code style acccording
  to kernel specification.

Changes in v4:
- It adjusts the patch based on the last usb-next branch
  of the usb-serial.

Changes in v5:
- Fixes:
  * According to the cp210x specification, use usb_control_msg()
requesttype 'REQTYPE_DEVICE_TO_HOST' is modified to
'REQTYPE_INTERFACE_TO_HOST' in cp210x_get_chars().

  * If modify IXOFF/IXON has been changed, we can call set software
flow control code.

  * If the setting software flow control wrong, do not continue
processing proceed with updating software flow control.

Changes in v6:
- Fix 'result' variable not uninitialized warning in cp210x_set_termios().

Changes in v7:
- Fix:
  * Rebase work code branch on cp210x-termios branch.

  * Support for software flow control to the new cp210x_set_flow_control()
function.

  * Modify cp210x_set_chars() parameter 'void *buf' take a pointer to
a struct special_chars (not void *).

  * Drop cp210x_get_chars() function.
---
 drivers/usb/serial/cp210x.c | 86 +
 1 file changed, 86 insertions(+)

diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c
index fbb10dfc56e3..bc84834cc416 100644
--- a/drivers/usb/serial/cp210x.c
+++ b/drivers/usb/serial/cp210x.c
@@ -408,6 +408,15 @@ struct cp210x_comm_status {
u8   bReserved;
 } __packed;
 
+struct cp210x_special_chars {
+   u8  bEofChar;
+   u8  bErrorChar;
+   u8  bBreakChar;
+   u8  bEventChar;
+   u8  bXonChar;
+   u8  bXoffChar;
+};
+
 /*
  * CP210X_PURGE - 16 bits passed in wValue of USB request.
  * SiLabs app note AN571 gives a strange description of the 4 bits:
@@ -619,6 +628,38 @@ static int cp210x_read_vendor_block(struct usb_serial 
*serial, u8 type, u16 val,
return result;
 }
 
+
+static int cp210x_set_chars(struct usb_serial_port *port, struct 
cp210x_special_chars *buf)
+{
+   struct usb_serial *serial = port->serial;
+   struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
+   struct cp210x_special_chars *special_chars;
+   void *dmabuf;
+   int result;
+
+   dmabuf = kmemdup(buf, sizeof(*special_chars), GFP_KERNEL);
+   if (!dmabuf)
+   return -ENOMEM;
+
+   result = usb_control_msg(serial->dev,
+   usb_sndctrlpipe(serial->dev, 0),
+   CP210X_SET_CHARS, REQTYPE_HOST_TO_INTERFACE, 0,
+   port_priv->bInterfaceNumber,
+   dmabuf, sizeof(*special_chars), 
USB_CTRL_SET_TIMEOUT);
+
+   if (result == sizeof(*special_chars)) {
+   result = 0;
+   } else {
+   dev_err(>dev, "failed to set special chars: %d\n", 
result);
+   if (result >= 0)
+   result = -EIO;
+   }
+
+   kfree(dmabuf);
+
+   return result;
+}
+
 /*
  * Writes any 16-bit CP210X_ register (req) whose value is passed
  * entirely in the wValue field of the USB request.
@@ -1087,9 +1128,12 @@ static void cp210x_set_flow_control(struct tty_struct 
*tty,
struct usb_serial_port *port, struct ktermios *old_termios)
 {
struct cp210x_flow_ctl flow_ctl;
+   struct cp210x_special_chars special_chars;
+   unsigned int iflag, old_iflag;
u32 flow_repl;
u32 ctl_hs;
int ret;
+   int result = 0;
 
if (old_termios && C_CRTSCTS(tty) == (old_termios->c_cflag & CRTSCTS))
return;
@@ -1126,6 +1170,48 @@ static void cp210x_set_flow_control(struct tty_struct 
*tty,
 
cp210x_write_reg_block(port, CP210X_SET_FLOW, _ctl,
sizeof(flow_ctl));
+
+   iflag = tty->termios.c_iflag;
+   old_iflag = old_termios->c_iflag;
+   if (((iflag & IXOFF) != (old_iflag & IXOFF)) ||
+   ((iflag & IXON) != (old_iflag & IXON))) {
+
+   special_chars.bXonChar  = START_CHAR(tty);
+   special_chars.bXoffChar = STOP_CHAR(tty);
+
+   result = cp210x_set_chars(port, _chars);
+   if (result < 0)

[PATCH v6] usb-serial:cp210x: add support to software flow control

2020-10-15 Thread Sheng Long Wang
From: Wang Sheng Long 

When data is transmitted between two serial ports,
the phenomenon of data loss often occurs. The two kinds
of flow control commonly used in serial communication
are hardware flow control and software flow control.

In serial communication, If you only use RX/TX/GND Pins, you
can't do hardware flow. So we often used software flow control
and prevent data loss. The user sets the software flow control
through the application program, and the application program
sets the software flow control mode for the serial port
chip through the driver.

For the cp210 serial port chip, its driver lacks the
software flow control setting code, so the user cannot set
the software flow control function through the application
program. This adds the missing software flow control.

Signed-off-by: Wang Sheng Long 

Changes in v3:
- fixed code style, It mainly adjusts the code style acccording
  to kernel specification.

Changes in v4:
- It mainly adjusts the patch based on the last usb-next branch
  of the usb-serial.

Changes in v5:
- Fixes:
  * According to the cp210x specification, use usb_control_msg()
requesttype 'REQTYPE_DEVICE_TO_HOST' is modified to
'REQTYPE_INTERFACE_TO_HOST' in cp210x_get_chars().

  * If modify IXOFF/IXON has been changed, we can call set software
flow control code.

  * If the setting software flow control wrong, do not continue
processing proceed with updating software flow control.

Changes in v6:
- Fix 'result' variable not uninitialized warning in cp210x_set_termios().
---
 drivers/usb/serial/cp210x.c | 128 ++--
 1 file changed, 123 insertions(+), 5 deletions(-)

diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c
index d0c05aa8a0d6..2d5e31282599 100644
--- a/drivers/usb/serial/cp210x.c
+++ b/drivers/usb/serial/cp210x.c
@@ -412,6 +412,15 @@ struct cp210x_comm_status {
u8   bReserved;
 } __packed;
 
+struct cp210x_special_chars {
+   u8  bEofChar;
+   u8  bErrorChar;
+   u8  bBreakChar;
+   u8  bEventChar;
+   u8  bXonChar;
+   u8  bXoffChar;
+};
+
 /*
  * CP210X_PURGE - 16 bits passed in wValue of USB request.
  * SiLabs app note AN571 gives a strange description of the 4 bits:
@@ -675,6 +684,70 @@ static int cp210x_read_vendor_block(struct usb_serial 
*serial, u8 type, u16 val,
return result;
 }
 
+static int cp210x_get_chars(struct usb_serial_port *port, void *buf)
+{
+   struct usb_serial *serial = port->serial;
+   struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
+   struct cp210x_special_chars *special_chars;
+   void *dmabuf;
+   int result;
+
+   dmabuf = kmemdup(buf, sizeof(*special_chars), GFP_KERNEL);
+   if (!dmabuf)
+   return -ENOMEM;
+
+   result = usb_control_msg(serial->dev,
+   usb_rcvctrlpipe(serial->dev, 0),
+   CP210X_GET_CHARS, REQTYPE_INTERFACE_TO_HOST, 0,
+   port_priv->bInterfaceNumber,
+   dmabuf, sizeof(*special_chars),
+   USB_CTRL_GET_TIMEOUT);
+
+   if (result == sizeof(*special_chars)) {
+   memcpy(buf, dmabuf, sizeof(*special_chars));
+   result = 0;
+   } else {
+   dev_err(>dev, "failed to get special chars: %d\n", 
result);
+   if (result >= 0)
+   result = -EIO;
+   }
+
+   kfree(dmabuf);
+
+   return result;
+}
+
+static int cp210x_set_chars(struct usb_serial_port *port, void *buf)
+{
+   struct usb_serial *serial = port->serial;
+   struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
+   struct cp210x_special_chars *special_chars;
+   void *dmabuf;
+   int result;
+
+   dmabuf = kmemdup(buf, sizeof(*special_chars), GFP_KERNEL);
+   if (!dmabuf)
+   return -ENOMEM;
+
+   result = usb_control_msg(serial->dev,
+   usb_sndctrlpipe(serial->dev, 0),
+   CP210X_SET_CHARS, REQTYPE_HOST_TO_INTERFACE, 0,
+   port_priv->bInterfaceNumber,
+   dmabuf, sizeof(*special_chars), 
USB_CTRL_SET_TIMEOUT);
+
+   if (result == sizeof(*special_chars)) {
+   result = 0;
+   } else {
+   dev_err(>dev, "failed to set special chars: %d\n", 
result);
+   if (result >= 0)
+   result = -EIO;
+   }
+
+   kfree(dmabuf);
+
+   return result;
+}
+
 /*
  * Writes any 16-bit CP210X_ register (req) whose value is passed
  * entirely in the wValue field of the USB request.
@@ -1356,11 +1429,18 @@ static void cp210x_set_termios(struct tty_struct *tty,
struct usb_serial_port *port, struct ktermios *old_termios)
 {
struct device *dev = >dev;
-   unsigned int cflag, old_cflag;
+   

[PATCH v5] usb-serial:cp210x: add support to software flow control

2020-10-15 Thread Sheng Long Wang
From: Wang Sheng Long 

When data is transmitted between two serial ports,
the phenomenon of data loss often occurs. The two kinds
of flow control commonly used in serial communication
are hardware flow control and software flow control.

In serial communication, If you only use RX/TX/GND Pins, you
can't do hardware flow. So we often used software flow control
and prevent data loss. The user sets the software flow control
through the application program, and the application program
sets the software flow control mode for the serial port
chip through the driver.

For the cp210 serial port chip, its driver lacks the
software flow control setting code, so the user cannot set
the software flow control function through the application
program. This adds the missing software flow control.

Signed-off-by: Wang Sheng Long 

Changes in v3:
- fixed code style, It mainly adjusts the code style acccording
  to kernel specification.

Changes in v4:
- It mainly adjusts the patch based on the last usb-next branch
  of the usb-serial.

Changes in v5:
- Fixes:
  * According to the cp210x specification, use usb_control_msg()
requesttype 'REQTYPE_DEVICE_TO_HOST' is modified to
'REQTYPE_INTERFACE_TO_HOST' in cp210x_get_chars().

  * If modify IXOFF/IXON has been changed, we can call set software
flow control code.

  * If the setting software flow control wrong, do not continue
processing proceed with updating software flow control.
---
 drivers/usb/serial/cp210x.c | 128 ++--
 1 file changed, 123 insertions(+), 5 deletions(-)

diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c
index d0c05aa8a0d6..d2edf9e4d484 100644
--- a/drivers/usb/serial/cp210x.c
+++ b/drivers/usb/serial/cp210x.c
@@ -412,6 +412,15 @@ struct cp210x_comm_status {
u8   bReserved;
 } __packed;
 
+struct cp210x_special_chars {
+   u8  bEofChar;
+   u8  bErrorChar;
+   u8  bBreakChar;
+   u8  bEventChar;
+   u8  bXonChar;
+   u8  bXoffChar;
+};
+
 /*
  * CP210X_PURGE - 16 bits passed in wValue of USB request.
  * SiLabs app note AN571 gives a strange description of the 4 bits:
@@ -675,6 +684,70 @@ static int cp210x_read_vendor_block(struct usb_serial 
*serial, u8 type, u16 val,
return result;
 }
 
+static int cp210x_get_chars(struct usb_serial_port *port, void *buf)
+{
+   struct usb_serial *serial = port->serial;
+   struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
+   struct cp210x_special_chars *special_chars;
+   void *dmabuf;
+   int result;
+
+   dmabuf = kmemdup(buf, sizeof(*special_chars), GFP_KERNEL);
+   if (!dmabuf)
+   return -ENOMEM;
+
+   result = usb_control_msg(serial->dev,
+   usb_rcvctrlpipe(serial->dev, 0),
+   CP210X_GET_CHARS, REQTYPE_INTERFACE_TO_HOST, 0,
+   port_priv->bInterfaceNumber,
+   dmabuf, sizeof(*special_chars),
+   USB_CTRL_GET_TIMEOUT);
+
+   if (result == sizeof(*special_chars)) {
+   memcpy(buf, dmabuf, sizeof(*special_chars));
+   result = 0;
+   } else {
+   dev_err(>dev, "failed to get special chars: %d\n", 
result);
+   if (result >= 0)
+   result = -EIO;
+   }
+
+   kfree(dmabuf);
+
+   return result;
+}
+
+static int cp210x_set_chars(struct usb_serial_port *port, void *buf)
+{
+   struct usb_serial *serial = port->serial;
+   struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
+   struct cp210x_special_chars *special_chars;
+   void *dmabuf;
+   int result;
+
+   dmabuf = kmemdup(buf, sizeof(*special_chars), GFP_KERNEL);
+   if (!dmabuf)
+   return -ENOMEM;
+
+   result = usb_control_msg(serial->dev,
+   usb_sndctrlpipe(serial->dev, 0),
+   CP210X_SET_CHARS, REQTYPE_HOST_TO_INTERFACE, 0,
+   port_priv->bInterfaceNumber,
+   dmabuf, sizeof(*special_chars), 
USB_CTRL_SET_TIMEOUT);
+
+   if (result == sizeof(*special_chars)) {
+   result = 0;
+   } else {
+   dev_err(>dev, "failed to set special chars: %d\n", 
result);
+   if (result >= 0)
+   result = -EIO;
+   }
+
+   kfree(dmabuf);
+
+   return result;
+}
+
 /*
  * Writes any 16-bit CP210X_ register (req) whose value is passed
  * entirely in the wValue field of the USB request.
@@ -1356,11 +1429,18 @@ static void cp210x_set_termios(struct tty_struct *tty,
struct usb_serial_port *port, struct ktermios *old_termios)
 {
struct device *dev = >dev;
-   unsigned int cflag, old_cflag;
+   unsigned int cflag, old_cflag, iflag, old_iflag;
+   struct cp210x_special_chars 

[PATCH v4] usb-serial:cp210x: add support to software flow control

2020-09-08 Thread Sheng Long Wang
From: Wang Sheng Long 

When data is transmitted between two serial ports,
the phenomenon of data loss often occurs. The two kinds
of flow control commonly used in serial communication
are hardware flow control and software flow control.

In serial communication, If you only use RX/TX/GND Pins, you
can't do hardware flow. So we often used software flow control
and prevent data loss. The user sets the software flow control
through the application program, and the application program
sets the software flow control mode for the serial port
chip through the driver.

For the cp210 serial port chip, its driver lacks the
software flow control setting code, so the user cannot set
the software flow control function through the application
program. This adds the missing software flow control.

Signed-off-by: Wang Sheng Long 

Changes in v3:
- fixed code style, It mainly adjusts the code style acccording
  to kernel specification.

Changes in v4:
- It mainly adjusts the patch based on the last usb-next branch
  of the usb-serial and optimized the relevant code.
---
 drivers/usb/serial/cp210x.c | 125 ++--
 1 file changed, 120 insertions(+), 5 deletions(-)

diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c
index d0c05aa8a0d6..bcbf8da99ebb 100644
--- a/drivers/usb/serial/cp210x.c
+++ b/drivers/usb/serial/cp210x.c
@@ -412,6 +412,15 @@ struct cp210x_comm_status {
u8   bReserved;
 } __packed;
 
+struct cp210x_special_chars {
+   u8  bEofChar;
+   u8  bErrorChar;
+   u8  bBreakChar;
+   u8  bEventChar;
+   u8  bXonChar;
+   u8  bXoffChar;
+};
+
 /*
  * CP210X_PURGE - 16 bits passed in wValue of USB request.
  * SiLabs app note AN571 gives a strange description of the 4 bits:
@@ -675,6 +684,69 @@ static int cp210x_read_vendor_block(struct usb_serial 
*serial, u8 type, u16 val,
return result;
 }
 
+static int cp210x_get_chars(struct usb_serial_port *port, void *buf, int 
bufsize)
+{
+   struct usb_serial *serial = port->serial;
+   struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
+   void *dmabuf;
+   int result;
+
+   dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
+   if (!dmabuf)
+   return -ENOMEM;
+
+   result = usb_control_msg(serial->dev,
+   usb_sndctrlpipe(serial->dev, 0),
+   CP210X_GET_CHARS, REQTYPE_DEVICE_TO_HOST, 0,
+   port_priv->bInterfaceNumber,
+   dmabuf, bufsize, USB_CTRL_SET_TIMEOUT);
+
+   if (result == bufsize) {
+   memcpy(buf, dmabuf, bufsize);
+   result = 0;
+   } else {
+   dev_err(>dev, "failed get req 0x%x size %d status: %d\n",
+   CP210X_GET_CHARS, bufsize, result);
+   if (result >= 0)
+   result = -EIO;
+   }
+
+   kfree(dmabuf);
+
+   return result;
+}
+
+static int cp210x_set_chars(struct usb_serial_port *port, void *buf, int 
bufsize)
+{
+   struct usb_serial *serial = port->serial;
+   struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
+   void *dmabuf;
+   int result;
+
+   dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
+   if (!dmabuf)
+   return -ENOMEM;
+
+   result = usb_control_msg(serial->dev,
+   usb_sndctrlpipe(serial->dev, 0),
+   CP210X_SET_CHARS, REQTYPE_HOST_TO_INTERFACE, 0,
+   port_priv->bInterfaceNumber,
+   dmabuf, bufsize, USB_CTRL_SET_TIMEOUT);
+
+   if (result == bufsize) {
+   result = 0;
+   } else {
+   dev_err(>dev, "failed get req 0x%x size %d status: %d\n",
+   CP210X_SET_CHARS, bufsize, result);
+   if (result >= 0)
+   result = -EIO;
+   }
+
+   kfree(dmabuf);
+
+   return result;
+}
+
 /*
  * Writes any 16-bit CP210X_ register (req) whose value is passed
  * entirely in the wValue field of the USB request.
@@ -1356,11 +1428,17 @@ static void cp210x_set_termios(struct tty_struct *tty,
struct usb_serial_port *port, struct ktermios *old_termios)
 {
struct device *dev = >dev;
-   unsigned int cflag, old_cflag;
+   unsigned int cflag, old_cflag, iflag;
+   struct cp210x_special_chars charsres;
+   struct cp210x_flow_ctl flow_ctl;
u16 bits;
+   int result;
+   u32 ctl_hs;
+   u32 flow_repl;
 
cflag = tty->termios.c_cflag;
old_cflag = old_termios->c_cflag;
+   iflag = tty->termios.c_iflag;
 
if (tty->termios.c_ospeed != old_termios->c_ospeed)
cp210x_change_speed(tty, port, old_termios);
@@ -1434,10 +1512,6 @@ static void cp210x_set_termios(struct tty_struct *tty,
}
 
if ((cflag & CRTSCTS) != 

[PATCH v3] usb-serial:cp210x: add support to software flow control

2020-08-20 Thread Sheng Long Wang
From: Wang Sheng Long 

When data is transmitted between two serial ports,
the phenomenon of data loss often occurs. The two kinds
of flow control commonly used in serial communication
are hardware flow control and software flow control.

In serial communication, If you only use RX/TX/GND Pins, you
can't do hardware flow. So we often used software flow control
and prevent data loss. The user sets the software flow control
through the application program, and the application program
sets the software flow control mode for the serial port
chip through the driver.

For the cp210 serial port chip, its driver lacks the
software flow control setting code, so the user cannot set
the software flow control function through the application
program. This adds the missing software flow control.

Signed-off-by: Wang Sheng Long 

Changes in v3:
-fixed code style, It mainly adjusts the code style acccording
 to kernel specification.
---
 drivers/usb/serial/cp210x.c | 118 ++--
 1 file changed, 113 insertions(+), 5 deletions(-)

diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c
index e732949f65..c66a0e0fb9 100644
--- a/drivers/usb/serial/cp210x.c
+++ b/drivers/usb/serial/cp210x.c
@@ -380,6 +380,9 @@ static struct usb_serial_driver * const serial_drivers[] = {
 #define CP210X_PARTNUM_CP2102N_QFN20   0x22
 #define CP210X_PARTNUM_UNKNOWN 0xFF
 
+#define CP210X_VSTART  0x11
+#define CP210X_VSTOP   0x13
+
 /* CP210X_GET_COMM_STATUS returns these 0x13 bytes */
 struct cp210x_comm_status {
__le32   ulErrors;
@@ -391,6 +394,15 @@ struct cp210x_comm_status {
u8   bReserved;
 } __packed;
 
+struct cp210x_chars_response {
+   u8  eofchar;
+   u8  errochar;
+   u8  breakchar;
+   u8  eventchar;
+   u8  xonchar;
+   u8  xoffchar;
+} __packed;
+
 /*
  * CP210X_PURGE - 16 bits passed in wValue of USB request.
  * SiLabs app note AN571 gives a strange description of the 4 bits:
@@ -624,6 +636,45 @@ static int cp210x_read_vendor_block(struct usb_serial 
*serial, u8 type, u16 val,
return result;
 }
 
+/*
+ * Read and Write Character Responses operate
+ * Register SET_CHARS/GET_CHATS
+ */
+static int cp210x_operate_chars_block(struct usb_serial_port *port,
+   u8 req, u8 type, void *buf, int bufsize)
+{
+   struct usb_serial *serial = port->serial;
+   struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
+   void *dmabuf;
+   int result;
+
+   dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
+   if (!dmabuf)
+   return -ENOMEM;
+
+   result = usb_control_msg(serial->dev,
+   usb_rcvctrlpipe(serial->dev, 0),
+   req, type, 0, port_priv->bInterfaceNumber,
+   dmabuf, bufsize, USB_CTRL_SET_TIMEOUT);
+
+   if (result == bufsize) {
+   if (type == REQTYPE_DEVICE_TO_HOST)
+   memcpy(buf, dmabuf, bufsize);
+
+   result = 0;
+   } else {
+   dev_err(>dev, "failed get req 0x%x size %d status: %d\n",
+   req, bufsize, result);
+   if (result >= 0)
+   result = -EIO;
+
+   }
+
+   kfree(dmabuf);
+
+   return result;
+}
+
 /*
  * Writes any 16-bit CP210X_ register (req) whose value is passed
  * entirely in the wValue field of the USB request.
@@ -1134,11 +1185,17 @@ static void cp210x_set_termios(struct tty_struct *tty,
struct usb_serial_port *port, struct ktermios *old_termios)
 {
struct device *dev = >dev;
-   unsigned int cflag, old_cflag;
+   struct cp210x_chars_response charsres;
+   struct cp210x_flow_ctl flow_ctl;
+   unsigned int cflag, old_cflag, iflag;
u16 bits;
+   int result;
+   u32 ctl_hs;
+   u32 flow_repl;
 
cflag = tty->termios.c_cflag;
old_cflag = old_termios->c_cflag;
+   iflag = tty->termios.c_iflag;
 
if (tty->termios.c_ospeed != old_termios->c_ospeed)
cp210x_change_speed(tty, port, old_termios);
@@ -1212,10 +1269,6 @@ static void cp210x_set_termios(struct tty_struct *tty,
}
 
if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
-   struct cp210x_flow_ctl flow_ctl;
-   u32 ctl_hs;
-   u32 flow_repl;
-
cp210x_read_reg_block(port, CP210X_GET_FLOW, _ctl,
sizeof(flow_ctl));
ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
@@ -1252,6 +1305,61 @@ static void cp210x_set_termios(struct tty_struct *tty,
sizeof(flow_ctl));
}
 
+   /*
+* Set Software  Flow  Control
+* Check the IXOFF/IXON status in the iflag component of the
+* termios structure.
+*
+*/
+   if ((iflag & IXOFF) || (iflag & IXON)) {
+
+   result = 

[PATCH] usb-serial:cp210x: add support to software flow control

2020-07-30 Thread Sheng Long Wang
From: Wang Sheng Long 

When data is transmitted between two serial ports,
the phenomenon of data loss often occurs. The two kinds
of flow control commonly used in serial communication
are hardware flow control and software flow control.

In serial communication, If you only use RX/TX/GND Pins, you
can't do hardware flow. So we often used software flow control
and prevent data loss. The user sets the software flow control
through the application program, and the application program
sets the software flow control mode for the serial port
chip through the driver.

For the cp210 serial port chip, its driver lacks the
software flow control setting code, so the user cannot set
the software flow control function through the application
program. This adds the missing software flow control.

Signed-off-by: Wang Sheng Long 
---
 drivers/usb/serial/cp210x.c | 118 ++--
 1 file changed, 113 insertions(+), 5 deletions(-)

diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c
index e732949f65..c66a0e0fb9 100644
--- a/drivers/usb/serial/cp210x.c
+++ b/drivers/usb/serial/cp210x.c
@@ -380,6 +380,9 @@ static struct usb_serial_driver * const serial_drivers[] = {
 #define CP210X_PARTNUM_CP2102N_QFN20   0x22
 #define CP210X_PARTNUM_UNKNOWN 0xFF
 
+#define CP210X_VSTART  0x11
+#define CP210X_VSTOP   0x13
+
 /* CP210X_GET_COMM_STATUS returns these 0x13 bytes */
 struct cp210x_comm_status {
__le32   ulErrors;
@@ -391,6 +394,15 @@ struct cp210x_comm_status {
u8   bReserved;
 } __packed;
 
+struct cp210x_chars_response {
+   u8  eofchar;
+   u8  errochar;
+   u8  breakchar;
+   u8  eventchar;
+   u8  xonchar;
+   u8  xoffchar;
+} __packed;
+
 /*
  * CP210X_PURGE - 16 bits passed in wValue of USB request.
  * SiLabs app note AN571 gives a strange description of the 4 bits:
@@ -624,6 +636,45 @@ static int cp210x_read_vendor_block(struct usb_serial 
*serial, u8 type, u16 val,
return result;
 }
 
+/*
+ * Read and Write Character Responses operate
+ * Register SET_CHARS/GET_CHATS
+ */
+static int cp210x_operate_chars_block(struct usb_serial_port *port,
+   u8 req, u8 type, void *buf, int bufsize)
+{
+   struct usb_serial *serial = port->serial;
+   struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
+   void *dmabuf;
+   int result;
+
+   dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
+   if (!dmabuf)
+   return -ENOMEM;
+
+   result = usb_control_msg(serial->dev,
+   usb_rcvctrlpipe(serial->dev, 0),
+   req, type, 0, port_priv->bInterfaceNumber,
+   dmabuf, bufsize, USB_CTRL_SET_TIMEOUT);
+
+   if (result == bufsize) {
+   if (type == REQTYPE_DEVICE_TO_HOST)
+   memcpy(buf, dmabuf, bufsize);
+
+   result = 0;
+   } else {
+   dev_err(>dev, "failed get req 0x%x size %d status: %d\n",
+   req, bufsize, result);
+   if (result >= 0)
+   result = -EIO;
+
+   }
+
+   kfree(dmabuf);
+
+   return result;
+}
+
 /*
  * Writes any 16-bit CP210X_ register (req) whose value is passed
  * entirely in the wValue field of the USB request.
@@ -1134,11 +1185,17 @@ static void cp210x_set_termios(struct tty_struct *tty,
struct usb_serial_port *port, struct ktermios *old_termios)
 {
struct device *dev = >dev;
-   unsigned int cflag, old_cflag;
+   struct cp210x_chars_response charsres;
+   struct cp210x_flow_ctl flow_ctl;
+   unsigned int cflag, old_cflag, iflag;
u16 bits;
+   int result;
+   u32 ctl_hs;
+   u32 flow_repl;
 
cflag = tty->termios.c_cflag;
old_cflag = old_termios->c_cflag;
+   iflag = tty->termios.c_iflag;
 
if (tty->termios.c_ospeed != old_termios->c_ospeed)
cp210x_change_speed(tty, port, old_termios);
@@ -1212,10 +1269,6 @@ static void cp210x_set_termios(struct tty_struct *tty,
}
 
if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
-   struct cp210x_flow_ctl flow_ctl;
-   u32 ctl_hs;
-   u32 flow_repl;
-
cp210x_read_reg_block(port, CP210X_GET_FLOW, _ctl,
sizeof(flow_ctl));
ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
@@ -1252,6 +1305,61 @@ static void cp210x_set_termios(struct tty_struct *tty,
sizeof(flow_ctl));
}
 
+   /*
+* Set Software  Flow  Control
+* Check the IXOFF/IXON status in the iflag component of the
+* termios structure.
+*
+*/
+   if ((iflag & IXOFF) || (iflag & IXON)) {
+
+   result = cp210x_operate_chars_block(port,
+   CP210X_GET_CHARS,
+   

[PATCH] usb-serial:cp210x: add CP210x support to software flow control

2020-07-24 Thread Sheng Long Wang
From: Wang Sheng Long 

  The cp210x driver lacks soft-flow function,so need and
  this function.

Signed-off-by: Wang Sheng Long 
---
 drivers/usb/serial/cp210x.c | 110 +---
 1 file changed, 103 insertions(+), 7 deletions(-)

diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c
index e732949f65..ad5db0e2ae 100644
--- a/drivers/usb/serial/cp210x.c
+++ b/drivers/usb/serial/cp210x.c
@@ -380,6 +380,9 @@ static struct usb_serial_driver * const serial_drivers[] = {
 #define CP210X_PARTNUM_CP2102N_QFN20   0x22
 #define CP210X_PARTNUM_UNKNOWN 0xFF
 
+#define CP210X_VSTART  0x11
+#define CP210X_VSTOP   0x13
+
 /* CP210X_GET_COMM_STATUS returns these 0x13 bytes */
 struct cp210x_comm_status {
__le32   ulErrors;
@@ -391,6 +394,15 @@ struct cp210x_comm_status {
u8   bReserved;
 } __packed;
 
+struct cp210x_chars_respones{
+   u8   bEofchar;
+   u8   bErrochar;
+   u8   bBreakchar;
+   u8   bEventchar;
+   u8   bXonchar;
+   u8   bXoffchar;
+} __packed;
+
 /*
  * CP210X_PURGE - 16 bits passed in wValue of USB request.
  * SiLabs app note AN571 gives a strange description of the 4 bits:
@@ -624,6 +636,43 @@ static int cp210x_read_vendor_block(struct usb_serial 
*serial, u8 type, u16 val,
return result;
 }
 
+/*
+ * Read and Write Characrters Respones operate
+ * Register SET_CHARS/GET_CHATS
+ */
+static int cp210x_operate_chars_block(struct usb_serial_port *port, u8 req, u8 
type,
+   void *buf, int bufsize)
+{
+   struct usb_serial *serial = port->serial;
+   struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
+   void *dmabuf;
+   int result;
+
+   dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
+   if (!dmabuf)
+   return -ENOMEM;
+
+   result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
+   req, type, 0, port_priv->bInterfaceNumber, dmabuf, 
bufsize,
+   USB_CTRL_SET_TIMEOUT);
+   if (result == bufsize) {
+   if (type == REQTYPE_DEVICE_TO_HOST)
+   memcpy(buf, dmabuf, bufsize);
+
+   result = 0;
+   } else {
+   dev_err(>dev, "failed get req 0x%x size %d status: %d\n",
+   req, bufsize, result);
+   if (result >= 0)
+   result = -EIO;
+
+   }
+
+   kfree(dmabuf);
+
+   return result;
+}
+
 /*
  * Writes any 16-bit CP210X_ register (req) whose value is passed
  * entirely in the wValue field of the USB request.
@@ -650,8 +699,8 @@ static int cp210x_write_u16_reg(struct usb_serial_port 
*port, u8 req, u16 val)
  * Writes a variable-sized block of CP210X_ registers, identified by req.
  * Data in buf must be in native USB byte order.
  */
-static int cp210x_write_reg_block(struct usb_serial_port *port, u8 req,
-   void *buf, int bufsize)
+static int cp210x_write_reg_block(struct usb_serial_port *port,
+   u8 req, void *buf, int bufsize)
 {
struct usb_serial *serial = port->serial;
struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
@@ -1134,11 +1183,17 @@ static void cp210x_set_termios(struct tty_struct *tty,
struct usb_serial_port *port, struct ktermios *old_termios)
 {
struct device *dev = >dev;
-   unsigned int cflag, old_cflag;
+   struct cp210x_chars_respones CharsRes;
+   struct cp210x_flow_ctl flow_ctl;
+   unsigned int cflag, old_cflag, iflag;
u16 bits;
+   int result;
+   u32 ctl_hs;
+   u32 flow_repl;
 
cflag = tty->termios.c_cflag;
old_cflag = old_termios->c_cflag;
+   iflag = tty->termios.c_iflag;
 
if (tty->termios.c_ospeed != old_termios->c_ospeed)
cp210x_change_speed(tty, port, old_termios);
@@ -1212,10 +1267,6 @@ static void cp210x_set_termios(struct tty_struct *tty,
}
 
if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
-   struct cp210x_flow_ctl flow_ctl;
-   u32 ctl_hs;
-   u32 flow_repl;
-
cp210x_read_reg_block(port, CP210X_GET_FLOW, _ctl,
sizeof(flow_ctl));
ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
@@ -1252,6 +1303,51 @@ static void cp210x_set_termios(struct tty_struct *tty,
sizeof(flow_ctl));
}
 
+   /* Set Software  Flow  Control
+* Xon/Xoff code
+* Check the IXOFF/IXON status in the iflag component of the
+* termios structure.
+*
+*/
+   if  ((iflag & IXOFF) || (iflag & IXON)) {
+   /*set vstart/vstop chars */
+   result = cp210x_operate_chars_block(port, CP210X_GET_CHARS,
+ REQTYPE_DEVICE_TO_HOST, , 
sizeof(CharsRes));
+   dev_dbg(dev, "%s -