Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9034389c authored by Konstantin Shkolnyy's avatar Konstantin Shkolnyy Committed by Johan Hovold
Browse files

USB: serial: cp210x: get rid of magic numbers in CRTSCTS flag code



Replaced magic numbers used in the CRTSCTS flag code with symbolic names
from the chip specification.

Signed-off-by: default avatarKonstantin Shkolnyy <konstantin.shkolnyy@gmail.com>
Signed-off-by: default avatarJohan Hovold <johan@kernel.org>
parent a377f9e9
Loading
Loading
Loading
Loading
+84 −25
Original line number Original line Diff line number Diff line
@@ -327,6 +327,42 @@ struct cp210x_comm_status {
 */
 */
#define PURGE_ALL		0x000f
#define PURGE_ALL		0x000f


/* CP210X_GET_FLOW/CP210X_SET_FLOW read/write these 0x10 bytes */
struct cp210x_flow_ctl {
	__le32	ulControlHandshake;
	__le32	ulFlowReplace;
	__le32	ulXonLimit;
	__le32	ulXoffLimit;
} __packed;

/* cp210x_flow_ctl::ulControlHandshake */
#define CP210X_SERIAL_DTR_MASK		GENMASK(1, 0)
#define CP210X_SERIAL_DTR_SHIFT(_mode)	(_mode)
#define CP210X_SERIAL_CTS_HANDSHAKE	BIT(3)
#define CP210X_SERIAL_DSR_HANDSHAKE	BIT(4)
#define CP210X_SERIAL_DCD_HANDSHAKE	BIT(5)
#define CP210X_SERIAL_DSR_SENSITIVITY	BIT(6)

/* values for cp210x_flow_ctl::ulControlHandshake::CP210X_SERIAL_DTR_MASK */
#define CP210X_SERIAL_DTR_INACTIVE	0
#define CP210X_SERIAL_DTR_ACTIVE	1
#define CP210X_SERIAL_DTR_FLOW_CTL	2

/* cp210x_flow_ctl::ulFlowReplace */
#define CP210X_SERIAL_AUTO_TRANSMIT	BIT(0)
#define CP210X_SERIAL_AUTO_RECEIVE	BIT(1)
#define CP210X_SERIAL_ERROR_CHAR	BIT(2)
#define CP210X_SERIAL_NULL_STRIPPING	BIT(3)
#define CP210X_SERIAL_BREAK_CHAR	BIT(4)
#define CP210X_SERIAL_RTS_MASK		GENMASK(7, 6)
#define CP210X_SERIAL_RTS_SHIFT(_mode)	(_mode << 6)
#define CP210X_SERIAL_XOFF_CONTINUE	BIT(31)

/* values for cp210x_flow_ctl::ulFlowReplace::CP210X_SERIAL_RTS_MASK */
#define CP210X_SERIAL_RTS_INACTIVE	0
#define CP210X_SERIAL_RTS_ACTIVE	1
#define CP210X_SERIAL_RTS_FLOW_CTL	2

/*
/*
 * Reads a variable-sized block of CP210X_ registers, identified by req.
 * Reads a variable-sized block of CP210X_ registers, identified by req.
 * Returns data into buf in native USB byte order.
 * Returns data into buf in native USB byte order.
@@ -694,9 +730,10 @@ static void cp210x_get_termios_port(struct usb_serial_port *port,
{
{
	struct device *dev = &port->dev;
	struct device *dev = &port->dev;
	unsigned int cflag;
	unsigned int cflag;
	u8 modem_ctl[16];
	struct cp210x_flow_ctl flow_ctl;
	u32 baud;
	u32 baud;
	u16 bits;
	u16 bits;
	u32 ctl_hs;


	cp210x_read_u32_reg(port, CP210X_GET_BAUDRATE, &baud);
	cp210x_read_u32_reg(port, CP210X_GET_BAUDRATE, &baud);


@@ -792,9 +829,10 @@ static void cp210x_get_termios_port(struct usb_serial_port *port,
		break;
		break;
	}
	}


	cp210x_read_reg_block(port, CP210X_GET_FLOW, modem_ctl,
	cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
			sizeof(modem_ctl));
			sizeof(flow_ctl));
	if (modem_ctl[0] & 0x08) {
	ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
	if (ctl_hs & CP210X_SERIAL_CTS_HANDSHAKE) {
		dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
		dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
		cflag |= CRTSCTS;
		cflag |= CRTSCTS;
	} else {
	} else {
@@ -863,7 +901,6 @@ static void cp210x_set_termios(struct tty_struct *tty,
	struct device *dev = &port->dev;
	struct device *dev = &port->dev;
	unsigned int cflag, old_cflag;
	unsigned int cflag, old_cflag;
	u16 bits;
	u16 bits;
	u8 modem_ctl[16];


	cflag = tty->termios.c_cflag;
	cflag = tty->termios.c_cflag;
	old_cflag = old_termios->c_cflag;
	old_cflag = old_termios->c_cflag;
@@ -947,34 +984,56 @@ static void cp210x_set_termios(struct tty_struct *tty,
	}
	}


	if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
	if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
		struct cp210x_flow_ctl flow_ctl;
		u32 ctl_hs;
		u32 flow_repl;


		/* Only bytes 0, 4 and 7 out of first 8 have functional bits */
		cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,

				sizeof(flow_ctl));
		cp210x_read_reg_block(port, CP210X_GET_FLOW, modem_ctl,
		ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
				sizeof(modem_ctl));
		flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace);
		dev_dbg(dev, "%s - read modem controls = %02x .. .. .. %02x .. .. %02x\n",
		dev_dbg(dev, "%s - read ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
			__func__, modem_ctl[0], modem_ctl[4], modem_ctl[7]);
				__func__, ctl_hs, flow_repl);


		if (cflag & CRTSCTS) {
		if (cflag & CRTSCTS) {
			modem_ctl[0] &= ~0x7B;
			ctl_hs &= ~(CP210X_SERIAL_DTR_MASK |
			modem_ctl[0] |= 0x09;
					CP210X_SERIAL_CTS_HANDSHAKE |
			modem_ctl[4] = 0x80;
					CP210X_SERIAL_DSR_HANDSHAKE |
			/* FIXME - why clear reserved bits just read? */
					CP210X_SERIAL_DCD_HANDSHAKE |
			modem_ctl[5] = 0;
					CP210X_SERIAL_DSR_SENSITIVITY);
			modem_ctl[6] = 0;
			ctl_hs |= CP210X_SERIAL_DTR_SHIFT(
			modem_ctl[7] = 0;
					CP210X_SERIAL_DTR_ACTIVE);
			ctl_hs |= CP210X_SERIAL_CTS_HANDSHAKE;
			/*
			 * FIXME: Why clear bits unrelated to flow control.
			 * Why clear CP210X_SERIAL_XOFF_CONTINUE which is
			 * never set
			 */
			flow_repl = 0;
			flow_repl |= CP210X_SERIAL_RTS_SHIFT(
					CP210X_SERIAL_RTS_FLOW_CTL);
			dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
			dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
		} else {
		} else {
			modem_ctl[0] &= ~0x7B;
			ctl_hs &= ~(CP210X_SERIAL_DTR_MASK |
			modem_ctl[0] |= 0x01;
					CP210X_SERIAL_CTS_HANDSHAKE |
			modem_ctl[4] = 0x40;
					CP210X_SERIAL_DSR_HANDSHAKE |
					CP210X_SERIAL_DCD_HANDSHAKE |
					CP210X_SERIAL_DSR_SENSITIVITY);
			ctl_hs |= CP210X_SERIAL_DTR_SHIFT(
					CP210X_SERIAL_DTR_ACTIVE);
			/* FIXME: Why clear bits unrelated to flow control */
			flow_repl &= 0xffffff00;
			flow_repl |= CP210X_SERIAL_RTS_SHIFT(
					CP210X_SERIAL_RTS_ACTIVE);
			dev_dbg(dev, "%s - flow control = NONE\n", __func__);
			dev_dbg(dev, "%s - flow control = NONE\n", __func__);
		}
		}


		dev_dbg(dev, "%s - write modem controls = %02x .. .. .. %02x .. .. %02x\n",
		dev_dbg(dev, "%s - write ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
			__func__, modem_ctl[0], modem_ctl[4], modem_ctl[7]);
				__func__, ctl_hs, flow_repl);
		cp210x_write_reg_block(port, CP210X_SET_FLOW, modem_ctl,
		flow_ctl.ulControlHandshake = cpu_to_le32(ctl_hs);
				sizeof(modem_ctl));
		flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl);
		cp210x_write_reg_block(port, CP210X_SET_FLOW, &flow_ctl,
				sizeof(flow_ctl));
	}
	}


}
}