Timeline
Timeline
2026-01-13
init
This article introduces the basics of Linux network devices, discusses in detail the development history of network communication, the characteristics and functions of common network protocols such as TCP/IP and UDP, and explains network transmission media such as twisted pair cables, coaxial cables, and optical fibers.
Linux Driver Notes
| Table of Contents | Links |
|---|---|
| 1. Linux Driver Framework | |
| 2. Linux Driver Loading Logic | |
| 3. Character Device Basics | |
| 4. Concurrency and Competition | |
| 5. Advanced Character Device Progression | |
| 6. Interrupts | |
| 7. Platform Bus | |
| 8. Device Tree | |
| 9. Device Model | |
| 10. Hot Plug | |
| 11. pinctrl Subsystem | |
| 12. GPIO Subsystem | |
| 13. Input Subsystem | |
| 14. Single Bus | |
| 15. I2C | |
| 16. SPI | |
| 17. UART | |
| 18. PWM | |
| 19. RTC | |
| 20. Watchdog | |
| 21. CAN | |
| 22. Network Device | |
| 23. ADC | |
| 24. IIO | |
| 25. USB | |
| 26. LCD |
Basics of Network Communication
Development History
In 1969, ARPAnet initially connected computers from four American universities, using packet-switching technology to achieve data transmission between computers.
In 1972, the first transatlantic connection was achieved with computers in the United Kingdom and Norway.
In 1973, research began on the Transmission Control Protocol (TCP) and Internet Protocol (IP).
In 1984, the TCP/IP protocol became the standard for computer network communication.
In 1986, the U.S. National Science Foundation (NSF) established a wide area network (the predecessor of the Internet).
In 1994, China’s NCFC network officially connected to NSFnet, marking China’s entry into the Internet.
Network Protocol
A network protocol is a set of rules and conventions for communication between devices on a network. Common network protocols include:
TCP/IP Protocol
TCP/IP (Transmission Control Protocol/Internet Protocol) is the fundamental protocol suite for internet communication.
Features
- Connection-oriented: Before transmitting data, the TCP protocol establishes a connection through a ‘three-way handshake’ to ensure the reliability of both communicating parties and the orderly transmission of data.
- High reliability: TCP ensures data reliability and integrity through mechanisms such as sequence numbers, acknowledgment responses, retransmission, and flow control. If a data packet is lost or corrupted during transmission, TCP will request retransmission until the data arrives accurately.
- Byte-stream oriented: TCP treats application layer data as an unstructured byte stream, without preserving message boundaries. This means TCP does not concern itself with the specific content of the application layer data, but merely transmits it as a series of bytes.
Function
- The TCP/IP protocol operates in a layered manner, with each layer performing specific functions, providing a complete communication solution from network hardware to applications. It enables data exchange between different computers and networks.
UDP Protocol
UDP (User Datagram Protocol) is a connectionless protocol. Unlike TCP, UDP does not provide connection-oriented communication, nor does it guarantee data reliability and integrity.
Features
Connectionless: UDP does not require establishing a connection before sending data. Each packet is sent independently, and the receiver does not need to send acknowledgment responses. This characteristic gives UDP low latency and high transmission efficiency.
Unreliability: UDP cannot guarantee the order, integrity, or reliability of data packets. If packets are lost or corrupted during transmission, UDP does not request retransmission.
Datagram-based: UDP encapsulates application layer data into independent datagrams for transmission, each containing complete destination and source address information.
Function
UDP is suitable for scenarios with high real-time requirements, such as video streaming, audio streaming, and online gaming. In these applications, even if a small number of packets are lost, it does not significantly impact the overall experience.
DNS Protocol
DNS (Domain Name System) is a distributed database system whose main function is to convert domain names into corresponding IP addresses (e.g., 192.0.2.1). With DNS, users can access websites using easy-to-remember domain names without having to memorize complex IP addresses.
DHCP Protocol
DHCP (Dynamic Host Configuration Protocol) is a network protocol that automatically assigns network parameters such as IP addresses, subnet masks, default gateways, and DNS server addresses to client devices within a local area network.
FTP Protocol
FTP (File Transfer Protocol) is a protocol for transferring files over a network, enabling file sharing between different computers, and is one of the important tools for file transmission on the internet.
HTTP/HTTPS Protocol
HTTP (HyperText Transfer Protocol) is an application-layer protocol for distributed, collaborative, hypermedia information systems, and is the foundation of data communication on the World Wide Web (WWW).
HTTPS (HyperText Transfer Protocol Secure) is the secure version of HTTP, which encrypts data using the SSL/TLS protocol to ensure the security of data transmission.
Function
The HTTP protocol allows users to access various resources on the internet, such as web pages, images, and videos, through a browser. The HTTPS protocol is dedicated to protecting user data security, preventing data from being stolen or tampered with during transmission, and is widely used in scenarios with high data security requirements, such as online banking, online payments, and e-commerce.
Transmission Medium
Including twisted pair cables, coaxial cables, and optical fibers
- Twisted Pair Cable
A twisted pair cable is generally composed of several wires, commonly 8 copper wires twisted together in pairs to form 4 twisted pairs. The exterior is wrapped with an insulating layer and a sheath to protect the internal wires, prevent information leakage, and resist external electromagnetic interference.
- Coaxial Cable
A coaxial cable is a device for transmitting electrical energy or signals, typically composed of several wires or groups of wires. It consists of a conductor, an insulating layer, and a sheath layer.
- Optical Fiber
An optical fiber is a cable that uses light as an information carrier and transmits data through the principle of total internal reflection in fibers made of glass or plastic. It is mainly divided into single-mode fiber and multi-mode fiber.
Types of Twisted Pair Cables
Category 1 and Category 2 cables
Category 1 and Category 2 cables were mainly used in early telephone communication systems and did not support data transmission or modern network applications. With the continuous advancement of network technology, these two cable categories have been phased out and are no longer used for network cabling.
Category 3 (Cat 3) cable
Category 3 cable is one of the early Ethernet cable standards, mainly used for traditional Ethernet with a transmission rate of 10Mbps. In modern network environments, Cat 3 cables are rarely used.
Category 4 (Cat 4) cable
Compared to Category 3 cables, Category 4 cables showed some improvement, but in practical applications, due to their low cost-effectiveness, they were not widely adopted and were soon replaced by the more advanced Category 5 cables.
Category 5 (Cat 5) cable
Enhanced Category 5 (Cat 5e) cable
Category 6 cable (Cat 6)
Augmented Category 6 cable (Category 6A, Cat 6a)
Augmented 6 Category cable is 6 a further upgraded version of Category cable,supports 10Gbps transmission rate of(10 Gigabit Ethernet)。Category 7 cable (Cat 7)
7 Category 7 cable is a type of shielded twisted pair(STP),supports 10Gbps transmission rate,has higher bandwidth and lower signal attenuation。Ethernet cable wiring standards
There are two standards for Ethernet cable wiring: T568A and T568B.
If both ends of the cable use the same wiring standard, it is called a straight-through cable, used to connect different types of devices, such as a computer to a router;
If the wiring standards differ, such as one end using T568A and the other T568B, it is called a crossover cable, commonly used to connect similar devices, such as computer to computer or switch to switch.
T568A
T568A standard wiring order: white-green, green, white-orange, blue, white-blue, orange, white-brown, brown.

