I. Structure of terminal Devices
The tty hierarchy chart in the Linux kernel contains the TTY core, tty line rules, and tty drivers. The core of tty is the abstraction of the entire tty device. It provides a unified interface for users. tty line rules are the formatting of transmitted data, while tty drivers are driver for tty devices, this is the driver for the device, which should be implemented by us. But we can see later that for the serial port driver Linux, We need to abstract and encapsulate the commonalities, so that the entire driver can be layered and simplified. Make the driver modifications only need to design the hardware differences of the device, but this makes the structure of the device driver more complex, too many process layers, it is difficult to understand.
The process of sending data from a tty device is as follows: the TTY core obtains the data to be sent to a tty device from a user, and the TTY core transmits the data to the TTY line rule driver, the data is then transmitted to the TTY driver, which converts the data into a format that can be sent to the hardware.
The process of receiving data is: Hand over the data received from the TTY hardware to the TTY driver, go to the TTY line rule driver, and then enter the TTY core, where it is obtained by a user. Although the data transmission between the TTY core and the TTY core usually goes through the TTY line procedure conversion, the TTY driver and the TTY core can also directly transmit data.
2. tty device driver structure
The figure shows the flow of the main source files and data related to TTY. Tty_io.c defines the file_operations struct common to TTY devices and implements the interface function tty_register_driver () to register tty devices. It will use the interface functions provided by fs/char_dev.c to register character devices, the tty driver corresponding to a specific device implements the member function in the tty_driver struct. At the same time, tty_io.c also provides the tty_register_ldisc () interface function for registering line rules. The n_tty.c file implements the members in the tty_disc struct. The main task of the driver of a specific tty device is to fill the members in the tty_driver struct and implement the member functions.