Source: tekkamanninja.blog.chinaunix.net
I have recently read in-depth Linux Device Driver mechanism. This book is clear and I have benefited a lot from it. I used to know how to use the kernel driver. I didn't know the Internal principles in this way. At that time, the capabilities were limited, and it was not easy to learn more. This time, I took the opportunity to review the driving principles and carefully studied this book.
When I explain character devices in Chapter 2, I think there are two main gains:
1. character device number management principle (char_device_struct) 2. How do the functions in the file_operation drive of the character device correspond to the corresponding structure in the file struct and are called by the application program.
For the above two key knowledge points, I think the book is well organized and easy to understand. I will repeat it here. I will summarize the two knowledge points I learned using graphs for your reference. 1. The management principle of character device numbers focuses on the data structure char_device_struct and global hash chrdevs on which the kernel depends during management. There is also a need to know that the kernel has no connection with the registration and cancellation of device numbers and the implementation of driver functions. The management of device numbers is an independent mechanism to avoid conflicts between drivers when using device numbers, resulting in a corresponding error in file_operation in the driver, which leads to device operation errors at the application layer. Because the application layer performs operations on the device through the device number corresponding to the specific file_operation. 2. How is the function in file_operation driven by the character device mapped to the corresponding structure in the file structure and called by the application. This part of content is mainly to be familiar with the calling process of open functions. The file_operation struct in the driver corresponds to the corresponding function in the open function through the file struct related to the process by the device number. After the open operation is completed, other file operations can directly call the function in the drive file_operation. The management method of the char_device_struct struct in the kernel is similar to that of the cdev_map of the device number, which is indexed by the master device number as the key of the hash table.