USB: cdc-acm: restore the line coding after a reset-resume
Subsystems (3)
PATCH v3 1/1 USB: cdc-acm: restore the line coding after a reset-resume
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. Send it with __acm_set_line(), a new variant of acm_set_line() 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.
Three other Claude instances reviewed the patch, the message and the test record. I reviewed the patch and I understand the change.
---
Notes:
Changes in v3:
- Send the request with a new __acm_set_line() macro, next to acm_set_line(), instead of a direct __acm_ctrl_msg() call (Oliver Neukum). acm_set_line() itself takes an autopm reference, which waits without end in a runtime reset-resume; the v2 thread has the test.
- Commit message: name __acm_set_line().
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.
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
v3 changes only the source text: objdump -d of cdc-acm.o (x86_64, W=1) is identical for v2 and v3. The tests below ran with v2.
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 3a8ea89f0189 of 2026-11-30, 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 warnings. checkpatch.pl --strict reports one check, "Macro argument reuse 'line'", for __acm_set_line(). It reports the same check for acm_set_line(), and sizeof does not evaluate its operand.
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
drivers/usb/class/cdc-acm.c | 42 ++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-)
drivers/usb/class/cdc-acm.c +35 -7
@@ -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.
@@ -145,6 +155,8 @@ static inline int acm_set_control(struct acm *acm, int control)#define acm_set_line (acm, line) \ acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line)) +#define __acm_set_line (acm, line) \ + __acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))#define acm_send_break (acm, ms) \ acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
@@ -1739,10 +1751,26 @@ 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_set_line (acm, &acm->line); +return acm_resume (intf); }
base-commit: f11fe7603df07fbb9fa02b0d385c1696c87a6dab
Signature · 1 line
2.55.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 27 lines
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(), butacm->linestill holds the line coding that the driver sent last.acm_tty_set_termios()sendsSET_LINE_CODINGonly when the termios differ fromacm->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, whichacm->susp_countshows, beforeacm_resume()restarts the URBs. The USB core callsreset_resumefor each interface in the order of the configuration descriptor, so the control interface is not always the last one. Send it with__acm_set_line(), a new variant ofacm_set_line()without the autopm reference, becausereset_resumealso runs in a runtime resume of the device, whereusb_autopm_get_interface()would wait for that same resume and never return. Ignore the result, asacm_probe()does, because a device withoutUSB_CDC_CAP_LINEneed 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->lineand 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.
Three other Claude instances reviewed the patch, the message and the test record. I reviewed the patch and I understand the change.
Quoted 3 lines
Fixes a91b0c502285 ("cdc_acm: add reset_resume method")Signed-off-by Piotr Kubik <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1