Linux USB
 help / color / mirror / Atom feed
* [PATCH v3] usb: gadget: u_serial: implement flush_buffer
@ 2026-11-16 15:01 Mehmet Fide
  2026-11-16 15:08 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Mehmet Fide @ 2026-11-16 15:01 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: Neill Kapron, Krystian Kaniewski, linux-usb, linux-kernel,
	mehmet.fide

From: Mehmet Fide <mehmet.fide@screeningeagle.com>

gs_close() waits up to GS_CLOSE_TIMEOUT (15 s) for the write buffer
to drain while the gadget is connected. When the host does not read
the port, for example because no application on the host has it open,
the last close blocks for the full timeout. tcflush(TCOFLUSH) cannot
avoid that: u_serial has no flush_buffer operation, so the request is
a no-op.

Implement flush_buffer: discard the buffered output under the port
lock and wake up the close and write waiters. As on other tty drivers,
a hangup and an interrupt character (ISIG without NOFLSH) now discard
the pending output as well.

Signed-off-by: Mehmet Fide <mehmet.fide@screeningeagle.com>
---

Notes:
    This applies on top of Krystian Kaniewski's "usb: gadget: u_serial: pin
    ports while TTYs are installed" in usb-linus, which keeps the port until
    the tty is released. A flush from a hangup needs that.
    
    Tested on a Colibri VF50: with output pending and no reader on the
    host, close() after tcflush(TCOFLUSH) returns at once instead of after
    15 s.
    
    v3:
    - Patch 1/2 of v2 dropped, Krystian's patch replaces it.
    
    v2: https://lore.kernel.org/linux-usb/20261019085855.3259716-1-mehmet.fide@gmail.com/
    - Sent after a patch that keeps the port until the tty is released:
      a flush from a hangup could use a freed port (Sashiko on v1).

 drivers/usb/gadget/function/u_serial.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/drivers/usb/gadget/function/u_serial.c b/drivers/usb/gadget/function/u_serial.c
index 0a304f508bc3..ff912c29d6be 100644
--- a/drivers/usb/gadget/function/u_serial.c
+++ b/drivers/usb/gadget/function/u_serial.c
@@ -891,6 +891,19 @@ static void gs_flush_chars(struct tty_struct *tty)
 	spin_unlock_irqrestore(&port->port_lock, flags);
 }
 
+static void gs_flush_buffer(struct tty_struct *tty)
+{
+	struct gs_port	*port = tty->driver_data;
+	unsigned long	flags;
+
+	spin_lock_irqsave(&port->port_lock, flags);
+	kfifo_reset(&port->port_write_buf);
+	spin_unlock_irqrestore(&port->port_lock, flags);
+
+	wake_up_interruptible(&port->drain_wait);
+	tty_wakeup(tty);
+}
+
 static unsigned int gs_write_room(struct tty_struct *tty)
 {
 	struct gs_port	*port = tty->driver_data;
@@ -985,6 +998,7 @@ static const struct tty_operations gs_tty_ops = {
 	.write =		gs_write,
 	.put_char =		gs_put_char,
 	.flush_chars =		gs_flush_chars,
+	.flush_buffer =		gs_flush_buffer,
 	.write_room =		gs_write_room,
 	.chars_in_buffer =	gs_chars_in_buffer,
 	.unthrottle =		gs_unthrottle,

base-commit: 3149e8b3237f58d4dd440fc64c45c93404c4b064
-- 
2.56.0


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

* Re: [PATCH v3] usb: gadget: u_serial: implement flush_buffer
  2026-11-16 15:01 [PATCH v3] usb: gadget: u_serial: implement flush_buffer Mehmet Fide
@ 2026-11-16 15:08 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-16 15:08 UTC (permalink / raw)
  To: Mehmet Fide; +Cc: linux-usb

> From: Mehmet Fide <mehmet.fide@screeningeagle.com>
> 
> gs_close() waits up to GS_CLOSE_TIMEOUT (15 s) for the write buffer
> to drain while the gadget is connected. When the host does not read
> the port, for example because no application on the host has it open,
> the last close blocks for the full timeout. tcflush(TCOFLUSH) cannot
> avoid that: u_serial has no flush_buffer operation, so the request is
> a no-op.
> 
> Implement flush_buffer: discard the buffered output under the port
> lock and wake up the close and write waiters. As on other tty drivers,
> a hangup and an interrupt character (ISIG without NOFLSH) now discard
> the pending output as well.
> 
> Signed-off-by: Mehmet Fide <mehmet.fide@screeningeagle.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261116150109.1474880-1-mehmet.fide@gmail.com?part=1


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

end of thread, other threads:[~2026-11-16 15:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 15:01 [PATCH v3] usb: gadget: u_serial: implement flush_buffer Mehmet Fide
2026-11-16 15:08 ` sashiko-bot

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