cp210x.c (60dd57c7479418e2bc902143eb46a2fdcfeecbbb) cp210x.c (f5cfbecb0a162319464c9408420282d22ed69721)
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Silicon Laboratories CP210x USB to RS232 serial adaptor driver
4 *
5 * Copyright (C) 2005 Craig Shelley (craig@microtron.org.uk)
6 * Copyright (C) 2010-2021 Johan Hovold (johan@kernel.org)
7 *
8 * Support to set flow control line levels using TIOCMGET and TIOCMSET

--- 219 unchanged lines hidden (view full) ---

228 { USB_DEVICE(0x1FB9, 0x0402) }, /* Lake Shore Model 455A Gaussmeter */
229 { USB_DEVICE(0x1FB9, 0x0403) }, /* Lake Shore Model 475A Gaussmeter */
230 { USB_DEVICE(0x1FB9, 0x0404) }, /* Lake Shore Model 465 Three Axis Gaussmeter */
231 { USB_DEVICE(0x1FB9, 0x0600) }, /* Lake Shore Model 625A Superconducting MPS */
232 { USB_DEVICE(0x1FB9, 0x0601) }, /* Lake Shore Model 642A Magnet Power Supply */
233 { USB_DEVICE(0x1FB9, 0x0602) }, /* Lake Shore Model 648 Magnet Power Supply */
234 { USB_DEVICE(0x1FB9, 0x0700) }, /* Lake Shore Model 737 VSM Controller */
235 { USB_DEVICE(0x1FB9, 0x0701) }, /* Lake Shore Model 776 Hall Matrix */
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Silicon Laboratories CP210x USB to RS232 serial adaptor driver
4 *
5 * Copyright (C) 2005 Craig Shelley (craig@microtron.org.uk)
6 * Copyright (C) 2010-2021 Johan Hovold (johan@kernel.org)
7 *
8 * Support to set flow control line levels using TIOCMGET and TIOCMSET

--- 219 unchanged lines hidden (view full) ---

228 { USB_DEVICE(0x1FB9, 0x0402) }, /* Lake Shore Model 455A Gaussmeter */
229 { USB_DEVICE(0x1FB9, 0x0403) }, /* Lake Shore Model 475A Gaussmeter */
230 { USB_DEVICE(0x1FB9, 0x0404) }, /* Lake Shore Model 465 Three Axis Gaussmeter */
231 { USB_DEVICE(0x1FB9, 0x0600) }, /* Lake Shore Model 625A Superconducting MPS */
232 { USB_DEVICE(0x1FB9, 0x0601) }, /* Lake Shore Model 642A Magnet Power Supply */
233 { USB_DEVICE(0x1FB9, 0x0602) }, /* Lake Shore Model 648 Magnet Power Supply */
234 { USB_DEVICE(0x1FB9, 0x0700) }, /* Lake Shore Model 737 VSM Controller */
235 { USB_DEVICE(0x1FB9, 0x0701) }, /* Lake Shore Model 776 Hall Matrix */
236 { USB_DEVICE(0x2184, 0x0030) }, /* GW Instek GDM-834x Digital Multimeter */
237 { USB_DEVICE(0x2626, 0xEA60) }, /* Aruba Networks 7xxx USB Serial Console */
238 { USB_DEVICE(0x3195, 0xF190) }, /* Link Instruments MSO-19 */
239 { USB_DEVICE(0x3195, 0xF280) }, /* Link Instruments MSO-28 */
240 { USB_DEVICE(0x3195, 0xF281) }, /* Link Instruments MSO-28 */
241 { USB_DEVICE(0x3923, 0x7A0B) }, /* National Instruments USB Serial Console */
242 { USB_DEVICE(0x413C, 0x9500) }, /* DW700 GPS USB interface */
243 { } /* Terminating Entry */
244};

--- 9 unchanged lines hidden (view full) ---

254 u16 gpio_input;
255#endif
256 u8 partnum;
257 u32 fw_version;
258 speed_t min_speed;
259 speed_t max_speed;
260 bool use_actual_rate;
261 bool no_flow_control;
236 { USB_DEVICE(0x2626, 0xEA60) }, /* Aruba Networks 7xxx USB Serial Console */
237 { USB_DEVICE(0x3195, 0xF190) }, /* Link Instruments MSO-19 */
238 { USB_DEVICE(0x3195, 0xF280) }, /* Link Instruments MSO-28 */
239 { USB_DEVICE(0x3195, 0xF281) }, /* Link Instruments MSO-28 */
240 { USB_DEVICE(0x3923, 0x7A0B) }, /* National Instruments USB Serial Console */
241 { USB_DEVICE(0x413C, 0x9500) }, /* DW700 GPS USB interface */
242 { } /* Terminating Entry */
243};

