Monday, June 30, 2014

Passing Parameters to Linux Device Drivers

  • In this section we will see how we can pass an argument to our module.,
  • Parameters are declared with the module_param macro, which is defined in moduleparam.h.
  •  module_param takes three parameters: the name of the variable, its type, and a permission given to that parameter.
  •  The macro should be placed outside of any function and is typically found near the head of the source file.
This will be more clear if we consider a program-


#include<linux/init.h>
#include<linux/module.h>
#include<linux/moduleparam.h>

MODULE_LICENSE("GPL");

int param;
module_param(param, int,S_IRUSR|S_IWUSR);  //line  6

static int param_init(void)
{
printk( "displaying the parameter");
printk("the value of parameter is
%d",param);
return 0;
}
static void param_exit(void)
{
printk("exiting");

}
module_init(param_init);
module_exit(param_exit);

A closer look at the program tells that it is similar to the previous program that we have discussed except for the lines in red. param is the parameter that we want to pass to this kernel module.In line number 6 we can see that we are passing the parameter param, its type is int and it has been given permission as Read and Write both. To understand the permissions in detail please read the advanced topic at the end of this section.

 
Like previously done we will write one Makefile in the same directory in which directory this file resides and build this file as a kernel object. Now we will pass the parameter while inserting the module itself.

sudo insmod test.ko param=10

If we use dmesg then the output will be displaying the parameter the value of parameter is 10



Lets see a self explainatory program on passing an array to the module


#include<linux/init.h>
#include<linux/module.h>
#include<linux/moduleparam.h>

MODULE_LICENSE("GPL");

int paramArray[3];
module_param_array(paramArray, int,NULL, S_IWUSR|S_IRUSR); /*Counter records how many parameter is passed.In our program we have not used that feature so setting its value as 0.*/

static int array_init(void)
{
printk("Parameter array");
printk("Array elements are
:%d\t%d\t%d",paramArray[0],paramArray[1], paramArray[2]);
return 0;
}
static void array_exit(void)
{
printk("Exiting ");
}
module_init(array_init);

module_exit(array_exit);

sudo insmod testarray.ko paramArray=1,2,3

When we do dmesg we can see the output on screen as 1 2 3




Advanced Topic:

There are 5 types of permissions:
S_IWUSR,S_IRUSR,S_IXUSR,S_IRGRP,S_WGRP.


• In this S_I is common header.
• R = read ,W =write ,X= Execute.
• USR =user ,GRP =Group
• Using OR ‘|’ (or operation) we can set
multiple perissions at a time.

For any clarifications or if you have any suggestion please post comments below.

2 comments:

  1. Hi pk007,

    your blog is awesome.It's very very useful for the people those who want to make their carrier on device driver.
    Could you please add programming stuff for all the device driver related topics like spinlock, tasklet,kernel memory allocating etc since we want to get more practical knowledge on DD or could you tell us any programming book for Linux device driver to learn and execute it by our own.

    Thanks in advance

    ReplyDelete