The Linux system maps each driver into a single file. These files are called device files or drive files and are saved in the/dev directory. The most important step in writing a Linux driver is to write a callback function, otherwise data that interacts with the device file cannot be processed.
1. steps to write a Linux driver:
1th Step: Build Linu x Drive skeleton (load and unload Linu x drive):
The Linux kernel needs to load the driver first when it uses the driver. When the Linux system exits, the Linux driver needs to be uninstalled, and the resources used by the Linux driver are freed during the uninstallation process, such as deleting the device files, freeing the memory address space, and so on. In the Linux driver, you need to provide two functions to handle the operation of the driver initialization and exit, respectively Module_init and MODULE_EXIT macros are specified.
2nd Step: Register and unregister the device files:
The establishment of the device file is generally done in the first step of the process of processing the Linux initialization function------misc_register Create the device file;
Deleting a device file is generally done in the first step of processing the Linux Exit Function-----misc_deregister Delete the device file.
3rd Step: Specify driver-related information:
Drivers are self-describing. For example: The Modififo command gets the driver's author, name, open source protocol used, alias, driver description, and so on. Pass
Module_author\module_license, etc. completed.
4th step: Specify the callback function
Event: Many of the actions that Linux contains. For example, if a device file is written to a data name called a write event, the Linux system invokes the write callback function of the corresponding driver, and the read event is triggered when the data is read from the device file, and the Linux system invokes the corresponding read function.
The callback function is registered through the mechanism, for example, the callback function associated with the device file is registered through the Misc_register function.
5th step: Write the business logic:
The core part, the concrete realization decides according to the concrete situation.
6th step: Write the Makefile file:
Linu: The compilation rules for the kernel source code are defined by the Makefi le file.
7th step: Compiling Linux drivers
Linux drivers, which can be compiled directly into the kernel or compiled separately as modules.
8th step: Install and Uninstall Linux drivers:
1. If the Linux driver is compiled into the kernel, the Linux kernel driver will be loaded automatically as soon as it is used.
2. If the Linux driver module exists separately, use the Insomod or modprobe command to load the Linux driver module, and use the Rmmod command to unload the module.
Note: Differentiate the insmod and Modprobe commands:
The modprobe can check the dependency of the driver module. such as a module dependent on the sub-B module (before loading A must first load B). If you use the Insmod command to mount a module, an error occurs. Using the modprobe command to load A module, the B module is now loaded. Before loading the driver module with the Modprobe command, you need to use the DEPMOD command to detect the dependencies of the Linux driver module.
2. Learn the first Linux driver: Count the number of words: Use device files as media to interact with the application.
Content: The application passes a space-delimited string to the device file (each substring separated by a space is called a word), and then reads from the device file the number of words that the string contains.
1> Preparatory work:
Code: 1. Create a directory to store Linux drivers:
Mkdir–p/root/drivers/ch06/word_count
Cd/root/drivers/ch06/word_count
2. Create a driver source code file (. c)
Ehco ' >word_count.c
3. Write a Makefile file
Echo ' obj-m: =WORD_COUNT.O ' > Makefile
Where obj-m indicates that the Linux driver is compiled as a module (. ko file).
If you use Obj-y, the Linux driver is compiled into the Linux kernel.
Where the BUILT-IN.O file is an intermediate destination file generated by a. o file that is connected to a class of programs. The target file contains all character drivers that can be connected to the Linux kernel (the Make Menuconfig command allows you to configure whether each driver and other kernel programs are allowed to compile into the kernel).
If the Linux driver relies on other programs, such as PROCESS.C, DATA.C, you need to write the Makefile file as follows.
Obj-m: = Word_count. 。
Word_count_y: = PROCESS.O DATA.O
Where dependent files are specified using Module_y or MODULE_OBJS. Module represents the name of the block, such as Word_count.
2> writing the skeleton of the Linux driver:
The Linux system includes: User space "printf" and kernel space "PRINTK", and user space can not directly access the kernel space. User space writes a driver that can access the kernel, and the user space accesses the driver by accessing the device file.
So how do you dynamically allocate memory space in a Linux driver? It's also easy to solve similar problems. Since the Linux driver cannot directly invoke functions running in user space, it is necessary to provide alternatives in the Linux kernel.
Use the following command to compile the Linux driver source code.
Make-c/usr/src/linux-headers-3.0.0-15-generic m=/Root/driver/ch06/word_count
Installation drive: Insmod Xxxx.ko
Uninstall driver: Rmmod xxxx
To see if the driver was installed successfully: Lsmod |grep xxx
To view log information from Linux-driven output:
DMESG | grep xxx|tail-n 2
Cat/usr/log/syslog|grep XXX |tail–n 2
3> Specify driver-related information:
- It is generally necessary for Linux drivers to specify the following information:
The module is specified using the module AUTHOR macro.
Module Description: Specified using the module DESCRIPTION macro.
Module aliases: Specified using the module alias macro.
Open Source protocol z is specified using the Module_license macro.
2. View. ko file information using:
Modinfo WORD_COUNT.KC
3. Use the previously described command to specify the above 4 types of information.
For example: Module_author ("linging");
5. Open Source Agreement: GPL, LGPL, BSD protocol, Apache license2.0 protocol, MIT Protocol.
Here is the introduction of the GPL: Universal Public License (General License, referred to as the GPL). The GPL, like other free software licenses, permits the public to enjoy the freedom to run and reproduce software, to distribute the software freely, to gain the freedom to source the software, to improve the software and to disseminate the improved version of itself to the society. And the other difference: Open source is more thorough and contagious.
4> registering and unregistering device files:
The device files are located in the/dev directory. Unlike normal files, device files cannot be established using the IO function, you need to use the Misc_register function to create a device file, and use the Misc_deregister function to log off (remove) the device file. The device file also requires a struct (miscdevice) to describe the information associated with it.
Attention:
1. The device file is described by the main device number and the secondary device number. Instead, use the Misc_register function to set only the secondary device number. The master device number is set to 10 uniformly. The device with the main device Number 10 is a simple character device with common features in the Linux system.
2. Fileoperations.owner=module structure: Indicates that fileoperation is applied to these driver modules with module designation
Fileoperations.owner=this.module indicates that only the current module can be applied
3. Registration successfully returns a non-0 integer, registration failed, return 0
The 4.static:c language uses static to declare the functions, variables and other resources, the system will put these functions and variables in one area of memory, until the program exits completely, otherwise these resources will not be freed.
5. Use Ls–l/dev to view the primary and secondary device numbers.
6. Use Cat/proc/devices to show which primary and secondary device numbers are in the current system.
5> Specifying a callback function
Through File_operations.read and file_operations. The write member variable can specify the callback function pointer to be called by the read-write device file, respectively.
6> algorithm to achieve the number of statistical words:
The string separated by a space, tab, carriage return, and newline character is counted as a word.
4. Test Linux drivers in many different ways:
1. Test Linux drivers with Ubuntu _linux:
2. Test the Linux driver with the native (Native) C program on the Android emulator
The first method:
1> See if Word Count.ko can be installed on the Android emulator?
Answer: Execute Bui ldsh script-à select "Android Emulator" [script will automatically upload word Count.ko file to the IDA full/local directory of Android emulator and install].
2> Note: If the reader chooses the s3c6410 Development Board, the following error message is printed when you install Word Count.ko:
Insmod:init_module '/data/local/word_count. Ko ' failed (Function not implemented)
The Linux kernel version that compiles Linux drivers is not the same version as the current Android emulator and cannot be installed. So when compiling Linux drivers, you must choose the same Linux kernel as the version of the Linux kernel that is currently running, or you will not be able to install Linux drivers.
3> the goldfish kernel for Android emulator does not allow dynamic loading of Linux drivers by default, so you need to configure the Linux kernel with the following command before compiling the Linux kernel: cd/kernel/goldfish
Make Menuconfig
4> now executes the build.sh script file to complete the compilation, upload, and installation of the Word_count driver, then goes to the Android emulator terminal and uses the Echo and DMESG commands to test the Word_count drive and view the test results.
The second method:
1>android Simulator can directly run the normal Linux program needs to meet a few conditions:
The Android emulator, board or phone needs to have root privileges.
The executable file needs to be compiled with a cross-compiler to support the ARM processor.
2> now starts Testing:
First, create a android.mk file in the/root/drivers/ch06/word_count directory. and enter the following content:
LOCAL path:= $ (call My-dir)
Include $ (clear_ VARS)
# Specify the source code file to compile
LOCAL _src _files: = Test_word _count. C
# Specifies the module name, which is also the executable file name generated after compilation
Local_module: = Test_word_count
LOCAL Module_tags: =optional
Include $ (build_ executable)
LOCAL. MODULE _tags. Indicates in what mode the current project (the directory where the Android.mk file resides) is compiled.
Second: 1. In order to compile the test_word_count.c file into an executable program that can be run on the Android emulator, you can copy the Word_count directory to a subdirectory of the <android source code directory, or you can use the
Ln-s/root/drivers/ch06/word_count/sources/android/android4/development/word_ Count
2. Now enter the/sources/android/android4 directory and execute the following command to initialize the compile command source./build/envsetup. sh
3. You can compile using the following two methods: Test_word_count.c.
(1) Enter the/sources/android/android4/development/word_count directory and execute the following command: MM
(2) Perform the following command on/sources/android/android self recording:
Mmm Development/word_count
4. Now execute the following command to upload the Test_word_count file to the Android emulator:
# adb push./emulator/test_word_count/data/local
5. Then enter the terminal of the Android emulator and perform the following command to test the Word_count driver (you need to set the executable permission of Test_word_count using the chmod command first):
# chmod 777/data/1ocal/test _word _count
#/data/local/test_word_count
#/data/1ocal/test _word_ count ' a BB CCC ddd Eee '
After executing the above command, if the output of the number of words is 5, indicating that the program test was successful.
3. Testing Linux drivers with the Android NDK
4. Use Java code to directly manipulate device files to test Linux drivers
5. Test the Linux driver with the s3c6410 Development Board
6. Compile the driver into the Linux kernel for testing
Use the following procedure to add the Word_count driver to the Linux kernel source tree:
1th step: Put the word_count.c file into the Linux kernel source code: Place the word_count.c file in the
2nd Step: Modify the Kconfig file:
Open the/root/kemel/goldfish/drivers/char/kconfig file, locate Endmenu, and add the following code in front of Eodmenu:
Config Word_count
Boo1 "Word_count Driver"
Help
This is a word count driver. It can get a word count from/dev/wordcount
3rd Step: Modify the Makefile file
obj-$ (config_word_count) + = Word_count. O
4th step: Set the. config file
5th step: Compiling the Linux kernel
Enter the/root/kernel/goldfish directory and execute the following command to compile the Linux kernel: Make
5. Developing and testing Linux drivers using eclipse
1> developing the Li nu x driver in Eclipse
The steps are as follows:
1th Step: Set up the C project---2nd step: Establish C source code file link [click the "New" > "Soruce Folder" menu item in the Word__count_eclipse Project context menu to open the "New Soruce Folder Dialog" in the " Folder Name text box enter "src"--à click "Finish" button to create a directory to save the Linux driver source code files----Click the SRC directory---Right-click on the "Import" menu item--Open the "Import" dialog box-select " File System Item---Click the Next button to enter the next Setup screen-select word_count.c--Click Advanced-----Select the Create links in workspace check box---finish]-- ----3rd Step: Set up the include path:/root/kernel/goldfish/include/root/kernel/goldfish/arc/arm/include------4th step: Compiling the Linux driver.
2> testing Linux drivers in Eclipse
1th Step: Import the test_word_count.c file---2nd step: Set up the Include path------3rd step: Build target[Click Word_count_ eclipse_ Test Project Right Menu "Create" menu item, open the "Create Make Target" dialog box, enter "Word_count_eclipse_test" in the "Target Name" text box, then click the "OK" button to close the dialog "4th Step-----: Build project [Click on the "Make Targets" and "Build" menu item in the Word_count_eclipse_test project's context menu to open the "Create Targets" dialog box and select the "Word_count_ eclip" in step 3rd Se_test ", and then click the" Build "button, which will generate some configuration files in the Word_count_eclipse_test project]------the 5th step; run the test program [click Run as > Local C + + on the project context menu] Application "menu item]
Statistics of the number of words case