Linux module parameter

简述

通常在用户态下编程,即应用程序,可以通过main()的来传递命令行参数;而编写一个内核模块,则通过module_param() 来传参。

在驱动的模块中声明一下你要传递的参数名称,类型和权限:

module_param(变量的名称,类型,权限)

声明

/* src dir: /kernel/msm-3.18/include/linux/moduleparam.h */
/**
 * module_param - typesafe helper for a module/cmdline parameter
 * @value: the variable to alter, and exposed parameter name.
 * @type: the type of the parameter
 * @perm: visibility in sysfs.
 *
 * @value becomes the module parameter, or (prefixed by KBUILD_MODNAME and a
 * ".") the kernel commandline parameter.  Note that - is changed to _, so
 * the user can use "foo-bar=1" even for variable "foo_bar".
 *
 * @perm is 0 if the the variable is not to appear in sysfs, or 0444
 * for world-readable, 0644 for root-writable, etc.  Note that if it
 * is writable, you may need to use kparam_block_sysfs_write() around
 * accesses (esp. charp, which can be kfreed when it changes).
 *
 * The @type is simply pasted to refer to a param_ops_##type and a
 * param_check_##type: for convenience many standard types are provided but
 * you can create your own by defining those variables.
 *
 * Standard types are:
 *	byte, short, ushort, int, uint, long, ulong
 *	charp: a character pointer
 *	bool: a bool, values 0/1, y/n, Y/N.
 *	invbool: the above, only sense-reversed (N = true).
 */
#define module_param(name, type, perm)				\
	module_param_named(name, name, type, perm)

/**
 * module_param_string - a char array parameter
 * @name: the name of the parameter
 * @string: the string variable
 * @len: the maximum length of the string, incl. terminator
 * @perm: visibility in sysfs.
 *
 * This actually copies the string when it's set (unlike type charp).
 * @len is usually just sizeof(string).
 */
#define module_param_string(name, string, len, perm)			\
	static const struct kparam_string __param_string_##name		\
		= { len, string };					\
	__module_param_call(MODULE_PARAM_PREFIX, name,			\
			    &param_ops_string,				\
			    .str = &__param_string_##name, perm, -1, 0);\
	__MODULE_PARM_TYPE(name, "string")

应用实例

1.测试驱动中追加module parameter声明,变量声明;

/* module parameters of type int */
int param_int = 0;
module_param(param_int, int, 0644);

/* module parameters of type string */
char *param_str = NULL;
module_param(param_str, charp, 0644);

/* module parameters of type char[] array */
char param_char_internal[256] = {0};
module_param_string(param_char, param_char_internal, 256, 0644);
MODULE_PARM_DESC(param_char, "Modules parameter char[] array test");

2.通过sysfs向应用层提供接口,已获取刚刚声明的变量

static ssize_t param_int_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	unsigned int size = 0;

	size += sprintf(buf, "param int val is %d\n", param_int);

	return size;
}

static ssize_t param_str_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	unsigned int size = 0;

	size += sprintf(buf, "param string is %s\n", param_str);

	return size;
}

static ssize_t param_char_internal_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	unsigned int size = 0;

	size += sprintf(buf, "param char[] array is %s\n", param_char_internal);

	return size;
}

3.module parameter使用

// 通过命令行加载驱动时使用
root:/ # insmod /system/lib/modules/test_driver.ko param_int=100 param_str="param_str" param_char="param_char"

// 驱动加载后通过sysfs查看或调整参数
root:/ # cd /sys/module/test_driver/parameters/
root:/sys/module/test_driver/parameters # ls -al
total 0
drwxr-xr-x 2 root root    0 2020-10-19 13:44 .
drwxr-xr-x 7 root root    0 2020-10-19 13:44 ..
-rw-r--r-- 1 root root 4096 2020-10-19 13:44 param_char
-rw-r--r-- 1 root root 4096 2020-10-19 13:44 param_int
-rw-r--r-- 1 root root 4096 2020-10-19 13:44 param_str

// 通过自己提供的sysfs接口查看调整后的参数
root:/ # cd /sys/class/test_class/test_node/                                         
root:/sys/class/test_class/test_node # ls -al
total 0
drwxr-xr-x 3 root root    0 2020-10-19 13:44 .
drwxr-xr-x 3 root root    0 2020-10-19 13:44 ..
-r--r--r-- 1 root root 4096 2020-10-19 13:45 dev
lrwxrwxrwx 1 root root    0 2020-10-19 13:45 device -> ../../../soc:test_dev_for_spongebob
-r--r--r-- 1 root root 4096 2020-10-19 13:45 devname
-r--r--r-- 1 root root 4096 2020-10-19 13:45 kobject_test
--w------- 1 root root 4096 2020-10-19 13:45 notifier
-r--r--r-- 1 root root 4096 2020-10-19 13:45 opentimes
-r--r--r-- 1 root root 4096 2020-10-19 13:45 param_char_internal
-r--r--r-- 1 root root 4096 2020-10-19 13:45 param_int
-r--r--r-- 1 root root 4096 2020-10-19 13:45 param_str
drwxr-xr-x 2 root root    0 2020-10-19 13:45 power
lrwxrwxrwx 1 root root    0 2020-10-19 13:45 subsystem -> ../../../../../class/test_class

参考链接:https://blog.csdn.net/u012142460/article/details/78884144?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值