Code for writing kernel driver modules
The required header files typically involved in the driver module:
<linux/init.h>
<linux/module.h>
<linux/kernel.h>
Specification of the entry function for the drive module:
int __init entry_name (void)
{
/*xxx*/
return 0;
}
Module_init (Entry_name);
Specification for export functions of drive modules:
void __exit exit_name (void)
{
}
Module_exit (Exit_name);
Declaration of the information of the module:
Module_license ("GPL"); This line must be written in the module, the following three lines are optional
Module_version ("version");
Module_author ("author");
Module_description ("Description of the module");
===========================================================
One module corresponds to a test.c so, Makefile is written as follows
Obj-m: = TEST.O//
kernle:=/lib/modules/2.6.32-358.el6.i686/build/
All
Make-c $ (Kernle) m=$ (shell pwd) modules
Clean
Make-c $ (Kernle) m= ' pwd ' clean;
Rm-f *.order
Mymove:
MV *.ko/nfsroot/
===========================================================
A module to respond to a. c, such as test1.c test2.c Test3.c,makefile as follows
Obj-m: = NAME.O
NAME-OBJS: = test1.o test2.o test3.o
kernle:=/lib/modules/2.6.32-358.el6.i686/build/
All
Make-c $ (Kernle) m=$ (shell pwd) modules
Clean
Make-c $ (Kernle) m= ' pwd ' clean;
Rm-f *.order
Mymove:
MV *.ko/nfsroot/
==========================================================
If you are in a separate directory, compile the pair with one makefile management at a time. C
corresponding. ko files for multiple modules, for example, test1.c test2.c to compile the build
Two separate modules, then the makefile is written as follows:
Obj-m: = TEST1.O test2.o
kernle:=/lib/modules/2.6.32-358.el6.i686/build/
All
Make-c $ (Kernle) m=$ (shell pwd) modules
Clean
Make-c $ (Kernle) m= ' pwd ' clean;
Rm-f *.order
Mymove:
MV *.ko/nfsroot/
=========================================================
If you want to insert a module in any position or want to be smart, you need to insert it automatically first.
Dependent module, that is, if you want to insert the module with the Modprobe command, then
Makefile must be written as follows:
obj-m:= TEST1.O TEST2.O
kernel:=/linux-millet-driver
Dest_dir: =/nfsroot/
Pwd:= $ (shell pwd)
All
Make-c $ (KERNEL) m=$ (PWD) modules
Install
Make-c $ (KERNEL) m=$ (PWD) install_mod_path=$ (dest_dir) Modules_install
Depmod-a
Clean
Make-c $ (KERNEL) m=$ (PWD) Clean
Rm-f *.order
Mycopy:
CP *.ko/nfsroot/
*************************************************************************************
Commands related to the operation of the drive module:
Insert the Xx.ko module in the current directory into the kernel:
Insmod Xx.ko
To remove the module XX that is running in the kernel:
Rmmod XX
To view the details of the Xx.ko module in the current directory:
Modinfo Xx.ko
Use this command to intelligently install the module if it has been successfully installed
Install the driver module (analyze dependencies, first install the dependent
Drive module):
modprobe XX
Smart removal of module drivers will also remove dependent modules:
Modprobe-r XX
To view the list of drivers inserted in the current kernel:
Lsmod
Code for writing Linux driver modules and writing specifications for Makefiel files