Analysis of parameter transfer process of Linux driver module

  • 2021-08-31 09:48:04
  • OfStack

Declare the parameter name, type and permission you want to pass under 1 in the driver module

module_param (variable name, type, permissions);


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

static char *p_name = "Usr";
module_param(p_name, charp, S_IRUGO);
MODULE_PARM_DESC(p_name, "This is a char * string.");

static int __init hi_init(void)
{
  printk(KERN_INFO "Hi enter %s\n",p_name);
  return 0;
}
module_init(hi_init);

static void __exit hi_exit(void)
{
  printk(KERN_INFO "Hi exit %s\n ",p_name);
}
module_exit(hi_exit);

MODULE_AUTHOR(" libra13179 ");
MODULE_LICENSE("GPL v2");

KVERS = $(shell uname -r)

# Kernel modules
obj-m += hi.o

# Specify flags for the module compilation.
#EXTRA_CFLAGS=-g -O0

build: kernel_modules

kernel_modules:
  make -C /lib/modules/$(KVERS)/build M=$(CURDIR) modules
#  @echo $(KVERS)

clean:
  make -C /lib/modules/$(KVERS)/build M=$(CURDIR) clean

Use the default when not set


root@lin-virtual-machine:/home/lin/hi# insmod ./hi.ko
root@lin-virtual-machine:/home/lin/hi# cat /var/log/syslog | grep Hi
...
Jul 25 15:11:38 lin-virtual-machine kernel: [ 203.238178] Hi enter Usr
...

When using settings

root@lin-virtual-machine:/home/lin/hi# insmod hi.ko p_name='God'

After the module is loaded, a module folder will be generated under/sys/modules, and there will be an parameters folder under the folder.

It contains a file node named after the parameter name, which holds the values we set, for example, in p_name

root@lin-virtual-machine:/home/lin/hi# cat /sys/module/hi/parameters/p_name
God


Related articles: