Sysfs File System
The sysfs file system is introduced in the linux 2.6 kernel as a medium for interaction between user space and kernel space. Compared with the old proc file system, it is based on the kernel data structure, so the organizational structure is more rigorous. Its design makes kernel information easier to obtain and clearer. The ing between kernel space and user space is shown in the following table:
| Kernel space (internel) |
User space (externel) |
Kernel Object (Kernel objects) |
Directory (Directories) |
Object Attributes (Object attributes) |
Common File (Regular files) |
Object relationship (Object relationshiops) |
Symbolic Link (Symbolic links) |
For the temperature sensor, the driver can use the interface provided by the kernel sysfs file system to establish the directory structure of the corresponding temperature sensor. By accessing common files in the corresponding directory, the application can obtain the corresponding temperature sensor attribute values. You can build an access interface for a temperature sensor following the kobject-example.c provided by the linux 2.6 kernel, located in <linux src>/samples/kobject/kobject-example.c. For example, you can use temperature as the temperature sensor object directory under the/sys/kernel/directory in the driver, and create a file named temp under this directory, it is an attribute of the temperature sensor, and a mode file that provides the working mode of the temperature sensor. According to the above instructions, our temperature sensor driver needs to be initialized in the module, use the statement
Temp_kobj = kobject_create_and_add ("temperature", kernel_kobj)Create the temperature directory, and then use
Sysfs_create_group (temp_kobj, & attr_group)Create an attribute file group under the temperature directory (which contains the temp and mode files). attr_group is an attribute-Related File defined by us, we describe the property files in the temperature directory, as well as the show and store functions of each file. In this example, an attribute array attrs can be used to describe this attribute group attr_group. The attrs array package contains two elements, corresponding to the temp and mode files. Only temp is used here.
File description. Temp_show of the temp file provides the underlying method for applications to access the file. to simulate the changing temperature, every time the application reads the temp file, we randomly change the temperature. Therefore, in the temp_show method, the get_random_bytes function provided by linux is used to generate random numbers (included in the header file linux/random. h ). Temp_show provides two methods to obtain the temperature at a single point and the average temperature based on different modes. After the HAL interface test driver is loaded, we can see that the sysfs file system contains the/sys/kernel/temperature/directory, and there is a file temp under the directory, we can use cat/sys/kernel/temperature/temp to read its value, so this file provides an upper-layer common interface for the application to access the temperature sensor driver. In the test program test_temp.c, open the/sys/kernel/temperature/temp File and read the file content every second (Note: Use the lseek function to move the file pointer to the beginning each time.
). Enable/sys/kernel/temperature/mode to view and set the mode. Run./test_temp in the command line after compilation. The test result is shown in: