天天看點

linux下的module_param()解釋【轉】

版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。

***************************************************************************************************************************

作者:EasyWave                                                                                 時間:2012.07.19

類别:linux驅動開發                                                                           聲明:轉載,請保留連結

1.引入module_param目的。 

          在使用者态下程式設計可以通過main()來傳遞指令行參數,而編寫一個核心子產品則可通過module_param()來傳遞指令行參數. 也就是核心允許對驅動程式在加載的時候傳遞參數,e.g.insmod hello who="world" times=5 其中who,times 均為要傳遞的參數變量。

2.module_param()的定義 。

         module_param宏是Linux 2.6核心中新增的,該宏被定義在include/linux/moduleparam.h檔案中,具體定義如下(我從源碼那裡找來的http://tomoyo.sourceforge.jp/cgi-bin/lxr/source/include/linux/moduleparam.h):

/**

 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)

103 

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  */

          其中使用了 3 個參數:要傳遞的參數變量名, 變量的資料類型, 以及通路參數的權限。

          注:宏的第三個參數用于指定通路權限,如果為 0,該參數将不出現在 sysfs 檔案系統中,允許的通路權限為      S_IRUSR,S_IWUSR,S_IRGRP,S_IWGRP,S_IROTH 和S_IWOTH 的組合,它們分别對應于使用者讀,使用者寫,使用者組讀,使用者組寫,其他使用者讀和其他使用者寫,是以用檔案的通路權限設定是一緻的。

而權限的設定可以參考如下:(也可以自己定義,它類似chmod中權限的使用)

          權限在include/linux/stat.h中有定義:

比如:

#define S_IRWXU 00700

#define S_IRUSR 00400

#define S_IWUSR 00200

#define S_IXUSR 00100

#define S_IRWXG 00070

#define S_IRGRP 00040

#define S_IWGRP 00020

#define S_IXGRP 00010

#define S_IRWXO 00007

#define S_IROTH 00004

#define S_IWOTH 00002

#define S_IXOTH 00001

本文轉自張昺華-sky部落格園部落格,原文連結:http://www.cnblogs.com/sky-heaven/p/5830214.html,如需轉載請自行聯系原作者

繼續閱讀