Module parameters

Module Parameters

In previous chapter we discussed about kernel modules  http://www.tutorialsdaddy.com/courses/linux-device-driver/8486-linux-kernel-module/ and there features . So when we write any kernel module, we might need to pass some information to module at the time of loading modules And these parameters might vary from system to system. Fortunately Linux kernel provides us the way of providing parameters to module at time of loading module.

We can pass parameters by insmod command . This command accept integer and string values on the command line. suppose your module require an string parameter str and an integer parameter intr , these parameters can be set at load time with n insmod command like

insmod hello.ko str=”hello” intr=50

But before passing any parameters to module, the module must make these parameters available . Linux kernel provide macro module_param() for this purpose. This macro is defined in moduleparam.h. module_param() takes three parameters

1) Name of variable :- These parameters accept variable name which module want to avail at load time.

2) Describing string:- These parameters takes a string which describe the type of module parameter.

3)Permission Mask:- permissions mask to be used for an accompanying sysfs entry.

These macro should be placed outside of any function as we place global variable in any C file.

Above mentioned parameters can be declared as

char *str;

int intr=0;

module_param (str,”s”);

module_param (intr,”i”):

Lets write a simple module in which we will use both of these parameters so that you can understand better.

Below is the source code ( test_module.c) of module which is simple like “Hello world module” (—>) which we discussed earlier except in this module we are declaring two parameters which our module will get at the time of loading .

=========================================================================

test_module.c

#include<linux/init.h>;

#include<linux/module.h>;

#include<linux/moduleparam.h>;

 

MODULE_LICENSE(“GPL”);

MODULE_AUTHOR(“tutorialsdaddy.com”);

char *str;

int intr=0;

 

module_param(intr, int, S_IRUGO);

module_param(str, charp, S_IRUGO);

static int hello_init(void) //init function to be called at the time of insmod

{

printk(KERN_ALERT “init function called\n”); //kernel print

 

printk(KERN_ALERT “value of str is= %s\n”,str); //kernel print to print value of string

printk(KERN_ALERT “value of intr is= %d\n”,intr); //kernel print to print value of integer

return 0;

}

 

static void hello_exit(void) //exit function to be called at the time of rmmod

{

printk(KERN_ALERT “exit function called”); //kernel print

}

 

module_init(hello_init);

module_exit(hello_exit);

=========================================================================

Now we can compile this module in the same way as we compiled our “hello world module” by using make command. after compilation we get (test_module.ko) as an output.

Now it’s time to load our module by using insmod command and pass command line parameters to our module. For that you just need to give both parameters values which our module is expecting with insmod command . Following is the example

insmod test_module.ko str=”hello” intr=50

As we discussed in device driver basic component chapter that init function get called at the time of loading of module . So in our example my_init() function of our module will be called which will print both parameters which passed to module at loading time. So you can see below output in kernel logs

string passed is = Hello

integer passed is= 50

I hope now you are clear about module parameters but if you have any query please feel free to drop your query at “info@tutorialsdaddy.com

If you like our post please appreciate us by sharing this post with your friends . You can connect with us on Facebook by just clicking on below like button of our Facebook page.

adana elektrikci

', 'auto'); ga('send', 'pageview');