--- 9 unchanged lines hidden (view full) ---

253 u16 gpio_input;
254#endif
255 u8 partnum;
256 u32 fw_version;
257 speed_t min_speed;
258 speed_t max_speed;
259 bool use_actual_rate;
260 bool no_flow_control;
262 bool no_event_mode;
263};
264
265enum cp210x_event_state {
266 ES_DATA,
267 ES_ESCAPE,
268 ES_LSR,
269 ES_LSR_DATA_0,
270 ES_LSR_DATA_1,

--- 355 unchanged lines hidden (view full) ---

626 * Reads a variable-sized block of CP210X_ registers, identified by req.
627 * Returns data into buf in native USB byte order.
628 */
629static int cp210x_read_reg_block(struct usb_serial_port *port, u8 req,
630 void *buf, int bufsize)
631{
632 struct usb_serial *serial = port->serial;
633 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
261};
262
263enum cp210x_event_state {
264 ES_DATA,
265 ES_ESCAPE,
266 ES_LSR,
267 ES_LSR_DATA_0,
268 ES_LSR_DATA_1,

--- 355 unchanged lines hidden (view full) ---

624 * Reads a variable-sized block of CP210X_ registers, identified by req.
625 * Returns data into buf in native USB byte order.
626 */
627static int cp210x_read_reg_block(struct usb_serial_port *port, u8 req,
628 void *buf, int bufsize)
629{
630 struct usb_serial *serial = port->serial;
631 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
634 void *dmabuf;
635 int result;
636
632 int result;
633
637 dmabuf = kmalloc(bufsize, GFP_KERNEL);
638 if (!dmabuf)
639 return -ENOMEM;
640
634
641 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
642 req, REQTYPE_INTERFACE_TO_HOST, 0,
643 port_priv->bInterfaceNumber, dmabuf, bufsize,
644 USB_CTRL_GET_TIMEOUT);
645 if (result == bufsize) {
646 memcpy(buf, dmabuf, bufsize);
647 result = 0;
648 } else {
635 result = usb_control_msg_recv(serial->dev, 0, req,
636 REQTYPE_INTERFACE_TO_HOST, 0,
637 port_priv->bInterfaceNumber, buf, bufsize,
638 USB_CTRL_SET_TIMEOUT, GFP_KERNEL);
639 if (result) {
649 dev_err(&port->dev, "failed get req 0x%x size %d status: %d\n",
650 req, bufsize, result);
640 dev_err(&port->dev, "failed get req 0x%x size %d status: %d\n",
641 req, bufsize, result);
651 if (result >= 0)
652 result = -EIO;
642 return result;
653 }
654
643 }
644
655 kfree(dmabuf);
656
657 return result;
645 return 0;
658}
659
660/*
661 * Reads any 8-bit CP210X_ register identified by req.
662 */
663static int cp210x_read_u8_reg(struct usb_serial_port *port, u8 req, u8 *val)
664{
665 return cp210x_read_reg_block(port, req, val, sizeof(*val));
666}
667
668/*
669 * Reads a variable-sized vendor block of CP210X_ registers, identified by val.
670 * Returns data into buf in native USB byte order.
671 */
672static int cp210x_read_vendor_block(struct usb_serial *serial, u8 type, u16 val,
673 void *buf, int bufsize)
674{
646}
647
648/*
649 * Reads any 8-bit CP210X_ register identified by req.
650 */
651static int cp210x_read_u8_reg(struct usb_serial_port *port, u8 req, u8 *val)
652{
653 return cp210x_read_reg_block(port, req, val, sizeof(*val));
654}
655
656/*
657 * Reads a variable-sized vendor block of CP210X_ registers, identified by val.
658 * Returns data into buf in native USB byte order.
659 */
660static int cp210x_read_vendor_block(struct usb_serial *serial, u8 type, u16 val,
661 void *buf, int bufsize)
662{
675 void *dmabuf;
676 int result;
677
663 int result;
664
678 dmabuf = kmalloc(bufsize, GFP_KERNEL);
679 if (!dmabuf)
680 return -ENOMEM;
681
682 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
683 CP210X_VENDOR_SPECIFIC, type, val,
684 cp210x_interface_num(serial), dmabuf, bufsize,
685 USB_CTRL_GET_TIMEOUT);
686 if (result == bufsize) {
687 memcpy(buf, dmabuf, bufsize);
688 result = 0;
689 } else {
665 result = usb_control_msg_recv(serial->dev, 0, CP210X_VENDOR_SPECIFIC,
666 type, val, cp210x_interface_num(serial), buf, bufsize,
667 USB_CTRL_GET_TIMEOUT, GFP_KERNEL);
668 if (result) {
690 dev_err(&serial->interface->dev,
691 "failed to get vendor val 0x%04x size %d: %d\n", val,
692 bufsize, result);
669 dev_err(&serial->interface->dev,
670 "failed to get vendor val 0x%04x size %d: %d\n", val,
671 bufsize, result);
693 if (result >= 0)
694 result = -EIO;
672 return result;
695 }
696
673 }
674
697 kfree(dmabuf);
698
699 return result;
675 return 0;
700}
701
702/*
703 * Writes any 16-bit CP210X_ register (req) whose value is passed
704 * entirely in the wValue field of the USB request.
705 */
706static int cp210x_write_u16_reg(struct usb_serial_port *port, u8 req, u16 val)
707{

--- 17 unchanged lines hidden (view full) ---

725 * Writes a variable-sized block of CP210X_ registers, identified by req.
726 * Data in buf must be in native USB byte order.
727 */
728static int cp210x_write_reg_block(struct usb_serial_port *port, u8 req,
729 void *buf, int bufsize)
730{
731 struct usb_serial *serial = port->serial;
732 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
676}
677
678/*
679 * Writes any 16-bit CP210X_ register (req) whose value is passed
680 * entirely in the wValue field of the USB request.
681 */
682static int cp210x_write_u16_reg(struct usb_serial_port *port, u8 req, u16 val)
683{

--- 17 unchanged lines hidden (view full) ---

701 * Writes a variable-sized block of CP210X_ registers, identified by req.
702 * Data in buf must be in native USB byte order.
703 */
704static int cp210x_write_reg_block(struct usb_serial_port *port, u8 req,
705 void *buf, int bufsize)
706{
707 struct usb_serial *serial = port->serial;
708 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
733 void *dmabuf;
734 int result;
735
709 int result;
710
736 dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
737 if (!dmabuf)
738 return -ENOMEM;
739
740 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
741 req, REQTYPE_HOST_TO_INTERFACE, 0,
742 port_priv->bInterfaceNumber, dmabuf, bufsize,
743 USB_CTRL_SET_TIMEOUT);
744
745 kfree(dmabuf);
746
747 if (result < 0) {
711 result = usb_control_msg_send(serial->dev, 0, req,
712 REQTYPE_HOST_TO_INTERFACE, 0,
713 port_priv->bInterfaceNumber, buf, bufsize,
714 USB_CTRL_SET_TIMEOUT, GFP_KERNEL);
715 if (result) {
748 dev_err(&port->dev, "failed set req 0x%x size %d status: %d\n",
749 req, bufsize, result);
750 return result;
751 }
752
753 return 0;
754}
755

--- 12 unchanged lines hidden (view full) ---

768#ifdef CONFIG_GPIOLIB
769/*
770 * Writes a variable-sized vendor block of CP210X_ registers, identified by val.
771 * Data in buf must be in native USB byte order.
772 */
773static int cp210x_write_vendor_block(struct usb_serial *serial, u8 type,
774 u16 val, void *buf, int bufsize)
775{
716 dev_err(&port->dev, "failed set req 0x%x size %d status: %d\n",
717 req, bufsize, result);
718 return result;
719 }
720
721 return 0;
722}
723

--- 12 unchanged lines hidden (view full) ---

736#ifdef CONFIG_GPIOLIB
737/*
738 * Writes a variable-sized vendor block of CP210X_ registers, identified by val.
739 * Data in buf must be in native USB byte order.
740 */
741static int cp210x_write_vendor_block(struct usb_serial *serial, u8 type,
742 u16 val, void *buf, int bufsize)
743{
776 void *dmabuf;
777 int result;
778
744 int result;
745
779 dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
780 if (!dmabuf)
781 return -ENOMEM;
782
783 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
784 CP210X_VENDOR_SPECIFIC, type, val,
785 cp210x_interface_num(serial), dmabuf, bufsize,
786 USB_CTRL_SET_TIMEOUT);
787
788 kfree(dmabuf);
789
790 if (result < 0) {
746 result = usb_control_msg_send(serial->dev, 0, CP210X_VENDOR_SPECIFIC,
747 type, val, cp210x_interface_num(serial), buf, bufsize,
748 USB_CTRL_SET_TIMEOUT, GFP_KERNEL);
749 if (result) {
791 dev_err(&serial->interface->dev,
792 "failed to set vendor val 0x%04x size %d: %d\n", val,
793 bufsize, result);
794 return result;
795 }
796
797 return 0;
798}

--- 148 unchanged lines hidden (view full) ---

947/*
948 * Read how many bytes are waiting in the TX queue.
949 */
950static int cp210x_get_tx_queue_byte_count(struct usb_serial_port *port,
951 u32 *count)
952{
953 struct usb_serial *serial = port->serial;
954 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
750 dev_err(&serial->interface->dev,
751 "failed to set vendor val 0x%04x size %d: %d\n", val,
752 bufsize, result);
753 return result;
754 }
755
756 return 0;
757}

