All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] USB: cdc-acm: restore the line coding after a reset-resume
@ 2026-11-20 17:18 Piotr Kubik
  2026-11-20 17:30 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Piotr Kubik @ 2026-11-20 17:18 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Oliver Neukum
  Cc: Piotr Kubik, linux-usb, linux-kernel, Francesco Lavra, stable

A device that loses power during system suspend comes back at its
power-on line coding. With USB persist, the USB core then resets the
device and calls acm_reset_resume(), but acm->line still holds the line
coding that the driver sent last. acm_tty_set_termios() sends
SET_LINE_CODING only when the termios differ from acm->line, so an open
with the same settings sends nothing. The port then runs at the wrong
rate until the termios change or the device is probed again.

Send the cached line coding from acm_reset_resume() of the last
interface to resume, which acm->susp_count shows, before acm_resume()
restarts the URBs. The USB core calls reset_resume for each interface in
the order of the configuration descriptor, so the control interface is
not always the last one. Use the variant without the autopm reference,
because reset_resume also runs in a runtime resume of the device, where
usb_autopm_get_interface() would wait for that same resume and never
return. Ignore the result, as acm_probe() does, because a device without
USB_CDC_CAP_LINE need not support the request.

I saw the fault on my workstation while using serial consoles.
Claude Code agent (Opus 5.5) traced the cause to the cached
acm->line and wrote the first draft of this patch and this commit message.
It also wrote and ran the test: a QEMU guest with the adapter passed
through, and a USB power cut while the guest was in S3.
Two other Claude instances reviewed the patch, the message and the test
record. I reviewed the patch and I understand the change.

Fixes: a91b0c502285 ("cdc_acm: add reset_resume method")
Cc: stable@vger.kernel.org
Signed-off-by: Piotr Kubik <piotr@kubik.pl>
---

Notes:
    Changes in v2:
    - Send the line coding from the last interface to resume, which
      acm->susp_count shows, instead of from the control interface, which
      need not resume last (Oliver Neukum).
    - Commit message: describe the interface order.
    
    v1: https://lore.kernel.org/all/20261119122842.3611681-1-piotr@kubik.pl/
    
    Reproduced and tested on v7.4-rc3 in a QEMU 10.0.13 q35 guest. The v2
    runs used f11fe7603df0, the usb-linus head of 2026-11-19; three of the
    runs without the patch used mainline 20eef29b7c84, which has the same
    cdc-acm.c. The guest runs (TCG, qemu-xhci) with a WCH CH344 quad UART
    (1a86:55d5) passed through from the host. Port 0 is wired to a 115200
    8N1 console that answers a carriage return with a 24-byte login prompt.
    The guest holds the port open and suspends to RAM. While the guest is
    suspended, the host cuts VBUS of the hub port of the adapter for 3 s and
    then wakes the guest, which reset-resumes the adapter. The host's own
    cdc-acm binds the adapter between the power cut and the wakeup, so the
    adapter is at 9600 or at its power-on line coding when the guest
    resumes. The patch applies without offset to usb-linus b0baaf6cbb16 of
    2026-11-20, whose cdc-acm.c adds only device IDs.
    
    Without this patch: the read after the resume returns 1 byte and no
    prompt, and usbmon shows no SET_LINE_CODING after the resume (4 runs).
    With v2: the read returns the prompt, and usbmon shows one
    SET_LINE_CODING per port, with the cached line coding of that port
    (2 runs, one with an extra dev_info() in acm_reset_resume()). On the
    CH344 each control interface resumes before its data interface; the
    extra print shows that the data interface, the last one, sends the
    request, in S3 and in a runtime resume.
    
    Also with v2: a runtime resume with the RESET_RESUME quirk set for the
    adapter sends the line coding once per port and completes. Without a
    power cut the CH344 keeps its line coding, so the unpatched driver
    shows no fault in that case.
    
    Build: make W=1 drivers/usb/class/cdc-acm.o gives no new warnings.
    checkpatch.pl --strict reports no errors, warnings or checks.
    
    Not tested:
    - a device whose control interface resumes after its data interface
    - a reset-resume of a single port that lost power: in the guest the
      reset-resume came from the xHCI reinit ("root hub lost power or was
      reset")
    - a device that rejects SET_LINE_CODING (no USB_CDC_CAP_LINE)
    - a device with a combined control and data interface
    - this patch on bare metal; the fault itself was seen on a 6.12 host
      after S3
    - a port that is open during a runtime reset-resume
    - an autopm reference in reset_resume: the wait without end comes from
      reading rpm_resume(), not from a test

 drivers/usb/class/cdc-acm.c | 41 ++++++++++++++++++++++++++++++-------
 1 file changed, 34 insertions(+), 7 deletions(-)

diff --git a/drivers/usb/class/cdc-acm.c b/drivers/usb/class/cdc-acm.c
index 666969ffd194..bcbebcd47b3a 100644
--- a/drivers/usb/class/cdc-acm.c
+++ b/drivers/usb/class/cdc-acm.c
@@ -108,15 +108,11 @@ static void acm_release_minor(struct acm *acm)
  * Functions for ACM control messages.
  */
 
-static int acm_ctrl_msg(struct acm *acm, int request, int value,
-							void *buf, int len)
+static int __acm_ctrl_msg(struct acm *acm, int request, int value,
+			  void *buf, int len)
 {
 	int retval;
 
-	retval = usb_autopm_get_interface(acm->control);
-	if (retval)
-		return retval;
-
 	retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
 		request, USB_RT_ACM, value,
 		acm->control->altsetting[0].desc.bInterfaceNumber,
@@ -126,9 +122,23 @@ static int acm_ctrl_msg(struct acm *acm, int request, int value,
 		"%s - rq 0x%02x, val %#x, len %#x, result %d\n",
 		__func__, request, value, len, retval);
 
+	return retval < 0 ? retval : 0;
+}
+
+static int acm_ctrl_msg(struct acm *acm, int request, int value,
+			void *buf, int len)
+{
+	int retval;
+
+	retval = usb_autopm_get_interface(acm->control);
+	if (retval)
+		return retval;
+
+	retval = __acm_ctrl_msg(acm, request, value, buf, len);
+
 	usb_autopm_put_interface(acm->control);
 
-	return retval < 0 ? retval : 0;
+	return retval;
 }
 
 /* devices aren't required to support these requests.
@@ -1739,10 +1749,27 @@ static int acm_resume(struct usb_interface *intf)
 static int acm_reset_resume(struct usb_interface *intf)
 {
 	struct acm *acm = usb_get_intfdata(intf);
+	bool last;
 
 	if (tty_port_initialized(&acm->port))
 		tty_port_tty_hangup(&acm->port, false);
 
+	spin_lock_irq(&acm->write_lock);
+	last = acm->susp_count == 1;
+	spin_unlock_irq(&acm->write_lock);
+
+	/*
+	 * The device can lose its line coding, for example when it loses
+	 * power during system suspend, and acm_tty_set_termios() sends the
+	 * line coding only when it differs from acm->line. Send it from the
+	 * last interface to resume, before acm_resume() restarts the URBs.
+	 * In a runtime resume of the device, an autopm reference here would
+	 * wait for this resume and never return.
+	 */
+	if (last)
+		__acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0,
+			       &acm->line, sizeof(acm->line));
+
 	return acm_resume(intf);
 }
 

base-commit: f11fe7603df07fbb9fa02b0d385c1696c87a6dab
-- 
2.55.0


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

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

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 17:18 [PATCH v2] USB: cdc-acm: restore the line coding after a reset-resume Piotr Kubik
2026-11-20 17:30 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.