简述
通常在用户态下编程,即应用程序,可以通过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, \
¶m_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