Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp2061510ybh; Fri, 24 Jul 2020 03:33:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzE13tovSJhvh77WHj1vTUB9mExkeVIbLji6RARJDAwgEIM8nx1XdFh6oKC9HfDgnbw0c6q X-Received: by 2002:a17:906:3842:: with SMTP id w2mr8721605ejc.273.1595586797724; Fri, 24 Jul 2020 03:33:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595586797; cv=none; d=google.com; s=arc-20160816; b=kcqebTtZ3A/lEf28wDzl2yltgcoyOmxmLpbpRUtf/yYAkU5N07BnDWfMK9L11SN/IG BT7ISbfzDrytqGUH+eidlkdO9dA2YABn0JjYmIT4vMpDHpo75TE1V0KWQDVcFp8lN1Qg PeC3sRRXc75fMBp7uRGzkrh/kaAs7hynHr0rfQtsk7fMdYHzHMW0SZaRprsY7oO10VEQ EHa6IGHjTW13Xj7Ih6FJAo/V9evYtNWjZpuBMdPxVdvRbTTySidb5cEtTy0kjtWJ08mH HMrpKk8+cgaNTlAwvL1hWJxJ+qbGZy7LXIYaJAnAaP4Np3O+A2a5JV0IY2f7MmR61PsP mN/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=4dflDOrAqQmVXppU0jBwTka3UlUGFsKY0391HvT87Vg=; b=09VioHJdlxje9oEVnV/R8XHOjNfDHQ/dFUru1ypX388MoznzF9jCNmVG2co1j0j6jq zbc7TTSllueYaFeIeDpcQnFJBNKfs7PWZ0vclY86haQ1sfIqWVFfyoqTJg0dwgOfHscT /VYELp2YhfCtR67fpnfQlPSjF4lv/vwsXWV5JtBDWoEpNFEIcNSDL8GhV7KcihFYSWf/ aP8niYHVmugGKr6TZlyHK2Q1+ZMszvYfk8tFUTARIuA66GUZAlKVNYFqXeuOsB3c6TFM I3Bwb7DkHdgPGybHyQTny2cCeqmjSRQbqNwclGgDDvCVOyN8FS+u6L79/jkef5MNlfhm uaug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=STmmjqhU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=163.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g14si285155edv.102.2020.07.24.03.32.54; Fri, 24 Jul 2020 03:33:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=STmmjqhU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=163.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726801AbgGXKal (ORCPT + 99 others); Fri, 24 Jul 2020 06:30:41 -0400 Received: from m12-17.163.com ([220.181.12.17]:58917 "EHLO m12-17.163.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726329AbgGXKal (ORCPT ); Fri, 24 Jul 2020 06:30:41 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id; bh=4dflDOrAqQmVXppU0j BwTka3UlUGFsKY0391HvT87Vg=; b=STmmjqhUEUE4byWnDgdfvbGHe3zy23AWd0 M0uJTAUn7fzKZ1lR6wygFZCvOqFo7ZRjvMXH3V6VyVF0Scsf8NVrar0+opiz6C+9 726S7Mis+gTWY1O58lVVjUdBdLA0d7l3dYF5uACfyQi5UuSx/b8Vp4434u81WxnB GI9ANeDPw= Received: from localhost.localdomain (unknown [118.113.11.27]) by smtp13 (Coremail) with SMTP id EcCowABnOLQhuBpf+hZ9AA--.62427S4; Fri, 24 Jul 2020 18:29:54 +0800 (CST) From: Sheng Long Wang To: johan@kernel.org, gregkh@linuxfoundation.org Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, jan.kiszka@siemens.com, Wang Sheng Long Subject: [PATCH] usb-serial:cp210x: add CP210x support to software flow control Date: Fri, 24 Jul 2020 18:29:46 +0800 Message-Id: <20200724102946.15404-1-china_shenglong@163.com> X-Mailer: git-send-email 2.17.1 X-CM-TRANSID: EcCowABnOLQhuBpf+hZ9AA--.62427S4 X-Coremail-Antispam: 1Uf129KBjvJXoW3Jr4DtFW8JF43tr4DAF45Awb_yoW7uF48pF 4Utay3tFWqvr47Wa1rAF4Uu39xuan7XryIvFy3G39aya13Krn3KF18Ca4Yvr1UAa4xGry5 Jrs8t3yUuw4UtrJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07bFnQUUUUUU= X-Originating-IP: [118.113.11.27] X-CM-SenderInfo: xfkl0tpbvkv0xjor0wi6rwjhhfrp/1tbiyQJrslQHKutUlgAAsC Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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(&port->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 = &port->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, &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, &CharsRes, sizeof(CharsRes)); + dev_dbg(dev, "%s - bXonchar=0x%x bXoffchar=0x%x \n", + __func__, CharsRes.bXonchar, CharsRes.bXoffchar); + if (result < 0) { + dev_err(dev, "Read Characrters Respones failed " + "xon/xoff software flow control\n"); + return; + } + CharsRes.bXonchar = CP210X_VSTART; + CharsRes.bXoffchar = CP210X_VSTOP; + result = cp210x_operate_chars_block(port, CP210X_SET_CHARS, + REQTYPE_HOST_TO_INTERFACE, &CharsRes, sizeof(CharsRes)); + if (result < 0) { + memset(&CharsRes, 0, sizeof(CharsRes)); + dev_err(dev, "Write Characrters Respones failed" + "xon/xoff software flow control\n"); + return; + } + /*Set Rx/Tx Flow Contrl Flag in ulFlowReplace*/ + cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl, sizeof(flow_ctl)); + flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace); + dev_dbg(dev, "%s - read ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n", + __func__, ctl_hs, flow_repl); + if (iflag & IXOFF) + flow_repl |= CP210X_SERIAL_AUTO_RECEIVE; + else + flow_repl &= ~CP210X_SERIAL_AUTO_RECEIVE; + + if (iflag & IXON) + flow_repl |= CP210X_SERIAL_AUTO_TRANSMIT; + else + flow_repl &= ~CP210X_SERIAL_AUTO_TRANSMIT; + + flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl); + cp210x_write_reg_block(port, CP210X_SET_FLOW, &flow_ctl, sizeof(flow_ctl)); + } } static int cp210x_tiocmset(struct tty_struct *tty, -- 2.17.1