From 67d9868f4af14e6c7601479d34611ccdbb682684 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Wed, 25 Apr 2018 10:54:13 +0200
Subject: [PATCH] WIP termios/tty spring cleaning.

---
 lib/libc/gen/Makefile.inc       |   2 +-
 lib/libc/gen/Symbol.map         |   1 +
 lib/libc/gen/tcgetsid.3         |   5 +-
 lib/libc/gen/tcsetsid.3         |  92 +++++++
 lib/libc/gen/termios.c          |  15 +-
 share/man/man4/termios.4        | 429 +++++++++++++++++++++-----------
 sys/bus/u4b/serial/usb_serial.h |   1 +
 sys/dev/misc/dcons/dcons_os.c   |   1 +
 sys/dev/misc/nmdm/nmdm.c        |   1 +
 sys/dev/misc/syscons/syscons.c  |   1 +
 sys/dev/misc/syscons/sysmouse.c |   2 +
 sys/dev/serial/digi/digi.c      |   1 +
 sys/dev/serial/rp/rp.c          |   1 +
 sys/dev/serial/si/si_drv.c      |   1 +
 sys/dev/serial/sio/sio.c        |   1 +
 sys/kern/tty.c                  |   1 +
 sys/kern/tty_pty.c              |   2 +
 sys/kern/uipc_syscalls.c        |   1 +
 sys/sys/_termios.h              |  12 +-
 sys/sys/termios.h               |  25 +-
 sys/sys/tty.h                   |   6 +-
 sys/sys/ttydefaults.h           |  72 +++---
 usr.sbin/ppp/physical.c         |   1 +
 23 files changed, 464 insertions(+), 210 deletions(-)
 create mode 100644 lib/libc/gen/tcsetsid.3

diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc
index 3dc82ee029..76a2c1902f 100644
--- a/lib/libc/gen/Makefile.inc
+++ b/lib/libc/gen/Makefile.inc
@@ -78,7 +78,7 @@ MAN+=	alarm.3 arc4random.3 clock.3 \
 	scandir.3 setjmp.3 setmode.3 setproctitle.3 shm_open.3 \
 	siginterrupt.3 signal.3 sigsetops.3 sleep.3 stringlist.3 \
 	strtofflags.3 sysconf.3 sysctl.3 syslog.3 tcgetpgrp.3 tcgetsid.3 \
-	tcsendbreak.3 tcsetattr.3 tcsetpgrp.3 time.3 times.3 \
+	tcsendbreak.3 tcsetattr.3 tcsetpgrp.3 tcsetsid.3 time.3 times.3 \
 	ttyname.3 tzset.3 ualarm.3 \
 	ucontext.3 ulimit.3 uname.3 unvis.3 usleep.3 utime.3 \
 	valloc.3 vis.3 wordexp.3
diff --git a/lib/libc/gen/Symbol.map b/lib/libc/gen/Symbol.map
index 92af43237a..013305a7ed 100644
--- a/lib/libc/gen/Symbol.map
+++ b/lib/libc/gen/Symbol.map
@@ -487,6 +487,7 @@ DF504.0 {
     strsvis;
     strsvisx;
     svis;
+    tcsetsid;
     uid_from_user;
 };
 
diff --git a/lib/libc/gen/tcgetsid.3 b/lib/libc/gen/tcgetsid.3
index 92b6b333b1..30f75a03f1 100644
--- a/lib/libc/gen/tcgetsid.3
+++ b/lib/libc/gen/tcgetsid.3
@@ -24,7 +24,7 @@
 .\"
 .\" $FreeBSD: src/lib/libc/gen/tcgetsid.3,v 1.1 2008/04/15 08:33:32 davidxu Exp $
 .\"
-.Dd April 15, 2008
+.Dd April 25, 2018
 .Dt TCGETSID 3
 .Os
 .Sh NAME
@@ -63,7 +63,8 @@ is not the controlling terminal.
 .Sh SEE ALSO
 .Xr getsid 2 ,
 .Xr setsid 2 ,
-.Xr tcgetpgrp 3
+.Xr tcgetpgrp 3 ,
+.Xr tcsetsid 3
 .Sh STANDARDS
 The
 .Fn tcgetsid
diff --git a/lib/libc/gen/tcsetsid.3 b/lib/libc/gen/tcsetsid.3
new file mode 100644
index 0000000000..f7f3d26155
--- /dev/null
+++ b/lib/libc/gen/tcsetsid.3
@@ -0,0 +1,92 @@
+.\" Copyright (c) 2009 Ed Schouten <ed@FreeBSD.org>
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD: head/lib/libc/gen/tcsetsid.3 191882 2009-05-07 13:49:48Z ed $
+.\"
+.Dd April 25, 2018
+.Dt TCSETSID 3
+.Os
+.Sh NAME
+.Nm tcsetsid
+.Nd set session ID associated with a controlling terminal
+.Sh LIBRARY
+.Lb libc
+.Sh SYNOPSIS
+.In sys/types.h
+.In termios.h
+.Ft int
+.Fn tcsetsid "int fd" "pid_t pid"
+.Sh DESCRIPTION
+The
+.Fn tcsetsid
+function sets associates a session identified by
+.Fa pid
+with a controlling terminal specified by
+.Fa fd .
+.Pp
+This implementation only allows the controlling terminal to be changed
+by the session leader itself.
+This implies that
+.Fa pid
+always has to be equal to the process ID.
+.Pp
+It is unsupported to associate with a terminal that already has an
+associated session.
+Conversely, it is also unsupported to associate to a terminal when
+the session is already associated with a different terminal.
+.Sh ERRORS
+If an error occurs,
+.Fn tcsetsid
+returns -1 and the global variable
+.Va errno
+is set to indicate the error, as follows:
+.Bl -tag -width Er
+.It Bq Er EBADF
+The
+.Fa fd
+argument is not a valid file descriptor.
+.It Bq Er ENOTTY
+The file descriptor represented by
+.Fa fd
+is not a terminal.
+.It Bq Er EINVAL
+The
+.Fa pid
+argument is not equal to the session ID of the calling process.
+.It Bq Er EPERM
+The calling process is not a session leader.
+.It Bq Er EPERM
+The session already has an associated terminal or the terminal already
+has an associated session.
+.El
+.Sh SEE ALSO
+.Xr getsid 2 ,
+.Xr setsid 2 ,
+.Xr tcgetpgrp 3 ,
+.Xr tcgetsid 3
+.Sh HISTORY
+A
+.Fn tcsetsid
+function first appeared in QNX.
+It does not comply to any standard.
diff --git a/lib/libc/gen/termios.c b/lib/libc/gen/termios.c
index 24fa4b57a7..eaa26fb498 100644
--- a/lib/libc/gen/termios.c
+++ b/lib/libc/gen/termios.c
@@ -26,9 +26,8 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: src/lib/libc/gen/termios.c,v 1.9.2.1 2000/03/18 23:13:25 jasone Exp $
- *
  * @(#)termios.c	8.2 (Berkeley) 2/21/94
+ * $FreeBSD: head/lib/libc/gen/termios.c 276630 2015-01-03 18:38:46Z kib $
  */
 
 #include "namespace.h"
@@ -105,6 +104,18 @@ tcgetsid(int fd)
 	return ((pid_t)s);
 }
 