T568B
T568B standard wiring order: white-orange, orange, white-green, blue, white-blue, green, white-brown, brown

Network model
OSI seven-layer model
The OSI seven-layer network model is a network architecture standard developed by the International Organization for Standardization (ISO) specifically to achieve interconnection and interoperability between different systems.
The OSI seven-layer model, from bottom to top, consists of: Physical Layer, Data Link Layer, Network Layer, Transport Layer, Session Layer, Presentation Layer, and Application Layer, as detailed below:

- Physical Layer
The physical layer only needs to consider how to send 0s and 1s, and how the receiving end recognizes them to complete the transmission of the bit stream, without caring about the specific meaning of the bit stream. Additionally, it also specifies some electrical characteristics in the network.
- Data Link Layer
Simple 0s and 1s are meaningless; the data link layer converts the raw bit stream from the physical layer into independent frames. It is also responsible for the establishment, maintenance, and termination of link connections, as well as functions such as frame synchronization, error control, and flow control.
- Network Layer
The network layer mainly handles the transmission and routing of data packets from the source node to the destination node, i.e., establishing host-to-host communication.
- Transport Layer
The transport layer is responsible for providing reliable port-to-port data transmission services, ensuring that data can be accurately and error-free transmitted from the sender to the receiver.
- Session Layer
It is mainly responsible for managing user sessions, controlling the dialogue between applications, including the start, end, and data exchange process of the dialogue.
- Presentation Layer
The presentation layer is responsible for data format conversion, such as conversion between device-specific formats and network standard formats, which can be understood as acting as a ‘translator’.
- Application Layer
The application layer is the layer directly facing users, completing specific network applications through interaction between application programs.
TCP/IP Four-Layer Model
The TCP/IP four-layer model is a simplification of the OSI seven-layer model, dividing network functions into the network interface layer, internet layer, transport layer, and application layer, as shown in the following diagram:

- Application Layer
The application layer directly interacts with applications. Common application layer protocols include HTTP, FTP, SMTP, DNS, etc. Different types of applications choose different protocols in the application layer based on their needs, for example, email transmission applications can select the SMTP protocol.
- Transport Layer
Provides reliable port-to-port data transmission services. The most important protocols in the transport layer are TCP and UDP. TCP provides connection-oriented, reliable services, while UDP provides connectionless, unreliable but fast services.
- Network Layer
Provides host-to-host communication. The most important protocol in the network layer is the IP protocol, which adds an IP address to each data packet.
- Network Interface Layer
The network interface layer includes the physical layer and the data link layer, so it encompasses both physical transmission and link establishment.
OSI (Open System Interconnection) is a seven-layer model developed by the International Organization for Standardization (ISO).
The TCP/IP four-layer model references the OSI seven-layer model and simplifies it into four layers, as shown in the diagram below. As a result, it is more efficient and cost-effective in applications, leading to its rapid development and establishment as the de facto standard.

Encapsulation and Decapsulation of Network Data
In network communication, encapsulation refers to adding the protocol header and trailer of the current layer to the data from the upper layer, forming a new data unit, and then passing it to the next layer for processing.
Decapsulation is the reverse process of encapsulation, where the receiver removes the protocol header and trailer of the current layer from the received data unit, extracts the upper-layer protocol data, and then passes it to the upper layer for processing.
The processes of encapsulation and decapsulation are carried out sequentially across different network layers.

Application Layer Encapsulation: Data generated by applications, such as web page data transmitted via HTTP protocol or email data transmitted via SMTP protocol, is referred to as application data at this stage. Application layer protocols format the data according to their own requirements; for example, HTTP adds request headers or response headers containing information such as request methods, URLs, and response status codes.
Transport Layer Encapsulation: Application data arrives at the transport layer (e.g., TCP or UDP). Taking TCP as an example, TCP adds a TCP header to the data, which includes source port number, destination port number, sequence number, acknowledgment number, control flags, and other information. Port numbers identify applications, allowing the receiver to correctly deliver data to the corresponding application. The UDP header is relatively simpler, also containing source and destination port numbers for multiplexing and demultiplexing. At this point, the data with the TCP or UDP header is called a segment.
Network Layer Encapsulation: The segment from the transport layer arrives at the network layer (e.g., IPv4 or IPv6), which adds an IP header. The IP header contains key information such as source IP address and destination IP address, used for routing between different networks. The data after adding the IP header is called a packet. For example, when you visit a website, your device encapsulates the TCP segment in an IP packet, with the destination IP address being that of the website server.
Data Link Layer Encapsulation: The packet from the network layer arrives at the data link layer (e.g., Ethernet), which adds a frame header and frame trailer. The Ethernet frame header contains source MAC address, destination MAC address, etc., while the frame trailer includes a cyclic redundancy check (CRC) value for error detection. The data at this stage is called a frame. The frame is then converted into electrical or optical signals by the physical layer for transmission over the physical medium.
Decapsulation is the reverse process of encapsulation. At the receiving end, data starts from the physical layer and moves upward through each network layer, with each layer removing the corresponding header information to restore the data into a form that the application layer can process, as shown in the following diagram:

