Commit 0a164a5b authored by Konstantin Shkolnyy's avatar Konstantin Shkolnyy Committed by Jiri Slaby

USB: cp210x: work around cp2108 GET_LINE_CTL bug

commit d0bf1ff0 upstream.

Add helper to access line-control register in order to work around a
cp2108 GET_LINE_CTL bug.

cp2108 GET_LINE_CTL returns the 16-bit value with the 2 bytes swapped.
However, SET_LINE_CTL functions properly. When the driver tries to modify
the register, it reads it, modifies some bits and writes back. Because the
read bytes were swapped, this often results in an invalid value to be
written. In turn, this causes cp2108 respond with a stall. The stall
sometimes doesn't clear properly and cp2108 starts responding to following
valid commands also with stalls, effectively failing.
Signed-off-by: default avatarKonstantin Shkolnyy <konstantin.shkolnyy@gmail.com>
[johan: amend commit message, modify probe error handling ]
Signed-off-by: default avatarJohan Hovold <johan@kernel.org>
Cc: Oliver Neukum <oliver@neukum.org>
Signed-off-by: default avatarJiri Slaby <jslaby@suse.cz>
parent c1b189c0
...@@ -202,6 +202,7 @@ MODULE_DEVICE_TABLE(usb, id_table); ...@@ -202,6 +202,7 @@ MODULE_DEVICE_TABLE(usb, id_table);
struct cp210x_port_private { struct cp210x_port_private {
__u8 bInterfaceNumber; __u8 bInterfaceNumber;
bool has_swapped_line_ctl;
}; };
static struct usb_serial_driver cp210x_device = { static struct usb_serial_driver cp210x_device = {
...@@ -425,6 +426,60 @@ static inline int cp210x_set_config_single(struct usb_serial_port *port, ...@@ -425,6 +426,60 @@ static inline int cp210x_set_config_single(struct usb_serial_port *port,
return cp210x_set_config(port, request, &data, 2); return cp210x_set_config(port, request, &data, 2);
} }
/*
* Detect CP2108 GET_LINE_CTL bug and activate workaround.
* Write a known good value 0x800, read it back.
* If it comes back swapped the bug is detected.
* Preserve the original register value.
*/
static int cp210x_detect_swapped_line_ctl(struct usb_serial_port *port)
{
struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
unsigned int line_ctl_save;
unsigned int line_ctl_test;
int err;
err = cp210x_get_config(port, CP210X_GET_LINE_CTL, &line_ctl_save, 2);
if (err)
return err;
line_ctl_test = 0x800;
err = cp210x_set_config(port, CP210X_SET_LINE_CTL, &line_ctl_test, 2);
if (err)
return err;
err = cp210x_get_config(port, CP210X_GET_LINE_CTL, &line_ctl_test, 2);
if (err)
return err;
if (line_ctl_test == 8) {
port_priv->has_swapped_line_ctl = true;
line_ctl_save = swab16((u16)line_ctl_save);
}
return cp210x_set_config(port, CP210X_SET_LINE_CTL, &line_ctl_save, 2);
}
/*
* Must always be called instead of cp210x_get_config(CP210X_GET_LINE_CTL)
* to workaround cp2108 bug and get correct value.
*/
static int cp210x_get_line_ctl(struct usb_serial_port *port, unsigned int *ctl)
{
struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
int err;
err = cp210x_get_config(port, CP210X_GET_LINE_CTL, ctl, 2);
if (err)
return err;
/* Workaround swapped bytes in 16-bit value from CP210X_GET_LINE_CTL */
if (port_priv->has_swapped_line_ctl)
*ctl = swab16((u16)(*ctl));
return 0;
}
/* /*
* cp210x_quantise_baudrate * cp210x_quantise_baudrate
* Quantises the baud rate as per AN205 Table 1 * Quantises the baud rate as per AN205 Table 1
...@@ -542,7 +597,7 @@ static void cp210x_get_termios_port(struct usb_serial_port *port, ...@@ -542,7 +597,7 @@ static void cp210x_get_termios_port(struct usb_serial_port *port,
cflag = *cflagp; cflag = *cflagp;
cp210x_get_config(port, CP210X_GET_LINE_CTL, &bits, 2); cp210x_get_line_ctl(port, &bits);
cflag &= ~CSIZE; cflag &= ~CSIZE;
switch (bits & BITS_DATA_MASK) { switch (bits & BITS_DATA_MASK) {
case BITS_DATA_5: case BITS_DATA_5:
...@@ -710,7 +765,7 @@ static void cp210x_set_termios(struct tty_struct *tty, ...@@ -710,7 +765,7 @@ static void cp210x_set_termios(struct tty_struct *tty,
/* If the number of data bits is to be updated */ /* If the number of data bits is to be updated */
if ((cflag & CSIZE) != (old_cflag & CSIZE)) { if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
cp210x_get_config(port, CP210X_GET_LINE_CTL, &bits, 2); cp210x_get_line_ctl(port, &bits);
bits &= ~BITS_DATA_MASK; bits &= ~BITS_DATA_MASK;
switch (cflag & CSIZE) { switch (cflag & CSIZE) {
case CS5: case CS5:
...@@ -744,7 +799,7 @@ static void cp210x_set_termios(struct tty_struct *tty, ...@@ -744,7 +799,7 @@ static void cp210x_set_termios(struct tty_struct *tty,
if ((cflag & (PARENB|PARODD|CMSPAR)) != if ((cflag & (PARENB|PARODD|CMSPAR)) !=
(old_cflag & (PARENB|PARODD|CMSPAR))) { (old_cflag & (PARENB|PARODD|CMSPAR))) {
cp210x_get_config(port, CP210X_GET_LINE_CTL, &bits, 2); cp210x_get_line_ctl(port, &bits);
bits &= ~BITS_PARITY_MASK; bits &= ~BITS_PARITY_MASK;
if (cflag & PARENB) { if (cflag & PARENB) {
if (cflag & CMSPAR) { if (cflag & CMSPAR) {
...@@ -770,7 +825,7 @@ static void cp210x_set_termios(struct tty_struct *tty, ...@@ -770,7 +825,7 @@ static void cp210x_set_termios(struct tty_struct *tty,
} }
if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) { if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
cp210x_get_config(port, CP210X_GET_LINE_CTL, &bits, 2); cp210x_get_line_ctl(port, &bits);
bits &= ~BITS_STOP_MASK; bits &= ~BITS_STOP_MASK;
if (cflag & CSTOPB) { if (cflag & CSTOPB) {
bits |= BITS_STOP_2; bits |= BITS_STOP_2;
...@@ -890,6 +945,7 @@ static int cp210x_port_probe(struct usb_serial_port *port) ...@@ -890,6 +945,7 @@ static int cp210x_port_probe(struct usb_serial_port *port)
struct usb_serial *serial = port->serial; struct usb_serial *serial = port->serial;
struct usb_host_interface *cur_altsetting; struct usb_host_interface *cur_altsetting;
struct cp210x_port_private *port_priv; struct cp210x_port_private *port_priv;
int ret;
port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL); port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
if (!port_priv) if (!port_priv)
...@@ -900,6 +956,12 @@ static int cp210x_port_probe(struct usb_serial_port *port) ...@@ -900,6 +956,12 @@ static int cp210x_port_probe(struct usb_serial_port *port)
usb_set_serial_port_data(port, port_priv); usb_set_serial_port_data(port, port_priv);
ret = cp210x_detect_swapped_line_ctl(port);
if (ret) {
kfree(port_priv);
return ret;
}
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment