First on the code:
#include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/ Moduleparam.h>static char* whom = "World"; static int howmany = 1;module_param (howmany, int, s_irugo); Module_param (WH OM, Charp, s_irugo); static int hello_init (void) { int i; PRINTK (Kern_alert "Hello, world! I ' m lizhixing.\n "); for (i=0; i< howmany; ++i) { printk (kern_alert "Hello,%s!\n", whom); } return 0;}; static void Hello_exit (void) { printk (kern_alert "Good Bye, Cruel world. Tommorow is another day! \ n ");}; Module_init (Hello_init); Module_exit (Hello_exit);
The Linux driver can accept parameters when initializing, the command is as follows:
Insmod Hello.ko howmany=10 whom= "Daddy"
Here 2 parameters are passed, Howmany and whom respectively.
In order for the module to be able to receive these two parameters:
1. Contains the header file moduleparam.h, this header file defines what the parameters need.
2. Declare the parameters. Several types of support are:
Bool/invbool, are all Boolean, Invbool apology, will be true and false interchange, good wonderful design, understand not .....
Charp, that's the string.
Int/long/short/uint/ulong/ushort, these are familiar old faces that can not be cooked.
There is the ability to support the array parameter, and that's it. If you want to use a different type, you need to use the method of registering the callback function to handle it yourself.
3. Register this parameter with Module_param (name, type, permission) so that the kernel will assign the variable using the arguments in the command line.
Load module:
Insmod Hello.ko howmany=10 whom= "Daddy"
DMESG, there are print out:
Initialization parameters of Linux driver development