contact@embeddedgeeks.com
Embedded World

Module Parameters

Passing Arguments to Device Driver

We can pass the arguments to any other functions in same program. But Is it possible to pass any arguments to any program? I think Probably yes. Right? Well, we can. In C Programming we can pass the arguments to the program. For that we need to add argc and argv in main function definition. I hope everyone knows that. Now come to our topic. Is it possible to pass the argument to the Device Driver? Fine. In this tutorial we are going to see that topic.

Module Parameters Macros

  • module_param()
  • module_param_array()
  • module_param_cb()

Before discuss these macros we have to know about permissions of the variable.

There are several types of permissions:

  • S_IWUSR
  • S_IRUSR
  • S_IXUSR
  • S_IRGRP
  • S_IWGRP
  • S_IXGRP

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 permissions at a time.

module_param()

This macro used to initialize the arguments. module_param takes three parameters: the name of the variable, its type, and a permissions mask to be used for an accompanying sysfs entry. The macro should be placed outside of any function and is typically found near the head of the source file. module_param() macro, defined in linux/moduleparam.h.

module_param(name, type, perm);

module_param() macro creates the sub-directory under /sys/module.

For example:

module_param(value, int, S_IWUSR|S_IRUSR);  

This will create the sysfs entry. (/sys/module/hello_world_module/parameters/value)

Numerous types are supported for module parameters:

  • bool
  • invbool

A boolean (true or false) value (the associated variable should be of type int). The invbool type inverts the value, so that true values become false and vice versa.

  • charp

A char pointer value. Memory is allocated for user-provided strings, and the pointer is set accordingly.

  • int
  • long
  • short
  • uint
  • ulong
  • ushort

Basic integer values of various lengths. The versions starting with u are for unsigned values.

module_param_array()

This macro is used to send the array as a argument. Array parameters, where the values are supplied as a comma-separated list, are also supported by the module loader. To declare an array parameter, use:

module_param_array(name,type,num,perm);

Where,

name is the name of your array (and of the parameter), 

type is the type of the array elements, 

num is an integer variable (optional) otherwise NULL, and 

perm is the usual permissions value.

module_param_cb()

This macro used to register the callback whenever the argument (parameter) got changed. I think you don’t understand. Let me explain properly.

For Example,

I have created one parameter by using module_param().

module_param(value, int, S_IWUSR|S_IRUSR);

This will create the sysfs entry. (/sys/module/hello_world_module/parameters/value)

You can change the value of value from the command line by

sudo su echo 1 > /sys/module/hello_world_module/parameters/value

This will update the value variable. But there is no way to notify your module that “value” has changed.

By using this module_param_cb() macro, we can get a notification.

If you want to get notification whenever value got change. we need to register our handler function to its file operation structure.

struct kernel_param_ops {         
int (*set)(const char *val, const struct kernel_param *kp);        
int (*get)(char *buffer, const struct kernel_param *kp);         
void (*free)(void *arg);
};

For further explanation, please refer below program.

When we will need this notification?

I will tell you about the practical scenario. Whenever value is set to 1, you have to write something into a hardware register. How can you do this if the change of value variable is not notified to you? Got it? I think you have understood. If you didn’t understand, just see the explanation posted below.

Programming

In this example, i explained all (module_param, module_param_array, module_param_cb).

For module_param(), I have created two variables. One is integer (value) and another one is a string (name).

For module_param_array(), i have created one integer array variable (arr_value).

For module_param_cb(), i have created one integer variable (cb_value).

You can change the all variable using their sysfs entry which is under /sys/module/hello_world_module/parameters/

But you won’t get any notification when they got change, except the variable which is created by module_param_cb() macro.

Compiling this Module / Driver code.

Now its time to compile the code by using a Makefile.

Sample Makefile.

Loading the Driver

sudo insmod <sampleProgram>.ko value=14 name=”EmbeddedGeeKS” arr_value=100,110,120,130

Verify the parameters by using dmesg

Now our module got loaded. now check dmesg. In the below picture, every value got passed to our device driver.

Now I’m going to check module_param_cb() is whether calling that handler function or not. For that, I need to change the variable in sysfs. You can write that variable by two ways.

  1. sudo sh -c “echo 15 > /sys/module/driver/parameters/cb_value”
  2. Type sudo su. Then enter the password if it asks. Then do echo 15 > /sys/module/hello_world_module/parameters/cb_value

Now do dmesg and check.