Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

TTY: fix DTR not being dropped on hang up

Move HUPCL handling to port shutdown so that DTR is dropped also on hang
up (tty_port_close is a noop for hung-up ports).

Also do not try to drop DTR for uninitialised ports where it has never
been raised (e.g. after a failed open).

Note that this is also the current behaviour of serial-core.

Nine drivers currently call tty_port_close_start directly (rather than
through tty_port_close) and seven of them lower DTR as part of their
close (if the port has been initialised). Fixup the remaining two
drivers so that it continues to be lowered also on normal (non-HUP)
close. [ Note that most of those other seven drivers did not expect DTR
to have been dropped by tty_port_close_start in the first place. ]

Signed-off-by: Johan Hovold <jhovold@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Johan Hovold and committed by
Greg Kroah-Hartman
957dacae e584a02c

+23 -12
+4
drivers/tty/mxser.c
··· 1084 1084 mutex_lock(&port->mutex); 1085 1085 mxser_close_port(port); 1086 1086 mxser_flush_buffer(tty); 1087 + if (test_bit(ASYNCB_INITIALIZED, &port->flags)) { 1088 + if (C_HUPCL(tty)) 1089 + tty_port_lower_dtr_rts(port); 1090 + } 1087 1091 mxser_shutdown_port(port); 1088 1092 clear_bit(ASYNCB_INITIALIZED, &port->flags); 1089 1093 mutex_unlock(&port->mutex);
+4
drivers/tty/n_gsm.c
··· 2964 2964 if (tty_port_close_start(&dlci->port, tty, filp) == 0) 2965 2965 goto out; 2966 2966 gsm_dlci_begin_close(dlci); 2967 + if (test_bit(ASYNCB_INITIALIZED, &dlci->port.flags)) { 2968 + if (C_HUPCL(tty)) 2969 + tty_port_lower_dtr_rts(&dlci->port); 2970 + } 2967 2971 tty_port_close_end(&dlci->port, tty); 2968 2972 tty_port_tty_set(&dlci->port, NULL); 2969 2973 out:
+15 -12
drivers/tty/tty_port.c
··· 196 196 } 197 197 EXPORT_SYMBOL(tty_port_tty_set); 198 198 199 - static void tty_port_shutdown(struct tty_port *port) 199 + static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty) 200 200 { 201 201 mutex_lock(&port->mutex); 202 202 if (port->console) 203 203 goto out; 204 204 205 205 if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) { 206 + /* 207 + * Drop DTR/RTS if HUPCL is set. This causes any attached 208 + * modem to hang up the line. 209 + */ 210 + if (tty && C_HUPCL(tty)) 211 + tty_port_lower_dtr_rts(port); 212 + 206 213 if (port->ops->shutdown) 207 214 port->ops->shutdown(port); 208 215 } ··· 227 220 228 221 void tty_port_hangup(struct tty_port *port) 229 222 { 223 + struct tty_struct *tty; 230 224 unsigned long flags; 231 225 232 226 spin_lock_irqsave(&port->lock, flags); 233 227 port->count = 0; 234 228 port->flags &= ~ASYNC_NORMAL_ACTIVE; 235 - if (port->tty) { 236 - set_bit(TTY_IO_ERROR, &port->tty->flags); 237 - tty_kref_put(port->tty); 238 - } 229 + tty = port->tty; 230 + if (tty) 231 + set_bit(TTY_IO_ERROR, &tty->flags); 239 232 port->tty = NULL; 240 233 spin_unlock_irqrestore(&port->lock, flags); 241 - tty_port_shutdown(port); 234 + tty_port_shutdown(port, tty); 235 + tty_kref_put(tty); 242 236 wake_up_interruptible(&port->open_wait); 243 237 wake_up_interruptible(&port->delta_msr_wait); 244 238 } ··· 493 485 /* Flush the ldisc buffering */ 494 486 tty_ldisc_flush(tty); 495 487 496 - /* Drop DTR/RTS if HUPCL is set. This causes any attached modem to 497 - hang up the line */ 498 - if (tty->termios.c_cflag & HUPCL) 499 - tty_port_lower_dtr_rts(port); 500 - 501 488 /* Don't call port->drop for the last reference. Callers will want 502 489 to drop the last active reference in ->shutdown() or the tty 503 490 shutdown path */ ··· 527 524 { 528 525 if (tty_port_close_start(port, tty, filp) == 0) 529 526 return; 530 - tty_port_shutdown(port); 527 + tty_port_shutdown(port, tty); 531 528 set_bit(TTY_IO_ERROR, &tty->flags); 532 529 tty_port_close_end(port, tty); 533 530 tty_port_tty_set(port, NULL);