Data Link Layer Decapsulation: The physical layer receives electrical or optical signals, converts them into data frames, and passes them to the data link layer. The data link layer checks the frame header and trailer, uses CRC to ensure error-free data transmission, removes the header and trailer, and passes the data packet to the network layer.
Network layer decapsulation: The network layer receives the data packet, checks the IP header, and determines whether it is destined for the local machine based on the destination IP address. If so, it removes the IP header and passes the segment to the transport layer.
Transport layer decapsulation: The transport layer receives the segment, delivers the data to the corresponding application based on the port number in the TCP or UDP header, and removes the TCP or UDP header. At this point, the data is restored to application layer data, which the application program can process.
Hardware connection
RJ45 interface
RJ45isRegistered Jack 45abbreviation, in computer networks, RJ45 is the short name for the standard 8-pin modular interface. RJ45 consists of a plug and a socket, connected by a network cable. The RJ45 plug is shown in the figure below

From the appearance,the RJ45 plugis rectangular, with a plastic outer shell,and inside there are 8 metal contacts, which are used for electrical connection with the RJ45 socket。
Corresponding to the RJ45 plug, inRJ45 socketalso has8 metal contact blades for connecting with the RJ45 plug. When the RJ45 plug is inserted, its 8 metal contacts at the front make tight contact with the metal blades inside the socket, thereby achieving signal transmission.

Both 100M and Gigabit Ethernet ports use the RJ45 interface for physical connection. However, although they look identical, their transmission capabilities are vastly different. The corresponding pin usage is also very different.
100M RJ45 interfacehas 8 pins, these 8 pins are
| Pin Number | Identifier | Chinese Name | Function Description |
|---|---|---|---|
| 1 | TX+ | Transmit Data + | Positive terminal of Ethernet differential transmit signal |
| 2 | TX- | Transmit Data - | Negative terminal of Ethernet differential transmit signal |
| 3 | RX+ | Receive data + | Positive terminal of Ethernet differential receive signal |
| 4 | NC | No connection | No electrical connection, unused |
| 5 | NC | No connection | No electrical connection, unused |
| 6 | RX- | Receive data - | Negative terminal of Ethernet differential receive signal |
| 7 | NC | No connection | No electrical connection, unused |
| 8 | NC | No connection | No electrical connection, unused |
Differential signal: TX+/- and RX+/- are paired differential signal lines. They use differential transmission to resist interference and enhance the stability of Ethernet data transmission. This is the core design of the RJ45 Ethernet port.
Standard Matching: This definition conforms toT568Bthe wiring standard (most commonly used in residential/industrial scenarios). It differs from the T568A standard only in wire sequence arrangement, while the pin functions are identical.
Unused Pin Function: Pins 4, 5, 7, and 8 are reserved pins. Some industrial-grade Ethernet ports may use them for power supply (e.g., PoE), but in conventional Ethernet applications, they are all NC (Not Connected).
Gigabit RJ45 Interfacealso has 8 pins. Compared to Fast Ethernet, the Gigabit Ethernet port utilizes all 8 pins, divided into 4 differential pairs to achieve full-duplex high-speed transmission.
| Pin Number | Name | Function Description | Transmission Direction | Differential Pair Group |
|---|---|---|---|---|
| 1 | TX_D1+ | Transceive Data1+ (Transmit Signal +) | Transmit | First Pair |
| 2 | TX_D1- | Transceive Data1-(Send Signal -) | Send | First Pair |
| 3 | RX_D2+ | Receive Data2+(Receive Signal +) | Receive | Second Pair |
| 4 | BI_D3+ | Bi-directional Data3+(Bidirectional Signal) | Bidirectional | Third Pair |
| 5 | BI_D3- | Bi-directional Data3-(Bidirectional Signal) | Bidirectional | Third Pair |
| 6 | RX_D2- | Receive Data2-(Receive Signal -) | Receive | Second Pair |
| 7 | BI_D4+ | Bi-directional Data4+ (bidirectional signal) | Bidirectional | Fourth pair |
| 8 | BI_D4- | Bi-directional Data4- (bidirectional signal) | Bidirectional | Fourth pair |
Transmission rate and pin utilization: Fast Ethernet (100BASE-TX) uses only 4 pins: 1/2 (TX) and 3/6 (RX), whileGigabit Ethernet (1000BASE-T) uses all 8 pins, achieving a transmission rate of 1 Gbps by simultaneously transmitting and receiving over 4 differential pairs.
Function of bidirectional pins (BI): Pins 4/5 and 7/8 are designed for bidirectional transmission. In Gigabit mode, these two pairs dynamically switch transmission and reception directions based on communication needs, achieving full-duplex communication with digital signal processing (DSP) technology.
Differential transmission characteristics: Each pair of pins (e.g., TX_D1+/TX_D1-) are differential signal lines, transmitting signals through the voltage difference between the two wires, effectively suppressing common-mode interference and enhancing the stability of high-speed data transmission. This is a core design of the Ethernet physical layer.
Wiring standard compatibility: This pin definition still follows the T568A/T568B wiring standard, and the wire sequence arrangement is consistent with that of a 100M Ethernet port. Therefore, a Gigabit Ethernet cable is backward compatible with a 100M Ethernet port.
MAC controller
The full name of the MAC controller is Media Access Control. It belongs to**the OSI Layer 2 (Data Link Layer)**part.
The main function of the MAC controller is to control the connection with the PHY chip. It acts like a “bridge manager,” responsible for managing data transmission between the data link layer and the physical layer, ensuring that data can flow smoothly between the two layers.
The main functions of the MAC controller include:
- Ethernet frame encapsulation and parsing
- Responsible for adding/parsing the Ethernet frame header (destination MAC, source MAC, type field)
- Check and generate CRC (FCS)
- Data transmission and reception control
- Read data from system memory (DMA), encapsulate it into Ethernet frames, and send
- Receive data from the PHY and write it to memory
- Traffic and buffer management
- Transmit/Receive FIFO
- Notify CPU via interrupt or polling
- DMA descriptor management (driver layer focus)
- Link layer control
- Supports full-duplex / half-duplex
- Supports flow control (PAUSE frames)
- Statistics (packet loss, collisions, error counts, etc.)
- Interface with PHY
- ViaMII / RMII / RGMII / SGMIIinterfaces to communicate with PHY
- ViaMDIO/MDCto configure and manage the PHY chip
PHY chip
PHY chip stands for Physical Layer chip. The main task of the PHY chip is toconvert digital signals into analog signals suitable for transmission over physical media (e.g., Ethernet cables), thenconvert the received analog signal back into a digital signal, so that the computer can recognize and process it.
The core functions of PHY include:
- Signal encoding and decoding
- 100M:MLT-3、4B/5B
- 1000M:PAM-5
- Adaptive equalization, echo cancellation
- Auto-Negotiation
- Negotiate speed (10M / 100M / 1000M)
- Negotiate duplex mode (half-duplex / full-duplex)
- Link detection
- Cable plug/unplug detection
- Report Link Up / Link Down status to MAC
- Clock recovery and synchronization
- Recover clock from the received signal
- Ensure accurate data sampling
- Management interface with MAC
- UsingMDIO/MDCregister interface
- Common in Linux drivers
phy_read()/phy_write()
Network transformer
Network transformer, also known as network isolation transformer or Ethernet transformer, mainly functions for signal coupling, electrical isolation, and impedance matching. It is located betweenthe PHY and RJ45 interface, and is a passive component.
The main functions of the network transformer include:
- Electrical isolation
- Provides isolation capability above 1500V
- Prevents surges and potential differences from damaging chips
- Meets Ethernet safety standards (e.g., IEEE 802.3)
- Signal coupling
- Couples the differential signal output from the PHY to the twisted pair
- Block DC component, transmit only AC signal
- Impedance matching
- Match PHY output impedance with network cable (100Ω)
- Reduce reflections, improve signal quality
- EMI suppression
- Work with common-mode choke to reduce electromagnetic interference
- Enhance anti-interference capability
Many RJ45 connectors internallyintegrate network transformers and common-mode chokes, commonly referred to in engineering as**“LED-equipped Ethernet port”**。
Connection method
Based on the integration of CPU, MAC, and PHY, it can be further subdivided into the following three methods:
Method 1: CPU integrates MAC and PHY internally
In this method, the CPU integrates MAC and PHY internally. While this reduces the number of peripheral components, it significantly increases CPU cost and offers poor flexibility due to full internal integration. Therefore, this method is rarely used in practical applications.
Method 2: CPU integrates MAC internally, PHY uses a separate chip
The CPU only integrates the MAC controller internally, while the PHY uses a separate chip. The advantage of this method is the flexibility to choose the PHY chip based on the application scenario, and it is alsothe current mainstream approach。

