A brief introduction to the concepts of the tty subsystem architecture
In the Linux system, the terminal is a type of character device, which includes many types. Usually tty is used to refer to various types of terminal devices.
1 Serial terminal (/dev/ttyS*)
The serial terminal is a terminal device connected to the computer serial port. Linux treats each serial port as a character device. The device names corresponding to these serial ports are /dev/ttySAC0; /dev/ttySAC1...
2 Console terminal (/dev/console)
In Linux systems, the output device of a computer is usually called a console terminal (Console), which specifically refers to the device to which printk information is output. /dev/console is a virtual device that needs to be mapped to a real tty. For example, the kernel startup parameter "console=ttySAC0" maps the console to serial port 0.
3 Virtual terminals (/dev/tty*)
When a user logs in, a virtual terminal is used. Using the Ctcl+Alt+[F1—F6] key combination, we can switch to tty1, tty2, tty3, etc. tty1–tty6 are called virtual terminals, and tty0 is an alias for the currently used virtual terminal.
The Linux tty subsystem includes: tty core, tty line discipline and tty driver. The tty core is an abstraction of the entire tty device, providing a unified interface for users, the tty line discipline is the formatting of transmission data, and the tty driver is the hardware driver for the tty device.
Serial port driver analysis initialization
To analyze the serial port driver, you must first remember two files: sumsung.c and s3c6400. These two files are very important and are located in .../drivers/tty/serial.
First, when the user uses the write function to send data through the serial port, write will find the corresponding pointer in file_operation through the system call. The tty_fops structure is the corresponding file_operations, in which tty_write responds to the system call.
Next, we enter the tty_write function and find the line discipline ops in the tty_write function. We need to find the line discipline here. The tty_ldisc_N_TTY structure is the structure we need to find. In this structure, we can see n_tty_write, which is the function that tty_write needs to call.
Next, let's go into the n_tty_write function and see that n_tty_write will call ops. Ops is actually a tty_operations. Here we need to know a structure uart_ops defined by tty_operations. This structure is the ops we want, which contains the uart_write function.
So far, the call relationship of write has been listed at the tty level. Simply put, when the user executes the write system call, the first thing to do is to find tty_write, then find n_tty_write, and finally find the uart_write function. Next, we will see how to make system calls in the driver and mainly learn some important data structures.
In the uart_write function, the uart_state data structure is used first, and the other is uart_port. We can see that uart_port is obtained from state.
In port, we can also get a data structure uart_ops. In this data structure, there are many function pointers, which can be used to operate the hardware.
In addition to these three data structures, there is another important data structure uart_driver. We can go back and look at the uart_write function. The first line tty->driver_data shows that state is obtained from driver_data. So where does driver_data come from? We can open the uart_open function to find out. This function also gives us the following information: driver_data is obtained from state, and state is obtained from driver through the uart_get function. In this way, we can roughly sort out the relationship between the four important data structures: uart_driver, uart_state, uart_port, and uart_ops.
Several important data structures are listed below. Next, we will understand these structures in depth and start analyzing initialization.
• UART driver structure: struct uart_driver
A uart_driver represents a serial port driver
• UART port structure: struct uart_port
One uart_port corresponds to one serial port (one driver can contain multiple serial ports)
• UART related operation function structure: struct uart_ops
uart_ops contains function pointers to implement related operations on the serial port hardware.
The above three constitute the main body of the serial port driver
• UART status structure: struct uart_state
• UART information structure: struct uart_info
1 Open Samsung.c. During the initialization process, the uart_register_driver function is used to register the serial port driver. The following s3c24xx_uart_drv is a uart_driver, representing a serial port driver, so the first task is to register the serial port driver.
In s3c6400.c, we can see that the s3c24xx_serial_init function is used. If we continue to follow this function, we will see that the platform_driver_register function is used, which reminds us of the registration of the platform bus driver in the previous blog.
2 In the platform bus chapter, we know that when the driver and the device are compared, once a match is found, the probe function will be called. The same is true here. Let's focus on analyzing the probe function here. The first task is to take out the corresponding uart_port structure - &s3c24xx_serial_ports[probe_index], and then initialize the uart_port - s3c24xx_serial_init_port.
Continue to analyze how the initialization work is carried out. In the s3c24xx_serial_init_port function, there are several codes that we need to master. First, the platform_get_resource function obtains the base address of the device, then uses the statically allocated virtual address S3C_VA_UART, and finally uses the platform_get_irq function to obtain the corresponding interrupt number.
Here we also need to pay attention to a function s3c24xx_serial_resetport, which is mainly used to reset fifos. Entering this function, we focus on the register UFCON that resets the fifo. In this register, by looking up the chip manual, we can see that the first and second bits are set to 1 at the same time to reset the FIFO. Similarly, we can find in the kernel that the first and second bits of the UFCON register are set to 1.
Next, let's return to the probe function. There are several functions that need to be understood:
uart_add_one_port is used to add a port.
platform_set_drvdata is used to put the port into driver_data in dev.
device_create_file is used to create a property file, through which you can see the relevant serial port information.
The s3c24xx_serial_cpufreq_register function is related to dynamic frequency adjustment.
Here we just need to understand the relevant steps in the initialization function. It may be difficult to understand such a complex calling relationship for the first time, but it doesn't matter, otherwise embedded systems are so simple that anyone can learn them casually and it will be useless. The following is a mind map for this section of analysis.
Serial port driver analysis-open device
Based on the analysis in the previous section, we can roughly understand the working steps of driver initialization. In this section, we will mainly analyze how the serial port driver opens the device.
First open Samsung.c and enter the initialization of the serial port driver registration program. There is a function uart_register_driver.
Entering uart_register_driver, you can see that a function tty_register_driver is called.
Entering the tty_register_driver function, we can see that cdev_init is used in the serial port registration function, which means that the serial port is also a character device, and tty_fops is actually the file_operations of the serial port device file.
Continue to open tty_fops, you will find various device methods, open, read, write, etc., so we can find the response entry of the open system call.
What should tty_open do next? Here we just need to know that tty_open calls the uart_open function in uart_ops.
Next, let's take a look at the uart-open function. After opening this function, we find that this function continues to call the uart_startup function.
We continue to open the uart_startup function, and we can find the following line of code. Now we call the startup of the serial port driver function set. uport is of uart_port type, representing a serial port, which contains a function operation set. (What is the operation function set? Don't worry, keep reading.) So where can we find the startup of the serial port driver function set? Let's try to find it in the driver program.
In the driver Samsung.c, the port is obtained through the s3c24xx_serial_ports array in the probe function.
Open s3c24xx_serial_ports, you can see that each port represents a serial port. In the port, there is a structure s3c24xx_serial_ops, which is the set of operation functions.
In the s3c24xx_serial_ops structure, we can find the startup function.
To summarize the above
1 When the user uses the open system call, a file_operations will be found, tty_fops acts as this file_operations, and then the tty_open function will be found in tty_fops.
2 In uart_ops there is a function uart_open, which is called by tty_open. In uart_open function, uart_startup function is called.
3. Get the operation function set s3c24xx_serial_ops through the array s3c24xx_serial_ports in the driver, and then find the startup function.
Next, let’s analyze what the startup does.
Here we mainly look at the following four lines of code
rx_enabled(port)=1; /*Enable serial port receiving function*/
ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_chars, 0, s3c24xx_serial_portname(port), ourport); /*Register interrupt handler for data reception*/
tx_enabled(port) = 1; /*Enable serial port sending function*/
ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_chars, 0, s3c24xx_serial_portname(port), ourport); /*Register interrupt handler for data transmission*/
Serial port driver analysis-data transmission
In this section, our analysis is carried out in two steps. The first step is to analyze the call relationship of tty data sending, and the second step is to analyze the serial port sending function. Let's take a look at the first step first.
Previous article:MMU configuration and usage
Next article:OK6410 key test C language program
Recommended ReadingLatest update time:2024-11-16 14:59
- Popular Resources
- Popular amplifiers
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
- Modern Product Design Guide
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- How to not set a password in AP mode
- LCD Segment Screen Screen Printing Notes
- hfss18 version 3D image setting problem
- The network transformer output does not connect to RJ45, but uses a custom interface!
- EEWORLD University Hall----Live Replay: TI Sitara? Multi-protocol Industrial Communication Optimization Solution, PLC Demo Real-time Demonstration
- [NXP Rapid IoT Review] Reduce the CPU frequency (K64) to save power
- CC1310 Two-wire Serial Bootloader Solution
- Filter applications for different scenarios - harmonics
- 4.3-inch screen design
- How does AD19 transfer a BMP format logo image to PCB?