Skip to contentSkip to conversation

usb: gadget serial: Honour termios CLOCAL on disconnect

PATCH In review Felipe Balbi 4 participants 10 messages Oct 27, 2014 – Nov 7, 2014 8 messages not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): 1414346490-19307-3-git-send-email-kyosti.malkki-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#3 lore raw

Hi,

Quoted 19 lines · Kyösti Mälkki

There are applications where it is desirable to not hangup ttyGS* when USB disconnect is detected. USB host side of communication may power-cycle periodically or there may be the actual need to physically disconnect and reconnect USB cable temporarily.

USB disconnects on serial gadget are comparable to loss of Carrier Detect of conventional UARTs. With the change, if ttyGS* has termios CLOCAL flag set, disconnect on USB does not hangup the TTY.

---
 drivers/usb/gadget/function/u_serial.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/usb/gadget/function/u_serial.c +0 -0
index 491082a..e68ffd7 100644

drivers/usb/gadget/function/u_serial.c void gserial_disconnect(struct gserial *gser) · line 1257

Quoted 6 lines
drivers/usb/gadget/function/u_serial.c +1 -2
@@ -1254,8 +1254,13 @@ void gserial_disconnect(struct gserial *gser)
 	gser->ioport = NULL;
 	if (port->port.count > 0 || port->openclose) {
 		wake_up_interruptible(&port->drain_wait);
-		if (port->port.tty)
-			tty_hangup(port->port.tty);
+		struct tty_struct *tty = port->port.tty;

declare above as Sergei said.

drivers/usb/gadget/function/u_serial.c void gserial_disconnect(struct gserial *gser) · line 1258

Quoted 1 line
drivers/usb/gadget/function/u_serial.c +1 -0
+		if (tty) {

is there any situation where tty would be NULL here ?

drivers/usb/gadget/function/u_serial.c void gserial_disconnect(struct gserial *gser) · line 1262

Quoted 4 lines
drivers/usb/gadget/function/u_serial.c +4 -0
+			if (tty->termios.c_cflag & CLOCAL)
+				stop_tty(tty);
+			else
+				tty_hangup(tty);

this I'll defer to Greg who also maintains tty.

Signature · 1 line
balbi
#4 lore raw
Quoted 35 lines · Felipe Balbi

Hi,

Quoted 19 lines · Kyösti Mälkki

There are applications where it is desirable to not hangup ttyGS* when USB disconnect is detected. USB host side of communication may power-cycle periodically or there may be the actual need to physically disconnect and reconnect USB cable temporarily.

USB disconnects on serial gadget are comparable to loss of Carrier Detect of conventional UARTs. With the change, if ttyGS* has termios CLOCAL flag set, disconnect on USB does not hangup the TTY.

---
  drivers/usb/gadget/function/u_serial.c | 9 +++++++--
  1 file changed, 7 insertions(+), 2 deletions(-)
drivers/usb/gadget/function/u_serial.c +0 -0
index 491082a..e68ffd7 100644

drivers/usb/gadget/function/u_serial.c void gserial_disconnect(struct gserial *gser) · line 1257

Quoted 6 lines
drivers/usb/gadget/function/u_serial.c +1 -2
@@ -1254,8 +1254,13 @@ void gserial_disconnect(struct gserial *gser)
  	gser->ioport = NULL;
  	if (port->port.count > 0 || port->openclose) {
  		wake_up_interruptible(&port->drain_wait);
-		if (port->port.tty)
-			tty_hangup(port->port.tty);
+		struct tty_struct *tty = port->port.tty;

declare above as Sergei said.

drivers/usb/gadget/function/u_serial.c void gserial_disconnect(struct gserial *gser) · line 1258

Quoted 1 line
drivers/usb/gadget/function/u_serial.c +1 -0
+		if (tty) {

is there any situation where tty would be NULL here ?

Quoted 7 lines

drivers/usb/gadget/function/u_serial.c void gserial_disconnect(struct gserial *gser) · line 1262

Quoted 4 lines
drivers/usb/gadget/function/u_serial.c +4 -0
+			if (tty->termios.c_cflag & CLOCAL)
+				stop_tty(tty);
+			else
+				tty_hangup(tty);

this I'll defer to Greg who also maintains tty.

My main concern is if someone runs getty on ttyACM without explicitly clearing CLOCAL. With the patch here login session would no longer get SIGHUP if cable is disconnected. And it is not possible to tell if it is the same cable that is plugged back in.

KM

--
To unsubscribe from this list: send the line "unsubscribe linux-serial" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): [email protected]

#6 lore raw
Quoted 44 lines · Kyösti Mälkki
Quoted 35 lines · Felipe Balbi

Hi,

Quoted 19 lines · Kyösti Mälkki

There are applications where it is desirable to not hangup ttyGS* when USB disconnect is detected. USB host side of communication may power-cycle periodically or there may be the actual need to physically disconnect and reconnect USB cable temporarily.

USB disconnects on serial gadget are comparable to loss of Carrier Detect of conventional UARTs. With the change, if ttyGS* has termios CLOCAL flag set, disconnect on USB does not hangup the TTY.

---
 drivers/usb/gadget/function/u_serial.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/usb/gadget/function/u_serial.c +0 -0
index 491082a..e68ffd7 100644

drivers/usb/gadget/function/u_serial.c void gserial_disconnect(struct gserial *gser) · line 1257

Quoted 6 lines
drivers/usb/gadget/function/u_serial.c +1 -2
@@ -1254,8 +1254,13 @@ void gserial_disconnect(struct gserial *gser)
 	gser->ioport = NULL;
 	if (port->port.count > 0 || port->openclose) {
 		wake_up_interruptible(&port->drain_wait);
-		if (port->port.tty)
-			tty_hangup(port->port.tty);
+		struct tty_struct *tty = port->port.tty;

declare above as Sergei said.

drivers/usb/gadget/function/u_serial.c void gserial_disconnect(struct gserial *gser) · line 1258

Quoted 1 line
drivers/usb/gadget/function/u_serial.c +1 -0
+		if (tty) {

is there any situation where tty would be NULL here ?

Quoted 7 lines

drivers/usb/gadget/function/u_serial.c void gserial_disconnect(struct gserial *gser) · line 1262

Quoted 4 lines
drivers/usb/gadget/function/u_serial.c +4 -0
+			if (tty->termios.c_cflag & CLOCAL)
+				stop_tty(tty);
+			else
+				tty_hangup(tty);

this I'll defer to Greg who also maintains tty.

Quoted 4 lines

My main concern is if someone runs getty on ttyACM without explicitly clearing CLOCAL. With the patch here login session would no longer get SIGHUP if cable is disconnected. And it is not possible to tell if it is the same cable that is plugged back in.

true, but woudln't the same thing happen with a real UART port using 8250.ko ?

Signature · 1 line
balbi
#7 lore raw
In reply to Felipe Balbi
Quoted 35 lines · Felipe Balbi

Hi,

Quoted 19 lines · Kyösti Mälkki

There are applications where it is desirable to not hangup ttyGS* when USB disconnect is detected. USB host side of communication may power-cycle periodically or there may be the actual need to physically disconnect and reconnect USB cable temporarily.

USB disconnects on serial gadget are comparable to loss of Carrier Detect of conventional UARTs. With the change, if ttyGS* has termios CLOCAL flag set, disconnect on USB does not hangup the TTY.

---
 drivers/usb/gadget/function/u_serial.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/usb/gadget/function/u_serial.c +0 -0
index 491082a..e68ffd7 100644

drivers/usb/gadget/function/u_serial.c void gserial_disconnect(struct gserial *gser) · line 1257

Quoted 6 lines
drivers/usb/gadget/function/u_serial.c +1 -2
@@ -1254,8 +1254,13 @@ void gserial_disconnect(struct gserial *gser)
 	gser->ioport = NULL;
 	if (port->port.count > 0 || port->openclose) {
 		wake_up_interruptible(&port->drain_wait);
-		if (port->port.tty)
-			tty_hangup(port->port.tty);
+		struct tty_struct *tty = port->port.tty;

declare above as Sergei said.

drivers/usb/gadget/function/u_serial.c void gserial_disconnect(struct gserial *gser) · line 1258

Quoted 1 line
drivers/usb/gadget/function/u_serial.c +1 -0
+		if (tty) {

is there any situation where tty would be NULL here ?

Quoted 6 lines

drivers/usb/gadget/function/u_serial.c void gserial_disconnect(struct gserial *gser) · line 1262

Quoted 4 lines
drivers/usb/gadget/function/u_serial.c +4 -0
+			if (tty->termios.c_cflag & CLOCAL)
+				stop_tty(tty);
+			else
+				tty_hangup(tty);

this I'll defer to Greg who also maintains tty.

I'm curious what happens without stop_tty().

The tty is restartable from userspace with

    tcflow(fd, TCOOFF);
    tcflow(fd, TCOON);

Regards,
Peter Hurley
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#8 lore raw

usb: gadget: Fixes to receive from USB EHCI Debug

In reply to a message not in the archive

USB gadget driver dbgp can be used as an EHCI debug dongle in replacement for product like Net20DC. With it one can receive early kernel messages from remote targets over USB. See parameter earlyprintk=dbgp for target kernel requirements and configuration.

The two patches are required for dbgp gadget driver to work. Longterm
plan: I think G_DBGP_PRINTK is an odd hack and could be removed. Exposing the debug descriptor can be implemented in cdc_acm so that we could seemlessly reconfigure endpoint from the 8 byte payload limitation EHCI Debug Port has, to 512 bytes, once ehci-hcd with DMA is available.

Handling of the termios CLOCAL flag calls for some discussion and I am not that familiar with the tty infra u_serial uses. See the previous change around gserial_disconnect():
http://permalink.gmane.org/gmane.linux.usb.general/4260

Regards,
Kyösti
--
To unsubscribe from this list: send the line "unsubscribe linux-serial" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 1415027885-2181-1-git-send-email-kyosti.malkki-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#10 lore raw

PATCH v2 1/2 usb: gadget dbgp: Fix endpoint config after USB disconnect

SET_FEATURE request with DEBUG_MODE only worked the first time after module initialisation. Per the USB 2.0 debug device specification, said request is to be treated as if it were a SET_CONFIGURATION request, i.e. endpoint must be re-configured.

As configure_endpoints() may now get called multiple times, move it outside __init and move serial_alloc_tty() call into __init.

Code has assumption that endpoint mapping remains unchanged with consecutive calls of configure_endpoints().

---
 drivers/usb/gadget/legacy/dbgp.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
drivers/usb/gadget/legacy/dbgp.c +14 -12
index 1b07513..633683a 100644
@@ -237,7 +237,7 @@ static void dbgp_unbind(struct usb_gadget *gadget)
 static unsigned char tty_line;
 #endif
 
-static int __init dbgp_configure_endpoints(struct usb_gadget *gadget)
+static int dbgp_configure_endpoints(struct usb_gadget *gadget)
 {
 	int stp;
 
@@ -273,19 +273,10 @@ static int __init dbgp_configure_endpoints(struct usb_gadget *gadget)
 
 	dbgp.serial->in->desc = &i_desc;
 	dbgp.serial->out->desc = &o_desc;
-
-	if (gserial_alloc_line(&tty_line)) {
-		stp = 3;
-		goto fail_3;
-	}
+#endif
 
 	return 0;
 
-fail_3:
-	dbgp.o_ep->driver_data = NULL;
-#else
-	return 0;
-#endif
 fail_2:
 	dbgp.i_ep->driver_data = NULL;
 fail_1:
@@ -324,10 +315,17 @@ static int __init dbgp_bind(struct usb_gadget *gadget,
 		err = -ENOMEM;
 		goto fail;
 	}
+
+	if (gserial_alloc_line(&tty_line)) {
+		stp = 4;
+		err = -ENODEV;
+		goto fail;
+	}
 #endif
+
 	err = dbgp_configure_endpoints(gadget);
 	if (err < 0) {
-		stp = 4;
+		stp = 5;
 		goto fail;
 	}
 
@@ -383,6 +381,10 @@ static int dbgp_setup(struct usb_gadget *gadget,
 #ifdef CONFIG_USB_G_DBGP_PRINTK
 		err = dbgp_enable_ep();
 #else
+		err = dbgp_configure_endpoints(gadget);
+		if (err < 0) {
+			goto fail;
+		}
 		err = gserial_connect(dbgp.serial, tty_line);
 #endif
 		if (err < 0)
Signature · 6 lines
1.8.1.1

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 1415027885-2181-2-git-send-email-kyosti.malkki-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#12 lore raw

Re: [PATCH v2 1/2] usb: gadget dbgp: Fix endpoint config after USB disconnect

Quoted 5 lines · Kyösti Mälkki

SET_FEATURE request with DEBUG_MODE only worked the first time after module initialisation. Per the USB 2.0 debug device specification, said request is to be treated as if it were a SET_CONFIGURATION request, i.e. endpoint must be re-configured.

Quoted 8 lines

As configure_endpoints() may now get called multiple times, move it outside __init and move serial_alloc_tty() call into __init.

Code has assumption that endpoint mapping remains unchanged with consecutive calls of configure_endpoints().

---

if there are no comments here, I'll add this one to my testing/next.

Signature · 1 line
balbi
#13 lore raw

PATCH v2 2/2 usb: gadget serial: Honour termios CLOCAL on disconnect

In reply to a message not in the archive

There are applications where it is desirable to not hangup ttyGS* when USB disconnect is detected. USB host side of communication may power-cycle periodically or there may be the actual need to physically disconnect and reconnect USB cable temporarily.

USB disconnects on serial gadget are comparable to loss of Carrier Detect of conventional UARTs. With the change, if ttyGS* has termios CLOCAL flag set, disconnect on USB does not hangup the TTY.

---
 drivers/usb/gadget/function/u_serial.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/usb/gadget/function/u_serial.c +9 -2
index 491082a..dabc165 100644
@@ -1253,9 +1253,16 @@ void gserial_disconnect(struct gserial *gser)
 	port->port_usb = NULL;
 	gser->ioport = NULL;
 	if (port->port.count > 0 || port->openclose) {
+		struct tty_struct *tty;
+
 		wake_up_interruptible(&port->drain_wait);
-		if (port->port.tty)
-			tty_hangup(port->port.tty);
+		tty = port->port.tty;
+		if (tty) {
+			if (tty->termios.c_cflag & CLOCAL)
+				stop_tty(tty);
+			else
+				tty_hangup(tty);
+		}
 	}
 	spin_unlock_irqrestore(&port->port_lock, flags);
 
Signature · 6 lines
1.8.1.1

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 1415027885-2181-3-git-send-email-kyosti.malkki-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#15 lore raw

Hi Kyösti,

Quoted 23 lines · Kyösti Mälkki

There are applications where it is desirable to not hangup ttyGS* when USB disconnect is detected. USB host side of communication may power-cycle periodically or there may be the actual need to physically disconnect and reconnect USB cable temporarily.

USB disconnects on serial gadget are comparable to loss of Carrier Detect of conventional UARTs. With the change, if ttyGS* has termios CLOCAL flag set, disconnect on USB does not hangup the TTY.

---
 drivers/usb/gadget/function/u_serial.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/usb/gadget/function/u_serial.c +1 -0
index 491082a..dabc165 100644
@@ -1253,9 +1253,16 @@ void gserial_disconnect(struct gserial *gser)
 	port->port_usb = NULL;
 	gser->ioport = NULL;
 	if (port->port.count > 0 || port->openclose) {
+		struct tty_struct *tty;

drivers/usb/gadget/function/u_serial.c void gserial_disconnect(struct gserial *gser) · line 1264

Quoted 10 lines
drivers/usb/gadget/function/u_serial.c +7 -2
+
 		wake_up_interruptible(&port->drain_wait);
-		if (port->port.tty)
-			tty_hangup(port->port.tty);
+		tty = port->port.tty;
+		if (tty) {
+			if (tty->termios.c_cflag & CLOCAL)
+				stop_tty(tty);
+			else
+				tty_hangup(tty);

It seems you missed my earlier email: what happens if you leave out the stop_tty() call here?

I ask because the tty is still restartable from userspace after you stop_tty() here. So if your goal is to prevent write() from happening, this won't work.

Regards,
Peter Hurley

drivers/usb/gadget/function/u_serial.c void gserial_disconnect(struct gserial *gser) · line 1268

Quoted 5 lines
drivers/usb/gadget/function/u_serial.c +1 -0
+		}
 	}
 	spin_unlock_irqrestore(&port->port_lock, flags);
 

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): [email protected]

#17 lore raw
Quoted 31 lines · Peter Hurley

Hi Kyösti,

Quoted 28 lines · Kyösti Mälkki

There are applications where it is desirable to not hangup ttyGS* when USB disconnect is detected. USB host side of communication may power-cycle periodically or there may be the actual need to physically disconnect and reconnect USB cable temporarily.

USB disconnects on serial gadget are comparable to loss of Carrier Detect of conventional UARTs. With the change, if ttyGS* has termios CLOCAL flag set, disconnect on USB does not hangup the TTY.

---
  drivers/usb/gadget/function/u_serial.c | 11 +++++++++--
  1 file changed, 9 insertions(+), 2 deletions(-)
drivers/usb/gadget/function/u_serial.c +3 -2
index 491082a..dabc165 100644
@@ -1253,9 +1253,16 @@ void gserial_disconnect(struct gserial *gser)
  	port->port_usb = NULL;
  	gser->ioport = NULL;
  	if (port->port.count > 0 || port->openclose) {
+		struct tty_struct *tty;
+
  		wake_up_interruptible(&port->drain_wait);
-		if (port->port.tty)
-			tty_hangup(port->port.tty);
+		tty = port->port.tty;
Quoted 9 lines

drivers/usb/gadget/function/u_serial.c void gserial_disconnect(struct gserial *gser) · line 1264

Quoted 5 lines
drivers/usb/gadget/function/u_serial.c +5 -0
+		if (tty) {
+			if (tty->termios.c_cflag & CLOCAL)
+				stop_tty(tty);
+			else
+				tty_hangup(tty);

It seems you missed my earlier email: what happens if you leave out the stop_tty() call here?

Hi Peter

I did not miss it. My intro to the patch series says I am not familiar with tty infra, so I did not realize the question was directed to me. Also my setup with serial gadget runs kernel 3.12.6 so I cannot really tell or test if tty changes since that would make my patch invalid or unnecessary.

In the previous change for gserial_disconnect(), where the tty_hangup() call was added, commit message described it fixes an endless loop on tty device read and select. Would we be back at that situation without call to stop_tty()?

Quoted 3 lines

I ask because the tty is still restartable from userspace after you stop_tty() here. So if your goal is to prevent write() from happening, this won't work.

There was no goal to prevent write() on the tty device, should there be? Or should it silently just drop any characters while in the disconnected state instead of buffering (some of) them?

Thanks,
KM

Quoted 10 lines

Regards,
Peter Hurley

drivers/usb/gadget/function/u_serial.c void gserial_disconnect(struct gserial *gser) · line 1268

Quoted 5 lines
drivers/usb/gadget/function/u_serial.c +1 -0
+		}
  	}
  	spin_unlock_irqrestore(&port->port_lock, flags);
 

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html