The iTOP-RK3568 also uses this method. It should be noted that the RJ45 connector on the iTOP-RK3568 development board integrates a network transformer internally. If the
RJ45 connector does not have an integrated network transformer, a separate network transformer chip is required.
Method 3: CPU does not integrate MAC and PHY, MAC and PHY use separate chips or integrated chips
In this connection method, the CPU lacks MAC and PHY functions, meaning the CPU itself does not support network functionality. To use network functions, it must be adapted through other interfaces. Therefore, this method has higher costs.

PHY Interface
In embedded network development, the most mainstream solution is shown in the following diagram:

From the block diagram, it can be seen that the left side of the PHY chip connects to the MAC, and the right side connects to the RJ45 connector. It is particularly evident that the PHY chip serves as a bridge.
The PHY chip can be connected to the MAC controller via MII, RMII, GMII, RGMII, and MIDO interfaces.
MII Interface
The full name of the MII interface isMedia Independent Interface, i.e., “Media Independent Interface”.
The transmission rate of the MII interface is 10Mbps or 100Mbps. At 100Mbps, the clock is 25MHz; at 10Mbps, the clock is 2.5MHz. The hardware connection is shown in the following diagram:

The MII interface requires a total of 16 signal lines, with the following meanings:
| Signal Line Type | Signal Line Name | Quantity | Description | Direction (MAC←→PHY) | Core Function |
|---|---|---|---|---|---|
| Transmit Channel | TX_CLK | 1 | Transmit Clock, generated by PHY | MAC ← PHY | Synchronous clock for transmitting data (25MHz at 100Mbps) |
| TX_EN | 1 | Transmit Enable, active high | MAC → PHY | Indicates data valid on TXD [3:0] | |
| TXD[3:0] | 4 | Transmit data bus | MAC → PHY | Parallel transmitted Ethernet data (4-bit wide) | |
| TX_ER | 1 | Transmit error, active high | MAC → PHY | Indicates an error during transmission (e.g., frame format error) | |
| Receive channel | RX_CLK | 1 | Receive clock, generated by PHY | MAC ← PHY | Synchronous clock for received data (25 MHz for 100 Mbps) |
| RX_DV | 1 | Receive data valid, active high | MAC ← PHY | Indicates data valid on RXD [3:0] (replaces RX_EN) | |
| RXD[3:0] | 4 | Receive data bus | MAC ← PHY | Parallel received Ethernet data (4-bit wide) | |
| RX_ER | 1 | Receive error, active high | MAC ← PHY | Indicates an error during reception (e.g., CRC error, frame too long) | |
| Collision / Carrier detection | COL | 1 | Collision detection, active high | MAC ← PHY | In half-duplex mode, indicates a data collision on the bus |
| CRS | 1 | Carrier sense, active high | MAC ← PHY | In half-duplex mode, indicates the bus is busy |
RMII Interface
The full name of the RMII interface isReduced Media Independent Interface, which stands for “Reduced Media Independent Interface”. It is a simplified version of the MII interface. Therefore, compared to the MII interface, the RMII interface reduces the number of signal lines for data transmission and reception by half. The hardware connection is shown in the following diagram:

The RMII interface requires a total of 7 signal lines, with the following meanings:
| Signal Line Name | Quantity | Direction (MAC ↔ PHY) | Core Description & Key Development Points |
|---|---|---|---|
| REF_CLK | 1 line | External → MAC + PHY | 50MHz fixed reference clock,the only synchronous clock for transmitting and receiving data(replaces MII’s TX_CLK/RX_CLK); must be supplied to both MAC and PHY, clock source can be PHY’s internal crystal oscillator or an external crystal oscillator |
| TX_EN | 1 line | MAC → PHY | Transmit enable, active high; PHY samples TXD [1:0] data only when this signal is high |
| TXD[1:0] | 2 lines | MAC → PHY | 2-bit parallel transmit data bus; achieves 100Mbps rate via ‘2-bit width × 50MHz’, a core design of RMII simplification |
| CRS_DV | 1 line | PHY → MAC | Multiplexed signal: Full-duplex mode: only serves as ‘RX_DV (Receive Data Valid)’ Half-duplex mode: simultaneously indicates ‘CRS (Carrier Sense)’ and ‘DV (Data Valid)’ |
| RXD[1:0] | 2 lines | PHY → MAC | 2-bit parallel receive data bus; MAC samples data only when CRS_DV is high |
GMII interface
The full name of the GMII interface isGigabit Media Independent Interface, which stands for “Gigabit Media Independent Interface.” It is an interface designed to meet the high-speed data transmission requirements of Gigabit Ethernet. Under 10M/100M/1000M, the clocks are 2.5M, 25M, and 125M respectively. The hardware connection is shown in the following diagram:

The GMII interface requires a total of 25 signal lines, with the following meanings:
| Signal Name | Bit Width / Quantity | Chinese Description | Signal Direction | Detailed Function (Development Core) | Rate Adaptation Description |
|---|---|---|---|---|---|
| GTX_CLK | 1 line | Gigabit Transmit Clock | MAC → PHY | Dedicated transmit synchronous clock for Gigabit mode, fixed125MHz, the MAC sends data based on this clock, and the PHY synchronously samples TXD data | Used only for Gigabit (1000M), core clock |
| TX_CLK | 1 line | 100M transmit clock | MAC → PHY | Transmit clock compatible with 10/100M rates, 10M=2.5MHz, 100M=25MHz | Used only in 10/100M mode, invalid in gigabit mode (floating) |
| TXD[7:0] | 8 lines | Transmit data bus | MAC → PHY | 8-bit parallel transmit data, core wide bit-width design of GMII | Fully compatible with gigabit/100M/10M, different rates have different clocks |
| TX_EN | 1 line | Transmit enable | MAC → PHY | Active high, indicates data on TXD [7:0] is valid; PHY samples data only at this time | Universal for all rates |
| TX_ER | 1 line | Transmit error indication | MAC → PHY | Active high, indicates a format error in the currently transmitted data frame | Universal for all rates |
| RX_CLK | 1 line | Receive clock | PHY → MAC | Receive synchronous clock, Gigabit mode = 125MHz, 100Mbps = 25MHz, 10Mbps = 2.5MHz,extracted and provided by PHY from received data, MAC synchronously samples RXD data | Full-rate universal, core receive clock |
| RXD[7:0] | 8 lines | Receive data bus | PHY → MAC | 8-bit parallel receive data, core wide-bit design of GMII | Fully compatible with Gigabit/100Mbps/10Mbps |
| RX_DV | 1 line | Receive data valid | PHY → MAC | Active high, indicates data valid on RXD [7:0], MAC samples data only at this time | Full-rate universal, replaces MII’s RX_EN naming |
| RX_ER | 1 line | Receive Error Indication | PHY → MAC | Active high, indicates that the currently received data frame has errors (CRC/FCS error, frame length error, etc.) | Full Rate Universal |
| CRS | 1 line | Carrier Sense Signal | PHY → MAC | OnlyHalf-Duplex ModeActive, high level indicates the Ethernet bus is busy, prohibiting MAC from sending data | Full rate universal, this signal is invalid (pulled low) in full-duplex mode |
| COL | 1 line | Collision Detection Signal | PHY → MAC | OnlyHalf-Duplex ModeActive, high level indicates a data collision on the bus, triggering frame retransmission by MAC | Full rate universal, no collision in full-duplex mode, this signal is invalid |
RGMII Interface
RGMII is a simplified version of GMII. It reduces the number of pins from 25 in GMII to 14 in RGMII. The clock frequencies are 2.5M, 25M, and 125M for 10M/100M/1000M modes respectively. The hardware connection is shown in the following diagram:

The RGMII interface requires a total of 14 signal lines, with the following meanings:
| Signal Name | Quantity | Direction (MAC ↔ PHY) | Core Description & Key Development Points | Rate Adaptation |
|---|---|---|---|---|
| TXC | 1 line | MAC → PHY | Transmit clock, fixed in gigabit mode125MHz;Double-edge sampling(rising edge + falling edge) is the core of RGMII simplification | 1000M=125MHz100M=25MHz10M=2.5MHz |
| TX_CTL | 1 line | MAC → PHY | Transmit control signal,Multiplexing TX_EN (transmit enable) and TX_ER (transmit error); double-edge synchronized with TXC | full-rate universal |
| TXD[3:0] | 4 | MAC → PHY | transmit data bus, 4-bit parallel;double-edge sampling, each clock edge transmits 1 bit of data, equivalent to 8-bit bandwidth | full-rate universal |
| RXC | 1 | PHY → MAC | receive clock, fixed in gigabit mode125MHz; provided by PHY, also supportsdouble-edge sampling | 1000M=125MHz100M=25MHz10M=2.5MHz |
| RX_CTL | 1 | PHY → MAC | receive control signal,multiplexed RX_DV (receive valid) and RX_ER (receive error); double-edge synchronized with RXC | Full Rate Universal |
| RXD[3:0] | 4 | MAC → PHY | Receive data bus, 4-bit parallel;Double-edge sampling, equivalent to 8-bit bandwidth | Full Rate Universal |
MDIO Interface
The full name of the MDIO interface isManagement Data Input/Output, i.e., Management Data Input/Output Interface.
Its main function isto enable the MAC controller to manage and control the PHY chip, for example, configuring the operating rate by setting PHY registers via the MDIO interface; it can also obtain the status of the Ethernet port by reading the status registers of the PHY chip.
The MDIO interface is asynchronous serial half-duplex interface, consisting of two signal lines, namely:
- MDIO: Acts as a data line for transmitting configuration and management data between the MAC layer and the PHY layer.
- MDC: Serves as a clock line, providing clock synchronization for data transmission.
Example
RTL8211 PHY Chip
The iTOP-RK3568 development board uses the RTL8211 PHY chip. The RTL8211 supports transmission rates of 10Mbps, 100Mbps, and 1000Mbps, and features auto-negotiation, allowing it to automatically adjust its working mode based on the network environment.
From the circuit connection diagram in the RTL8211 chip datasheet, it can be seen that the RTL8211 chip connects to the RJ45 interface via the MDI interface. The MAC controller connects to the PHY chip through the RGMII interface and the MDIO interface, as shown in the following figure:

Simplify the block diagram from the datasheet:

The iTOP-RK3568 core board has an exposed RGMII interface, as shown in the following figure:

The iTOP-RK3568 core board connects to the RTL8211 chip via the RGMII interface and the MDIO interface. The schematic is as follows:

- Pins 15, 16, 17, and 18 correspond to TXD3, TXD2, TXD1, and TXD0 respectively. These are transmit pins, with direction from MAC to PHY.
- Pins 22, 23, 24, 25, and 26 correspond to RXD3, RXD2, RXD1, and RXD0 respectively. These are receive pins, with direction from PHY to MAC.
- Pin 20 is the TXC pin, the transmit clock signal pin.
- Pin 27 is the RXC pin, the receive clock signal pin.
- Pin 26 is the RXCTL pin, used for receiving control signals.
- Pins 13 and 14 are the MDIO pins.
- Pin 31 is the INTB/PMEB pin. If these two functions are not used, this pin should be left floating. If Page 0xd40, REG.22 bit[5] is set to 1, it functions as PMEB; if set to 0, it functions as an interrupt.
The RTL8211 is connected to the RJ45 connector via the MDI interface, as shown in the schematic below:

Pins 1 to 10 correspond to the MDI interface pins MDIP0, MDIN0, MDIP1, MDIN1, MDIP2, MDIN2, MDIP3, and MDIN3, respectively.
The MDI interface is used to connect the PHY chip to the network transformer or the RJ45 connector.

PHY registers
In the 802.3 specification, the first 16 registers of the PHY chip are defined, as shown in the figure below:

In development, we most frequently interact with Register 0 and Register 1.
Register 0 is the control register: Used to configure the operating mode and parameters of the PHY chip. The control register can set the chip’s transmission rate (10Mbps, 100Mbps, or 1000Mbps), duplex mode (full-duplex or half-duplex), and enable or disable auto-negotiation, among other settings.
Register 1 is the status register: Used to indicate the current working status of the PHY chip. For example, through the status register, you can check whether the network connection has been successfully established, whether a link fault is detected, the current transmission rate and duplex mode, and other information.
Registers 16 to 31 are extended registers.: Registers 0 to 15 are defined by IEEE 802.3. Regardless of the PHY chip manufacturer, registers 0 to 15 are identical. Therefore, in general, the generic PHY driver in Linux can be used to drive the PHY chip. However, different manufacturers’ PHY chips may have additional features, so the extended registers are left for PHY manufacturers to customize. That is, the extended registers are only involved when these specific features are used.
PHY Address
Each PHY register has a unique address, through which we can accurately access and operate the corresponding register. The address of the RTL8211 PHY chip on the iTOP-RK3568 development board can be set via pins 22, 27, and 26.

Physical Address (MAC Address)
The physical address, also known as the MAC address, is a unique identifier for network devices. It consists of 48 binary bits and is usually represented as 12 hexadecimal digits, such as 00:11:22:33:44:55.
IP Address
An IP address is a network layer address used to identify the logical location of a device in a network. It comes in two versions: IPv4 and IPv6:
- An IPv4 address consists of 32 binary bits and is usually represented in dotted decimal notation, such as 192.168.1.1;
- An IPv6 address consists of 128 binary bits. The main function of an IP address is to enable communication between different networks, allowing data packets to be routed from the source network to the destination network.
PHY Address
A PHY address is used to uniquely identify a PHY chip within a network. In a system, there may be multiple PHY chips present simultaneously. The PHY address allows different PHY chips to be distinguished for individual configuration and management.
Determination of PHY Chip Address
Taking the Xunwei iTOP-RK3568 development board as an example, the address of its RTL8211 PHY chip is determined in the following way.
Open the datasheet of the RTL8211 PHY chip. As shown in the figure below, pins 22, 26, and 27 are used to configure the address of the PHY chip.

Furthermore, on the RK3568 baseboard schematic, the three pins of the PHY chip are connected as follows:

From the figure above, it can be seen that the address of the PHY chip is 001, which is 1. The PHY chip sets its address through hardware pins.
Operating PHY Registers
First, open the debug serial port of the iTOP-RK3568 development board, then power on the board. After entering the terminal, execute the commandcd /sys/bus/mdio_bus/devices, to enter the development board’smdio_busbus device directory.
Then, execute the commandcd stmmac-0:00/to enter the device for network card 0. The specific operation process is as follows:
1 | cd /sys/bus/mdio_bus/devices |
Basics of Network Application Programming
Byte Order
Little-endian mode: The low-order byte is stored at the low address, and the high-order byte is stored at the high address.
Big-endian mode: The high-order byte is stored at the low address, and the low-order byte is stored at the high address.
Byte order conversion functions
There are four common byte order conversion functions: htonl, htons, ntohl, and ntohs. A brief introduction to each function is shown in the table below:
Their core function is to resolve the incompatibility of byte order between different hosts and the network.
| Function Name | Function Description | Data Type | Conversion Direction |
|---|---|---|---|
htonl | Convert 32-bit host byte order to network byte order | uint32_t | Host → Network |
htons | Convert 16-bit host byte order to network byte order | uint16_t | Host → Network |
ntohl | Convert 32-bit network byte order to host byte order | uint32_t | Network → Host |
ntohs | Convert 16-bit network byte order to host byte order | uint16_t | Network → Host |
uint32_t htonl(uint32_t hostlong)
- Function: Used to convert a locally stored 32-bit integer into a format suitable for network transmission (big-endian mode).
- Parameter: A 32-bit integer in host byte order.
- Return value: Returns a 32-bit integer in network byte order.
uint16_t htons(uint16_t hostshort)
- Function: Used to convert a locally stored 16-bit integer (such as a port number) into a format suitable for network transmission.
- Parameter: A 16-bit integer in host byte order.
- Return value: Returns a 16-bit integer in network byte order.
uint32_t ntohl(uint32_t netlong)
- Function: Used to convert 32-bit data received from the network (such as an IPv4 address) into a format that the local system can process.
- Parameter: A 32-bit integer in network byte order.
- Return value: Returns a 32-bit integer in host byte order.
uint16_t ntohs(uint16_t netshort)
- Function: Used to convert 16-bit data received from the network (such as a port number) into a format that the local system can process.
- Parameter: A 16-bit integer in network byte order.
- Return value: Returns a 16-bit integer in host byte order.
For example, when writing TCP and UDP code, a port number such as 8080 is needed, and byte order conversion is required. The htons function is used to convert 8080 from host byte order to network byte order. A simple example program is as follows:
1 |
|
Address conversion functions
In addition to byte order conversion, IP addresses also need to be converted. IP addresses are typically displayed in a human-readable form such as dotted decimal (e.g., 192.168.1.1), but internally in programs, they need to be stored and processed in binary form.
In network programming, there are two IP address conversion functions, namelyinet_ptonandinet_ntop, which support the conversion of IPv4 and IPv6 addresses. A brief introduction to the two functions is as follows:
This table shows two core functions used for IP address format conversion in network programming, which support the conversion of both IPv4 and IPv6 addresses.
| Function Name | Function Description | Conversion Direction |
|---|---|---|
inet_pton | Converts an IP address in string format (dotted decimal for IPv4 or colon-hexadecimal for IPv6) to binary data in network byte order | String → Binary |
inet_ntop | Converts a binary IP address in network byte order to string format (dotted decimal for IPv4 or colon-hexadecimal for IPv6) | Binary → String |
inet_pton()
- Converts a string address to binary data
- Converts an IP address string in dotted decimal (IPv4) or colon-hexadecimal (IPv6) format to binary data in network byte order.
inet_ptonThe function prototype is as follows: returns 1 on success, 0 if the src format is invalid, and -1 if af is not supported.
1 | int inet_pton(int af, const char *src, void *dst); |
The specific parameter descriptions are as follows:
| Parameter Name | Description | Data Type / Value Range | Example Value |
|---|---|---|---|
af | Specifies the address family, used to distinguish between IPv4 or IPv6 addresses | AF_INET:IPv4AF_INET6:IPv6 | AF_INET(IPv4) |
src | Pointer to the IP address string to be converted | String | "192.168.1.1" |
dst | Pointer to the buffer storing the converted binary data | Binary data | struct in_addr |
Example:
1 |
|
inet_ntop()
- Convert binary data to string address
inet_ntopThe function is used to convert a binary IP address in network byte order into a human-readable string form (dotted decimal or colon hexadecimal).
inet_ntopThe function prototype is as follows: returns a pointer to dst on success, returns NULL and sets errno on failure.
1 | const char *inet_ntop(int af, const void *src, char *dst, socklen_t size); |
The parameters are as follows:
| Parameter Name | Description | Data Type / Value Range | Example Value |
|---|---|---|---|
af | Specifies the address family to distinguish between IPv4 or IPv6 addresses | AF_INET:IPv4AF_INET6:IPv6 | AF_INET(IPv4) |
src | Pointer to the buffer storing the binary IP address | Binary Data | struct in_addr |
dst | Pointer to the buffer storing the converted string-form IP address | String | char ip[INET_ADDRSTRLEN] |
size | dstSize of the buffer, ensuring it is large enough to hold the resulting string | socklen_t | INET_ADDRSTRLENorINET6_ADDRSTRLEN |
Example Code:
1 |
|
UDP
The full English name of UDP isUser Datagram Protocol, meaning User Datagram Protocol, it is aconnectionlesstransport layer protocol that is part of the TCP/IP protocol suite. It provides a simple, lightweight method of data transmission, suitable for scenarios that require high speed but have low reliability requirements, such as video conferencing, online gaming, and voice calls, which are sensitive to latency and can tolerate occasional packet loss.

UDP functions
| Function Name | Function Description | Usage Scenario |
|---|---|---|
socket() | Create a socket as an endpoint for network communication. | Both server and client need to call this function to create a handle for subsequent communication. |
bind() | Bind the socket to a specified IP address and port number, so the system can deliver data sent to that address to this socket. | The server must call this to bind a fixed listening port so clients can find it; clients typically do not need to call it explicitly. |
sendto() | Send a datagram to a specified target IP address and port; it is the core sending interface of the UDP protocol. | Clients use it to send requests to the server; servers use it to send responses to clients. |
recvfrom() | Receive a datagram from the network and obtain the sender’s IP address and port information. | The server uses it to receive requests from clients; the client uses it to receive responses from the server. |
sendto()
sendto()is a system call used for sending data, primarily for the connectionless UDP protocol. It allows a program to send data to a specified destination address (i.e., destination IP and port). Unlikesend()different,sendto()requires explicitly specifying the destination address, making it ideal for connectionless communication. Its function prototype is as follows:
1 | ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen); |
Parameters:
| Parameter Name | Type | Description |
|---|---|---|
sockfd | int | Socket file descriptor, created bysocket()function, is the handle for all subsequent network operations. |
buf | const void * | Pointer to the data buffer to be sent, it isconsttype, indicating that the function will not modify the contents of the buffer. |
len | size_t | Length of the data buffer in bytes, telling the function how many bytes of data to send. |
flags | int | Send flags, usually set to0. Special flags can be set (such asMSG_DONTROUTEindicates not to check the routing table,MSG_CONFIRMused for link-layer acknowledgment), but rarely used in regular UDP communication. |
dest_addr | struct sockaddr * | a pointer to the destination address structure, typicallystruct sockaddr_in(IPv4) orstruct sockaddr_in6(IPv6), containing the destination IP and port information. |
addrlen | socklen_t | the size of the destination address structure, in bytes, used to tell the functiondest_addrhow large the pointed structure is. |
on success, returns the actual number of bytes sent
recvfrom()
recvfrom()is a system call for receiving data, mainly used in connectionless UDP protocol. It allows a program to receive packets from a specified source address (i.e., the sender’s IP and port). Unlikerecv()which differs,recvfrom()it can obtain the sender’s address information, making it very suitable for connectionless communication. Its function prototype is as follows:
1 | ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen); |
The parameters of this function are described as follows:
| Parameter Name | Type | Description |
|---|---|---|
sockfd | int | Socket file descriptor, created bysocket()function, is a handle for network operations. |
buf | void * | Pointer to a buffer that stores received data; the function writes received data into this buffer. |
len | size_t | Maximum length of the buffer (in bytes). If received data exceeds this length, the excess is truncated. |
flags | int | Receiving flags, usually set to0. Special flags can be set (e.g.,MSG_PEEKpeek at data without removing,MSG_DONTWAITnon-blocking receive), rarely used in regular UDP communication. |
src_addr | struct sockaddr * | Pointer to a structure storing the sender’s address, typicallystruct sockaddr_in(IPv4) orstruct sockaddr_in6(IPv6), used to obtain the sender’s IP and port. |
addrlen | socklen_t * | Pointer to the size of the sender’s address structure. Must be initialized to the expected size before the call, and updated to the actual address length after the call. |
On success, returns the actual number of bytes received
Example
udp_client.c
1 |
|
udp_server.c
1 |
|
TCP
The full English name of TCP isTransmission Control Protocol, which means Transmission Control Protocol, it is aconnection-orientedtransport layer protocol, and like UDP, it is a core part of the TCP/IP protocol stack.
TCP ensures the integrity, order, and losslessness of data during transmission by establishing a reliable end-to-end connection. It uses a three-way handshake to establish a connection and guarantees the reliability of data transmission through mechanisms such as acknowledgment, retransmission, and flow control.
Therefore, TCP is suitable for scenarios that require high data accuracy, such as file transfer, email, web browsing, etc. In these scenarios, data integrity and order are crucial, while tolerance for latency is relatively high.

- Compared to a UDP client, a TCP client has an additional step of calling connect to initiate a connection request.
- Compared to a UDP server, a TCP server has two additional steps: calling listen to start listening mode and calling accept to block and wait for client requests.
- listen sets the socket to passive mode to wait for client connection requests.
- When a client initiates a connection, accept blocks and waits until it receives a request, then returns a new socket file descriptor dedicated to communicating with that client.
After the client initiates a connection request via connect, it performs a TCP three-way handshake with the server to establish a connection. Once the connection is established, the client and server can send and receive data using write() and read().
After data transmission is complete, the client and server each close the connection using close().
TCP functions
connect()
connect() is a system call used to establish a connection, primarily on the client side. It initiates a connection request to the server by specifying the server’s address (IP address and port number). For the TCP protocol, connect() triggers the TCP three-way handshake process. Note that for the UDP protocol, connect can also be used, but it does not actually establish a connection; instead, it binds the target address to the socket for subsequent communication. Its function prototype is as follows:
1 | int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); |
Returns 0 on success, -1 on failure
| Parameter name | Type | Description |
|---|---|---|
sockfd | int | Socket file descriptor, created bysocket()The function creates a handle for all subsequent network operations. |
addr | struct sockaddr * | A pointer to the target address structure, typicallystruct sockaddr_in(IPv4) orstruct sockaddr_in6(IPv6), containing the IP address and port number to bind. |
addrlen | socklen_t | The size of the target address structure in bytes, e.g.,sizeof(struct sockaddr_in)。 |
listen()
listen()is a system call used to set a socket to listening mode, primarily for TCP servers. It converts the socket from active mode (for initiating connections) to passive mode (for receiving connection requests). Callinglisten()after that, the socket starts listening for connection requests from clients and maintains a waiting queue to manage these requests. Its function prototype is as follows
1 | int listen(int sockfd, int backlog); |
Function Length
| Parameter Name | Type | Description |
|---|---|---|
sockfd | int | Socket file descriptor, created bysocket()and bound to a specific address and port viabind(). |
backlog | int | The maximum length of the waiting connection queue, representing the number of TCP connection requests that can be queued for processing simultaneously. |
Returns 0 on success, -1 on failure,
accept()
accept()is a system call used to accept client connection requests, primarily for TCP servers. It retrieves a connection request from the listening socket’s waiting queue and creates a new socket dedicated to communicating with that client. Throughaccept(), the server can handle connection requests from multiple clients. Its function prototype is as follows:
1 | int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); |
The parameters of this function are described as follows:
| Parameter Name | Type | Description |
|---|---|---|
sockfd | int | Listening socket file descriptor, created bysocket()and set to listening mode vialisten(). |
addr | struct sockaddr * | Pointer to a structure storing the client address (e.g.,struct sockaddr_infor IPv4 addresses). Can beNULL, indicating no need to retrieve the client address. |
addrlen | socklen_t * | Pointer to the size of the client address structure. Must be initialized to the size of the target address structure before the call, and will be updated to the actual address length after the call. |
Returns 0 on success, -1 on failure,
Example
tcp_client.c
1 |
|
tcp_server.c
1 |
|
Minimal network device driver
You can use an existing driver in the Linux source code as a template, the driver path isdrivers/net/loopback.c. First, back up this file asloopback.c.bak, then create a newloopback.cfile, in which we write our minimal network device driver.
The driver content is as follows; the code implements a simple loopback network device driver.
1 |
|
Port the RTL8723DU (WiFi) driver
After extracting the vendor-provided code, modify the Makefile
1 | CONFIG_MULTIDRV = n |
makeCommand generationwlan.koFile