+int
+tcsetsid(int fd, pid_t pid)
+{
+
+	if (pid != getsid(0)) {
+		errno = EINVAL;
+		return (-1);
+	}
+
+	return (_ioctl(fd, TIOCSCTTY, NULL));
+}
+
 speed_t
 cfgetospeed(const struct termios *t)
 {
diff --git a/share/man/man4/termios.4 b/share/man/man4/termios.4
index 6d60388e4c..47148cfe87 100644
--- a/share/man/man4/termios.4
+++ b/share/man/man4/termios.4
@@ -26,9 +26,9 @@
 .\" SUCH DAMAGE.
 .\"
 .\"	@(#)termios.4	8.4 (Berkeley) 4/19/94
-.\" $FreeBSD: src/share/man/man4/termios.4,v 1.12.2.9 2001/12/17 11:30:12 ru Exp $
+.\" $FreeBSD: head/share/man/man4/termios.4 263142 2014-03-14 03:07:51Z eadler $
 .\"
-.Dd December 31, 2017
+.Dd April 25, 2018
 .Dt TERMIOS 4
 .Os
 .Sh NAME
@@ -41,7 +41,8 @@ This describes a general terminal line discipline that is
 supported on tty asynchronous communication ports.
 .Ss Opening a Terminal Device File
 When a terminal file is opened, it normally causes the process to wait
-until a connection is established.  For most hardware, the presence
+until a connection is established.
+For most hardware, the presence
 of a connection is indicated by the assertion of the hardware
 .Dv CARRIER
 line.
@@ -65,7 +66,8 @@ an application's standard input, output, and error files.
 .Ss Job Control in a Nutshell
 Every process is associated with a particular process group and session.
 The grouping is hierarchical: every member of a particular process group is a
-member of the same session.  This structuring is used in managing groups
+member of the same session.
+This structuring is used in managing groups
 of related processes for purposes of
 .\" .Gw "job control" ;
 .Em "job control" ;
@@ -73,12 +75,16 @@ that is, the
 ability from the keyboard (or from program control) to simultaneously
 stop or restart
 a complex command (a command composed of one or more related
-processes).  The grouping into process groups allows delivering
+processes).
+The grouping into process groups allows delivering
 of signals that stop or start the group as a whole, along with
 arbitrating which process group has access to the single controlling
-terminal.  The grouping at a higher layer into sessions is to restrict
+terminal.
+The grouping at a higher layer into sessions is to restrict
 the job control related signals and system calls to within processes
-resulting from a particular instance of a "login".  Typically, a session
+resulting from a particular instance of a
+.Dq login .
+Typically, a session
 is created when a user logs in, and the login terminal is setup
 to be the controlling terminal; all processes spawned from that
 login shell are in the same session, and inherit the controlling
@@ -87,23 +93,32 @@ terminal.
 A job control shell
 operating interactively (that is, reading commands from a terminal)
 normally groups related processes together by placing them into the
-same process group.  A set of processes in the same process group
-is collectively referred to as a "job". When the foreground process
+same process group.
+A set of processes in the same process group
+is collectively referred to as a
+.Dq job .
+When the foreground process
 group of the terminal is the same as the process group of a particular
-job, that job is said to be in the "foreground".  When the process
-group of the terminal is different from the process group of
+job, that job is said to be in the
+.Dq foreground .
+When the process group of the terminal is different from the process group of
 a job (but is still the controlling terminal), that job is said
-to be in the "background".  Normally the
+to be in the
+.Dq background .
+Normally the
 shell reads a command and starts the job that implements that
-command.  If the command is to be started in the foreground (typical), it
+command.
+If the command is to be started in the foreground (typical), it
 sets the process group of the terminal to the process group
 of the started job, waits for the job to complete, and then
 sets the process group of the terminal back to its own process
-group (it puts itself into the foreground).  If the job is to
+group (it puts itself into the foreground).
+If the job is to
 be started in the background (as denoted by the shell operator "&"),
-it never changes the process group of the terminal and doesn't
+it never changes the process group of the terminal and does not
 wait for the job to complete (that is, it immediately attempts to read the next
-command).  If the job is started in the foreground, the user may
+command).
+If the job is started in the foreground, the user may
 type a key (usually
 .Ql \&^Z )
 which generates the terminal stop signal
@@ -116,20 +131,26 @@ and for placing stopped or background jobs into the foreground.
 .Ss Orphaned Process Groups
 An orphaned process group is a process group that has no process
 whose parent is in a different process group, yet is in the same
-session.  Conceptually it means a process group that doesn't have
-a parent that could do anything if it were to be stopped.  For example,
+session.
+Conceptually it means a process group that does not have
+a parent that could do anything if it were to be stopped.
+For example,
 the initial login shell is typically in an orphaned process group.
 Orphaned process groups are immune to keyboard generated stop
 signals and job control signals resulting from reads or writes to the
 controlling terminal.
 .Ss The Controlling Terminal
-A terminal may belong to a process as its controlling terminal.  Each
+A terminal may belong to a process as its controlling terminal.
+Each
 process of a session that has a controlling terminal has the same
-controlling terminal.  A terminal may be the controlling terminal for at
-most one session.  The controlling terminal for a session is allocated by
+controlling terminal.
+A terminal may be the controlling terminal for at
+most one session.
+The controlling terminal for a session is allocated by
 the session leader by issuing the
 .Dv TIOCSCTTY
-ioctl.  A controlling terminal
+ioctl.
+A controlling terminal
 is never acquired by merely opening a terminal device file.
 When a controlling terminal becomes
 associated with a session, its foreground process group is set to
@@ -137,7 +158,8 @@ the process group of the session leader.
 .Pp
 The controlling terminal is inherited by a child process during a
 .Xr fork 2
-function call.  A process relinquishes its controlling terminal when it
+function call.
+A process relinquishes its controlling terminal when it
 creates a new session with the
 .Xr setsid 2
 function; other processes
@@ -150,7 +172,8 @@ have it open.
 .Pp
 When a controlling process terminates, the controlling terminal is
 disassociated from the current session, allowing it to be acquired by a
-new session leader.  Subsequent access to the terminal by other processes
+new session leader.
+Subsequent access to the terminal by other processes
 in the earlier session will be denied, with attempts to access the
 terminal treated as if modem disconnect had been sensed.
 .Ss Terminal Access Control
@@ -163,7 +186,7 @@ causes a
 signal to be sent to
 the process's group
 unless one of the
-following special cases apply:  If the reading process is ignoring or
+following special cases apply: if the reading process is ignoring or
 blocking the
 .Dv SIGTTIN
 signal, or if the process group of the reading
@@ -174,7 +197,8 @@ returns -1 with
 set to
 .Er EIO
 and no
-signal is sent.  The default action of the
+signal is sent.
+The default action of the
 .Dv SIGTTIN
 signal is to stop the
 process to which it is sent.
@@ -184,7 +208,7 @@ terminal, write operations are allowed.
 Attempts by a process in a background process group to write to its
 controlling terminal will cause the process group to be sent a
 .Dv SIGTTOU
-signal unless one of the following special cases apply:  If
+signal unless one of the following special cases apply: if
 .Dv TOSTOP
 is not
 set, or if
@@ -193,7 +217,8 @@ is set and the process is ignoring or blocking the
 .Dv SIGTTOU
 signal, the process is allowed to write to the terminal and the
 .Dv SIGTTOU
-signal is not sent.  If
+signal is not sent.
+If
 .Dv TOSTOP
 is set, and the process group of
 the writing process is orphaned, and the writing process is not ignoring
