简介
https://www.google.com.hk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&ved=2ahUKEwiU1Kiz5aOKAxW0wTgGHZjnDL8QFnoECBYQAQ&url=https%3A%2F%2Fblog.csdn.net%2Fzongzidedandan%2Farticle%2Fdetails%2F132475615&usg=AOvVaw0CR6uvFlW4mqDItx560AHq&opi=89978449
源码
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/kern_levels.h> // For pr_infostatic int my_int_param = 42;static int lyrix_get_int(char *buffer, const struct kernel_param *kp)
{return sprintf(buffer, "%d\n", my_int_param);
}static int lyrix_set_int(const char *val, const struct kernel_param *kp)
{int my_value;int ret;ret = kstrtoint(val, 0, &my_value);if (ret)return ret;my_int_param = my_value;return 0;
}static const struct kernel_param_ops my_param_ops = {.set = lyrix_set_int,.get = lyrix_get_int,
};module_param_cb(my_int_param, &my_param_ops, &my_int_param, 0644);static int __init my_module_init(void)
{pr_info("my_module_init: my_int_param = %d\n", my_int_param);return 0;
}static void __exit my_module_exit(void)
{pr_info("my_module_exit\n");
}module_init(my_module_init);
module_exit(my_module_exit);MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A simple module demonstrating module_param_cb usage");
编译
makefile写入:obj-m := lyrix_test.o
执行make指令:
make -C <kernel/source> M=`pwd` modules ARCH=arm64 CROSS_COMPILE=<交叉编译器/bin/aarch64-none-linux-gnu->
运行
root@imx93evk:~# modinfo lyrix_test.ko
filename: /root/lyrix_test.ko
description: A simple module demonstrating module_param_cb usage
author: Your Name
license: GPL
depends:
name: lyrix_test
vermagic: 6.6.23-06237-gce96b1e1d872-dirty SMP preempt mod_unload modversions aarch64
root@imx93evk:~#
root@imx93evk:~#
root@imx93evk:~# insmod lyrix_test.ko
[ 46.541639] lyrix_test: loading out-of-tree module taints kernel.
[ 46.548249] my_module_init: my_int_param = 42
root@imx93evk:~#
root@imx93evk:~# echo 11 > /sys/module/lyrix_test/parameters/my_int_param
root@imx93evk:~#
root@imx93evk:~# cat /sys/module/lyrix_test/parameters/my_int_param
11