[Linux Basics] - Linux TTY Framework (2)_Software Architecture

I. Introduction

According to the introduction of " Linux TTY Framework (1)_Basic Concepts ", in the Linux kernel, TTY is the abbreviation for various types of terminals. In order to simplify the use of terminals and the writing of terminal drivers, Linux kernel abstracts the TTY framework; on the top, it provides a unified interface for using the terminal; on the bottom, it provides a unified framework for writing terminal drivers (such as serial driver) .

This article is the second article of Linux Framework analysis, from the perspective of the overall architecture, introduces the Linux TTY framework in order to decompose relatively independent sub-modules for subsequent analysis.

Second, the software architecture

The Linux kernel TTY Framework is located in the "drivers/tty" directory, and its software architecture is shown in Picture 1 below:

Similar to other Linux frameworks, TTY Framework shields TTY-related technical details through TTY core, provides a unified interface to applications in the form of character devices, and provides a framework for writing drivers in the form of TTY device/TTY driver. For details, please refer to the following chapters.

2.1、TTY core

TTY core is the core logic of TTY Framework. Its functions include:

1) Provide user space with an interface to access TTY devices in the form of character devices , for example:

Device number (primary, secondary) Character device Remarks
(5, 0) /dev/tty Controlling Terminal
(5, 1) /dev/console Console Terminal
(4, 0) /dev/vc /0 or /dev/tty0 Virtual Terminal
(4, 1) /dev/vc/1 or /dev/tty1 Same as above
...……
(x, x) /dev/ttyS0 Serial terminal (name and device number are determined by The driver decides by itself)
………
(x, x) /dev/ttyUSB0 USB to serial port terminal
…                         …                                             …

Note 1: The concepts of control terminal, console terminal, virtual terminal, etc. are relatively abstract, and I will introduce them in detail in subsequent articles .

2) Abstract the TTY device through the struct device structure in the device model, and abstract the device driver through struct tty_driver, and provide the corresponding register interface. TTY driver writing is simplified to fill and register the corresponding struct tty_driver structure .

Note 2: TTY Framework weakens the concept of TTY devices (marked with a dashed box in picture 1). Normally, when registering TTY drivers, TTY devices can be automatically assigned and registered .

3) Use data structures such as struct tty_struct and struct tty_port to logically abstract the TTY device and its "components" to realize hardware-independent logic .

4) Abstract the module named Line Discipines, before sending data to the TTY hardware, and after receiving data from the TTY device, perform corresponding processing (such as conversion of special characters, etc.) .

2.2、System Console Core

The system console of Linux kernel has two main functions:

1) Provide a console terminal to the system to allow users to log in for interactive operations;

2) Provide printk function for log output of kernel code.

2.3、TTY Line Discipilines

Line Disciplines is a very elegant design in TTY Framework. We can regard it as an adaptation layer between the device driver and the application interface. Literally understood, it is to assist the TTY driver to convert the characters we type through the TTY device into line by line data. Of course, the actual situation is far more complicated than this. For example, in China, the kernel version used by the Wowo x project, there are the following Line Disciplines (prefix with n_, our follow-up article will introduce it in more detail):

pengo@DESKTOP-CH8SB7C:~/work/xprj/linux$ ls drivers/tty/n_*
drivers/tty/n_gsm.c   drivers/tty/n_r3964.c        drivers/tty/n_tracesink.c  drivers/tty/n_tty.c
drivers/tty/n_hdlc.c  drivers/tty/n_tracerouter.c  drivers/tty/n_tracesink.h

2.4, TTY Drivers and System Console Drivers

Finally, the kernel and driver engineers are more concerned with specific TTY device drivers. With such a beauty framework built for us by the kernel, writing the corresponding driver becomes a relatively simple matter. Of course, in the kernel, there are two main types of TTY drivers:

1) The Virtual Terminal (VT) driver, located in drivers/tty/vt, is responsible for implementing VT related functions (detailed in subsequent articles);

2) The serial terminal driver, also known as the serial subsystem, is located in drivers/tty/serial.

Three, summary

This article gives a brief introduction to the software framework of Linux TTY Framework, the purpose is to understand the software implementation of Linux TTY as a whole. Based on the description in this article, follow-up plans to continue the analysis of TTY Framework from the following perspectives:

Understanding and interpretation of concepts such as control terminal, console terminal, and virtual terminal;

Analysis of TTY core;

Analysis of System Console Core;

Analysis of Serial subsystem;

Analysis of Virtual Terminal (VT);

Introduction and analysis of Line Disciplines;

and so on.

Four, reference documents

[1] TTY driver analysis

[2] Controlling terminal, https://linux.die.net/man/4/tty

[3] https://utcc.utoronto.ca/~cks/space/blog/unix/TTYLineDisciplineWhy

 

This switched: snail nest Technology , www.wowotech.net

 

Guess you like

Origin blog.csdn.net/u014674293/article/details/115296095