Linux Device Driver Tutorial Part 3 – Module Parameter

This is the Series on Linux Device Driver. The aim of this series is to provide the easy and practical examples of Linux Device Drivers that anyone can understand easily. Now we are going to see Linux Device Driver Tutorial Part 3 – Passing Arguments to Device Driver.

Linux Device Driver Tutorial Part 3 – 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

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

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().

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

You can change the value of valueETX from command line by

echo 1 > /sys/module/hello_world_module/parameters/valueETX

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

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

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

For further explanation, please refer below program.

When we will need this notification?

I will tell you the practical scenario. Whenever value is set to 1, you have to write a something in to 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 understood, 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 (valueETX) and another one is string (nameETX).

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

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

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

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

Download the code by clicking below link.

[Download Project Here]

Compiling

This is the code of Makefile.

In terminal enter sudo make

linux-device-driver-tutorial-passing-arguments-to-device-driver-make Linux Device Driver Tutorial Part 3 – Module Parameter

Loading the Driver

sudo insmod hello_world_module.ko valueETX=14 nameETX="EmbeTronicX" arr_valueETX=100,102,104,106

linux-device-driver-tutorial-passing-arguments-to-device-driver-insmod Linux Device Driver Tutorial Part 3 – Module Parameter

Verify the parameters by using dmesg

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

linux-device-driver-tutorial-passing-arguments-to-device-driver-dmesg Linux Device Driver Tutorial Part 3 – Module ParameterNow i’m going to check module_param_cb() is weather calling that handler function or not. For that i need to change the variable in sysfs.

echo 13 > /sys/module/hello_world_module/parameters/cb_valueETX

linux-device-driver-tutorial-passing-arguments-to-device-driver-echo Linux Device Driver Tutorial Part 3 – Module ParameterNow do dmesg and check.

linux-device-driver-tutorial-passing-arguments-to-device-driver-echo-dmesg Linux Device Driver Tutorial Part 3 – Module ParameterSee the above result. So Our callback function got called. But if you change the value of other variables, you wont get notification.

Unloading the Driver

Finally unload the driver by using sudo rmmod hello_world_module.

I hope you understood. If you have any doubt, please comment below. 🙂

Download our new Android app. You can learn all Embedded Tutorials from your Android Phone easily.

Click Here to Download App!