Sixth, the first Linux driver: Count the numbers of words
First, the steps to write a Linux driver
1. Build Linux driver skeleton (load and unload Linux drivers)
Module_init processing driver initialization, Module_exit processing driver exit
2. Registering and unregistering device files
Misc_register function creation and misc_deregister removal of device files
3. Specify driver-related information
Modinfo
4. Specifying a callback function
5. Writing business logic
6. Writing the makefile file
7. Compiling Linux Drivers
8. Install and uninstall Linux drivers
Two Linux drivers: Count the number of words
1. Preparatory work
(1) Create a directory to store Linux drivers
Mkdir–p/root/drivers/ch06/word_count
Cd/root/drivers/ch06/word_count
Create a drive source code file
Echo ' >word_count.c
Writing makefile
Echo ' obj-m: = WORD_COUNT.O ' > Makefile (obj-m means compiling Linux driver as module, obj-y means compiling into Linux kernel)
When Linux drivers rely on other programs, you need to write makefile files like this
Obj-m:=word_count.o
Word_count-y: =PROCESS.O DATA.O
2. Writing the skeleton of the Linux driver (Initialize and exit drivers)
The PRINTK function, which is used to output log information
Test compiling Linux driver source code (not necessarily on Android device)
Make–c/usr/src/linux-headers-3.0.0-15-generic M=/root/driver/ch06/word_count
Installing Linux Drivers
Insmod Word_ount.ko
To see if the Word_count installation was successful
Lsmod | grep word_count
Uninstalling Linux Drivers
Rmmod Word-count
View log information for Linux driver output
DMESSG |grep Word_count | Tail–n 2 or Cat/var/log/syslog | grep Word_count | Tail–n 2
3. Open source Agreement, GPL agreement, LGPL agreement, BSD agreement, Apachelicence2.0 agreement, MIT Agreement
4. Registration and cancellation of equipment
Extern int Misc_register (struct Miscdevice * misc)
Extern int Misc_deregister (struct Miscdevice * misc)
You also need to modify the Word_count_init and Word_count_exit functions in the word_count.c file
The device file is described by the main device number and the secondary device number. The master device number is set to 10, which is a simple character device with common features in a Linux system, called a misc device. When the device file is successfully registered, the Misc_register function returns an integer other than 0, and the failure returns 0.
Insmod Word_count.ko
Rmmod Word_count
Ls–a/dev
Ls–l/dev
Cat/proc/devices (get master device and its main device number)
5. Specify callback functions Word_count_read and Word_count_write
The most common way to interact is to read and write device files.
6. Implement
The number of words is stored using the INT type variable. The mem array has a length of 10000, and the last character is "s", so the string with the statistic has a maximum length of 999
7. Compiling, installing, and uninstalling Linux drivers
III. testing Linux drivers in a variety of ways
1. Testing Linux drivers with Ubuntu Linux
2. Test the Linux driver with the native (Native) C program on the Android emulator
3. Testing Linux drivers with the Android NDK
4. Use Java code to directly manipulate device files to test Linux drivers
5. Using the s3c6410 Development Board side-Test Linux driver
6. Test the driver compilation by the Linux kernel
Iv. developing and testing Linux drivers using eclipse
1. Developing Linux drivers in eclipse
Build C Project
Establish c source code file link
Setting the Include path
Compiling Linux Drivers
2. Testing Linux drivers in eclipse
Import test-word_count.c
Setting the Include path
Set Target
Build Project
Run the test program
Sixth, the first Linux driver: Count the number of words reading notes