How it works...
Once compiled as before, a new file, module_par.ko, should be ready to be loaded into our ESPRESSObin. However, before doing it, let's use the modinfo utility on it, as follows:
# modinfo module_par.ko
filename: /root/module_par.ko
version: 0.1
description: Module with parameters
author: Rodolfo Giometti
license: GPL
srcversion: 21315B65C307ABE9769814F
depends:
name: module_par
vermagic: 4.18.0 SMP preempt mod_unload aarch64
parm: var:an integer value (int)
parm: str:a string value (charp)
parm: arr:an array of 8 values (array of int)
As we can see in the last three lines (all prefixed by the parm: string), we have a list of module's parameters defined in the code by the module_param() and module_param_array() macros and described with MODULE_PARM_DESC().
Now, if we simply insert the module as before, we get default values, as shown in the following code block:
# insmod module_par.ko
[ 6021.345064] module_par:module_par_init: loaded
[ 6021.347028] module_par:module_par_init: var = 0x3f
[ 6021.351810] module_par:module_par_init: str = "default string"
[ 6021.357904] module_par:module_par_init: arr = 0 0 0 0 0 0 0 0
But if we use the next command line, we force new values:
# insmod module_par.ko var=0x01 str=\"new value\" arr='1,2,3'
[ 6074.175964] module_par:module_par_init: loaded
[ 6074.177915] module_par:module_par_init: var = 0x01
[ 6074.184932] module_par:module_par_init: str = "new value"
[ 6074.189765] module_par:module_par_init: arr = 1 2 3 0 0 0 0 0
As a final note, let me suggest taking a closer look at the following module parameter definition:
static int var = 0x3f;
module_param(var, int, S_IRUSR | S_IWUSR);
MODULE_PARM_DESC(var, "an integer value");
First, we have the declaration of the variable that represents the parameter, then we have the real module parameter definition (where we specify the type and the file access permissions), and then we have the description.
The modinfo command is able to display all of the preceding information, except the file access permissions, which refer to the file related to this parameter within the sysfs filesystem! In fact, if we take a look at the /sys/module/module_par/parameters/ directory, we get the following:
# ls -l /sys/module/module_par/parameters/
total 0
-rw------- 1 root root 4096 Feb 1 12:46 arr
-rw------- 1 root root 4096 Feb 1 12:46 str
-rw------- 1 root root 4096 Feb 1 12:46 var
Now, it should be clear what parameters S_IRUSR and S_IWUSR means; they allow the module user (that is, the root user) to write into these files and then read from them the corresponding parameters.