aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKonstantin Shkolnyy <konstantin.shkolnyy@gmail.com>2016-05-04 17:57:02 -0400
committerJohan Hovold <johan@kernel.org>2016-05-05 05:45:10 -0400
commit9034389cd81681b4f0123173eb836624199209c7 (patch)
treeada46d018d7941b370937c8967bea69b8ccafa4c
parenta377f9e906af4df9071ba8ddba60188cb4013d93 (diff)
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: Konstantin Shkolnyy <konstantin.shkolnyy@gmail.com> Signed-off-by: Johan Hovold <johan@kernel.org>
-rw-r--r--drivers/usb/serial/cp210x.c109
1 files changed, 84 insertions, 25 deletions
diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c
index fef7a512bff4..a33a4265125d 100644
--- a/drivers/usb/serial/cp210x.c
+++ b/drivers/usb/serial/cp210x.c
@@ -327,6 +327,42 @@ struct cp210x_comm_status {
327 */ 327 */
328#define PURGE_ALL 0x000f 328#define PURGE_ALL 0x000f
329 329
330/* CP210X_GET_FLOW/CP210X_SET_FLOW read/write these 0x10 bytes */
331struct cp210x_flow_ctl {
332 __le32 ulControlHandshake;
333 __le32 ulFlowReplace;
334 __le32 ulXonLimit;
335 __le32 ulXoffLimit;
336} __packed;
337
338/* cp210x_flow_ctl::ulControlHandshake */
339#define CP210X_SERIAL_DTR_MASK GENMASK(1, 0)
340#define CP210X_SERIAL_DTR_SHIFT(_mode) (_mode)
341#define CP210X_SERIAL_CTS_HANDSHAKE BIT(3)
342#define CP210X_SERIAL_DSR_HANDSHAKE BIT(4)
343#define CP210X_SERIAL_DCD_HANDSHAKE BIT(5)
344#define CP210X_SERIAL_DSR_SENSITIVITY BIT(6)
345
346/* values for cp210x_flow_ctl::ulControlHandshake::CP210X_SERIAL_DTR_MASK */
347#define CP210X_SERIAL_DTR_INACTIVE 0
348#define CP210X_SERIAL_DTR_ACTIVE 1
349#define CP210X_SERIAL_DTR_FLOW_CTL 2
350
351/* cp210x_flow_ctl::ulFlowReplace */
352#define CP210X_SERIAL_AUTO_TRANSMIT BIT(0)
353#define CP210X_SERIAL_AUTO_RECEIVE BIT(1)
354#define CP210X_SERIAL_ERROR_CHAR BIT(2)
355#define CP210X_SERIAL_NULL_STRIPPING BIT(3)
356#define CP210X_SERIAL_BREAK_CHAR BIT(4)
357#define CP210X_SERIAL_RTS_MASK GENMASK(7, 6)
358#define CP210X_SERIAL_RTS_SHIFT(_mode) (_mode << 6)
359#define CP210X_SERIAL_XOFF_CONTINUE BIT(31)
360
361/* values for cp210x_flow_ctl::ulFlowReplace::CP210X_SERIAL_RTS_MASK */
362#define CP210X_SERIAL_RTS_INACTIVE 0
363#define CP210X_SERIAL_RTS_ACTIVE 1
364#define CP210X_SERIAL_RTS_FLOW_CTL 2
365
330/* 366/*
331 * Reads a variable-sized block of CP210X_ registers, identified by req. 367 * Reads a variable-sized block of CP210X_ registers, identified by req.
332 * Returns data into buf in native USB byte order. 368 * Returns data into buf in native USB byte order.
@@ -694,9 +730,10 @@ static void cp210x_get_termios_port(struct usb_serial_port *port,
694{ 730{
695 struct device *dev = &port->dev; 731 struct device *dev = &port->dev;
696 unsigned int cflag; 732 unsigned int cflag;
697 u8 modem_ctl[16]; 733 struct cp210x_flow_ctl flow_ctl;
698 u32 baud; 734 u32 baud;
699 u16 bits; 735 u16 bits;
736 u32 ctl_hs;
700 737
701 cp210x_read_u32_reg(port, CP210X_GET_BAUDRATE, &baud); 738 cp210x_read_u32_reg(port, CP210X_GET_BAUDRATE, &baud);
702 739
@@ -792,9 +829,10 @@ static void cp210x_get_termios_port(struct usb_serial_port *port,
792 break; 829 break;
793 } 830 }
794 831
795 cp210x_read_reg_block(port, CP210X_GET_FLOW, modem_ctl, 832 cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
796 sizeof(modem_ctl)); 833 sizeof(flow_ctl));
797 if (modem_ctl[0] & 0x08) { 834 ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
835 if (ctl_hs & CP210X_SERIAL_CTS_HANDSHAKE) {
798 dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__); 836 dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
799 cflag |= CRTSCTS; 837 cflag |= CRTSCTS;
800 } else { 838 } else {
@@ -863,7 +901,6 @@ static void cp210x_set_termios(struct tty_struct *tty,
863 struct device *dev = &port->dev; 901 struct device *dev = &port->dev;
864 unsigned int cflag, old_cflag; 902 unsigned int cflag, old_cflag;
865 u16 bits; 903 u16 bits;
866 u8 modem_ctl[16];
867 904
868 cflag = tty->termios.c_cflag; 905 cflag = tty->termios.c_cflag;
869 old_cflag = old_termios->c_cflag; 906 old_cflag = old_termios->c_cflag;
@@ -947,34 +984,56 @@ static void cp210x_set_termios(struct tty_struct *tty,
947 } 984 }
948 985
949 if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) { 986 if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
987 struct cp210x_flow_ctl flow_ctl;
988 u32 ctl_hs;
989 u32 flow_repl;
950 990
951 /* Only bytes 0, 4 and 7 out of first 8 have functional bits */ 991 cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
952 992 sizeof(flow_ctl));
953 cp210x_read_reg_block(port, CP210X_GET_FLOW, modem_ctl, 993 ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
954 sizeof(modem_ctl)); 994 flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace);
955 dev_dbg(dev, "%s - read modem controls = %02x .. .. .. %02x .. .. %02x\n", 995 dev_dbg(dev, "%s - read ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
956 __func__, modem_ctl[0], modem_ctl[4], modem_ctl[7]); 996 __func__, ctl_hs, flow_repl);
957 997
958 if (cflag & CRTSCTS) { 998 if (cflag & CRTSCTS) {
959 modem_ctl[0] &= ~0x7B; 999 ctl_hs &= ~(CP210X_SERIAL_DTR_MASK |
960 modem_ctl[0] |= 0x09; 1000 CP210X_SERIAL_CTS_HANDSHAKE |
961 modem_ctl[4] = 0x80; 1001 CP210X_SERIAL_DSR_HANDSHAKE |
962 /* FIXME - why clear reserved bits just read? */ 1002 CP210X_SERIAL_DCD_HANDSHAKE |
963 modem_ctl[5] = 0; 1003 CP210X_SERIAL_DSR_SENSITIVITY);
964 modem_ctl[6] = 0; 1004 ctl_hs |= CP210X_SERIAL_DTR_SHIFT(
965 modem_ctl[7] = 0; 1005 CP210X_SERIAL_DTR_ACTIVE);
1006 ctl_hs |= CP210X_SERIAL_CTS_HANDSHAKE;
1007 /*
1008 * FIXME: Why clear bits unrelated to flow control.
1009 * Why clear CP210X_SERIAL_XOFF_CONTINUE which is
1010 * never set
1011 */
1012 flow_repl = 0;
1013 flow_repl |= CP210X_SERIAL_RTS_SHIFT(
1014 CP210X_SERIAL_RTS_FLOW_CTL);
966 dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__); 1015 dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
967 } else { 1016 } else {
968 modem_ctl[0] &= ~0x7B; 1017 ctl_hs &= ~(CP210X_SERIAL_DTR_MASK |
969 modem_ctl[0] |= 0x01; 1018 CP210X_SERIAL_CTS_HANDSHAKE |
970 modem_ctl[4] = 0x40; 1019 CP210X_SERIAL_DSR_HANDSHAKE |
1020 CP210X_SERIAL_DCD_HANDSHAKE |
1021 CP210X_SERIAL_DSR_SENSITIVITY);
1022 ctl_hs |= CP210X_SERIAL_DTR_SHIFT(
1023 CP210X_SERIAL_DTR_ACTIVE);
1024 /* FIXME: Why clear bits unrelated to flow control */
1025 flow_repl &= 0xffffff00;
1026 flow_repl |= CP210X_SERIAL_RTS_SHIFT(
1027 CP210X_SERIAL_RTS_ACTIVE);
971 dev_dbg(dev, "%s - flow control = NONE\n", __func__); 1028 dev_dbg(dev, "%s - flow control = NONE\n", __func__);
972 } 1029 }
973 1030
974 dev_dbg(dev, "%s - write modem controls = %02x .. .. .. %02x .. .. %02x\n", 1031 dev_dbg(dev, "%s - write ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
975 __func__, modem_ctl[0], modem_ctl[4], modem_ctl[7]); 1032 __func__, ctl_hs, flow_repl);
976 cp210x_write_reg_block(port, CP210X_SET_FLOW, modem_ctl, 1033 flow_ctl.ulControlHandshake = cpu_to_le32(ctl_hs);
977 sizeof(modem_ctl)); 1034 flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl);
1035 cp210x_write_reg_block(port, CP210X_SET_FLOW, &flow_ctl,
1036 sizeof(flow_ctl));
978 } 1037 }
979 1038
980} 1039}