@@ -219,10 +244,12 @@ A terminal device associated with a terminal device file may operate in
 full-duplex mode, so that data may arrive even while output is occurring.
 Each terminal device file has associated with it an input queue, into
 which incoming data is stored by the system before being read by a
-process.  The system imposes a limit,
+process.
+The system imposes a limit,
 .Pf \&{ Dv MAX_INPUT Ns \&} ,
 on the number of
-bytes that may be stored in the input queue.  The behavior of the system
+bytes that may be stored in the input queue.
+The behavior of the system
 when this limit is exceeded depends on the setting of the
 .Dv IMAXBEL
 flag in the termios
@@ -232,8 +259,8 @@ is sent an
 .Tn ASCII
 .Dv BEL
 character each time a character is received
-while the input queue is full.  Otherwise, the input queue is flushed
-upon receiving the character.
+while the input queue is full.
+Otherwise, the input queue is flushed upon receiving the character.
 .Pp
 Two general kinds of input processing are available, determined by
 whether the terminal device file is in canonical mode or noncanonical
@@ -243,10 +270,11 @@ input characters are processed according to the
 .Fa c_iflag
 and
 .Fa c_lflag
-fields.  Such processing can include echoing, which
+fields.
+Such processing can include echoing, which
 in general means transmitting input characters immediately back to the
-terminal when they are received from the terminal.  This is useful for
-terminals that can operate in full-duplex mode.
+terminal when they are received from the terminal.
+This is useful for terminals that can operate in full-duplex mode.
 .Pp
 The manner in which data is provided to a process reading from a terminal
 device file is dependent on whether the terminal device file is in
@@ -261,7 +289,8 @@ or
 If the
 .Dv O_NONBLOCK
 flag is clear, then the read request is
-blocked until data is available or a signal has been received.  If the
+blocked until data is available or a signal has been received.
+If the
 .Dv O_NONBLOCK
 flag is set, then the read request is completed, without
 blocking, in one of three ways:
@@ -285,13 +314,15 @@ When data is available depends on whether the input processing mode is
 canonical or noncanonical.
 .Ss Canonical Mode Input Processing
 In canonical mode input processing, terminal input is processed in units
-of lines.  A line is delimited by a newline
+of lines.
+A line is delimited by a newline
 .Ql \&\en
 character, an end-of-file
 .Pq Dv EOF
 character, or an end-of-line
 .Pq Dv EOL
-character.  See the
+character.
+See the
 .Sx "Special Characters"
 section for
 more information on
@@ -300,8 +331,10 @@ and
 .Dv EOL .
 This means that a read request will
 not return until an entire line has been typed, or a signal has been
-received.  Also, no matter how many bytes are requested in the read call,
-at most one line is returned.  It is not, however, necessary to
+received.
+Also, no matter how many bytes are requested in the read call,
+at most one line is returned.
+It is not, however, necessary to
 read a whole line at once; any number of bytes, even one, may be
 requested in a read without losing information.
 .Pp
@@ -327,14 +360,18 @@ delimited by a newline
 .Dv EOF ,
 or
 .Dv EOL
-character.  This un-delimited
-data makes up the current line.  The
+character.
+This un-delimited
+data makes up the current line.
+The
 .Dv ERASE
 character deletes the last
-character in the current line, if there is any.  The
+character in the current line, if there is any.
+The
 .Dv KILL
 character
-deletes all data in the current line, if there is any.  The
+deletes all data in the current line, if there is any.
+The
 .Dv ERASE
 and
 .Dv KILL
@@ -347,7 +384,8 @@ characters themselves are not placed in the input
 queue.
 .Ss Noncanonical Mode Input Processing
 In noncanonical mode input processing, input bytes are not assembled into
-lines, and erase and kill processing does not occur.  The values of the
+lines, and erase and kill processing does not occur.
+The values of the
 .Dv VMIN
 and
 .Dv VTIME
@@ -364,12 +402,14 @@ function successfully returns.
 .Dv TIME
 is a timer of 0.1 second
 granularity that is used to time out bursty and short term data
-transmissions.  If
+transmissions.
+If
 .Dv MIN
 is greater than
 .Dv \&{ Dv MAX_INPUT Ns \&} ,
 the response to the
-request is undefined.  The four possible values for
+request is undefined.
+The four possible values for
 .Dv MIN
 and
 .Dv TIME
@@ -379,24 +419,30 @@ their interactions are described below.
 In this case
 .Dv TIME
 serves as an inter-byte timer and is activated after
-the first byte is received.  Since it is an inter-byte timer, it is reset
-after a byte is received.  The interaction between
+the first byte is received.
+Since it is an inter-byte timer, it is reset
+after a byte is received.
+The interaction between
 .Dv MIN
 and
 .Dv TIME
 is as
-follows:  as soon as one byte is received, the inter-byte timer is
-started.  If
+follows: as soon as one byte is received, the inter-byte timer is
+started.
+If
 .Dv MIN
 bytes are received before the inter-byte timer expires
 (remember that the timer is reset upon receipt of each byte), the read is
-satisfied.  If the timer expires before
+satisfied.
+If the timer expires before
 .Dv MIN
 bytes are received, the
-characters received to that point are returned to the user.  Note that if
+characters received to that point are returned to the user.
+Note that if
 .Dv TIME
 expires at least one byte is returned because the timer would
-not have been enabled unless a byte was received.  In this case
+not have been enabled unless a byte was received.
+In this case
 .Pf \&( Dv MIN
 > 0,
 .Dv TIME
@@ -405,8 +451,8 @@ not have been enabled unless a byte was received.  In this case
 and
 .Dv TIME
 mechanisms are
-activated by the receipt of the first byte, or a signal is received.  If
-data is in the buffer at the time of the
+activated by the receipt of the first byte, or a signal is received.
+If data is in the buffer at the time of the
 .Fn read ,
 the result is as
 if data had been received immediately after the
@@ -417,13 +463,14 @@ In this case, since the value of
 is zero, the timer plays no role
 and only
 .Dv MIN
-is significant.  A pending read is not satisfied until
+is significant.
+A pending read is not satisfied until
 .Dv MIN
 bytes are received (i.e., the pending read blocks until
 .Dv MIN
 bytes
-are received), or a signal is received.  A program that uses this case to
-read record-based terminal
+are received), or a signal is received.
+A program that uses this case to read record-based terminal
 .Dv I/O
 may block indefinitely in the read
 operation.
@@ -433,22 +480,27 @@ In this case, since
 = 0,
 .Dv TIME
 no longer represents an inter-byte
-timer.  It now serves as a read timer that is activated as soon as the
-read function is processed.  A read is satisfied as soon as a single
-byte is received or the read timer expires.  Note that in this case if
-the timer expires, no bytes are returned.  If the timer does not
+timer.
+It now serves as a read timer that is activated as soon as the
+read function is processed.
+A read is satisfied as soon as a single
+byte is received or the read timer expires.
+Note that in this case if the timer expires, no bytes are returned.
+If the timer does not
 expire, the only way the read can be satisfied is if a byte is received.
 In this case the read will not block indefinitely waiting for a byte; if
 no byte is received within
 .Dv TIME Ns *0.1
 seconds after the read is initiated,
