During Linux kernel module programming, parameters are often required to be passed to the module. The role of parameters is from the device number used to the operations that the driver should perform. for example, a SCSI Adapter Driver often has options to control the use of the tag command queue,
The IDE driver allows users to control DMA operations. If your driver controls old hardware, you need to be clearly indicated where to find the hardware I/O port or I/O memory address.
The kernel supports these requirements by specifying variable parameter values when loading the driver module.
The parameter is often declared as a static global variable, such as static int num = 10. Then, use module_param (parameter name, parameter type, parameter read/write permission) to define a parameter for the module, for example:
Module_prarm (Num, Int, s_irugo );
In this way, you can specify a value for the parameter when loading the module. If no value is specified, the default value is used. For example, if num is 10, 10 is the default value of the parameter num. The following is an example:
# Include
# Include
Module_license ("dual BSD/GPL ");
Static int num = 10;
Module_param (Num, Int, s_irugo );
Static int hello_init (void)
{
Printk ("Hello module init./N ");
Printk ("num = % d/N", num );
Return 0;
}
Static void hello_exit (void)
{
Printk ("Goodbye module exit./N ");
}
Module_init (hello_init );
Module_exit (hello_exit );
Module_description ("A simple module ");
Module_alias ("hello ");
Save the above Code as the file hello. C and compile the MAKEFILE file:
# Makefile2.6
Ifneq ($ (kernelrelease ),)
OBJ-M: = Hello. o
Else
PWD: = $ (shell PWD)
Kver? = $ (Shell uname-R)
Kdir: =/lib/modules/$ (kver)/build
ALL:
$ (Make)-C $ (kdir) M = $ (PWD)
Endif
Then, make will generate:
| -- Module. symvers
| -- Built-in.o
| -- Hello. Ko
| -- Hello. Mod. c
| -- Hello. Mod. o
'-- Hello. o
These files, the hello. ko is the module generated by compilation. Use insmod to set hello. the Ko module is loaded into the kernel. This requires the superuser permission. My system is ubuntu7.10, so run the following command to load:
Sudo insmod hello. Ko num = 20
Run the dmesg command to view the running result:
Xiyoulinux @ xiyoulinux-desktop :~ /Module $ dmesg # actually reads the content of the/var/log/messages file.
... # Omitting previous information
[2, 14801.675260] Hello module init.
[14801.675265] num = 20
This is the running result. If you insert sudo insmod hello. Ko, then:
Run the dmesg command to view the running result:
Xiyoulinux @ xiyoulinux-desktop :~ /Module $ dmesg # actually reads the content of the/var/log/messages file.
... # Omitting previous information
[2, 14801.675260] Hello module init.
[14801.675265] num = 10 # use the default value
The above is the compilation and running status of the kernel module. In fact, you still need to use lsmod (check whether the module is inserted, usually in the first line of the print) and rmmod (uninstall the loaded module, commands such.
In essence, When you load module hello. Ko, the system will generate a hello folder under/sys/module:
Xiyoulinux @ xiyoulinux-desktop:/sys/module/Hello $ tree
.
| -- Holders
| -- Initstate
| -- Parameters
| '-- Num
| -- Refcnt
| -- Sections
| -- _ Param
| '-- _ Versions
'-- Srcversion
3 directories, 6 files
The parameters directory stores the parameters of this module. A parameter corresponds to a file. The content of the file is the default value of the parameter, for example:
Xiyoulinux @ xiyoulinux-desktop:/sys/module/Hello/parameters $ tree
.
'-- Num
0 directories, 1 file
Xiyoulinux @ xiyoulinux-desktop:/sys/module/Hello/parameters $ cat num
10
In module_param (Num, Int, s_irugo );
When defining parameters, the parameter read/write permission s_irugo is actually the read/write permission for the parameter file, so the permission setting value is the same as the setting value for the file, for example, s_irugo is set to allow all users to read the num parameter, while s_irugo | s_iwusr allows root to change the parameter.