From 3391ca1dcd70a8e958984f7e95f242d36f0b9ab8 Mon Sep 17 00:00:00 2001 From: Chengguang Xu Date: Mon, 25 Jun 2018 15:35:18 +0800 Subject: [PATCH 01/27] USB: serial: cast sizeof() to int when comparing with error code Negative error code will be larger than sizeof(). Note that none of these bugs prevent errors from being detected, even if the ir-usb one would cause a less precise debug message to printed. Signed-off-by: Chengguang Xu [ johan: add comment about implications ] Signed-off-by: Johan Hovold --- drivers/usb/serial/ir-usb.c | 2 +- drivers/usb/serial/quatech2.c | 2 +- drivers/usb/serial/ssu100.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/usb/serial/ir-usb.c b/drivers/usb/serial/ir-usb.c index 24b06c7e5e2d..7643716b5299 100644 --- a/drivers/usb/serial/ir-usb.c +++ b/drivers/usb/serial/ir-usb.c @@ -132,7 +132,7 @@ irda_usb_find_class_desc(struct usb_serial *serial, unsigned int ifnum) 0, ifnum, desc, sizeof(*desc), 1000); dev_dbg(&serial->dev->dev, "%s - ret=%d\n", __func__, ret); - if (ret < sizeof(*desc)) { + if (ret < (int)sizeof(*desc)) { dev_dbg(&serial->dev->dev, "%s - class descriptor read %s (%d)\n", __func__, (ret < 0) ? "failed" : "too short", ret); diff --git a/drivers/usb/serial/quatech2.c b/drivers/usb/serial/quatech2.c index 958e12e1e7c7..ff2322ea5e14 100644 --- a/drivers/usb/serial/quatech2.c +++ b/drivers/usb/serial/quatech2.c @@ -194,7 +194,7 @@ static inline int qt2_getregister(struct usb_device *dev, ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), QT_SET_GET_REGISTER, 0xc0, reg, uart, data, sizeof(*data), QT2_USB_TIMEOUT); - if (ret < sizeof(*data)) { + if (ret < (int)sizeof(*data)) { if (ret >= 0) ret = -EIO; } diff --git a/drivers/usb/serial/ssu100.c b/drivers/usb/serial/ssu100.c index 2083c267787b..0900b47b5f57 100644 --- a/drivers/usb/serial/ssu100.c +++ b/drivers/usb/serial/ssu100.c @@ -104,7 +104,7 @@ static inline int ssu100_getregister(struct usb_device *dev, ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), QT_SET_GET_REGISTER, 0xc0, reg, uart, data, sizeof(*data), 300); - if (ret < sizeof(*data)) { + if (ret < (int)sizeof(*data)) { if (ret >= 0) ret = -EIO; } From c75d18cc5a97e9d01d04ee7af457fddea38ca539 Mon Sep 17 00:00:00 2001 From: John Ogness Date: Sun, 24 Jun 2018 00:32:04 +0200 Subject: [PATCH 02/27] USB: serial: cyberjack: use irqsave() in USB's complete callback The USB completion callback does not disable interrupts while acquiring the lock. We want to remove the local_irq_disable() invocation from __usb_hcd_giveback_urb() and therefore it is required for the callback handler to disable the interrupts while acquiring the lock. The callback may be invoked either in IRQ or BH context depending on the USB host controller. Use the _irqsave() variant of the locking primitives. Signed-off-by: John Ogness Signed-off-by: Sebastian Andrzej Siewior Signed-off-by: Johan Hovold --- drivers/usb/serial/cyberjack.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/drivers/usb/serial/cyberjack.c b/drivers/usb/serial/cyberjack.c index dc67a2eb98d7..ebd76ab07b72 100644 --- a/drivers/usb/serial/cyberjack.c +++ b/drivers/usb/serial/cyberjack.c @@ -255,6 +255,7 @@ static void cyberjack_read_int_callback(struct urb *urb) struct device *dev = &port->dev; unsigned char *data = urb->transfer_buffer; int status = urb->status; + unsigned long flags; int result; /* the urb might have been killed. */ @@ -270,13 +271,13 @@ static void cyberjack_read_int_callback(struct urb *urb) /* This is a announcement of coming bulk_ins. */ unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3; - spin_lock(&priv->lock); + spin_lock_irqsave(&priv->lock, flags); old_rdtodo = priv->rdtodo; if (old_rdtodo > SHRT_MAX - size) { dev_dbg(dev, "To many bulk_in urbs to do.\n"); - spin_unlock(&priv->lock); + spin_unlock_irqrestore(&priv->lock, flags); goto resubmit; } @@ -285,7 +286,7 @@ static void cyberjack_read_int_callback(struct urb *urb) dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo); - spin_unlock(&priv->lock); + spin_unlock_irqrestore(&priv->lock, flags); if (!old_rdtodo) { result = usb_submit_urb(port->read_urb, GFP_ATOMIC); @@ -309,6 +310,7 @@ static void cyberjack_read_bulk_callback(struct urb *urb) struct cyberjack_private *priv = usb_get_serial_port_data(port); struct device *dev = &port->dev; unsigned char *data = urb->transfer_buffer; + unsigned long flags; short todo; int result; int status = urb->status; @@ -325,7 +327,7 @@ static void cyberjack_read_bulk_callback(struct urb *urb) tty_flip_buffer_push(&port->port); } - spin_lock(&priv->lock); + spin_lock_irqsave(&priv->lock, flags); /* Reduce urbs to do by one. */ priv->rdtodo -= urb->actual_length; @@ -334,7 +336,7 @@ static void cyberjack_read_bulk_callback(struct urb *urb) priv->rdtodo = 0; todo = priv->rdtodo; - spin_unlock(&priv->lock); + spin_unlock_irqrestore(&priv->lock, flags); dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo); @@ -354,6 +356,7 @@ static void cyberjack_write_bulk_callback(struct urb *urb) struct cyberjack_private *priv = usb_get_serial_port_data(port); struct device *dev = &port->dev; int status = urb->status; + unsigned long flags; set_bit(0, &port->write_urbs_free); if (status) { @@ -362,7 +365,7 @@ static void cyberjack_write_bulk_callback(struct urb *urb) return; } - spin_lock(&priv->lock); + spin_lock_irqsave(&priv->lock, flags); /* only do something if we have more data to send */ if (priv->wrfilled) { @@ -406,7 +409,7 @@ static void cyberjack_write_bulk_callback(struct urb *urb) } exit: - spin_unlock(&priv->lock); + spin_unlock_irqrestore(&priv->lock, flags); usb_serial_port_softint(port); } From 041b7db9668a60dc7f03c53fe4fe1c8e137e935b Mon Sep 17 00:00:00 2001 From: John Ogness Date: Sun, 24 Jun 2018 00:32:05 +0200 Subject: [PATCH 03/27] USB: serial: digi_acceleport: use irqsave() in USB's complete callback The USB completion callback does not disable interrupts while acquiring the lock. We want to remove the local_irq_disable() invocation from __usb_hcd_giveback_urb() and therefore it is required for the callback handler to disable the interrupts while acquiring the lock. The callback may be invoked either in IRQ or BH context depending on the USB host controller. Use the _irqsave() variant of the locking primitives. Signed-off-by: John Ogness Signed-off-by: Sebastian Andrzej Siewior Signed-off-by: Johan Hovold --- drivers/usb/serial/digi_acceleport.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/drivers/usb/serial/digi_acceleport.c b/drivers/usb/serial/digi_acceleport.c index b0526786fb02..ae512fed08af 100644 --- a/drivers/usb/serial/digi_acceleport.c +++ b/drivers/usb/serial/digi_acceleport.c @@ -984,6 +984,7 @@ static void digi_write_bulk_callback(struct urb *urb) struct usb_serial *serial; struct digi_port *priv; struct digi_serial *serial_priv; + unsigned long flags; int ret = 0; int status = urb->status; @@ -1004,15 +1005,15 @@ static void digi_write_bulk_callback(struct urb *urb) /* handle oob callback */ if (priv->dp_port_num == serial_priv->ds_oob_port_num) { dev_dbg(&port->dev, "digi_write_bulk_callback: oob callback\n"); - spin_lock(&priv->dp_port_lock); + spin_lock_irqsave(&priv->dp_port_lock, flags); priv->dp_write_urb_in_use = 0; wake_up_interruptible(&port->write_wait); - spin_unlock(&priv->dp_port_lock); + spin_unlock_irqrestore(&priv->dp_port_lock, flags); return; } /* try to send any buffered data on this port */ - spin_lock(&priv->dp_port_lock); + spin_lock_irqsave(&priv->dp_port_lock, flags); priv->dp_write_urb_in_use = 0; if (priv->dp_out_buf_len > 0) { *((unsigned char *)(port->write_urb->transfer_buffer)) @@ -1035,7 +1036,7 @@ static void digi_write_bulk_callback(struct urb *urb) /* lost the race in write_chan(). */ schedule_work(&priv->dp_wakeup_work); - spin_unlock(&priv->dp_port_lock); + spin_unlock_irqrestore(&priv->dp_port_lock, flags); if (ret && ret != -EPERM) dev_err_console(port, "%s: usb_submit_urb failed, ret=%d, port=%d\n", @@ -1381,6 +1382,7 @@ static int digi_read_inb_callback(struct urb *urb) struct usb_serial_port *port = urb->context; struct digi_port *priv = usb_get_serial_port_data(port); unsigned char *buf = urb->transfer_buffer; + unsigned long flags; int opcode; int len; int port_status; @@ -1407,7 +1409,7 @@ static int digi_read_inb_callback(struct urb *urb) return -1; } - spin_lock(&priv->dp_port_lock); + spin_lock_irqsave(&priv->dp_port_lock, flags); /* check for throttle; if set, do not resubmit read urb */ /* indicate the read chain needs to be restarted on unthrottle */ @@ -1444,7 +1446,7 @@ static int digi_read_inb_callback(struct urb *urb) tty_flip_buffer_push(&port->port); } } - spin_unlock(&priv->dp_port_lock); + spin_unlock_irqrestore(&priv->dp_port_lock, flags); if (opcode == DIGI_CMD_RECEIVE_DISABLE) dev_dbg(&port->dev, "%s: got RECEIVE_DISABLE\n", __func__); @@ -1474,6 +1476,7 @@ static int digi_read_oob_callback(struct urb *urb) struct digi_port *priv = usb_get_serial_port_data(port); unsigned char *buf = urb->transfer_buffer; int opcode, line, status, val; + unsigned long flags; int i; unsigned int rts; @@ -1506,7 +1509,7 @@ static int digi_read_oob_callback(struct urb *urb) rts = C_CRTSCTS(tty); if (tty && opcode == DIGI_CMD_READ_INPUT_SIGNALS) { - spin_lock(&priv->dp_port_lock); + spin_lock_irqsave(&priv->dp_port_lock, flags); /* convert from digi flags to termiox flags */ if (val & DIGI_READ_INPUT_SIGNALS_CTS) { priv->dp_modem_signals |= TIOCM_CTS; @@ -1530,12 +1533,12 @@ static int digi_read_oob_callback(struct urb *urb) else priv->dp_modem_signals &= ~TIOCM_CD; - spin_unlock(&priv->dp_port_lock); + spin_unlock_irqrestore(&priv->dp_port_lock, flags); } else if (opcode == DIGI_CMD_TRANSMIT_IDLE) { - spin_lock(&priv->dp_port_lock); + spin_lock_irqsave(&priv->dp_port_lock, flags); priv->dp_transmit_idle = 1; wake_up_interruptible(&priv->dp_transmit_idle_wait); - spin_unlock(&priv->dp_port_lock); + spin_unlock_irqrestore(&priv->dp_port_lock, flags); } else if (opcode == DIGI_CMD_IFLUSH_FIFO) { wake_up_interruptible(&priv->dp_flush_wait); } From dd1fae527612543e560e84f2eba4f6ef2006ac55 Mon Sep 17 00:00:00 2001 From: John Ogness Date: Sun, 24 Jun 2018 00:32:06 +0200 Subject: [PATCH 04/27] USB: serial: io_edgeport: use irqsave() in USB's complete callback The USB completion callback does not disable interrupts while acquiring the lock. We want to remove the local_irq_disable() invocation from __usb_hcd_giveback_urb() and therefore it is required for the callback handler to disable the interrupts while acquiring the lock. The callback may be invoked either in IRQ or BH context depending on the USB host controller. Use the _irqsave() variant of the locking primitives. Signed-off-by: John Ogness Signed-off-by: Sebastian Andrzej Siewior Signed-off-by: Johan Hovold --- drivers/usb/serial/io_edgeport.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/drivers/usb/serial/io_edgeport.c b/drivers/usb/serial/io_edgeport.c index 17283f4b4779..97c69d373ca6 100644 --- a/drivers/usb/serial/io_edgeport.c +++ b/drivers/usb/serial/io_edgeport.c @@ -648,6 +648,7 @@ static void edge_interrupt_callback(struct urb *urb) struct usb_serial_port *port; unsigned char *data = urb->transfer_buffer; int length = urb->actual_length; + unsigned long flags; int bytes_avail; int position; int txCredits; @@ -679,7 +680,7 @@ static void edge_interrupt_callback(struct urb *urb) if (length > 1) { bytes_avail = data[0] | (data[1] << 8); if (bytes_avail) { - spin_lock(&edge_serial->es_lock); + spin_lock_irqsave(&edge_serial->es_lock, flags); edge_serial->rxBytesAvail += bytes_avail; dev_dbg(dev, "%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n", @@ -702,7 +703,8 @@ static void edge_interrupt_callback(struct urb *urb) edge_serial->read_in_progress = false; } } - spin_unlock(&edge_serial->es_lock); + spin_unlock_irqrestore(&edge_serial->es_lock, + flags); } } /* grab the txcredits for the ports if available */ @@ -715,9 +717,11 @@ static void edge_interrupt_callback(struct urb *urb) port = edge_serial->serial->port[portNumber]; edge_port = usb_get_serial_port_data(port); if (edge_port->open) { - spin_lock(&edge_port->ep_lock); + spin_lock_irqsave(&edge_port->ep_lock, + flags); edge_port->txCredits += txCredits; - spin_unlock(&edge_port->ep_lock); + spin_unlock_irqrestore(&edge_port->ep_lock, + flags); dev_dbg(dev, "%s - txcredits for port%d = %d\n", __func__, portNumber, edge_port->txCredits); @@ -758,6 +762,7 @@ static void edge_bulk_in_callback(struct urb *urb) int retval; __u16 raw_data_length; int status = urb->status; + unsigned long flags; if (status) { dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n", @@ -777,7 +782,7 @@ static void edge_bulk_in_callback(struct urb *urb) usb_serial_debug_data(dev, __func__, raw_data_length, data); - spin_lock(&edge_serial->es_lock); + spin_lock_irqsave(&edge_serial->es_lock, flags); /* decrement our rxBytes available by the number that we just got */ edge_serial->rxBytesAvail -= raw_data_length; @@ -801,7 +806,7 @@ static void edge_bulk_in_callback(struct urb *urb) edge_serial->read_in_progress = false; } - spin_unlock(&edge_serial->es_lock); + spin_unlock_irqrestore(&edge_serial->es_lock, flags); } From 6778b0cbdbb44afdaa97dd16395be31e239777cf Mon Sep 17 00:00:00 2001 From: John Ogness Date: Sun, 24 Jun 2018 00:32:07 +0200 Subject: [PATCH 05/27] USB: serial: io_ti: use irqsave() in USB's complete callback The USB completion callback does not disable interrupts while acquiring the lock. We want to remove the local_irq_disable() invocation from __usb_hcd_giveback_urb() and therefore it is required for the callback handler to disable the interrupts while acquiring the lock. The callback may be invoked either in IRQ or BH context depending on the USB host controller. Use the _irqsave() variant of the locking primitives. Signed-off-by: John Ogness Signed-off-by: Sebastian Andrzej Siewior Signed-off-by: Johan Hovold --- drivers/usb/serial/io_ti.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/usb/serial/io_ti.c b/drivers/usb/serial/io_ti.c index 0fbadb37c104..6d1d6efa3055 100644 --- a/drivers/usb/serial/io_ti.c +++ b/drivers/usb/serial/io_ti.c @@ -1729,6 +1729,7 @@ static void edge_bulk_in_callback(struct urb *urb) struct edgeport_port *edge_port = urb->context; struct device *dev = &edge_port->port->dev; unsigned char *data = urb->transfer_buffer; + unsigned long flags; int retval = 0; int port_number; int status = urb->status; @@ -1780,13 +1781,13 @@ static void edge_bulk_in_callback(struct urb *urb) exit: /* continue read unless stopped */ - spin_lock(&edge_port->ep_lock); + spin_lock_irqsave(&edge_port->ep_lock, flags); if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) retval = usb_submit_urb(urb, GFP_ATOMIC); else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING) edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED; - spin_unlock(&edge_port->ep_lock); + spin_unlock_irqrestore(&edge_port->ep_lock, flags); if (retval) dev_err(dev, "%s - usb_submit_urb failed with result %d\n", __func__, retval); } From f7c8a9ccc9afe3781dfd6e1977775457102c5c83 Mon Sep 17 00:00:00 2001 From: John Ogness Date: Sun, 24 Jun 2018 00:32:08 +0200 Subject: [PATCH 06/27] USB: serial: mos7720: use irqsave() in USB's complete callback The USB completion callback does not disable interrupts while acquiring the lock. We want to remove the local_irq_disable() invocation from __usb_hcd_giveback_urb() and therefore it is required for the callback handler to disable the interrupts while acquiring the lock. The callback may be invoked either in IRQ or BH context depending on the USB host controller. Use the _irqsave() variant of the locking primitives. Signed-off-by: John Ogness Signed-off-by: Sebastian Andrzej Siewior Signed-off-by: Johan Hovold --- drivers/usb/serial/mos7720.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/usb/serial/mos7720.c b/drivers/usb/serial/mos7720.c index bd57630e67e2..8f11e759ad61 100644 --- a/drivers/usb/serial/mos7720.c +++ b/drivers/usb/serial/mos7720.c @@ -340,14 +340,15 @@ static void async_complete(struct urb *urb) { struct urbtracker *urbtrack = urb->context; int status = urb->status; + unsigned long flags; if (unlikely(status)) dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status); /* remove the urbtracker from the active_urbs list */ - spin_lock(&urbtrack->mos_parport->listlock); + spin_lock_irqsave(&urbtrack->mos_parport->listlock, flags); list_del(&urbtrack->urblist_entry); - spin_unlock(&urbtrack->mos_parport->listlock); + spin_unlock_irqrestore(&urbtrack->mos_parport->listlock, flags); kref_put(&urbtrack->ref_count, destroy_urbtracker); } From 19bfbf462e8921d8e520bcc0a23ebc4988223631 Mon Sep 17 00:00:00 2001 From: John Ogness Date: Sun, 24 Jun 2018 00:32:09 +0200 Subject: [PATCH 07/27] USB: serial: mos7840: use irqsave() in USB's complete callback The USB completion callback does not disable interrupts while acquiring the lock. We want to remove the local_irq_disable() invocation from __usb_hcd_giveback_urb() and therefore it is required for the callback handler to disable the interrupts while acquiring the lock. The callback may be invoked either in IRQ or BH context depending on the USB host controller. Use the _irqsave() variant of the locking primitives. Signed-off-by: John Ogness Signed-off-by: Sebastian Andrzej Siewior Signed-off-by: Johan Hovold --- drivers/usb/serial/mos7840.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/usb/serial/mos7840.c b/drivers/usb/serial/mos7840.c index fdceb46d9fc6..4efffbbef5ae 100644 --- a/drivers/usb/serial/mos7840.c +++ b/drivers/usb/serial/mos7840.c @@ -802,18 +802,19 @@ static void mos7840_bulk_out_data_callback(struct urb *urb) struct moschip_port *mos7840_port; struct usb_serial_port *port; int status = urb->status; + unsigned long flags; int i; mos7840_port = urb->context; port = mos7840_port->port; - spin_lock(&mos7840_port->pool_lock); + spin_lock_irqsave(&mos7840_port->pool_lock, flags); for (i = 0; i < NUM_URBS; i++) { if (urb == mos7840_port->write_urb_pool[i]) { mos7840_port->busy[i] = 0; break; } } - spin_unlock(&mos7840_port->pool_lock); + spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); if (status) { dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status); From 2ba02c8dd063c4396111629e96dec5a3f231f995 Mon Sep 17 00:00:00 2001 From: John Ogness Date: Sun, 24 Jun 2018 00:32:10 +0200 Subject: [PATCH 08/27] USB: serial: quatech2: use irqsave() in USB's complete callback The USB completion callback does not disable interrupts while acquiring the lock. We want to remove the local_irq_disable() invocation from __usb_hcd_giveback_urb() and therefore it is required for the callback handler to disable the interrupts while acquiring the lock. The callback may be invoked either in IRQ or BH context depending on the USB host controller. Use the _irqsave() variant of the locking primitives. Signed-off-by: John Ogness Signed-off-by: Sebastian Andrzej Siewior Signed-off-by: Johan Hovold --- drivers/usb/serial/quatech2.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/usb/serial/quatech2.c b/drivers/usb/serial/quatech2.c index ff2322ea5e14..b61c2a9b6b11 100644 --- a/drivers/usb/serial/quatech2.c +++ b/drivers/usb/serial/quatech2.c @@ -621,16 +621,17 @@ static void qt2_write_bulk_callback(struct urb *urb) { struct usb_serial_port *port; struct qt2_port_private *port_priv; + unsigned long flags; port = urb->context; port_priv = usb_get_serial_port_data(port); - spin_lock(&port_priv->urb_lock); + spin_lock_irqsave(&port_priv->urb_lock, flags); port_priv->urb_in_use = false; usb_serial_port_softint(port); - spin_unlock(&port_priv->urb_lock); + spin_unlock_irqrestore(&port_priv->urb_lock, flags); } From e60870012e5a35b1506d7b376fddfb30e9da0b27 Mon Sep 17 00:00:00 2001 From: John Ogness Date: Sun, 24 Jun 2018 00:32:11 +0200 Subject: [PATCH 09/27] USB: serial: sierra: fix potential deadlock at close The portdata spinlock can be taken in interrupt context (via sierra_outdat_callback()). Disable interrupts when taking the portdata spinlock when discarding deferred URBs during close to prevent a possible deadlock. Fixes: 014333f77c0b ("USB: sierra: fix urb and memory leak on disconnect") Cc: stable Signed-off-by: John Ogness Signed-off-by: Sebastian Andrzej Siewior [ johan: amend commit message and add fixes and stable tags ] Signed-off-by: Johan Hovold --- drivers/usb/serial/sierra.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/usb/serial/sierra.c b/drivers/usb/serial/sierra.c index d189f953c891..55956a638f5b 100644 --- a/drivers/usb/serial/sierra.c +++ b/drivers/usb/serial/sierra.c @@ -770,9 +770,9 @@ static void sierra_close(struct usb_serial_port *port) kfree(urb->transfer_buffer); usb_free_urb(urb); usb_autopm_put_interface_async(serial->interface); - spin_lock(&portdata->lock); + spin_lock_irq(&portdata->lock); portdata->outstanding_urbs--; - spin_unlock(&portdata->lock); + spin_unlock_irq(&portdata->lock); } sierra_stop_rx_urbs(port); From d4bf25b3fc250a5d950691e824bbfa7a732a6608 Mon Sep 17 00:00:00 2001 From: John Ogness Date: Sun, 24 Jun 2018 00:32:12 +0200 Subject: [PATCH 10/27] USB: serial: sierra: use irqsave() in USB's complete callback The USB completion callback does not disable interrupts while acquiring the lock. We want to remove the local_irq_disable() invocation from __usb_hcd_giveback_urb() and therefore it is required for the callback handler to disable the interrupts while acquiring the lock. The callback may be invoked either in IRQ or BH context depending on the USB host controller. Use the _irqsave() variant of the locking primitives. Signed-off-by: John Ogness Signed-off-by: Sebastian Andrzej Siewior Signed-off-by: Johan Hovold --- drivers/usb/serial/sierra.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/drivers/usb/serial/sierra.c b/drivers/usb/serial/sierra.c index 55956a638f5b..a43263a0edd8 100644 --- a/drivers/usb/serial/sierra.c +++ b/drivers/usb/serial/sierra.c @@ -409,6 +409,7 @@ static void sierra_outdat_callback(struct urb *urb) struct sierra_port_private *portdata = usb_get_serial_port_data(port); struct sierra_intf_private *intfdata; int status = urb->status; + unsigned long flags; intfdata = usb_get_serial_data(port->serial); @@ -419,12 +420,12 @@ static void sierra_outdat_callback(struct urb *urb) dev_dbg(&port->dev, "%s - nonzero write bulk status " "received: %d\n", __func__, status); - spin_lock(&portdata->lock); + spin_lock_irqsave(&portdata->lock, flags); --portdata->outstanding_urbs; - spin_unlock(&portdata->lock); - spin_lock(&intfdata->susp_lock); + spin_unlock_irqrestore(&portdata->lock, flags); + spin_lock_irqsave(&intfdata->susp_lock, flags); --intfdata->in_flight; - spin_unlock(&intfdata->susp_lock); + spin_unlock_irqrestore(&intfdata->susp_lock, flags); usb_serial_port_softint(port); } From 5e02bfcf3f4f8c76c47718063d408c8a09d61bca Mon Sep 17 00:00:00 2001 From: John Ogness Date: Sun, 24 Jun 2018 00:32:13 +0200 Subject: [PATCH 11/27] USB: serial: symbolserial: use irqsave() in USB's complete callback The USB completion callback does not disable interrupts while acquiring the lock. We want to remove the local_irq_disable() invocation from __usb_hcd_giveback_urb() and therefore it is required for the callback handler to disable the interrupts while acquiring the lock. The callback may be invoked either in IRQ or BH context depending on the USB host controller. Use the _irqsave() variant of the locking primitives. Signed-off-by: John Ogness Signed-off-by: Sebastian Andrzej Siewior Signed-off-by: Johan Hovold --- drivers/usb/serial/symbolserial.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/usb/serial/symbolserial.c b/drivers/usb/serial/symbolserial.c index cd2f8dc8b58c..6ca24e86f686 100644 --- a/drivers/usb/serial/symbolserial.c +++ b/drivers/usb/serial/symbolserial.c @@ -35,6 +35,7 @@ static void symbol_int_callback(struct urb *urb) struct symbol_private *priv = usb_get_serial_port_data(port); unsigned char *data = urb->transfer_buffer; int status = urb->status; + unsigned long flags; int result; int data_length; @@ -73,7 +74,7 @@ static void symbol_int_callback(struct urb *urb) } exit: - spin_lock(&priv->lock); + spin_lock_irqsave(&priv->lock, flags); /* Continue trying to always read if we should */ if (!priv->throttled) { @@ -84,7 +85,7 @@ exit: __func__, result); } else priv->actually_throttled = true; - spin_unlock(&priv->lock); + spin_unlock_irqrestore(&priv->lock, flags); } static int symbol_open(struct tty_struct *tty, struct usb_serial_port *port) From cf83be24b2efc423f6869a155372e3694c12a558 Mon Sep 17 00:00:00 2001 From: John Ogness Date: Sun, 24 Jun 2018 00:32:14 +0200 Subject: [PATCH 12/27] USB: serial: ti_usb_3410_5052: use irqsave() in USB's complete callback The USB completion callback does not disable interrupts while acquiring the lock. We want to remove the local_irq_disable() invocation from __usb_hcd_giveback_urb() and therefore it is required for the callback handler to disable the interrupts while acquiring the lock. The callback may be invoked either in IRQ or BH context depending on the USB host controller. Use the _irqsave() variant of the locking primitives. Signed-off-by: John Ogness Signed-off-by: Sebastian Andrzej Siewior Signed-off-by: Johan Hovold --- drivers/usb/serial/ti_usb_3410_5052.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/drivers/usb/serial/ti_usb_3410_5052.c b/drivers/usb/serial/ti_usb_3410_5052.c index 6b22857f6e52..3010878f7f8e 100644 --- a/drivers/usb/serial/ti_usb_3410_5052.c +++ b/drivers/usb/serial/ti_usb_3410_5052.c @@ -1215,6 +1215,7 @@ static void ti_bulk_in_callback(struct urb *urb) struct usb_serial_port *port = tport->tp_port; struct device *dev = &urb->dev->dev; int status = urb->status; + unsigned long flags; int retval = 0; switch (status) { @@ -1247,20 +1248,20 @@ static void ti_bulk_in_callback(struct urb *urb) __func__); else ti_recv(port, urb->transfer_buffer, urb->actual_length); - spin_lock(&tport->tp_lock); + spin_lock_irqsave(&tport->tp_lock, flags); port->icount.rx += urb->actual_length; - spin_unlock(&tport->tp_lock); + spin_unlock_irqrestore(&tport->tp_lock, flags); } exit: /* continue to read unless stopping */ - spin_lock(&tport->tp_lock); + spin_lock_irqsave(&tport->tp_lock, flags); if (tport->tp_read_urb_state == TI_READ_URB_RUNNING) retval = usb_submit_urb(urb, GFP_ATOMIC); else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING) tport->tp_read_urb_state = TI_READ_URB_STOPPED; - spin_unlock(&tport->tp_lock); + spin_unlock_irqrestore(&tport->tp_lock, flags); if (retval) dev_err(dev, "%s - resubmit read urb failed, %d\n", __func__, retval); From a323f94611aac0f68b710048ae53c73a53b5c7b5 Mon Sep 17 00:00:00 2001 From: John Ogness Date: Sun, 24 Jun 2018 00:32:15 +0200 Subject: [PATCH 13/27] USB: serial: usb_wwan: use irqsave() in USB's complete callback The USB completion callback does not disable interrupts while acquiring the lock. We want to remove the local_irq_disable() invocation from __usb_hcd_giveback_urb() and therefore it is required for the callback handler to disable the interrupts while acquiring the lock. The callback may be invoked either in IRQ or BH context depending on the USB host controller. Use the _irqsave() variant of the locking primitives. Signed-off-by: John Ogness Signed-off-by: Sebastian Andrzej Siewior Signed-off-by: Johan Hovold --- drivers/usb/serial/usb_wwan.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/usb/serial/usb_wwan.c b/drivers/usb/serial/usb_wwan.c index 107e64c42e94..912472f26e4f 100644 --- a/drivers/usb/serial/usb_wwan.c +++ b/drivers/usb/serial/usb_wwan.c @@ -326,6 +326,7 @@ static void usb_wwan_outdat_callback(struct urb *urb) struct usb_serial_port *port; struct usb_wwan_port_private *portdata; struct usb_wwan_intf_private *intfdata; + unsigned long flags; int i; port = urb->context; @@ -334,9 +335,9 @@ static void usb_wwan_outdat_callback(struct urb *urb) usb_serial_port_softint(port); usb_autopm_put_interface_async(port->serial->interface); portdata = usb_get_serial_port_data(port); - spin_lock(&intfdata->susp_lock); + spin_lock_irqsave(&intfdata->susp_lock, flags); intfdata->in_flight--; - spin_unlock(&intfdata->susp_lock); + spin_unlock_irqrestore(&intfdata->susp_lock, flags); for (i = 0; i < N_OUT_URB; ++i) { if (portdata->out_urbs[i] == urb) { From 1d1de580a3e0b1cfacd63b4093573d8f327b0a1a Mon Sep 17 00:00:00 2001 From: Johan Hovold Date: Tue, 26 Jun 2018 15:43:13 +0200 Subject: [PATCH 14/27] USB: serial: digi_acceleport: rename tty flag variable Add a "tty_" prefix to the tty "flag" variable to avoid any future mixups with the recently added irq-mask "flags" one. Reviewed-by: Greg Kroah-Hartman Signed-off-by: Johan Hovold --- drivers/usb/serial/digi_acceleport.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/usb/serial/digi_acceleport.c b/drivers/usb/serial/digi_acceleport.c index ae512fed08af..e7f244cf2c07 100644 --- a/drivers/usb/serial/digi_acceleport.c +++ b/drivers/usb/serial/digi_acceleport.c @@ -1387,7 +1387,7 @@ static int digi_read_inb_callback(struct urb *urb) int len; int port_status; unsigned char *data; - int flag, throttled; + int tty_flag, throttled; /* short/multiple packet check */ if (urb->actual_length < 2) { @@ -1423,7 +1423,7 @@ static int digi_read_inb_callback(struct urb *urb) data = &buf[3]; /* get flag from port_status */ - flag = 0; + tty_flag = 0; /* overrun is special, not associated with a char */ if (port_status & DIGI_OVERRUN_ERROR) @@ -1432,17 +1432,17 @@ static int digi_read_inb_callback(struct urb *urb) /* break takes precedence over parity, */ /* which takes precedence over framing errors */ if (port_status & DIGI_BREAK_ERROR) - flag = TTY_BREAK; + tty_flag = TTY_BREAK; else if (port_status & DIGI_PARITY_ERROR) - flag = TTY_PARITY; + tty_flag = TTY_PARITY; else if (port_status & DIGI_FRAMING_ERROR) - flag = TTY_FRAME; + tty_flag = TTY_FRAME; /* data length is len-1 (one byte of len is port_status) */ --len; if (len > 0) { tty_insert_flip_string_fixed_flag(&port->port, data, - flag, len); + tty_flag, len); tty_flip_buffer_push(&port->port); } } From a420b5d939ee58f1d950f0ea782834056520aeaa Mon Sep 17 00:00:00 2001 From: Johan Hovold Date: Wed, 4 Jul 2018 17:02:18 +0200 Subject: [PATCH 15/27] USB: serial: kobil_sct: fix modem-status error handling Make sure to return -EIO in case of a short modem-status read request. While at it, split the debug message to not include the (zeroed) transfer-buffer content in case of errors. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Johan Hovold --- drivers/usb/serial/kobil_sct.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/drivers/usb/serial/kobil_sct.c b/drivers/usb/serial/kobil_sct.c index a31ea7e194dd..a6ebed1e0f20 100644 --- a/drivers/usb/serial/kobil_sct.c +++ b/drivers/usb/serial/kobil_sct.c @@ -393,12 +393,20 @@ static int kobil_tiocmget(struct tty_struct *tty) transfer_buffer_length, KOBIL_TIMEOUT); - dev_dbg(&port->dev, "%s - Send get_status_line_state URB returns: %i. Statusline: %02x\n", - __func__, result, transfer_buffer[0]); + dev_dbg(&port->dev, "Send get_status_line_state URB returns: %i\n", + result); + if (result < 1) { + if (result >= 0) + result = -EIO; + goto out_free; + } + + dev_dbg(&port->dev, "Statusline: %02x\n", transfer_buffer[0]); result = 0; if ((transfer_buffer[0] & SUSBCR_GSL_DSR) != 0) result = TIOCM_DSR; +out_free: kfree(transfer_buffer); return result; } From af846a6f6de27eabcefa81ea98b0c3bd21aa25c7 Mon Sep 17 00:00:00 2001 From: Johan Hovold Date: Wed, 4 Jul 2018 17:02:19 +0200 Subject: [PATCH 16/27] USB: serial: kobil_sct: add missing version error handling Add missing version-request error handling and suppress printing of the (zeroed) transfer-buffer content in case of errors. Signed-off-by: Johan Hovold --- drivers/usb/serial/kobil_sct.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/usb/serial/kobil_sct.c b/drivers/usb/serial/kobil_sct.c index a6ebed1e0f20..e9882ba20933 100644 --- a/drivers/usb/serial/kobil_sct.c +++ b/drivers/usb/serial/kobil_sct.c @@ -190,8 +190,10 @@ static int kobil_open(struct tty_struct *tty, struct usb_serial_port *port) KOBIL_TIMEOUT ); dev_dbg(dev, "%s - Send get_HW_version URB returns: %i\n", __func__, result); - dev_dbg(dev, "Hardware version: %i.%i.%i\n", transfer_buffer[0], - transfer_buffer[1], transfer_buffer[2]); + if (result >= 3) { + dev_dbg(dev, "Hardware version: %i.%i.%i\n", transfer_buffer[0], + transfer_buffer[1], transfer_buffer[2]); + } /* get firmware version */ result = usb_control_msg(port->serial->dev, @@ -205,8 +207,10 @@ static int kobil_open(struct tty_struct *tty, struct usb_serial_port *port) KOBIL_TIMEOUT ); dev_dbg(dev, "%s - Send get_FW_version URB returns: %i\n", __func__, result); - dev_dbg(dev, "Firmware version: %i.%i.%i\n", transfer_buffer[0], - transfer_buffer[1], transfer_buffer[2]); + if (result >= 3) { + dev_dbg(dev, "Firmware version: %i.%i.%i\n", transfer_buffer[0], + transfer_buffer[1], transfer_buffer[2]); + } if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID || priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) { From d118851a4d1f2e67ef9442cd3caa35e555bb370e Mon Sep 17 00:00:00 2001 From: Johan Hovold Date: Thu, 5 Jul 2018 12:38:32 +0200 Subject: [PATCH 17/27] USB: serial: kl5kusb105: remove KLSI device id This driver was apparently never tested with an actual KLSI device. In fact, even the device-id entry which was supposed to allow for this had a typo in it. Tests now reveal that the predicted firmware differences with the PalmConnect adapters are real and that the driver does not support KLSI devices with PID 0x000c, so let's remove the broken entry. Reported-by: Chris Jakob Signed-off-by: Johan Hovold --- drivers/usb/serial/kl5kusb105.c | 1 - drivers/usb/serial/kl5kusb105.h | 3 --- 2 files changed, 4 deletions(-) diff --git a/drivers/usb/serial/kl5kusb105.c b/drivers/usb/serial/kl5kusb105.c index 5046ffd53cde..5ee48b0650c4 100644 --- a/drivers/usb/serial/kl5kusb105.c +++ b/drivers/usb/serial/kl5kusb105.c @@ -67,7 +67,6 @@ static int klsi_105_prepare_write_buffer(struct usb_serial_port *port, */ static const struct usb_device_id id_table[] = { { USB_DEVICE(PALMCONNECT_VID, PALMCONNECT_PID) }, - { USB_DEVICE(KLSI_VID, KLSI_KL5KUSB105D_PID) }, { } /* Terminating entry */ }; diff --git a/drivers/usb/serial/kl5kusb105.h b/drivers/usb/serial/kl5kusb105.h index 41c9bf60fbf0..dbe98d85ca8e 100644 --- a/drivers/usb/serial/kl5kusb105.h +++ b/drivers/usb/serial/kl5kusb105.h @@ -7,9 +7,6 @@ #define PALMCONNECT_VID 0x0830 #define PALMCONNECT_PID 0x0080 -#define KLSI_VID 0x05e9 -#define KLSI_KL5KUSB105D_PID 0x00c0 - /* Vendor commands: */ From 3738c506657f34adbfc860df729b9e38b1567ed4 Mon Sep 17 00:00:00 2001 From: Johan Hovold Date: Thu, 5 Jul 2018 12:38:33 +0200 Subject: [PATCH 18/27] USB: serial: clean up kl5kusb105 documentation Remove references to long-gone kl5kusb105 module parameters in the usb-serial documentation. Signed-off-by: Johan Hovold --- Documentation/usb/usb-serial.txt | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Documentation/usb/usb-serial.txt b/Documentation/usb/usb-serial.txt index 349f3104fa4f..ab100d6ee436 100644 --- a/Documentation/usb/usb-serial.txt +++ b/Documentation/usb/usb-serial.txt @@ -418,15 +418,6 @@ Current status: why it is wise to cut down on the rate used is wise for large transfers until this is settled. -Options supported: - If this driver is compiled as a module you can pass the following - options to it: - debug - extra verbose debugging info - (default: 0; nonzero enables) - use_lowlatency - use low_latency flag to speed up tty layer - when reading from the device. - (default: 0; nonzero enables) - See http://www.uuhaus.de/linux/palmconnect.html for up-to-date information on this driver. From 1e2ae1d7e43657f6d1a08a7e52ef85ed735cc538 Mon Sep 17 00:00:00 2001 From: Colin Ian King Date: Fri, 13 Jul 2018 11:08:31 +0100 Subject: [PATCH 19/27] USB: serial: mos7720: remove redundant variables iflag, mask and serial Variables iflag, mask and serial are being assigned but are never used hence are redundant and can be removed. Cleans up clang warnings: warning: variable 'iflag' set but not used [-Wunused-but-set-variable] warning: variable 'mask' set but not used [-Wunused-but-set-variable] warning: variable 'serial' set but not used [-Wunused-but-set-variable] Signed-off-by: Colin Ian King Signed-off-by: Johan Hovold --- drivers/usb/serial/mos7720.c | 9 --------- 1 file changed, 9 deletions(-) diff --git a/drivers/usb/serial/mos7720.c b/drivers/usb/serial/mos7720.c index 8f11e759ad61..27109522fd8b 100644 --- a/drivers/usb/serial/mos7720.c +++ b/drivers/usb/serial/mos7720.c @@ -1527,8 +1527,6 @@ static void change_port_settings(struct tty_struct *tty, struct usb_serial *serial; int baud; unsigned cflag; - unsigned iflag; - __u8 mask = 0xff; __u8 lData; __u8 lParity; __u8 lStop; @@ -1552,23 +1550,19 @@ static void change_port_settings(struct tty_struct *tty, lParity = 0x00; /* No parity */ cflag = tty->termios.c_cflag; - iflag = tty->termios.c_iflag; /* Change the number of bits */ switch (cflag & CSIZE) { case CS5: lData = UART_LCR_WLEN5; - mask = 0x1f; break; case CS6: lData = UART_LCR_WLEN6; - mask = 0x3f; break; case CS7: lData = UART_LCR_WLEN7; - mask = 0x7f; break; default: case CS8: @@ -1686,11 +1680,8 @@ static void mos7720_set_termios(struct tty_struct *tty, struct usb_serial_port *port, struct ktermios *old_termios) { int status; - struct usb_serial *serial; struct moschip_port *mos7720_port; - serial = port->serial; - mos7720_port = usb_get_serial_port_data(port); if (mos7720_port == NULL) From b8f6515445c170016014154f2e84cc863b4cc7a4 Mon Sep 17 00:00:00 2001 From: Johan Hovold Date: Mon, 16 Jul 2018 13:51:55 +0200 Subject: [PATCH 20/27] USB: serial: iuu_phoenix: drop unused driver-data baud rate Drop unused driver-data baud rate. Reviewed-by: Greg Kroah-Hartman Signed-off-by: Johan Hovold --- drivers/usb/serial/iuu_phoenix.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/drivers/usb/serial/iuu_phoenix.c b/drivers/usb/serial/iuu_phoenix.c index 2fb71303ec3a..87c8dd064205 100644 --- a/drivers/usb/serial/iuu_phoenix.c +++ b/drivers/usb/serial/iuu_phoenix.c @@ -58,7 +58,6 @@ struct iuu_private { u8 *buf; /* used for initialize speed */ u8 len; int vcc; /* vcc (either 3 or 5 V) */ - u32 baud; u32 boost; u32 clk; }; @@ -991,7 +990,6 @@ static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port) if (boost < 100) boost = 100; priv->boost = boost; - priv->baud = baud; switch (clockmode) { case 2: /* 3.680 Mhz */ priv->clk = IUU_CLK_3680000; From 3528651e89aa044347ca06db6e9a106aa9aff914 Mon Sep 17 00:00:00 2001 From: Johan Hovold Date: Mon, 16 Jul 2018 13:51:56 +0200 Subject: [PATCH 21/27] USB: serial: iuu_phoenix: drop redundant input-speed re-encoding Drop redundant input-speed re-encoding at every open(). The output and input speeds are initialised to the same value and are kept in sync on termios updates. Reviewed-by: Greg Kroah-Hartman Signed-off-by: Johan Hovold --- drivers/usb/serial/iuu_phoenix.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/drivers/usb/serial/iuu_phoenix.c b/drivers/usb/serial/iuu_phoenix.c index 87c8dd064205..449e89db9cea 100644 --- a/drivers/usb/serial/iuu_phoenix.c +++ b/drivers/usb/serial/iuu_phoenix.c @@ -962,9 +962,6 @@ static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port) struct iuu_private *priv = usb_get_serial_port_data(port); baud = tty->termios.c_ospeed; - tty->termios.c_ispeed = baud; - /* Re-encode speed */ - tty_encode_baud_rate(tty, baud, baud); dev_dbg(dev, "%s - baud %d\n", __func__, baud); usb_clear_halt(serial->dev, port->write_urb->pipe); From 95fd4f47c857cf887ec0f6718ffb6a6ec3b62bd6 Mon Sep 17 00:00:00 2001 From: Johan Hovold Date: Wed, 18 Jul 2018 14:24:57 +0200 Subject: [PATCH 22/27] USB: serial: cp210x: make line-speed quantisation data driven Older cp210x devices only support a fixed set of line speeds to which a requested speed is mapped. Reimplement this mapping using a table instead of a long if-else construct. Reviewed-by: Greg Kroah-Hartman Signed-off-by: Johan Hovold --- drivers/usb/serial/cp210x.c | 99 +++++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 43 deletions(-) diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c index eb6c26cbe579..1b380309f653 100644 --- a/drivers/usb/serial/cp210x.c +++ b/drivers/usb/serial/cp210x.c @@ -752,48 +752,6 @@ static int cp210x_get_line_ctl(struct usb_serial_port *port, u16 *ctl) return 0; } -/* - * cp210x_quantise_baudrate - * Quantises the baud rate as per AN205 Table 1 - */ -static unsigned int cp210x_quantise_baudrate(unsigned int baud) -{ - if (baud <= 300) - baud = 300; - else if (baud <= 600) baud = 600; - else if (baud <= 1200) baud = 1200; - else if (baud <= 1800) baud = 1800; - else if (baud <= 2400) baud = 2400; - else if (baud <= 4000) baud = 4000; - else if (baud <= 4803) baud = 4800; - else if (baud <= 7207) baud = 7200; - else if (baud <= 9612) baud = 9600; - else if (baud <= 14428) baud = 14400; - else if (baud <= 16062) baud = 16000; - else if (baud <= 19250) baud = 19200; - else if (baud <= 28912) baud = 28800; - else if (baud <= 38601) baud = 38400; - else if (baud <= 51558) baud = 51200; - else if (baud <= 56280) baud = 56000; - else if (baud <= 58053) baud = 57600; - else if (baud <= 64111) baud = 64000; - else if (baud <= 77608) baud = 76800; - else if (baud <= 117028) baud = 115200; - else if (baud <= 129347) baud = 128000; - else if (baud <= 156868) baud = 153600; - else if (baud <= 237832) baud = 230400; - else if (baud <= 254234) baud = 250000; - else if (baud <= 273066) baud = 256000; - else if (baud <= 491520) baud = 460800; - else if (baud <= 567138) baud = 500000; - else if (baud <= 670254) baud = 576000; - else if (baud < 1000000) - baud = 921600; - else if (baud > 2000000) - baud = 2000000; - return baud; -} - static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *port) { int result; @@ -1013,6 +971,58 @@ static void cp210x_get_termios_port(struct usb_serial_port *port, *cflagp = cflag; } +struct cp210x_rate { + speed_t rate; + speed_t high; +}; + +static const struct cp210x_rate cp210x_an205_table1[] = { + { 300, 300 }, + { 600, 600 }, + { 1200, 1200 }, + { 1800, 1800 }, + { 2400, 2400 }, + { 4000, 4000 }, + { 4800, 4803 }, + { 7200, 7207 }, + { 9600, 9612 }, + { 14400, 14428 }, + { 16000, 16062 }, + { 19200, 19250 }, + { 28800, 28912 }, + { 38400, 38601 }, + { 51200, 51558 }, + { 56000, 56280 }, + { 57600, 58053 }, + { 64000, 64111 }, + { 76800, 77608 }, + { 115200, 117028 }, + { 128000, 129347 }, + { 153600, 156868 }, + { 230400, 237832 }, + { 250000, 254234 }, + { 256000, 273066 }, + { 460800, 491520 }, + { 500000, 567138 }, + { 576000, 670254 }, + { 921600, UINT_MAX } +}; + +/* + * Quantises the baud rate as per AN205 Table 1 + */ +static speed_t cp210x_get_an205_rate(speed_t baud) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(cp210x_an205_table1); ++i) { + if (baud <= cp210x_an205_table1[i].high) + break; + } + + return cp210x_an205_table1[i].rate; +} + /* * CP2101 supports the following baud rates: * @@ -1051,7 +1061,10 @@ static void cp210x_change_speed(struct tty_struct *tty, * * NOTE: B0 is not implemented. */ - baud = cp210x_quantise_baudrate(baud); + if (baud < 1000000) + baud = cp210x_get_an205_rate(baud); + else if (baud > 2000000) + baud = 2000000; dev_dbg(&port->dev, "%s - setting baud rate to %u\n", __func__, baud); if (cp210x_write_u32_reg(port, CP210X_SET_BAUDRATE, baud)) { From d4706c05c59d7afdadd8e7cfc1bf470356938c89 Mon Sep 17 00:00:00 2001 From: Johan Hovold Date: Wed, 18 Jul 2018 14:24:58 +0200 Subject: [PATCH 23/27] USB: serial: cp210x: honour device-type maximum line speed Newer cp210x devices support higher line speeds than the older ones which supported a discrete set of speeds up to 921.6 kbaud. To support these higher speeds, we have for some time mapped speeds lower than 1 Mbaud to the speeds supported by older devices, while allowing the device to pick the closest possible rate for higher speeds (without trying to guess and report back what rate was actually chosen). As this implementation can lead to undefined behaviour for older devices which do not support the higher rates, let's use the later-added device-type detection to determine the maximum supported speed. This will also be useful when adding support for cp2102n which can handle rates up to 3 Mbaud. As per the data sheets the following maximum speeds are used cp2101 921.6 kbaud cp2102/3 1 Mbaud cp2104/8 2 Mbaud cp2105 - ECI port 2 Mbaud - SCI port 921.6 kbaud while keeping the maximum 2 Mbaud for unknown device types in order to avoid any regressions. Reviewed-by: Greg Kroah-Hartman Signed-off-by: Johan Hovold --- drivers/usb/serial/cp210x.c | 41 ++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c index 1b380309f653..4281f2bfe0e1 100644 --- a/drivers/usb/serial/cp210x.c +++ b/drivers/usb/serial/cp210x.c @@ -229,6 +229,7 @@ struct cp210x_serial_private { bool gpio_registered; #endif u8 partnum; + speed_t max_speed; }; struct cp210x_port_private { @@ -1052,19 +1053,20 @@ static speed_t cp210x_get_an205_rate(speed_t baud) static void cp210x_change_speed(struct tty_struct *tty, struct usb_serial_port *port, struct ktermios *old_termios) { + struct cp210x_serial_private *priv = usb_get_serial_data(port->serial); u32 baud; baud = tty->termios.c_ospeed; /* This maps the requested rate to a rate valid on cp2102 or cp2103, - * or to an arbitrary rate in [1M,2M]. + * or to an arbitrary rate in [1M, max_speed] * * NOTE: B0 is not implemented. */ if (baud < 1000000) baud = cp210x_get_an205_rate(baud); - else if (baud > 2000000) - baud = 2000000; + else if (baud > priv->max_speed) + baud = priv->max_speed; dev_dbg(&port->dev, "%s - setting baud rate to %u\n", __func__, baud); if (cp210x_write_u32_reg(port, CP210X_SET_BAUDRATE, baud)) { @@ -1495,6 +1497,37 @@ static int cp210x_port_remove(struct usb_serial_port *port) return 0; } +static void cp210x_init_max_speed(struct usb_serial *serial) +{ + struct cp210x_serial_private *priv = usb_get_serial_data(serial); + speed_t max; + + switch (priv->partnum) { + case CP210X_PARTNUM_CP2101: + max = 921600; + break; + case CP210X_PARTNUM_CP2102: + case CP210X_PARTNUM_CP2103: + max = 1000000; + break; + case CP210X_PARTNUM_CP2104: + case CP210X_PARTNUM_CP2108: + max = 2000000; + break; + case CP210X_PARTNUM_CP2105: + if (cp210x_interface_num(serial) == 0) + max = 2000000; /* ECI */ + else + max = 921600; /* SCI */ + break; + default: + max = 2000000; + break; + } + + priv->max_speed = max; +} + static int cp210x_attach(struct usb_serial *serial) { int result; @@ -1515,6 +1548,8 @@ static int cp210x_attach(struct usb_serial *serial) usb_set_serial_data(serial, priv); + cp210x_init_max_speed(serial); + if (priv->partnum == CP210X_PARTNUM_CP2105) { result = cp2105_shared_gpio_init(serial); if (result < 0) { From 6f0bcf720ea81e90e6066d14d5506565049a2eb5 Mon Sep 17 00:00:00 2001 From: Karoly Pados Date: Wed, 18 Jul 2018 14:24:59 +0200 Subject: [PATCH 24/27] USB: serial: cp210x: improve baudrate support for CP2102N CP2102N devices support a lot more baudrates than earlier chips by SiLabs. These devices are not constrained anymore by the table in AN205, and are able to generate almost any baudrate in the supported range with only minimal errors. This has also been verified with a scope on a physical device. This patch adds support for all baudrates supported by the CP2102N. Signed-off-by: Karoly Pados [johan: rework on top of an205 and max-speed patches ] Reviewed-by: Greg Kroah-Hartman Signed-off-by: Johan Hovold --- drivers/usb/serial/cp210x.c | 39 ++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c index 4281f2bfe0e1..3778685c7b99 100644 --- a/drivers/usb/serial/cp210x.c +++ b/drivers/usb/serial/cp210x.c @@ -355,6 +355,9 @@ static struct usb_serial_driver * const serial_drivers[] = { #define CP210X_PARTNUM_CP2104 0x04 #define CP210X_PARTNUM_CP2105 0x05 #define CP210X_PARTNUM_CP2108 0x08 +#define CP210X_PARTNUM_CP2102N_QFN28 0x20 +#define CP210X_PARTNUM_CP2102N_QFN24 0x21 +#define CP210X_PARTNUM_CP2102N_QFN20 0x22 #define CP210X_PARTNUM_UNKNOWN 0xFF /* CP210X_GET_COMM_STATUS returns these 0x13 bytes */ @@ -454,6 +457,15 @@ struct cp210x_gpio_write { u8 state; } __packed; +static bool cp210x_is_cp2102n(struct usb_serial *serial) +{ + struct cp210x_serial_private *priv = usb_get_serial_data(serial); + + return (priv->partnum == CP210X_PARTNUM_CP2102N_QFN28) || + (priv->partnum == CP210X_PARTNUM_CP2102N_QFN24) || + (priv->partnum == CP210X_PARTNUM_CP2102N_QFN20); +} + /* * Helper to get interface number when we only have struct usb_serial. */ @@ -1053,20 +1065,32 @@ static speed_t cp210x_get_an205_rate(speed_t baud) static void cp210x_change_speed(struct tty_struct *tty, struct usb_serial_port *port, struct ktermios *old_termios) { - struct cp210x_serial_private *priv = usb_get_serial_data(port->serial); + struct usb_serial *serial = port->serial; + struct cp210x_serial_private *priv = usb_get_serial_data(serial); u32 baud; baud = tty->termios.c_ospeed; - /* This maps the requested rate to a rate valid on cp2102 or cp2103, - * or to an arbitrary rate in [1M, max_speed] + /* + * This maps the requested rate to the actual rate on cp2102n, a valid + * rate on cp2102 or cp2103, or to an arbitrary rate in + * [1M, max_speed]. * * NOTE: B0 is not implemented. */ - if (baud < 1000000) + if (cp210x_is_cp2102n(serial)) { + int clk_div; + int prescaler; + + baud = clamp(baud, 300u, priv->max_speed); + prescaler = (baud <= 365) ? 4 : 1; + clk_div = DIV_ROUND_CLOSEST(48000000, 2 * prescaler * baud); + baud = 48000000 / (2 * prescaler * clk_div); + } else if (baud < 1000000) { baud = cp210x_get_an205_rate(baud); - else if (baud > priv->max_speed) + } else if (baud > priv->max_speed) { baud = priv->max_speed; + } dev_dbg(&port->dev, "%s - setting baud rate to %u\n", __func__, baud); if (cp210x_write_u32_reg(port, CP210X_SET_BAUDRATE, baud)) { @@ -1520,6 +1544,11 @@ static void cp210x_init_max_speed(struct usb_serial *serial) else max = 921600; /* SCI */ break; + case CP210X_PARTNUM_CP2102N_QFN28: + case CP210X_PARTNUM_CP2102N_QFN24: + case CP210X_PARTNUM_CP2102N_QFN20: + max = 3000000; + break; default: max = 2000000; break; From 7aecd7fc5d95f1447611ab5e14db5ab9549f979c Mon Sep 17 00:00:00 2001 From: Johan Hovold Date: Wed, 18 Jul 2018 14:25:00 +0200 Subject: [PATCH 25/27] USB: serial: cp210x: generalise CP2102N line-speed handling The CP2102N equations for determining the actual baud rate can be used also for other device types, so let's factor it out. Note that this removes the now unused cp210x_is_cp2102n() helper. Reviewed-by: Greg Kroah-Hartman Signed-off-by: Johan Hovold --- drivers/usb/serial/cp210x.c | 50 ++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c index 3778685c7b99..957406aac9bd 100644 --- a/drivers/usb/serial/cp210x.c +++ b/drivers/usb/serial/cp210x.c @@ -230,6 +230,7 @@ struct cp210x_serial_private { #endif u8 partnum; speed_t max_speed; + bool use_actual_rate; }; struct cp210x_port_private { @@ -457,15 +458,6 @@ struct cp210x_gpio_write { u8 state; } __packed; -static bool cp210x_is_cp2102n(struct usb_serial *serial) -{ - struct cp210x_serial_private *priv = usb_get_serial_data(serial); - - return (priv->partnum == CP210X_PARTNUM_CP2102N_QFN28) || - (priv->partnum == CP210X_PARTNUM_CP2102N_QFN24) || - (priv->partnum == CP210X_PARTNUM_CP2102N_QFN20); -} - /* * Helper to get interface number when we only have struct usb_serial. */ @@ -1036,6 +1028,23 @@ static speed_t cp210x_get_an205_rate(speed_t baud) return cp210x_an205_table1[i].rate; } +static speed_t cp210x_get_actual_rate(struct usb_serial *serial, speed_t baud) +{ + struct cp210x_serial_private *priv = usb_get_serial_data(serial); + unsigned int prescale = 1; + unsigned int div; + + baud = clamp(baud, 300u, priv->max_speed); + + if (baud <= 365) + prescale = 4; + + div = DIV_ROUND_CLOSEST(48000000, 2 * prescale * baud); + baud = 48000000 / (2 * prescale * div); + + return baud; +} + /* * CP2101 supports the following baud rates: * @@ -1072,25 +1081,17 @@ static void cp210x_change_speed(struct tty_struct *tty, baud = tty->termios.c_ospeed; /* - * This maps the requested rate to the actual rate on cp2102n, a valid - * rate on cp2102 or cp2103, or to an arbitrary rate in - * [1M, max_speed]. + * This maps the requested rate to the actual rate, a valid rate on + * cp2102 or cp2103, or to an arbitrary rate in [1M, max_speed]. * * NOTE: B0 is not implemented. */ - if (cp210x_is_cp2102n(serial)) { - int clk_div; - int prescaler; - - baud = clamp(baud, 300u, priv->max_speed); - prescaler = (baud <= 365) ? 4 : 1; - clk_div = DIV_ROUND_CLOSEST(48000000, 2 * prescaler * baud); - baud = 48000000 / (2 * prescaler * clk_div); - } else if (baud < 1000000) { + if (priv->use_actual_rate) + baud = cp210x_get_actual_rate(serial, baud); + else if (baud < 1000000) baud = cp210x_get_an205_rate(baud); - } else if (baud > priv->max_speed) { + else if (baud > priv->max_speed) baud = priv->max_speed; - } dev_dbg(&port->dev, "%s - setting baud rate to %u\n", __func__, baud); if (cp210x_write_u32_reg(port, CP210X_SET_BAUDRATE, baud)) { @@ -1524,6 +1525,7 @@ static int cp210x_port_remove(struct usb_serial_port *port) static void cp210x_init_max_speed(struct usb_serial *serial) { struct cp210x_serial_private *priv = usb_get_serial_data(serial); + bool use_actual_rate = false; speed_t max; switch (priv->partnum) { @@ -1547,6 +1549,7 @@ static void cp210x_init_max_speed(struct usb_serial *serial) case CP210X_PARTNUM_CP2102N_QFN28: case CP210X_PARTNUM_CP2102N_QFN24: case CP210X_PARTNUM_CP2102N_QFN20: + use_actual_rate = true; max = 3000000; break; default: @@ -1555,6 +1558,7 @@ static void cp210x_init_max_speed(struct usb_serial *serial) } priv->max_speed = max; + priv->use_actual_rate = use_actual_rate; } static int cp210x_attach(struct usb_serial *serial) From 5edb65a33710bbf10f38b42e0d497b35ec1ed908 Mon Sep 17 00:00:00 2001 From: Johan Hovold Date: Wed, 18 Jul 2018 14:25:01 +0200 Subject: [PATCH 26/27] USB: serial: cp210x: improve line-speed handling for CP2104 and CP2105 CP2104 and the ECI interface of CP2105 support further baud rates than the ones specified in AN205 table 1, and we can use the same equations as for CP2102N to determine and report back the actual baud rates used. Note that this could eventually be generalised also to CP2108, which uses a different base clock. There appears to be an error in the CP2108 equations which needs to be confirmed on actual hardware first however (specifically, the subtraction of one from the divisor appears to be incorrect as it introduces larger errors). Reviewed-by: Greg Kroah-Hartman Signed-off-by: Johan Hovold --- drivers/usb/serial/cp210x.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c index 957406aac9bd..4a118eb13590 100644 --- a/drivers/usb/serial/cp210x.c +++ b/drivers/usb/serial/cp210x.c @@ -1537,14 +1537,19 @@ static void cp210x_init_max_speed(struct usb_serial *serial) max = 1000000; break; case CP210X_PARTNUM_CP2104: + use_actual_rate = true; + max = 2000000; + break; case CP210X_PARTNUM_CP2108: max = 2000000; break; case CP210X_PARTNUM_CP2105: - if (cp210x_interface_num(serial) == 0) + if (cp210x_interface_num(serial) == 0) { + use_actual_rate = true; max = 2000000; /* ECI */ - else + } else { max = 921600; /* SCI */ + } break; case CP210X_PARTNUM_CP2102N_QFN28: case CP210X_PARTNUM_CP2102N_QFN24: From c8acfe0aadbeb78f65826959891be15cc0a709a3 Mon Sep 17 00:00:00 2001 From: Karoly Pados Date: Fri, 20 Jul 2018 12:52:40 +0200 Subject: [PATCH 27/27] USB: serial: cp210x: implement GPIO support for CP2102N This patch adds GPIO support for CP2102N devices. It introduces new generic code to support emulating separate input and outputs directions even though these devices only know output modes (open-drain and pushpull). Existing GPIO support for CP2105 has been migrated over to the new code structure. Only limitation is that for the QFN28 variant, only 4 out of 7 GPIOs are supported. This is because the config array locations of the last 3 pins are not documented, and reverse engineering revealed offsets that conflicted with other documented functions. Hence we'll play it safe instead until somebody clears this up further. Signed-off-by: Karoly Pados [ johan: fix style issues and a couple of minor bugs; use Karoly's updated commit message ] Acked-by: Martyn Welch Signed-off-by: Johan Hovold --- drivers/usb/serial/cp210x.c | 245 ++++++++++++++++++++++++++++++------ 1 file changed, 209 insertions(+), 36 deletions(-) diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c index 4a118eb13590..b9bc80700be7 100644 --- a/drivers/usb/serial/cp210x.c +++ b/drivers/usb/serial/cp210x.c @@ -224,9 +224,10 @@ MODULE_DEVICE_TABLE(usb, id_table); struct cp210x_serial_private { #ifdef CONFIG_GPIOLIB struct gpio_chip gc; - u8 config; - u8 gpio_mode; bool gpio_registered; + u8 gpio_pushpull; + u8 gpio_altfunc; + u8 gpio_input; #endif u8 partnum; speed_t max_speed; @@ -343,6 +344,7 @@ static struct usb_serial_driver * const serial_drivers[] = { #define CONTROL_WRITE_RTS 0x0200 /* CP210X_VENDOR_SPECIFIC values */ +#define CP210X_READ_2NCONFIG 0x000E #define CP210X_READ_LATCH 0x00C2 #define CP210X_GET_PARTNUM 0x370B #define CP210X_GET_PORTCONFIG 0x370C @@ -452,6 +454,12 @@ struct cp210x_config { #define CP2105_GPIO1_RXLED_MODE BIT(1) #define CP2105_GPIO1_RS485_MODE BIT(2) +/* CP2102N configuration array indices */ +#define CP210X_2NCONFIG_CONFIG_VERSION_IDX 2 +#define CP210X_2NCONFIG_GPIO_MODE_IDX 581 +#define CP210X_2NCONFIG_GPIO_RSTLATCH_IDX 587 +#define CP210X_2NCONFIG_GPIO_CONTROL_IDX 600 + /* CP210X_VENDOR_SPECIFIC, CP210X_WRITE_LATCH call writes these 0x2 bytes. */ struct cp210x_gpio_write { u8 mask; @@ -1313,17 +1321,8 @@ static int cp210x_gpio_request(struct gpio_chip *gc, unsigned int offset) struct usb_serial *serial = gpiochip_get_data(gc); struct cp210x_serial_private *priv = usb_get_serial_data(serial); - switch (offset) { - case 0: - if (priv->config & CP2105_GPIO0_TXLED_MODE) - return -ENODEV; - break; - case 1: - if (priv->config & (CP2105_GPIO1_RXLED_MODE | - CP2105_GPIO1_RS485_MODE)) - return -ENODEV; - break; - } + if (priv->gpio_altfunc & BIT(offset)) + return -ENODEV; return 0; } @@ -1331,10 +1330,15 @@ static int cp210x_gpio_request(struct gpio_chip *gc, unsigned int offset) static int cp210x_gpio_get(struct gpio_chip *gc, unsigned int gpio) { struct usb_serial *serial = gpiochip_get_data(gc); + struct cp210x_serial_private *priv = usb_get_serial_data(serial); + u8 req_type = REQTYPE_DEVICE_TO_HOST; int result; u8 buf; - result = cp210x_read_vendor_block(serial, REQTYPE_INTERFACE_TO_HOST, + if (priv->partnum == CP210X_PARTNUM_CP2105) + req_type = REQTYPE_INTERFACE_TO_HOST; + + result = cp210x_read_vendor_block(serial, req_type, CP210X_READ_LATCH, &buf, sizeof(buf)); if (result < 0) return result; @@ -1345,7 +1349,9 @@ static int cp210x_gpio_get(struct gpio_chip *gc, unsigned int gpio) static void cp210x_gpio_set(struct gpio_chip *gc, unsigned int gpio, int value) { struct usb_serial *serial = gpiochip_get_data(gc); + struct cp210x_serial_private *priv = usb_get_serial_data(serial); struct cp210x_gpio_write buf; + int result; if (value == 1) buf.state = BIT(gpio); @@ -1354,25 +1360,68 @@ static void cp210x_gpio_set(struct gpio_chip *gc, unsigned int gpio, int value) buf.mask = BIT(gpio); - cp210x_write_vendor_block(serial, REQTYPE_HOST_TO_INTERFACE, - CP210X_WRITE_LATCH, &buf, sizeof(buf)); + if (priv->partnum == CP210X_PARTNUM_CP2105) { + result = cp210x_write_vendor_block(serial, + REQTYPE_HOST_TO_INTERFACE, + CP210X_WRITE_LATCH, &buf, + sizeof(buf)); + } else { + u16 wIndex = buf.state << 8 | buf.mask; + + result = usb_control_msg(serial->dev, + usb_sndctrlpipe(serial->dev, 0), + CP210X_VENDOR_SPECIFIC, + REQTYPE_HOST_TO_DEVICE, + CP210X_WRITE_LATCH, + wIndex, + NULL, 0, USB_CTRL_SET_TIMEOUT); + } + + if (result < 0) { + dev_err(&serial->interface->dev, "failed to set GPIO value: %d\n", + result); + } } static int cp210x_gpio_direction_get(struct gpio_chip *gc, unsigned int gpio) { - /* Hardware does not support an input mode */ - return 0; + struct usb_serial *serial = gpiochip_get_data(gc); + struct cp210x_serial_private *priv = usb_get_serial_data(serial); + + return priv->gpio_input & BIT(gpio); } static int cp210x_gpio_direction_input(struct gpio_chip *gc, unsigned int gpio) { - /* Hardware does not support an input mode */ - return -ENOTSUPP; + struct usb_serial *serial = gpiochip_get_data(gc); + struct cp210x_serial_private *priv = usb_get_serial_data(serial); + + if (priv->partnum == CP210X_PARTNUM_CP2105) { + /* hardware does not support an input mode */ + return -ENOTSUPP; + } + + /* push-pull pins cannot be changed to be inputs */ + if (priv->gpio_pushpull & BIT(gpio)) + return -EINVAL; + + /* make sure to release pin if it is being driven low */ + cp210x_gpio_set(gc, gpio, 1); + + priv->gpio_input |= BIT(gpio); + + return 0; } static int cp210x_gpio_direction_output(struct gpio_chip *gc, unsigned int gpio, int value) { + struct usb_serial *serial = gpiochip_get_data(gc); + struct cp210x_serial_private *priv = usb_get_serial_data(serial); + + priv->gpio_input &= ~BIT(gpio); + cp210x_gpio_set(gc, gpio, value); + return 0; } @@ -1385,11 +1434,11 @@ static int cp210x_gpio_set_config(struct gpio_chip *gc, unsigned int gpio, /* Succeed only if in correct mode (this can't be set at runtime) */ if ((param == PIN_CONFIG_DRIVE_PUSH_PULL) && - (priv->gpio_mode & BIT(gpio))) + (priv->gpio_pushpull & BIT(gpio))) return 0; if ((param == PIN_CONFIG_DRIVE_OPEN_DRAIN) && - !(priv->gpio_mode & BIT(gpio))) + !(priv->gpio_pushpull & BIT(gpio))) return 0; return -ENOTSUPP; @@ -1402,12 +1451,13 @@ static int cp210x_gpio_set_config(struct gpio_chip *gc, unsigned int gpio, * this driver that provide GPIO do so in a way that does not impact other * signals and are thus expected to have very different initialisation. */ -static int cp2105_shared_gpio_init(struct usb_serial *serial) +static int cp2105_gpioconf_init(struct usb_serial *serial) { struct cp210x_serial_private *priv = usb_get_serial_data(serial); struct cp210x_pin_mode mode; struct cp210x_config config; u8 intf_num = cp210x_interface_num(serial); + u8 iface_config; int result; result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST, @@ -1424,20 +1474,26 @@ static int cp2105_shared_gpio_init(struct usb_serial *serial) /* 2 banks of GPIO - One for the pins taken from each serial port */ if (intf_num == 0) { - if (mode.eci == CP210X_PIN_MODE_MODEM) + if (mode.eci == CP210X_PIN_MODE_MODEM) { + /* mark all GPIOs of this interface as reserved */ + priv->gpio_altfunc = 0xff; return 0; + } - priv->config = config.eci_cfg; - priv->gpio_mode = (u8)((le16_to_cpu(config.gpio_mode) & + iface_config = config.eci_cfg; + priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) & CP210X_ECI_GPIO_MODE_MASK) >> CP210X_ECI_GPIO_MODE_OFFSET); priv->gc.ngpio = 2; } else if (intf_num == 1) { - if (mode.sci == CP210X_PIN_MODE_MODEM) + if (mode.sci == CP210X_PIN_MODE_MODEM) { + /* mark all GPIOs of this interface as reserved */ + priv->gpio_altfunc = 0xff; return 0; + } - priv->config = config.sci_cfg; - priv->gpio_mode = (u8)((le16_to_cpu(config.gpio_mode) & + iface_config = config.sci_cfg; + priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) & CP210X_SCI_GPIO_MODE_MASK) >> CP210X_SCI_GPIO_MODE_OFFSET); priv->gc.ngpio = 3; @@ -1445,6 +1501,125 @@ static int cp2105_shared_gpio_init(struct usb_serial *serial) return -ENODEV; } + /* mark all pins which are not in GPIO mode */ + if (iface_config & CP2105_GPIO0_TXLED_MODE) /* GPIO 0 */ + priv->gpio_altfunc |= BIT(0); + if (iface_config & (CP2105_GPIO1_RXLED_MODE | /* GPIO 1 */ + CP2105_GPIO1_RS485_MODE)) + priv->gpio_altfunc |= BIT(1); + + /* driver implementation for CP2105 only supports outputs */ + priv->gpio_input = 0; + + return 0; +} + +static int cp2102n_gpioconf_init(struct usb_serial *serial) +{ + struct cp210x_serial_private *priv = usb_get_serial_data(serial); + const u16 config_size = 0x02a6; + u8 gpio_rst_latch; + u8 config_version; + u8 gpio_pushpull; + u8 *config_buf; + u8 gpio_latch; + u8 gpio_ctrl; + int result; + u8 i; + + /* + * Retrieve device configuration from the device. + * The array received contains all customization settings done at the + * factory/manufacturer. Format of the array is documented at the + * time of writing at: + * https://www.silabs.com/community/interface/knowledge-base.entry.html/2017/03/31/cp2102n_setconfig-xsfa + */ + config_buf = kmalloc(config_size, GFP_KERNEL); + if (!config_buf) + return -ENOMEM; + + result = cp210x_read_vendor_block(serial, + REQTYPE_DEVICE_TO_HOST, + CP210X_READ_2NCONFIG, + config_buf, + config_size); + if (result < 0) { + kfree(config_buf); + return result; + } + + config_version = config_buf[CP210X_2NCONFIG_CONFIG_VERSION_IDX]; + gpio_pushpull = config_buf[CP210X_2NCONFIG_GPIO_MODE_IDX]; + gpio_ctrl = config_buf[CP210X_2NCONFIG_GPIO_CONTROL_IDX]; + gpio_rst_latch = config_buf[CP210X_2NCONFIG_GPIO_RSTLATCH_IDX]; + + kfree(config_buf); + + /* Make sure this is a config format we understand. */ + if (config_version != 0x01) + return -ENOTSUPP; + + /* + * We only support 4 GPIOs even on the QFN28 package, because + * config locations of GPIOs 4-6 determined using reverse + * engineering revealed conflicting offsets with other + * documented functions. So we'll just play it safe for now. + */ + priv->gc.ngpio = 4; + + /* + * Get default pin states after reset. Needed so we can determine + * the direction of an open-drain pin. + */ + gpio_latch = (gpio_rst_latch >> 3) & 0x0f; + + /* 0 indicates open-drain mode, 1 is push-pull */ + priv->gpio_pushpull = (gpio_pushpull >> 3) & 0x0f; + + /* 0 indicates GPIO mode, 1 is alternate function */ + priv->gpio_altfunc = (gpio_ctrl >> 2) & 0x0f; + + /* + * The CP2102N does not strictly has input and output pin modes, + * it only knows open-drain and push-pull modes which is set at + * factory. An open-drain pin can function both as an + * input or an output. We emulate input mode for open-drain pins + * by making sure they are not driven low, and we do not allow + * push-pull pins to be set as an input. + */ + for (i = 0; i < priv->gc.ngpio; ++i) { + /* + * Set direction to "input" iff pin is open-drain and reset + * value is 1. + */ + if (!(priv->gpio_pushpull & BIT(i)) && (gpio_latch & BIT(i))) + priv->gpio_input |= BIT(i); + } + + return 0; +} + +static int cp210x_gpio_init(struct usb_serial *serial) +{ + struct cp210x_serial_private *priv = usb_get_serial_data(serial); + int result; + + switch (priv->partnum) { + case CP210X_PARTNUM_CP2105: + result = cp2105_gpioconf_init(serial); + break; + case CP210X_PARTNUM_CP2102N_QFN28: + case CP210X_PARTNUM_CP2102N_QFN24: + case CP210X_PARTNUM_CP2102N_QFN20: + result = cp2102n_gpioconf_init(serial); + break; + default: + return 0; + } + + if (result < 0) + return result; + priv->gc.label = "cp210x"; priv->gc.request = cp210x_gpio_request; priv->gc.get_direction = cp210x_gpio_direction_get; @@ -1477,7 +1652,7 @@ static void cp210x_gpio_remove(struct usb_serial *serial) #else -static int cp2105_shared_gpio_init(struct usb_serial *serial) +static int cp210x_gpio_init(struct usb_serial *serial) { return 0; } @@ -1588,12 +1763,10 @@ static int cp210x_attach(struct usb_serial *serial) cp210x_init_max_speed(serial); - if (priv->partnum == CP210X_PARTNUM_CP2105) { - result = cp2105_shared_gpio_init(serial); - if (result < 0) { - dev_err(&serial->interface->dev, - "GPIO initialisation failed, continuing without GPIO support\n"); - } + result = cp210x_gpio_init(serial); + if (result < 0) { + dev_err(&serial->interface->dev, "GPIO initialisation failed: %d\n", + result); } return 0;