-the read returns a value of zero, having read no data.  If data is
+the read returns a value of zero, having read no data.
+If data is
 in the buffer at the time of the read, the timer is started as if
 data had been received immediately after the read.
 .Ss Case D: MIN = 0, TIME = 0
 The minimum of either the number of bytes requested or the number of
 bytes currently available is returned without waiting for more
-bytes to be input.  If no characters are available, read returns a
+bytes to be input.
+If no characters are available, read returns a
 value of zero, having read no data.
 .Ss Writing Data and Output Processing
 When a process writes one or more bytes to a terminal device file, they
@@ -456,7 +508,8 @@ are processed according to the
 .Fa c_oflag
 field (see the
 .Sx "Output Modes"
-section).  The
+section).
+The
 implementation may provide a buffering mechanism; as such, when a call to
 .Fn write
 completes, all of the bytes written have been scheduled for
@@ -474,11 +527,13 @@ Special character on input and is recognized if the
 .Dv ISIG
 flag (see the
 .Sx "Local Modes"
-section) is enabled.  Generates a
+section) is enabled.
+Generates a
 .Dv SIGINT
 signal which is sent to all processes in the foreground
 process group for which the terminal is the controlling
-terminal.  If
+terminal.
+If
 .Dv ISIG
 is set, the
 .Dv INTR
@@ -487,11 +542,13 @@ discarded when processed.
 .It Dv QUIT
 Special character on input and is recognized if the
 .Dv ISIG
-flag is enabled.  Generates a
+flag is enabled.
+Generates a
 .Dv SIGQUIT
 signal which is
 sent to all processes in the foreground process group
-for which the terminal is the controlling terminal.  If
+for which the terminal is the controlling terminal.
+If
 .Dv ISIG
 is set, the
 .Dv QUIT
@@ -500,7 +557,8 @@ processed.
 .It Dv ERASE
 Special character on input and is recognized if the
 .Dv ICANON
-flag is set.  Erases the last character in the
+flag is set.
+Erases the last character in the
 current line; see
 .Sx "Canonical Mode Input Processing" .
 It does not erase beyond
@@ -509,7 +567,8 @@ the start of a line, as delimited by an
 .Dv EOF ,
 or
 .Dv EOL
-character.  If
+character.
+If
 .Dv ICANON
 is set, the
 .Dv ERASE
@@ -518,13 +577,15 @@ discarded when processed.
 .It Dv KILL
 Special character on input and is recognized if the
 .Dv ICANON
-flag is set.  Deletes the entire line, as
+flag is set.
+Deletes the entire line, as
 delimited by a
 .Dv NL ,
 .Dv EOF ,
 or
 .Dv EOL
-character.  If
+character.
+If
 .Dv ICANON
 is set, the
 .Dv KILL
@@ -532,17 +593,19 @@ character is discarded when processed.
 .It Dv EOF
 Special character on input and is recognized if the
 .Dv ICANON
-flag is set.  When received, all the bytes
+flag is set.
+When received, all the bytes
 waiting to be read are immediately passed to the
 process, without waiting for a newline, and the
 .Dv EOF
-is discarded.  Thus, if there are no bytes waiting (that
-is, the
+is discarded.
+Thus, if there are no bytes waiting (that is, the
 .Dv EOF
 occurred at the beginning of a line), a byte
 count of zero is returned from the
 .Fn read ,
-representing an end-of-file indication.  If
+representing an end-of-file indication.
+If
 .Dv ICANON
 is
 set, the
@@ -551,13 +614,14 @@ character is discarded when processed.
 .It Dv NL
 Special character on input and is recognized if the
 .Dv ICANON
-flag is set.  It is the line delimiter
+flag is set.
+It is the line delimiter
 .Ql \&\en .
 .It Dv EOL
 Special character on input and is recognized if the
 .Dv ICANON
-flag is set.  Is an additional line delimiter,
-like
+flag is set.
+Is an additional line delimiter, like
 .Dv NL .
 .It Dv SUSP
 If the
@@ -579,8 +643,9 @@ recognized if the
 (output control) or
 .Dv IXOFF
 (input
-control) flag is set.  Can be used to temporarily
-suspend output.  It is useful with fast terminals to
+control) flag is set.
+Can be used to temporarily suspend output.
+It is useful with fast terminals to
 prevent output from disappearing before it can be read.
 If
 .Dv IXON
@@ -595,10 +660,11 @@ recognized if the
 (output control) or
 .Dv IXOFF
 (input
-control) flag is set.  Can be used to resume output that
-has been suspended by a
+control) flag is set.
+Can be used to resume output that has been suspended by a
 .Dv STOP
-character.  If
+character.
+If
 .Dv IXON
 is set, the
 .Dv START
@@ -610,7 +676,8 @@ flag is set; it is the
 .Ql \&\er ,
 as denoted in the
 .Tn \&C
-Standard {2}.  When
+Standard {2}.
+When
 .Dv ICANON
 and
 .Dv ICRNL
@@ -632,17 +699,20 @@ termios.
 .It Dv EOL2
 Secondary
 .Dv EOL
-character.  Same function as
+character.
+Same function as
 .Dv EOL .
 .It Dv WERASE
 Special character on input and is recognized if the
 .Dv ICANON
-flag is set.  Erases the last word in the current
-line according to one of two algorithms.  If the
+flag is set.
+Erases the last word in the current line according to one of two algorithms.
+If the
 .Dv ALTWERASE
 flag is not set, first any preceding whitespace is
 erased, and then the maximal sequence of non-whitespace
-characters.  If
+characters.
+If
 .Dv ALTWERASE
 is set, first any preceding
 whitespace is erased, and then the maximal sequence
@@ -650,13 +720,13 @@ of alphabetic/underscores or non alphabetic/underscores.
 As a special case in this second algorithm, the first previous
 non-whitespace character is skipped in determining
 whether the preceding word is a sequence of
-alphabetic/underscores.  This sounds confusing but turns
-out to be quite practical.
+alphabetic/underscores.
+This sounds confusing but turns out to be quite practical.
 .It Dv REPRINT
 Special character on input and is recognized if the
 .Dv ICANON
-flag is set.  Causes the current input edit line
-to be retyped.
+flag is set.
+Causes the current input edit line to be retyped.
 .It Dv DSUSP
 Has similar actions to the
 .Dv SUSP
@@ -671,20 +741,22 @@ controlling terminal.
 .It Dv LNEXT
 Special character on input and is recognized if the
 .Dv IEXTEN
-flag is set.  Receipt of this character causes the next
-character to be taken literally.
+flag is set.
+Receipt of this character causes the next character to be taken literally.
 .It Dv DISCARD
 Special character on input and is recognized if the
 .Dv IEXTEN
-flag is set.  Receipt of this character toggles the flushing
-of terminal output.
+flag is set.
+Receipt of this character toggles the flushing of terminal output.
 .It Dv STATUS
 Special character on input and is recognized if the
 .Dv ICANON
-flag is set.  Receipt of this character causes a
+flag is set.
+Receipt of this character causes a
 .Dv SIGINFO
 signal to be sent to the foreground process group of the
-terminal.  Also, if the
+terminal.
+Also, if the
 .Dv NOKERNINFO
 flag is not set, it
 causes the kernel to write a status message to the terminal
@@ -723,12 +795,14 @@ field for
 the terminal, the
 .Dv SIGHUP
 signal is sent to the controlling
