nanzh

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

内容来源于迅为电子Linux视频学习教程。

本节主要是学习在加载驱动模块的时候传参数。

1. 传单个参数使用的函数

    头文件:include/linux/moduleparam.h

    函数:module_param(name, type, perm)

               - name:模块参数的名称

               - type:模块参数的数据类型(支持int long short uint ulong ushort类型)

               - perm:模块参数的访问权限(S_IRUSR参数表示所有文件所有者可读)

 76 /**
 77  * module_param - typesafe helper for a module/cmdline parameter
 78  * @value: the variable to alter, and exposed parameter name.
 79  * @type: the type of the parameter
 80  * @perm: visibility in sysfs.
 81  *
 82  * @value becomes the module parameter, or (prefixed by KBUILD_MODNAME and a
 83  * ".") the kernel commandline parameter.  Note that - is changed to _, so
 84  * the user can use "foo-bar=1" even for variable "foo_bar".
 85  *
 86  * @perm is 0 if the the variable is not to appear in sysfs, or 0444
 87  * for world-readable, 0644 for root-writable, etc.  Note that if it
 88  * is writable, you may need to use kparam_block_sysfs_write() around
 89  * accesses (esp. charp, which can be kfreed when it changes).
 90  *
 91  * The @type is simply pasted to refer to a param_ops_##type and a
 92  * param_check_##type: for convenience many standard types are provided but
 93  * you can create your own by defining those variables.
 94  *
 95  * Standard types are:
 96  *      byte, short, ushort, int, uint, long, ulong
 97  *      charp: a character pointer
 98  *      bool: a bool, values 0/1, y/n, Y/N.
 99  *      invbool: the above, only sense-reversed (N = true).
100  */
101 #define module_param(name, type, perm)                          \
102         module_param_named(name, name, type, perm)
104 /**
105  * module_param_named - typesafe helper for a renamed module/cmdline parameter
106  * @name: a valid C identifier which is the parameter name.
107  * @value: the actual lvalue to alter.
108  * @type: the type of the parameter
109  * @perm: visibility in sysfs.
110  *
111  * Usually it's a good idea to have variable names and user-exposed names the
112  * same, but that's harder if the variable must be non-static or is inside a
113  * structure.  This allows exposure under a different name.
114  */
115 #define module_param_named(name, value, type, perm)                        \
116         param_check_##type(name, &(value));                                \
117         module_param_cb(name, &param_ops_##type, &value, perm);            \
118         __MODULE_PARM_TYPE(name, #type)

2. 传多个参数使用的函数

    头文件:include/linux/moduleparam.h

    函数:module_param_array(name, type, nump, perm)

               - name:模块参数的名称

               - type:模块参数的数据类型(支持int long short uint ulong ushort类型)

               - nump:保存参数个数的地址

               - perm:模块参数的访问权限(S_IRUSR参数表示所有文件所有者可读)

345 /**
346  * module_param_array - a parameter which is an array of some type
347  * @name: the name of the array variable
348  * @type: the type, as per module_param()
349  * @nump: optional pointer filled in with the number written
350  * @perm: visibility in sysfs
351  *
352  * Input and output are as comma-separated values.  Commas inside values
353  * don't work properly (eg. an array of charp).
354  *
355  * ARRAY_SIZE(@name) is used to determine the number of elements in the
356  * array, so the definition must be visible.
357  */
358 #define module_param_array(name, type, nump, perm)              \
359         module_param_array_named(name, name, type, nump, perm)
361 /**
362  * module_param_array_named - renamed parameter which is an array of some type
363  * @name: a valid C identifier which is the parameter name
364  * @array: the name of the array variable
365  * @type: the type, as per module_param()
366  * @nump: optional pointer filled in with the number written
367  * @perm: visibility in sysfs
368  *
369  * This exposes a different name than the actual variable name.  See
370  * module_param_named() for why this might be necessary.
371  */
372 #define module_param_array_named(name, array, type, nump, perm)         \
373         static const struct kparam_array __param_arr_##name             \
374         = { .max = ARRAY_SIZE(array), .num = nump,                      \
375             .ops = &param_ops_##type,                                   \
376             .elemsize = sizeof(array[0]), .elem = array };              \
377         __module_param_call(MODULE_PARAM_PREFIX, name,                  \
378                             &param_array_ops,                           \
379                             .arr = &__param_arr_##name,                 \
380                             __same_type(array[0], bool), perm);         \
381         __MODULE_PARM_TYPE(name, "array of " #type)

3. 参数perm:表示参数在sysfs文件系统中所对应的文件节点的属性,其权限在include/linux/stat.h中

 32 #define S_IRWXU 00700
 33 #define S_IRUSR 00400        //文件所有者可读
 34 #define S_IWUSR 00200        //文件所有者可写
 35 #define S_IXUSR 00100        //文件所有者可执行
 36 
 37 #define S_IRWXG 00070
 38 #define S_IRGRP 00040         //与文件所有者同组的用户可读
 39 #define S_IWGRP 00020
 40 #define S_IXGRP 00010
 41 
 42 #define S_IRWXO 00007
 43 #define S_IROTH 00004         //与文件所有者不同组的用户可读
 44 #define S_IWOTH 00002
 45 #define S_IXOTH 00001

4. 例子:

 

posted on 2020-03-24 23:00  深海里的星星nanzh  阅读(248)  评论(0编辑  收藏  举报