What is a 1.Linux drive:
the Linux system maps each driver into a file, which is called a device file or a driver file. exchanging data with a Linux driver is equivalent to exchanging data with a device file. The most important step in writing a Linux driver is to write a callback function, otherwise data that interacts with the device file will not be processed.
2. steps to write a Linux driver:
Build a linux driver skeleton (load and download Linux drivers), register and unregister device files, specify driver-related information, specify callback functions, write business logic, write Makefile files, compiling Linux drivers, installing and uninstalling Linux drivers.
3. First Linux driver (count of words):
Using device files as media to interact with the application, the application passes a space-delimited string to the device file, and then reads the number of words from the device file as a string.
Before writing the Linux driver (build The directory where the Linux driver is stored, build the drive source code file, and finally write a Makefile files).
WriteLinuxThe skeleton of the driver is the initialization and exit drive (PRINTKfunction is used to output log information,printffunctions run in user space,LinuxThe driver does not have direct access to programs running in user space.mallocThe function allocates memory space dynamically;LinuxDrive:#insmod Word_count.ko,ViewWord_countwhether the installation was successful:# lsmod |grep word_count; uninstallLinuxDrive:# rmmod Word_count; View aLinuxlog information for driver output:#dmesg |grep word_count |tail-n 2).
Specifies driver-related information (typically requires the following information for Linux drivers: module author, module description, module alias, open Source protocol).
Registering and unregistering device files (unlike device files and normal files, cannot be established using IO functions, you need to use the misc_register function to set up device files, use misc_ The deregister function unregisters the device file).
Specifies the callback function (via file_oerations.read and file_operations.write Member variables can specify the callback function pointer to be called by the read-write device file, respectively.
An algorithm that implements the number of statistical words ( The number of words is stored with a variable of type int).
compiling, installing, and uninstalling Linux drivers (both theinsmod command and the modprobe command can install Linux Drive, the difference is modprobe command to check the dependency of the driver module , when using modprobe command before loading the driver module, you need to first use the Depmod Command Detection Linux dependency of the driver module).
4. Test Linux drivers in a variety of ways:
>> use Ubuntu linux to test linux drivers (#sh build.sh, #echo ' I love you. >/dev/wordcount ', #dmesg).
In android simulator via native ( native) C program test linux Span style= "font-family: the song Body;" > drive (compilation can be in android The that runs on the emulator. linux driver module to use the goldfish kernel).
>> using android ndk test linux drive (in android system linux apk program, so Span style= "FONT-FAMILY:CALIBRI;" >linux After the driver is done, you must use apk Span style= "font-family: the song Body;" > The program is tested to illustrate the linux driver can be used normally).
>> using java code to directly manipulate device files to test linux driver (if android have root permissions, you can directly use the java code to manipulate the device file, no root permissions, linux The driver module is not installed.)
>> using s3c6410 Dev Board test Span style= "FONT-FAMILY:CALIBRI;" >linux drive (in s3c6410 The board uses an executable program to test the linux drive, in s3c6410 Development board using ANDROID NDK test linux drive, in s3c6410 use java code test linux drive).
>> compiles the driver into the Linux kernel for testing (the embedded system generally uses the way the Linux driver is compiled into the kernel, although it does not have dynamic loading flexibility, but Linux drivers are automatically loaded as Android starts).
5. Develop and test Linux drivers using Eclipse :
>> in eclipse development linux driver (build c project, establish c Source-code file link, set include path, compile linux
>> testing Linux drivers in Eclipse (importing test_word_count.c files, setting Include path, set Target, build project, run test program).
Address: http://www.cnblogs.com/zpm123123
Sixth. First Linux driver: Count the number of words