Write a Linux driver
1. Build Linux driver skeleton
Linux kernel requires load and unload drivers when using drivers
Load driver: Set up device file, allocate memory address space, etc. module_init function processing driver initialization
Uninstall drive: Delete device files, free memory address space, etc. module_exit function processing exit
A C program file containing the two macros of these two functions can also be seen as a Linux-powered skeleton
2. Registering and unregistering device files
Any Linux driver requires a device file, or the application will not be able to interact with the driver.
Set up the device file: Completed in the first step of the function to process the Linux initialization work. Misc_register function
Delete device files: Completed in the first step of the function to process the Linux exit. Misc_deregister function
3. Specify driver-related information
The driver is self-describing, the driver's author name, the open source protocol used, the alias, the driver description, and so on. This information needs to be specified in the driver source code.
Module_author, Module_license, Module_alls, Module_descripion, and other macros can specify driver-related information
4. Specifying a callback function
A driver does not have to specify all the callback functions, and the callback function is registered by the relevant mechanism.
5. Writing business logic
Specific business logic is related to the functionality of the drive, and the business logic may consist of multiple functions, multiple files, or even multiple Linux driver modules
6. Writing the makefile file
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
Can be compiled directly into the kernel or can be compiled separately as a module
8. Install and uninstall Linux drivers
If the Linux driver is compiled into the kernel, as long as Linux uses the kernel, the driver will be automatically reproduced
If the Linux driver is in a separate module, you need to load the Linux driver module with the Insmod or Modprode command, and the Rmmod command uninstalls the Linux driver module.
Two first Linux drivers, taking Word_count as an example
(i) The basis for writing code
#mkdir-P/root/drivers/ch06/word_count build directory to store Linux drivers
#cd/root/drivers/ch06/word_count
#echo ' > word_count.c build Drive source code files
#echo ' obj-m: = WORD_COUNT.O ' > Makefile write a Makefile file make command to compile word_count.c or WORD_COUNT.S files in the Linux drive source directory into Word_ COUNT.O file
Obj-m means that the Linux driver is compiled as a module (. ko file) and WORD_COUNT.O is connected to the Word_count.ko file and then loaded with Insmod or Modprode commands Word_count.ko
Obj-y means the Linux driver is compiled into the Linux kernel, WORD_COUNT.O will be connected to the BUILT-IN.O file and will eventually be connected into the kernel
The Linux system divides the memory into the user space and the kernel space, the space program cannot directly access, the PRINTK function runs in the kernel space, the printf function runs in the user space, therefore the Linux driver which belongs to the kernel program does not have the direct access to the printf function.
#make-C/usr/src/linux-headers-3.0.0-15-generic m=/root/driver/ch06/word_count compiling Linux driver source code
# insmod Word_count.ko Load Drive
# Lsmod | grep Word_count to see if Word_count installed successfully
# rmmod word_count Uninstall Linux driver
#dmesg | grep Word_count | Tail-n 2 Viewing log information with Linux driver output
(ii) Inclusion of the code for the specified information
Module Module_author ("Lining");
Module Description: Module_descrption ("Statistics of Word count.");
Module alias: Module_alias ("word Count MODULE.");
Open Source Agreement: Module_license ("GPL");
#define DEVICE_NAME "WordCount"//define Device file name
Describes the callback function pointer corresponding to the event triggered by the device file
Owner: Device event callback which driver module is applied to, This_module represents the current driver module
static struct File_operations Dev_fops={.owner = this_module};
Information describing the device file
Minor: Secondary device number Misc_dynamic_minor,: Dynamically generated secondary device number name: Device filename
fops:file_operations struct variable pointer
static struct Miscdevice Misc={.minor = Misc_dynamic_minor,. Name=device_name,.fops = &dev_fops};
Initializing Linux drivers
static int word_count_init (void)
{int ret;
ret = Misc_register (&MISC);
PRINTK ("word_count_init_success\n");
return ret;
}
Uninstalling Linux Drivers
static void Word_count_exit (void)
{Misc_deregister (&MISC);
PRINTK ("word_inti_exit_success\n");
}
Because kernel-space programs do not have direct access to data in user space, you need to use Copy_to_user and Copy_ respectively in Word_count_read (read data from device files) and Word_count_write (write data to device files) functions The From_user function copies data from the kernel space to the user space or from the user space to the kernel space
(iii) loading and unloading drive
Check if the Word_count drive is fully operational
#dmesg | Tail-n 1
#modinfo Word_count.ko
Detecting dependencies for Linux driver modules
#depmod/root/drivers/ch06/word_count/word_count.ko
Calling commands to load Linux drivers
#modprode Word_count
Note: Both the Insmod and Modprode commands are load-driven, which can check the dependency of the driver module
Three + ways to test Linux drivers
(i) Testing Linux drivers with Ubuntu Linux
You need to write a program specifically for testing, such as test_word_count.c
#gcc Test_word_count.c-o Test_word_count
#test_word_count
#test_word_count "I love you."
Output result: String:i love you.
Word byte display:0,0,0,3
Word count:3
(ii) test Linux drivers with native C programs on the Android emulator
#cd ~/kernel/goldfish
#make Menuconfig
Following the setup, after recompiling the Linux kernel, the Android emulator can dynamically load the Linux driver module using the sexually generated Zimage kernel file after the kernel has been successfully compiled.
Two conditions can directly run the normal Linux program: Android simulator, board or mobile phone need to have root permission, executable files need to use cross compiler to compile Test_word_ count.c file, build android.mk set compilation parameters and compile with make command
#mm compile the. c file in the appropriate directory
#adb push./emulator/test_word_count/data/local Upload to Android emulator
Execute the following command to test the driver
#chmod 777/data/local/test_word_count Set executable permissions
#/data/local/test_word_count
#/data/local/test_word_count ' a BB CCC ddd Eee '
Output: 5, indicating successful test
(iii) using the Android NDK test
(iv) Use of Development Board testing
(v) Compiling the driver into the Linux kernel for testing
Four developing Linux drivers in eclipse
The first step: Build C Engineering
Step Two: Establish C source code file link New>source folder folder name Input src, import word_count.c file
Step three: Set include path click menu Items properties,c/c++general > Paths and Symbols, select the GNU C entry for include, add two paths:/root/kernel/goldfish /include and/root/kernel/goldfish/arch/arm/include
Fourth step: Compiling Linux drivers
Testing Linux Drivers
First step: Import the Test_word_count.c file
Step two: Set the Include path
Step three: Set Target make target > Create, enter Word_count_eclipse_test in the Target Name text box, click OK
Fourth step: Build Project Make Target > Build, select the file created in the third step, then click Build
Fifth step: Running the test program run as > Local C + + application
Detailed explanation of Linux drivers