Friday, 20 July 2018

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.


Module Parameters Macros

  • module_param()// module_param(name, type, perm);
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.

  • module_param_array()//
    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.
This macro is used to send the array as a argument. Array parameters, where the values are supplied as a comma-separated list.


  • module_param_cb()
This macro used to register the callback whenever the argument (parameter) got changed.



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

-----------------------



Compiling

This is the code of Makefile.


In terminal enter sudo make


Loading the Driver

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

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.
Now 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
Now do dmesg and check.
Se

No comments:

Post a Comment