Android Deep Explore (Vol. 1) Hal with driver development
Sixth Chapter First Linux drivers: Count the number of words
The function of counting the number of words is encapsulated in the Linux driver.
Linux drivers work and access is one of the Linux highlights, at the same time by the industry's high praise. The Linux system maps each driver into a single file. These files, called device files or drive files, are stored in the/dev directory. This design concept makes interacting with Linux drivers as easy as interacting with ordinary files. Of course, it's easier than accessing the Linux API. Since most Linux drivers have their corresponding device files, exchanging data with Linux drives becomes a data exchange with the file.
Steps to write a Linux driver
1. Build Linux drive skeleton (load and unload Linux drivers)
The Linux kernel requires a load driver first when it uses drivers. Some initialization operations are required during the loading process. When the system exits, the Linux driver needs to be uninstalled, and the resources consumed by the Linux driver need to be freed during the uninstallation process. In the Linux driver, you need to provide two functions to handle the operation of the driver initialization and exit separately. The two functions are specified using the module and MODDULE_EXIT macros, respectively.
2. Registration and cancellation of equipment files
3, specify the information related to the drive
4. Specifying a callback function
5. Write Business logic
6. Writing Makefile Files
The compilation rules for the Linux kernel source code are defined by the makefile file. So writing a new Linux driver must have a makefile file
7. Compiling Linux Drivers
8. Install and uninstall Linux drivers
First Linux driver: Count the numbers of words
1. #mkdir建立目录文件夹
2. Writing makefile Files
Obj-m: = Vord COUNT.O
Word_count-y: = PROCESS.O DATA.O
Writing the skeleton of the Linux driver (Initialize and exit drivers)
Specify driver-related information
Specifying a callback function
An algorithm for implementing statistical word numbers
To write the Word_count driver:
1. The Get_word_count function takes the first character in the Mem array as the end of the string, so the value of Mem[count] in the Word_count_write function is set to "X", otherwise Get_word_count function does not know where to end the string to count the word book.
2, the number of words using the INT type variable storage, in the Word_count_write function to count the number of words (word_count variable value), in the Word_count_read function will word_count integer variable value into 4 characters stored in buf, Therefore, you need to combine these four bytes into a value of type int in your application
compiling, installing, and uninstalling Linux drivers
Test Linux drivers in a variety of ways
Developing and testing Linux drivers using Exlipse
The Word_count driver for counting the number of words is the first complete Linux drive implemented in this book.
Android Deep Explore (Volume 1) HAL and Driver Development chapter sixth the first Linux driver: Count the number of words