Line discipline (ldisc) is a software driver layer for Linux and Unix-like system terminal subsystems. The terminal subsystem can be divided into three layers from top to bottom:
- The top layer tty core driver layer provides the character device interface (because all terminal devices are character devices );
- The underlying layer is the TTY driver layer used to communicate with the hardware, so that tty_operations can be called by the TTY core and ldisc layers;
- The middle layer line discipline implements the input/output data processing policy of the terminal.
Line discipline binds tty core and tty driver. Policy separation eliminates the need for tty core and tty driver to focus on data syntax processing. tty driver can be reused by the same hardware, you only need to change the line discipline.
The default line for Linux terminal devices is n_tty. It receives data from tty driver and applications and processes the data according to the terminal settings. For input data, it processes special interrupt characters (such as control-C), delete characters (backspace, delete), and so on; for output data, it replaces lf characters with Cr/LF sequences. The n_tty line procedure is used when the UART port is used as a common serial port.
When the UART port device uses an Internet dialing connection for the serial modem, the PPP line procedure is used to process data. The PPP ldisc assembles the serial port data from the UART core into the PPP input packet, then it is distributed to the network protocol stack. PPP ldisc sends data packages sent from the network protocol stack to the UART port.
When I first read the Linux tty terminal system code, I was very confused about ldisc. Unless I want to understand the details of line discipline syntax processing, I can skip most of the ldisc code.
Ldisc works between tty core and tty driver. ldisc must provide the following interface functions for both:
Tty core interface:
Open ()
Close ()
Hangup ()
Write ()
Flush_buffer ()
Chars_in_buffer ()
Set_termios ()
Read ()
Poll ()
IOCTL ()
Compat_ioctl ()
Tty driver interface:
Receive_buf ()
Write_wakeup ()
Dcd_change ()