Linux USB
 help / color / mirror / Atom feed
* [PATCH v2] USB: serial: kobil_sct: fix URB submitted while active
@ 2026-11-30 10:57 YuanZhe Liu
  2026-11-30 11:24 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: YuanZhe Liu @ 2026-11-30 10:57 UTC (permalink / raw)
  To: Johan Hovold
  Cc: Greg Kroah-Hartman, linux-usb, linux-kernel, stable, Bin Yu,
	Cheng Wen

kobil_write() splits a command block into interrupt-out URBs of at most
the endpoint packet size and submits the same port interrupt-out URB in
a loop, sleeping 24 ms between submissions. The completion handler is
empty, so nothing guarantees that the previous transfer has finished
before the URB is reused.

A device may delay or NAK a packet, in which case the URB stays active
and the next usb_submit_urb() call triggers a "URB submitted while
active" warning, after which the packet is silently dropped. The driver
also rewrites the transfer buffer and length of an URB that may still be
in flight.

Wait for the URB to complete before submitting the next chunk, stop
sending the block when the transfer fails or times out, and do not
restart the interrupt-in URB unless the block has been sent. Bail out
if the port was disconnected while waiting, since the private data may
already have been freed.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Co-developed-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Bin Yu <byu@xidian.edu.cn>
Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>

---
v2:
- Check urb->status and stop sending the block when the transfer fails
  or the URB is killed (for example at close).
- Only restart the interrupt-in URB once the block has been sent.
- Bail out if the port was disconnected while waiting.

Note that kobil_write() already sleeps between chunks (msleep), so the
completion wait does not introduce a new requirement on the calling
context.
---
 drivers/usb/serial/kobil_sct.c | 44 +++++++++++++++++++++++++++++++---
 1 file changed, 41 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/serial/kobil_sct.c b/drivers/usb/serial/kobil_sct.c
index 4a622cf8e6ae..59fa951e34b8 100644
--- a/drivers/usb/serial/kobil_sct.c
+++ b/drivers/usb/serial/kobil_sct.c
@@ -105,6 +105,7 @@ struct kobil_private {
 	int filled;  /* index of the last char in buf */
 	int cur_pos; /* index of the next char to send in buf */
 	__u16 device_type;
+	struct completion write_done;
 };
 
 static int kobil_ctrl_send(struct usb_serial_port *port, u8 req, u16 val)
@@ -135,6 +136,7 @@ static int kobil_port_probe(struct usb_serial_port *port)
 	priv->filled = 0;
 	priv->cur_pos = 0;
 	priv->device_type = le16_to_cpu(serial->dev->descriptor.idProduct);
+	init_completion(&priv->write_done);
 
 	switch (priv->device_type) {
 	case KOBIL_ADAPTER_B_PRODUCT_ID:
@@ -264,6 +266,10 @@ static void kobil_read_int_callback(struct urb *urb)
 
 static void kobil_write_int_callback(struct urb *urb)
 {
+	struct usb_serial_port *port = urb->context;
+	struct kobil_private *priv = usb_get_serial_port_data(port);
+
+	complete(&priv->write_done);
 }
 
 
@@ -301,6 +307,7 @@ static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
 		}
 
 		todo = priv->filled - priv->cur_pos;
+		result = 0;
 
 		while (todo > 0) {
 			/* max 8 byte in one urb (endpoint size) */
@@ -311,9 +318,39 @@ static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
 			port->interrupt_out_urb->transfer_buffer_length = length;
 
 			priv->cur_pos = priv->cur_pos + length;
+			reinit_completion(&priv->write_done);
 			result = usb_submit_urb(port->interrupt_out_urb,
 					GFP_ATOMIC);
-			dev_dbg(&port->dev, "%s - Send write URB returns: %i\n", __func__, result);
+			if (result) {
+				dev_err(&port->dev,
+					"%s - failed to send write URB: %d\n",
+					__func__, result);
+				break;
+			}
+
+			/* Do not reuse the URB before the transfer has completed. */
+			if (!wait_for_completion_timeout(&priv->write_done,
+					msecs_to_jiffies(KOBIL_TIMEOUT))) {
+				usb_kill_urb(port->interrupt_out_urb);
+				result = -ETIMEDOUT;
+				break;
+			}
+
+			/*
+			 * The port may have been disconnected while waiting, in
+			 * which case the private data may already have been freed.
+			 */
+			if (port->serial->disconnected)
+				return count;
+
+			result = port->interrupt_out_urb->status;
+			if (result) {
+				dev_dbg(&port->dev,
+					"%s - write URB status: %d\n",
+					__func__, result);
+				break;
+			}
+
 			todo = priv->filled - priv->cur_pos;
 
 			if (todo > 0)
@@ -324,8 +361,9 @@ static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
 		priv->cur_pos = 0;
 
 		/* start reading (except TWIN and KAAN SIM) */
-		if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
-				priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
+		if (result != -ENOENT &&
+		    (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
+		     priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID)) {
 			result = usb_submit_urb(port->interrupt_in_urb,
 					GFP_ATOMIC);
 			dev_dbg(&port->dev, "%s - Send read URB returns: %i\n", __func__, result);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30 11:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 10:57 [PATCH v2] USB: serial: kobil_sct: fix URB submitted while active YuanZhe Liu
2026-11-30 11:24 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox