Follow★Star the official account to get information first
1. Introduction
Opening a file is one of the most basic operations in the Linux system. The <span>open</span> function can achieve the functionality of opening files. Below, I will introduce to you the detailed process of how the <span>open</span> function connects the upper layer to the lower layer hardware.
2. Introduction to the open Function Bridging Software and Hardware
<span>open</span> function is a type of system call, its prototype is defined in the header file <span>unistd.h</span>:
#include <unistd.h>
int open(const char *pathname, int flags, mode_t mode);
Among them, the <span>pathname</span> parameter is the name of the file to be opened, the <span>flags</span> parameter is the flag for opening the file, and the <span>mode</span> parameter is the access permissions for the file.
When an application calls the <span>open</span> function, this function first creates a <span>file</span> structure in memory, which represents the connection between the application and the file, containing various attributes of the file, such as file type, file access permissions, file length, etc. This <span>file</span> structure is commonly referred to as a file handle or file descriptor, represented by a non-negative integer.
Next, the <span>open</span> function will call the VFS (<span>Virtual File System</span>) layer of the file system. The VFS layer is the core part of the Linux file system, whose role is to hide the various implementation details of the file system and provide a unified interface for upper-layer applications. The VFS layer determines the type and location of the file by looking up the super block of the file system and finds the corresponding file system object (<span>inode</span>).
After the VFS layer finds the file system object, it implements the file opening operation through the file operation functions in the driver (<span>file operations</span>), which typically include <span>open</span>, <span>read</span>, <span>write</span>, <span>lseek</span>, <span>ioctl</span>, etc. The file operation functions of the driver are defined in a structure, which is usually referred to as the <span>file_operations</span> structure, containing pointers to various file operation functions implemented in the driver.

3. The open Function in Drivers
In the driver program, the function that implements the file opening operation is usually the <span>open</span> function, whose prototype is defined in the header file <span>linux/fs.h</span>:
int (*open) (struct inode *, struct file *);
<span>open</span> function’s first parameter is the file system object (<span>inode</span>), and the second parameter is the file handle (<span>file</span>).
When the driver’s <span>open</span> function is called, it determines the way to open the file based on the file attributes and access flags (<span>flags</span>), such as read-only, write-only, read-write, etc. Then, it will perform a series of operations to bridge the file and the hardware device.
Specifically, when opening a file, the driver’s <span>open</span> function will call the <span>probe</span> function of the underlying device driver. The <span>probe</span> function initializes the hardware device and establishes a connection between the driver and the device based on the hardware type and address. During this process, the driver needs to complete a series of operations, such as opening the device bus, locating the device, initializing the device, registering the device, etc.
After establishing a connection between the device driver and the hardware device, the driver can send various commands to the hardware device by accessing device registers and executing device instructions, thus achieving control and access to the device.
When the application calls <span>read</span>, <span>write</span>, <span>ioctl</span>, and other functions to read and write files, these functions actually achieve this by calling the corresponding operation functions in the driver. These operation functions in the driver will copy data from the application space to the kernel space, and then copy the data from the kernel space to the registers of the hardware device, achieving data transmission and processing.
When the application calls the <span>close</span> function to close the file, the system will call the <span>release</span> function in the driver to free the resources occupied by the file. The <span>release</span> function is usually used to perform some cleanup tasks, such as closing the device, freeing memory, etc.
<span>open</span> function is the key to bridging the upper-layer application and the lower-layer hardware device. It connects the application and hardware device through a series of operations involving the file system, VFS layer, device driver, etc., enabling control and access to hardware devices. Therefore, for Linux driver development engineers, it is very important to deeply understand the implementation principles and internal mechanisms of the <span>open</span> function.
4. An Example
Below, I will take controlling GPIO as an example to introduce the role of the <span>open</span> function in it.
GPIO (<span>General Purpose Input/Output</span>) is a type of general-purpose input/output pin that can be controlled through programming to achieve control and interaction with external devices. In the Linux system, GPIO control is usually implemented through device drivers, which provide <span>open</span>, <span>read</span>, <span>write</span>, <span>ioctl</span>, and other functions to interact with user-space applications.
In a program that controls GPIO, it is usually necessary to first open the GPIO device before performing subsequent read and write operations. When the <span>open</span> function is called to open the GPIO device, the system will automatically call the <span>open</span> function of the GPIO device’s driver. In the <span>open</span> function, the device driver will determine the state and attributes of the GPIO device based on the parameters passed in and initialize it.
Next, the device driver will call the GPIO subsystem in the kernel to access the hardware device, such as reading the GPIO level state, setting the GPIO level state, etc. The GPIO subsystem is responsible for converting the data from the kernel space into the signals required by the hardware, achieving control over the GPIO.
When the application needs to perform read and write operations on GPIO, it will call the <span>read</span>, <span>write</span> functions in the device driver. In the <span>read</span> function, the device driver will read the GPIO level state through the GPIO subsystem and return it to the application; in the <span>write</span> function, the device driver will set the GPIO level state through the GPIO subsystem, achieving control over the GPIO.
Finally, when the application closes the GPIO device, it will call the <span>release</span> function in the device driver, which is responsible for releasing the resources occupied by the GPIO device and closing the GPIO device.
In summary, the <span>open</span> function plays a role in connecting the application and the underlying hardware device in the GPIO control program. It connects the application and the GPIO device through calling the driver’s open function and the GPIO subsystem in the kernel, achieving control and access to the GPIO.

END
Author:JamesBin
Source:Embedded Yuxiang GardenCopyright belongs to the original author. If there is any infringement, please contact for deletion..▍Recommended ReadingAn Irresistible VSCode Plugin!A Mobile Operating System Designed Based on FreeRTOSThe Most Legendary American Programmer, Only Using China’s Loongson Computer…→ Follow to avoid getting lost ←