Our drive module has been able to automatically assign the main device number, can say so far, a drive module has a number of "infrastructure" it has, like threat the previous date, Lloyd, has been able to let first_drv this drive module go out to war.
So to go out to war, there must be a commanding officer, in the Linux system, the manoeuvres's head is the application, the application is the big boss, we "have been finely crafted drive module" plainly, just a runner.
That's why our topic is the 1th Linux driver to talk about the application, because the application is for the driver to be able to be applied, and the driver is to let the application be driven, the two is a servant relationship, partnership, in the Linux system is indispensable.
In fact, when it comes to applications, you should also have a sense of affinity, it is written in C, but before you write the application, you'd better have some knowledge of the C language's file IO operations.
The name of the application is set to APP.C, first there must be a main function:
int main (void) {return 0;}
The framework has been completed, so what can we write in app.c to achieve the call to first_drv this driver?
We said that by Register_chrdev this function to register the driver in the kernel, the system will be able to access and use the driver, and the kernel registration driver refers to the driver information is stored in an array, and the number of elements stored in this array is dependent on the driver's main device numbers, For example, the main device number is 100, which is stored in the array of the 100th lattice.
In fact, the application is through the device files to find the driver, then the best way is to open the device file, know the main device number of the drive, and then through the main device number to find the driver.
We set up a device file by executing the command "Mknod/dev/test Master device number".
If the implementation: Mknod/dev/test 100 0 Set up a master device number 100, the second device number 0 device file, this secondary device number is what we do not care, we just know that the main device number 100 corresponds to a unique driver module.
With 100 of this main device number, you can find a certain driver module.
We will later set up the FIRST_DRV device file/dev/test, first in app.c to open the device file/dev/test code to write good:
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>int main ( void) {int FD =-1; FD = open ("/dev/test", O_RDWR); if (FD < 0) {printf ("Open/dev/test failure! \ n "); return-1; } printf ("Open/dev/test success...\n"); Close (FD); return 0;}
Open is the API interface that the Linux kernel provides for us to access the system, such as system calls such as read, write, close, and so on.
With the addition of APP.C, we have to modify the makefile all, CP, and Clean commands as follows before compiling:
#KERN_VER = $ (Shell uname-r) #KERN_DIR =/lib/modules/$ (kern_ver)/buildkern_dir =/work/kernel/linux-2.6.22.6all:m Ake-c $ (kern_dir) m= ' pwd ' modules arm-linux-gcc-o app app.c CP:CP *.ko App/work/my_drivers/ko_fi Le clean:make-c $ (kern_dir) m= ' pwd ' modules clean rm-rf module.symvers app obj-m + = Zero_ drv.o
After modifying the makefile, perform the Make:
You can see the executable file that generated the app, and make CP copies it and First_drv.ko to the/mnt directory of the smallest root filesystem.
Execute under/mnt directory:./app (Make sure driver module First_drv is installed before execution)
Printed: Open/dev/test failure!
/dev/test failed to open the device file, this is the printing information we wrote in our app.c when opening the device file failed.
Why the open failed because we haven't created this device file yet,
Let's go first. Cat/proc/devices Check the current FIRST_DRV Drive module's main device number, I am here is 252, you go to see your main device number is how much. The second equipment number tentative 0 good.
I'm doing this here: Mknod/dev/test C 252 0
No exception, creation succeeded.
Then execute the./app
Show:
It's in First_drv_open.
Open/dev/test success ... It's in First_drv_release.
(Here is a blank line)
(cursor in this line)
Where "It's in First_drv_open." is the printing information for the First_drv_open function in our driver, and "open/dev/test success ..." is the printing information in the application, and we see that the "it is in First_drv_release" was printed immediately after the carriage return. , this is the printing information for the First_drv_release function in our driver.
The strange thing is that we added/N, why did not enter, because the driver priority than the application is higher, when the application is printing information, if the driver also to perform printing information, then the driver will be given priority to print information, when the driver print is finished, then let the application to print information , this is why the 3rd sentence is directly behind the 2nd sentence, and the 3rd sentence after the output of the 2nd sentence the last return.
This does not affect our functional testing, we are aware of this phenomenon is good.
In a word, we run the application and let the driver work.
Let's take a little bit of an improvement on the application app.c:
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #define FILE "/dev/test" int main (void) {int FD =-1; FD = open (FILE, O_RDWR); if (FD < 0) {printf ("Open%s failure!\n", FILE); return-1; } printf ("Open%s success...\n", FILE); Close (FD); return 0;}
It simply adds a macro definition that makes it easy to modify the device file name.
Once again, the driver is also allowed to work.
But our current call is simply to let the driver print a message, and the next blog post we will continue to refine the driver so that the driver can do more things when it is called by the application.
This article is from the "12253782" blog, please be sure to keep this source http://12263782.blog.51cto.com/12253782/1874748
1th Linux Driver ___ application is the Big Boss