--- 148 unchanged lines hidden (view full) ---

906/*
907 * Read how many bytes are waiting in the TX queue.
908 */
909static int cp210x_get_tx_queue_byte_count(struct usb_serial_port *port,
910 u32 *count)
911{
912 struct usb_serial *serial = port->serial;
913 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
955 struct cp210x_comm_status *sts;
914 struct cp210x_comm_status sts;
956 int result;
957
915 int result;
916
958 sts = kmalloc(sizeof(*sts), GFP_KERNEL);
959 if (!sts)
960 return -ENOMEM;
961
962 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
963 CP210X_GET_COMM_STATUS, REQTYPE_INTERFACE_TO_HOST,
964 0, port_priv->bInterfaceNumber, sts, sizeof(*sts),
965 USB_CTRL_GET_TIMEOUT);
966 if (result == sizeof(*sts)) {
967 *count = le32_to_cpu(sts->ulAmountInOutQueue);
968 result = 0;
969 } else {
917 result = usb_control_msg_recv(serial->dev, 0, CP210X_GET_COMM_STATUS,
918 REQTYPE_INTERFACE_TO_HOST, 0,
919 port_priv->bInterfaceNumber, &sts, sizeof(sts),
920 USB_CTRL_GET_TIMEOUT, GFP_KERNEL);
921 if (result) {
970 dev_err(&port->dev, "failed to get comm status: %d\n", result);
922 dev_err(&port->dev, "failed to get comm status: %d\n", result);
971 if (result >= 0)
972 result = -EIO;
923 return result;
973 }
974
924 }
925
975 kfree(sts);
926 *count = le32_to_cpu(sts.ulAmountInOutQueue);
976
927
977 return result;
928 return 0;
978}
979
980static bool cp210x_tx_empty(struct usb_serial_port *port)
981{
982 int err;
983 u32 count;
984
985 err = cp210x_get_tx_queue_byte_count(port, &count);

--- 124 unchanged lines hidden (view full) ---

1110 baud = 9600;
1111 }
1112
1113 tty_encode_baud_rate(tty, baud, baud);
1114}
1115
1116static void cp210x_enable_event_mode(struct usb_serial_port *port)
1117{
929}
930
931static bool cp210x_tx_empty(struct usb_serial_port *port)
932{
933 int err;
934 u32 count;
935
936 err = cp210x_get_tx_queue_byte_count(port, &count);

--- 124 unchanged lines hidden (view full) ---

1061 baud = 9600;
1062 }
1063
1064 tty_encode_baud_rate(tty, baud, baud);
1065}
1066
1067static void cp210x_enable_event_mode(struct usb_serial_port *port)
1068{
1118 struct cp210x_serial_private *priv = usb_get_serial_data(port->serial);
1119 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
1120 int ret;
1121
1122 if (port_priv->event_mode)
1123 return;
1124
1069 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
1070 int ret;
1071
1072 if (port_priv->event_mode)
1073 return;
1074
1125 if (priv->no_event_mode)
1126 return;
1127
1128 port_priv->event_state = ES_DATA;
1129 port_priv->event_mode = true;
1130
1131 ret = cp210x_write_u16_reg(port, CP210X_EMBED_EVENTS, CP210X_ESCCHAR);
1132 if (ret) {
1133 dev_err(&port->dev, "failed to enable events: %d\n", ret);
1134 port_priv->event_mode = false;
1135 }

--- 939 unchanged lines hidden (view full) ---

2075 break;
2076 }
2077
2078 priv->min_speed = min;
2079 priv->max_speed = max;
2080 priv->use_actual_rate = use_actual_rate;
2081}
2082
1075 port_priv->event_state = ES_DATA;
1076 port_priv->event_mode = true;
1077
1078 ret = cp210x_write_u16_reg(port, CP210X_EMBED_EVENTS, CP210X_ESCCHAR);
1079 if (ret) {
1080 dev_err(&port->dev, "failed to enable events: %d\n", ret);
1081 port_priv->event_mode = false;
1082 }

--- 939 unchanged lines hidden (view full) ---

2022 break;
2023 }
2024
2025 priv->min_speed = min;
2026 priv->max_speed = max;
2027 priv->use_actual_rate = use_actual_rate;
2028}
2029
2083static void cp2102_determine_quirks(struct usb_serial *serial)
2084{
2085 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
2086 u8 *buf;
2087 int ret;
2088
2089 buf = kmalloc(2, GFP_KERNEL);
2090 if (!buf)
2091 return;
2092 /*
2093 * Some (possibly counterfeit) CP2102 do not support event-insertion
2094 * mode and respond differently to malformed vendor requests.
2095 * Specifically, they return one instead of two bytes when sent a
2096 * two-byte part-number request.
2097 */
2098 ret = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2099 CP210X_VENDOR_SPECIFIC, REQTYPE_DEVICE_TO_HOST,
2100 CP210X_GET_PARTNUM, 0, buf, 2, USB_CTRL_GET_TIMEOUT);
2101 if (ret == 1) {
2102 dev_dbg(&serial->interface->dev,
2103 "device does not support event-insertion mode\n");
2104 priv->no_event_mode = true;
2105 }
2106
2107 kfree(buf);
2108}
2109
2110static int cp210x_get_fw_version(struct usb_serial *serial, u16 value)
2111{
2112 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
2113 u8 ver[3];
2114 int ret;
2115
2116 ret = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST, value,
2117 ver, sizeof(ver));