-process associated with the terminal.  Unless other arrangements have
+process associated with the terminal.
+Unless other arrangements have
 been made, this causes the controlling process to terminate.
 Any subsequent call to the
 .Fn read
 function returns the value zero,
-indicating end of file.  Thus, processes that read a terminal
+indicating end of file.
+Thus, processes that read a terminal
 file and test for end-of-file can terminate appropriately after a
 disconnect.
 .\" If the
@@ -751,7 +825,8 @@ until the device is closed.
 .Sh General Terminal Interface
 .Ss Closing a Terminal Device File
 The last process to close a terminal device file causes any output
-to be sent to the device and any input to be discarded.  Then, if
+to be sent to the device and any input to be discarded.
+Then, if
 .Dv HUPCL
 is set in the control modes, and the communications port supports a
 disconnect function, the terminal device performs a disconnect.
@@ -762,8 +837,8 @@ characteristics
 do so by using the termios structure as defined in the header
 .In termios.h .
 This structure contains minimally four scalar elements of bit flags
-and one array of special characters.  The scalar flag elements are
-named:
+and one array of special characters.
+The scalar flag elements are named:
 .Fa c_iflag ,
 .Fa c_oflag ,
 .Fa c_cflag ,
@@ -811,16 +886,19 @@ following masks:
 .Pp
 In the context of asynchronous serial data transmission, a break
 condition is defined as a sequence of zero-valued bits that continues for
-more than the time to send one byte.  The entire sequence of zero-valued
+more than the time to send one byte.
+The entire sequence of zero-valued
 bits is interpreted as a single break condition, even if it continues for
-a time equivalent to more than one byte.  In contexts other than
+a time equivalent to more than one byte.
+In contexts other than
 asynchronous serial data transmission the definition of a break condition
 is implementation defined.
 .Pp
 If
 .Dv IGNBRK
 is set, a break condition detected on input is ignored, that
-is, not put on the input queue and therefore not read by any process.  If
+is, not put on the input queue and therefore not read by any process.
+If
 .Dv IGNBRK
 is not set and
 .Dv BRKINT
@@ -829,7 +907,8 @@ input and output queues and if the terminal is the controlling terminal
 of a foreground process group, the break condition generates a
 single
 .Dv SIGINT
-signal to that foreground process group.  If neither
+signal to that foreground process group.
+If neither
 .Dv IGNBRK
 nor
 .Dv BRKINT
@@ -861,7 +940,8 @@ X, where
 .Ql \&\e0
 is a two-character
 flag preceding each sequence and X is the data of the character received
-in error.  To avoid ambiguity in this case, if
+in error.
+To avoid ambiguity in this case, if
 .Dv ISTRIP
 is not set, a valid
 character of
@@ -880,11 +960,13 @@ break) is given to the application as a single character
 .Pp
 If
 .Dv INPCK
-is set, input parity checking is enabled.  If
+is set, input parity checking is enabled.
+If
 .Dv INPCK
 is not set,
 input parity checking is disabled, allowing output parity generation
-without input parity errors.  Note that whether input parity checking is
+without input parity errors.
+Note that whether input parity checking is
 enabled or disabled is independent of whether parity detection is enabled
 or disabled (see
 .Sx "Control Modes" ) .
@@ -904,12 +986,14 @@ is set, a received
 .Dv NL
 character is translated into a
 .Dv CR
-character.  If
+character.
+If
 .Dv IGNCR
 is set, a received
 .Dv CR
 character is ignored (not
-read).  If
+read).
+If
 .Dv IGNCR
 is not set and
 .Dv ICRNL
@@ -922,7 +1006,8 @@ character.
 .Pp
 If
 .Dv IXON
-is set, start/stop output control is enabled.  A received
+is set, start/stop output control is enabled.
+A received
 .Dv STOP
 character suspends output and a received
 .Dv START
@@ -939,7 +1024,8 @@ is set,
 and
 .Dv STOP
 characters are not
-read, but merely perform flow control functions.  When
+read, but merely perform flow control functions.
+When
 .Dv IXON
 is not set,
 the
@@ -950,8 +1036,8 @@ characters are read.
 .Pp
 If
 .Dv IXOFF
-is set, start/stop input control is enabled.  The system shall
-transmit one or more
+is set, start/stop input control is enabled.
+The system shall transmit one or more
 .Dv STOP
 characters, which are intended to cause the
 terminal device to stop transmitting data, as needed to prevent the input
@@ -962,10 +1048,11 @@ and shall transmit one or more
 characters, which are
 intended to cause the terminal device to resume transmitting data, as
 soon as the device can continue transmitting data without risk of
-overflowing the input queue.  The precise conditions under which
+overflowing the input queue.
+The precise conditions under which
 .Dv STOP
 and
-START
+.Dv START
 characters are transmitted are implementation defined.
 .Pp
 If
@@ -985,7 +1072,7 @@ Values of the
 field describe the basic terminal output control,
 and are composed of the following masks:
 .Pp
-.Bl -tag -width OXTABS -offset indent -compact
+.Bl -tag -width ONOEOT -offset indent -compact
 .It Dv OPOST
 /* enable following output processing */
 .It Dv ONLCR
@@ -994,7 +1081,11 @@ and are composed of the following masks:
 */
 .It Dv OCRNL
 /* map CR to NL */
-.It Dv OXTABS
+.It Dv TABDLY
+/* tab delay mask */
+.It Dv TAB0
+/* no tab delay and expansion */
+.It Dv TAB3
 /* expand tabs to spaces */
 .It Dv ONOEOT
 /* discard
@@ -1020,8 +1111,20 @@ If
 .Dv OCRNL
 is set, carriage returns are translated to newlines.
 .Pp
+The
+.Dv TABDLY
+bits specify the tab delay.
+The
+.Fa c_oflag
+is masked with
+.Dv TABDLY
+and compared with the
+values
+.Dv TAB0
+or
+.Dv TAB3 .
 If
-.Dv OXTABS
+.Dv TAB3
 is set, tabs are expanded to the appropriate number of
 spaces (assuming 8 column tab stops).
 .Pp
@@ -1089,7 +1192,8 @@ flow control of output */
 The
 .Dv CSIZE
 bits specify the byte size in bits for both transmission and
-reception.  The
+reception.
+The
 .Fa c_cflag
 is masked with
 .Dv CSIZE
@@ -1100,17 +1204,18 @@ values
 .Dv CS7 ,
 or
 .Dv CS8 .
-This size does not include the parity bit, if any.  If
+This size does not include the parity bit, if any.
+If
 .Dv CSTOPB
-is set, two stop bits are used, otherwise one stop bit.  For example, at
-110 baud, two stop bits are normally used.
+is set, two stop bits are used, otherwise one stop bit.
+For example, at 110 baud, two stop bits are normally used.
 .Pp
 If
 .Dv CREAD
-is set, the receiver is enabled.  Otherwise, no character is
-received.
-Not all hardware supports this bit.  In fact, this flag
-is pretty silly and if it were not part of the
+is set, the receiver is enabled.
+Otherwise, no character is received.
+Not all hardware supports this bit.
+In fact, this flag is pretty silly and if it were not part of the
 .Nm
 specification
 it would be omitted.
@@ -1118,7 +1223,8 @@ it would be omitted.
 If
 .Dv PARENB
 is set, parity generation and detection are enabled and a parity
-bit is added to each character.  If parity is enabled,
+bit is added to each character.
+If parity is enabled,
 .Dv PARODD
 specifies
 odd parity if set, otherwise even parity is used.
@@ -1127,12 +1233,14 @@ If
 .Dv HUPCL
 is set, the modem control lines for the port are lowered
 when the last process with the port open closes the port or the process
-terminates.  The modem connection is broken.
+terminates.
+The modem connection is broken.
 .Pp
 If
 .Dv CLOCAL
 is set, a connection does not depend on the state of the modem
-status lines.  If
+status lines.
+If
 .Dv CLOCAL
 is clear, the modem status lines are
 monitored.
@@ -1140,7 +1248,8 @@ monitored.
 Under normal circumstances, a call to the
 .Fn open
 function waits for
-the modem connection to complete.  However, if the
+the modem connection to complete.
+However, if the
 .Dv O_NONBLOCK
 flag is set
 or if
@@ -1226,7 +1335,8 @@ and
 .Pp
 If
 .Dv ECHO
-is set, input characters are echoed back to the terminal.  If
+is set, input characters are echoed back to the terminal.
+If
 .Dv ECHO
 is not set, input characters are not echoed.
 .Pp
@@ -1238,7 +1348,8 @@ are set, the
 .Dv ERASE
 character causes the terminal
 to erase the last character in the current line from the display, if
-possible.  If there is no character to erase, an implementation may echo
+possible.
+If there is no character to erase, an implementation may echo
 an indication that this was the case or do nothing.
 .Pp
 If
@@ -1300,7 +1411,8 @@ is not set.
 .Pp
 If
 .Dv ICANON
-is set, canonical processing is enabled.  This enables the
+is set, canonical processing is enabled.
+This enables the
 erase and kill edit functions, and the assembly of input characters into
 lines delimited by
 .Dv NL ,
@@ -1313,13 +1425,16 @@ as described in
 If
 .Dv ICANON
 is not set, read requests are satisfied directly from the input
-queue.  A read is not satisfied until at least
+queue.
+A read is not satisfied until at least
 .Dv MIN
 bytes have been
 received or the timeout value
 .Dv TIME
-expired between bytes.  The time value
-represents tenths of seconds.  See
+expired between bytes.
+The time value
+represents tenths of seconds.
+See
 .Sx "Noncanonical Mode Input Processing"
 for more details.
 .Pp
@@ -1331,12 +1446,15 @@ control characters
 .Dv QUIT ,
 and
 .Dv SUSP
-(job control only).  If an input
+(job control only).
+If an input
 character matches one of these control characters, the function
-associated with that character is performed.  If
+associated with that character is performed.
+If
 .Dv ISIG
 is not set, no
-checking is done.  Thus these special input functions are possible only
+checking is done.
+Thus these special input functions are possible only
 if
 .Dv ISIG
 is set.
@@ -1344,7 +1462,8 @@ is set.
 If
 .Dv IEXTEN
 is set, implementation-defined functions are recognized
-from the input data.  How
+from the input data.
+How
 .Dv IEXTEN
 being set
 interacts with
@@ -1382,9 +1501,11 @@ is set, the signal
 .Dv SIGTTOU
 is sent to the process group of a process that tries to write to
 its controlling terminal if it is not in the foreground process group for
-that terminal.  This signal, by default, stops the members of the process
-group.  Otherwise, the output generated by that process is output to the
-current output stream.  Processes that are blocking or ignoring
+that terminal.
+This signal, by default, stops the members of the process group.
+Otherwise, the output generated by that process is output to the
+current output stream.
+Processes that are blocking or ignoring
 .Dv SIGTTOU
 signals are excepted and allowed to produce output and the
 .Dv SIGTTOU
@@ -1402,7 +1523,8 @@ characters (see
 The special control characters values are defined by the array
 .Fa c_cc .
 This table lists the array index, the corresponding special character,
-and the system default value.  For an accurate list of
+and the system default value.
+For an accurate list of
 the system defaults, consult the header file
 .In sys/ttydefaults.h .
 .Pp
@@ -1454,3 +1576,10 @@ after
 is set according to
 the values in the header
 .In sys/ttydefaults.h .
+.Sh SEE ALSO
+.Xr stty 1 ,
+.Xr tcgetsid 3 ,
+.Xr tcsendbreak 3 ,
+.Xr tcsetattr 3 ,
+.Xr tcsetsid 3 ,
+.Xr tty 4
diff --git a/sys/bus/u4b/serial/usb_serial.h b/sys/bus/u4b/serial/usb_serial.h
index b4abfc7108..db83a731ee 100644
--- a/sys/bus/u4b/serial/usb_serial.h
+++ b/sys/bus/u4b/serial/usb_serial.h
@@ -61,6 +61,7 @@
 #define	_USB_SERIAL_H_
 
 #include <sys/tty.h>
+#include <sys/ttydefaults.h>
 #include <sys/fcntl.h>
 #include <sys/sysctl.h>
 #include <sys/timepps.h>
diff --git a/sys/dev/misc/dcons/dcons_os.c b/sys/dev/misc/dcons/dcons_os.c
index 2065a0e87a..73a6aa8a75 100644
--- a/sys/dev/misc/dcons/dcons_os.c
+++ b/sys/dev/misc/dcons/dcons_os.c
@@ -49,6 +49,7 @@
 #include <sys/cons.h>
 #include <sys/consio.h>
 #include <sys/tty.h>
+#include <sys/ttydefaults.h>
 #include <sys/malloc.h>
 #include <sys/proc.h>
 #include <sys/priv.h>
diff --git a/sys/dev/misc/nmdm/nmdm.c b/sys/dev/misc/nmdm/nmdm.c
index cf361a35ca..5d2b23fc9c 100644
--- a/sys/dev/misc/nmdm/nmdm.c
+++ b/sys/dev/misc/nmdm/nmdm.c
@@ -45,6 +45,7 @@
 #include <sys/priv.h>
 #include <sys/thread2.h>
 #include <sys/tty.h>
+#include <sys/ttydefaults.h>
 #include <sys/conf.h>
 #include <sys/fcntl.h>
 #include <sys/kernel.h>
diff --git a/sys/dev/misc/syscons/syscons.c b/sys/dev/misc/syscons/syscons.c
index 62f5f2a14b..8b83574858 100644
--- a/sys/dev/misc/syscons/syscons.c
+++ b/sys/dev/misc/syscons/syscons.c
@@ -48,6 +48,7 @@
 #include <sys/sysctl.h>
 #include <sys/taskqueue.h>
 #include <sys/tty.h>
+#include <sys/ttydefaults.h>
 #include <sys/kernel.h>
 #include <sys/cons.h>
 #include <sys/random.h>
diff --git a/sys/dev/misc/syscons/sysmouse.c b/sys/dev/misc/syscons/sysmouse.c
index 99e3e1153f..d22076bb32 100644
--- a/sys/dev/misc/syscons/sysmouse.c
+++ b/sys/dev/misc/syscons/sysmouse.c
@@ -41,6 +41,8 @@
 #include <sys/uio.h>
 #include <sys/priv.h>
 #include <sys/vnode.h>
+#include <sys/tty.h>
+#include <sys/ttydefaults.h>
 #include <sys/kernel.h>
 #include <sys/thread2.h>
 #include <sys/signalvar.h>
diff --git a/sys/dev/serial/digi/digi.c b/sys/dev/serial/digi/digi.c
index a99bddbcf3..5a6611e1c3 100644
--- a/sys/dev/serial/digi/digi.c
+++ b/sys/dev/serial/digi/digi.c
@@ -47,6 +47,7 @@
 #include <sys/mbuf.h>
 #include <sys/malloc.h>
 #include <sys/tty.h>
+#include <sys/ttydefaults.h>
 #include <sys/syslog.h>
 #include <sys/fcntl.h>
 #include <sys/bus.h>
diff --git a/sys/dev/serial/rp/rp.c b/sys/dev/serial/rp/rp.c
index 252ade41d5..de65f91330 100644
--- a/sys/dev/serial/rp/rp.c
+++ b/sys/dev/serial/rp/rp.c
@@ -43,6 +43,7 @@
 #include <sys/fcntl.h>
 #include <sys/malloc.h>
 #include <sys/tty.h>
+#include <sys/ttydefaults.h>
 #include <sys/proc.h>
 #include <sys/priv.h>
 #include <sys/dkstat.h>
diff --git a/sys/dev/serial/si/si_drv.c b/sys/dev/serial/si/si_drv.c
index 90c59ecfdd..e4b206bc21 100644
--- a/sys/dev/serial/si/si_drv.c
+++ b/sys/dev/serial/si/si_drv.c
@@ -40,6 +40,7 @@
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/tty.h>
+#include <sys/ttydefaults.h>
 #include <sys/proc.h>
 #include <sys/priv.h>
 #include <sys/conf.h>
diff --git a/sys/dev/serial/sio/sio.c b/sys/dev/serial/sio/sio.c
index 8ad637bcf6..0199c15307 100644
--- a/sys/dev/serial/sio/sio.c
+++ b/sys/dev/serial/sio/sio.c
@@ -53,6 +53,7 @@
 #include <sys/reboot.h>
 #include <sys/malloc.h>
 #include <sys/tty.h>
+#include <sys/ttydefaults.h>
 #include <sys/proc.h>
 #include <sys/priv.h>
 #include <sys/module.h>
diff --git a/sys/kern/tty.c b/sys/kern/tty.c
index d6f1f78f56..1299ebe115 100644
--- a/sys/kern/tty.c
+++ b/sys/kern/tty.c
@@ -79,6 +79,7 @@
 #include <sys/proc.h>
 #include <sys/priv.h>
 #define	TTYDEFCHARS
+#include <sys/termios.h>
 #include <sys/tty.h>
 #include <sys/clist.h>
 #undef	TTYDEFCHARS
diff --git a/sys/kern/tty_pty.c b/sys/kern/tty_pty.c
index 0f6b3839d3..9148def97c 100644
--- a/sys/kern/tty_pty.c
+++ b/sys/kern/tty_pty.c
@@ -49,7 +49,9 @@
 #include <sys/systm.h>
 #include <sys/proc.h>
 #include <sys/priv.h>
+#include <sys/termios.h>
 #include <sys/tty.h>
+#include <sys/ttydefaults.h>
 #include <sys/conf.h>
 #include <sys/fcntl.h>
 #include <sys/kernel.h>
diff --git a/sys/kern/uipc_syscalls.c b/sys/kern/uipc_syscalls.c
index 5d30492ba9..5645a3dda9 100644
--- a/sys/kern/uipc_syscalls.c
+++ b/sys/kern/uipc_syscalls.c
@@ -1461,6 +1461,7 @@ getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len)
 	} else {
 #if BYTE_ORDER != BIG_ENDIAN
 		/*
+		 * XXX
 		 * The bind(), connect(), and sendto() syscalls were not
 		 * versioned for COMPAT_43.  Thus, this check must stay.
 		 */
diff --git a/sys/sys/_termios.h b/sys/sys/_termios.h
index 582e96562d..c0ab7928cb 100644
--- a/sys/sys/_termios.h
+++ b/sys/sys/_termios.h
@@ -102,12 +102,9 @@
 #define	OPOST		0x00000001	/* enable following output processing */
 #if __XSI_VISIBLE
 #define	ONLCR		0x00000002	/* map NL to CR-NL (ala CRMOD) */
-#endif
-#if __BSD_VISIBLE
-#define	OXTABS		0x00000004	/* expand tabs to spaces */
-#define	ONOEOT		0x00000008	/* discard EOT's (^D) on output) */
-#endif  /*__BSD_VISIBLE */
-#if __XSI_VISIBLE
+#define	TABDLY		0x00000004	/* tab delay mask */
+#define	    TAB0	    0x00000000	    /* no tab delay and expansion */
+#define	    TAB3	    0x00000004	    /* expand tabs to spaces */
 #define	OCRNL		0x00000010	/* map CR to NL on output */
 #define	ONOCR		0x00000020	/* no CR output at column 0 */
 #define	ONLRET		0x00000040	/* NL performs CR function */
@@ -137,7 +134,6 @@
 #define	CDTR_IFLOW	0x00040000	/* DTR flow control of input */
 #define	CDSR_OFLOW	0x00080000	/* DSR flow control of output */
 #define	CCAR_OFLOW	0x00100000	/* DCD flow control of output */
-#define	MDMBUF		0x00100000	/* old name for CCAR_OFLOW */
 #endif
 
 
@@ -204,6 +200,8 @@
 #define	B76800	76800
 #define	B115200	115200
 #define	B230400	230400
+#define	B460800	460800
+#define	B921600	921600
 #define	EXTA	19200
 #define	EXTB	38400
 #endif  /* __BSD_VISIBLE */
diff --git a/sys/sys/termios.h b/sys/sys/termios.h
index fa0e17f9de..79e3ae32a1 100644
--- a/sys/sys/termios.h
+++ b/sys/sys/termios.h
@@ -1,4 +1,4 @@
-/*
+/*-
  * Copyright (c) 1988, 1989, 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
  *
@@ -27,7 +27,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)termios.h	8.3 (Berkeley) 3/28/94
- * $FreeBSD: src/sys/sys/termios.h,v 1.13.2.1 2001/03/06 06:31:44 jhb Exp $
+ * $FreeBSD: head/include/termios.h 265878 2014-05-11 13:48:21Z jilles $
  */
 
 #ifndef _SYS_TERMIOS_H_
@@ -37,12 +37,16 @@
 #include <sys/_termios.h>
 /* Needed by tcgetsid(3). */
 #include <sys/stdint.h>
+
 #ifndef _PID_T_DECLARED
-typedef	__pid_t pid_t;
+typedef	__pid_t		pid_t;
 #define	_PID_T_DECLARED
 #endif
 
 #if __BSD_VISIBLE
+#define	OXTABS		TAB3
+#define	ONOEOT		0x00000008	/* discard EOT's (^D) on output) */
+#define	MDMBUF		CCAR_OFLOW
 #define	CCEQ(val, c)	((c) == (val) ? (val) != _POSIX_VDISABLE : 0)
 #endif
 
@@ -84,24 +88,13 @@ pid_t	tcgetsid(int);
 void	cfmakeraw(struct termios *);
 void	cfmakesane(struct termios *);
 int	cfsetspeed(struct termios *, speed_t);
+int	tcsetsid(int, pid_t);
 #endif /* __BSD_VISIBLE */
 __END_DECLS
 
-#if __BSD_VISIBLE
-
-/*
- * Include tty ioctl's that aren't just for backwards compatibility
- * with the old tty driver.  These ioctl definitions were previously
- * in <sys/ioctl.h>.
- */
-#include <sys/ttycom.h>
-#endif
-
-/*
- * END OF PROTECTED INCLUDE.
- */
 #endif /* !_SYS_TERMIOS_H_ */
 
 #if __BSD_VISIBLE
+#include <sys/ttycom.h>
 #include <sys/ttydefaults.h>
 #endif
diff --git a/sys/sys/tty.h b/sys/sys/tty.h
index 2cfe20b198..a9f6b1fbb5 100644
--- a/sys/sys/tty.h
+++ b/sys/sys/tty.h
@@ -33,21 +33,19 @@
  *
  *	@(#)tty.h	8.6 (Berkeley) 1/21/94
  * $FreeBSD: src/sys/sys/tty.h,v 1.53.2.1 2001/02/26 04:23:21 jlemon Exp $
- * $DragonFly: src/sys/sys/tty.h,v 1.12 2006/09/10 01:26:40 dillon Exp $
  */
 
 #ifndef _SYS_TTY_H_
 #define	_SYS_TTY_H_
 
-#ifndef _SYS_TERMIOS_H_
-#include <sys/termios.h>
-#endif
+#include <sys/_termios.h>
 #ifndef _SYS_EVENT_H_
 #include <sys/event.h>
 #endif
 #ifdef _KERNEL
 #include <sys/device.h>
 #endif
+#include <sys/ttycom.h>
 
 /*
  * Clists are character lists, which is a variable length linked list
diff --git a/sys/sys/ttydefaults.h b/sys/sys/ttydefaults.h
index 7cad640898..2f8d514c25 100644
--- a/sys/sys/ttydefaults.h
+++ b/sys/sys/ttydefaults.h
@@ -32,8 +32,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)ttydefaults.h	8.4 (Berkeley) 1/21/94
- * $FreeBSD: src/sys/sys/ttydefaults.h,v 1.13.2.1 2001/03/06 03:37:08 jkh Exp $
- * $DragonFly: src/sys/sys/ttydefaults.h,v 1.3 2003/10/13 21:08:50 dillon Exp $
+ * $FreeBSD: head/sys/sys/ttydefaults.h 249311 2013-04-09 16:16:34Z ed $
  */
 
 /*
@@ -46,38 +45,48 @@
  * Defaults on "first" open.
  */
 #define	TTYDEF_IFLAG	(BRKINT	| ICRNL	| IMAXBEL | IXON | IXANY)
-#define TTYDEF_OFLAG	(OPOST | ONLCR)
-#define TTYDEF_LFLAG	(ECHO | ICANON | ISIG | IEXTEN | ECHOE|ECHOKE|ECHOCTL)
+#define	TTYDEF_OFLAG	(OPOST | ONLCR)
+#define	TTYDEF_LFLAG_NOECHO (ICANON | ISIG | IEXTEN)
+#define	TTYDEF_LFLAG_ECHO (TTYDEF_LFLAG_NOECHO \
+	| ECHO | ECHOE | ECHOKE | ECHOCTL)
+#define	TTYDEF_LFLAG TTYDEF_LFLAG_ECHO
 #define	TTYDEF_CFLAG	(CREAD | CS8 | HUPCL)
-#define TTYDEF_SPEED	(B9600)
+#define	TTYDEF_SPEED	(B9600)
 
 /*
  * Control Character Defaults
  */
-#define CTRL(x)	(x&037)
-#define CCHECKPT	CTRL('e')
-#define	CEOF		CTRL('d')
+/*
+ * XXX: A lot of code uses lowercase characters, but control-character
+ * conversion is actually only valid when applied to uppercase
+ * characters. We just treat lowercase characters as if they were
+ * inserted as uppercase.
+ */
+#define	CTRL(x) ((x) >= 'a' && (x) <= 'z' ? \
+	((x) - 'a' + 1) : (((x) - 'A' + 1) & 0x7f))
+#define	CCHECKPT	CTRL('E')
+#define	CEOF		CTRL('D')
 #define	CEOL		0xff		/* XXX avoid _POSIX_VDISABLE */
-#define	CERASE		0177
-#define	CERASE2		CTRL('h')
-#define	CINTR		CTRL('c')
-#define	CSTATUS		CTRL('t')
-#define	CKILL		CTRL('u')
+#define	CERASE		CTRL('?')
+#define	CERASE2		CTRL('H')
+#define	CINTR		CTRL('C')
+#define	CSTATUS		CTRL('T')
+#define	CKILL		CTRL('U')
 #define	CMIN		1
-#define	CQUIT		034		/* FS, ^\ */
-#define	CSUSP		CTRL('z')
+#define	CQUIT		CTRL('\\')
+#define	CSUSP		CTRL('Z')
 #define	CTIME		0
-#define	CDSUSP		CTRL('y')
-#define	CSTART		CTRL('q')
-#define	CSTOP		CTRL('s')
-#define	CLNEXT		CTRL('v')
-#define	CDISCARD 	CTRL('o')
-#define	CWERASE 	CTRL('w')
-#define	CREPRINT 	CTRL('r')
+#define	CDSUSP		CTRL('Y')
+#define	CSTART		CTRL('Q')
+#define	CSTOP		CTRL('S')
+#define	CLNEXT		CTRL('V')
+#define	CDISCARD	CTRL('O')
+#define	CWERASE		CTRL('W')
+#define	CREPRINT	CTRL('R')
 #define	CEOT		CEOF
 /* compat */
 #define	CBRK		CEOL
-#define CRPRNT		CREPRINT
+#define	CRPRNT		CREPRINT
 #define	CFLUSH		CDISCARD
 
 /* PROTECTED INCLUSION ENDS HERE */
@@ -87,10 +96,17 @@
  * #define TTYDEFCHARS to include an array of default control characters.
  */
 #ifdef TTYDEFCHARS
-static cc_t	ttydefchars[NCCS] = {
-	CEOF,	CEOL,	CEOL,	CERASE, CWERASE, CKILL, CREPRINT,
-	CERASE2, CINTR,	CQUIT,	CSUSP,	CDSUSP,	CSTART,	CSTOP,	CLNEXT,
-	CDISCARD, CMIN,	CTIME,  CSTATUS, _POSIX_VDISABLE
+
+#include <sys/cdefs.h>
+#include <sys/_termios.h>
+
+static const cc_t ttydefchars[] = {
+	CEOF, CEOL, CEOL, CERASE, CWERASE, CKILL, CREPRINT, CERASE2, CINTR,
+	CQUIT, CSUSP, CDSUSP, CSTART, CSTOP, CLNEXT, CDISCARD, CMIN, CTIME,
+	CSTATUS, CCHECKPT
 };
+_Static_assert(sizeof(ttydefchars) / sizeof(cc_t) == NCCS,
+    "Size of ttydefchars does not match NCCS");
+
 #undef TTYDEFCHARS
-#endif
+#endif /* TTYDEFCHARS */
diff --git a/usr.sbin/ppp/physical.c b/usr.sbin/ppp/physical.c
index 7baaf1c0b8..9c1260e713 100644
--- a/usr.sbin/ppp/physical.c
+++ b/usr.sbin/ppp/physical.c
@@ -38,6 +38,7 @@
 #include <string.h>
 #include <sys/tty.h>	/* TIOCOUTQ */
 #include <sys/uio.h>
+#include <termios.h>
 #include <time.h>
 #include <unistd.h>
 #include <utmp.h>
-- 
2.17.0