--- 18 unchanged lines hidden (view full) ---

2136 sizeof(priv->partnum));
2137 if (ret < 0) {
2138 dev_warn(&serial->interface->dev,
2139 "querying part number failed\n");
2140 priv->partnum = CP210X_PARTNUM_UNKNOWN;
2141 return;
2142 }
2143
2030static int cp210x_get_fw_version(struct usb_serial *serial, u16 value)
2031{
2032 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
2033 u8 ver[3];
2034 int ret;
2035
2036 ret = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST, value,
2037 ver, sizeof(ver));

--- 18 unchanged lines hidden (view full) ---

2056 sizeof(priv->partnum));
2057 if (ret < 0) {
2058 dev_warn(&serial->interface->dev,
2059 "querying part number failed\n");
2060 priv->partnum = CP210X_PARTNUM_UNKNOWN;
2061 return;
2062 }
2063
2144 dev_dbg(&serial->interface->dev, "partnum = 0x%02x\n", priv->partnum);
2145
2146 switch (priv->partnum) {
2064 switch (priv->partnum) {
2147 case CP210X_PARTNUM_CP2102:
2148 cp2102_determine_quirks(serial);
2149 break;
2150 case CP210X_PARTNUM_CP2105:
2151 case CP210X_PARTNUM_CP2108:
2152 cp210x_get_fw_version(serial, CP210X_GET_FW_VER);
2153 break;
2154 case CP210X_PARTNUM_CP2102N_QFN28:
2155 case CP210X_PARTNUM_CP2102N_QFN24:
2156 case CP210X_PARTNUM_CP2102N_QFN20:
2157 ret = cp210x_get_fw_version(serial, CP210X_GET_FW_VER_2N);

--- 51 unchanged lines hidden ---
2065 case CP210X_PARTNUM_CP2105:
2066 case CP210X_PARTNUM_CP2108:
2067 cp210x_get_fw_version(serial, CP210X_GET_FW_VER);
2068 break;
2069 case CP210X_PARTNUM_CP2102N_QFN28:
2070 case CP210X_PARTNUM_CP2102N_QFN24:
2071 case CP210X_PARTNUM_CP2102N_QFN20:
2072 ret = cp210x_get_fw_version(serial, CP210X_GET_FW_VER_2N);

--- 51 unchanged lines hidden ---