Chinaunix首页 | 论坛 | 博客
  • 博客访问: 845218
  • 博文数量: 133
  • 博客积分: 7117
  • 博客等级: 少将
  • 技术积分: 1846
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-16 21:30
文章分类

全部博文(133)

文章存档

2012年(1)

2011年(4)

2010年(2)

2009年(57)

2008年(69)

分类: LINUX

2009-01-08 11:19:55

module_param(name, type, perm)是一个宏,向当前模块传入参数,对源码分析如下

在include\linux\moduleparam.h中

#define module_param(name, type, perm)                \
    module_param_named(name, name, type, perm)

#define module_param_named(name, value, type, perm)               \
    param_check_##type(name, &(value));                   \
    module_param_call(name, param_set_##type, param_get_##type, &value, perm); \
    __MODULE_PARM_TYPE(name, #type)



#define module_param_call(name, set, get, arg, perm)                  \
    __module_param_call(MODULE_PARAM_PREFIX, name, set, get, arg, perm)


#define __module_param_call(prefix, name, set, get, arg, perm)        \
    /* Default value instead of permissions? */            \
    static int __param_perm_check_##name __attribute__((unused)) =    \
    BUILD_BUG_ON_ZERO((perm) < 0 || (perm) > 0777 || ((perm) & 2));    \
    static char __param_str_##name[] = prefix #name;        \
    static struct kernel_param const __param_##name            \
    __attribute_used__                        \
    __attribute__ ((unused,__section__ ("__param"),aligned(sizeof(void *)))) \
    = { __param_str_##name, perm, set, get, arg }



__attibute__ 是gcc的关键字,可参考

__attibute__将参数编译到__param段中,

module_param是一步一步展开的,
上面几个宏在include\linux\moduleparam.h中的顺序刚好相反

module_param宏的类似函数调用的顺序
module_param->module_param_named->module_param_call->__module_param_call

展开的顺序正好相反
__module_param_call->module_param_call->module_param_named->module_param



type类型可以是byte,short,ushort,int,
uint,long,ulong,charp(注:字符指针),bool,invbool,
perm表示此参数在sysfs文件系统中所对应的文件节点的属性。
权限在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

当perm为0时,表示此参数不存在 sysfs文件系统下对应的文件节点。
模块被加载后,在/sys/module/ 目录下将出现以此模块名命名的目录。


测试一下,插入一个驱动模块mp.ko,
sudo insmod mp.ko
发现 /sys/module下出现mp这个文件夹
mp中有drivers/,sections/,parameters/,initstate,refcnt,srcversion

其中initstate 里面内容为:live
refcnt里面内容为:0
srcversion里面内容为:F9BDEBF706329B443C28E08,很长,应该是一个唯一数字
driver文件夹里面是空的
sections有__param,__versions
__param里面内容为0xd081e0a8
__versions里面内容为0xd081e100

parameters有count,info(注:自己定义的参数)
count里面的内容为1(注:输出次数)
info里面的内容为a site about linux driver.(注:输出内容)


如果此模块存在perm不为0的命令行参数,
在此模块的目录下将出现parameters目录,
包含一系列以参数名命名的文件节点,
这些文件的权限值等于perm,文件的内容为参数的值。


在/sys/module/mp/出现的参数,在module结构中的对应如下
struct module
{
    enum module_state state;

    /* Member of list of modules */
    struct list_head list;

    ......

    /* Sysfs stuff. */
    struct module_kobject mkobj;
    struct module_param_attrs *param_attrs;
    struct module_attribute *modinfo_attrs;
    const char *version;
    const char *srcversion;
    struct kobject *drivers_dir;

    /* Exported symbols */
    const struct kernel_symbol *syms;
    unsigned int num_syms;
    const unsigned long *crcs;

    ......

    /* Section attributes */
    struct module_sect_attrs *sect_attrs;
#endif

    ......
};


enum module_state
{
    MODULE_STATE_LIVE,
    MODULE_STATE_COMING,
    MODULE_STATE_GOING,
};

state对应/sys/module/mp/initstate
drivers_dir对应/sys/module/mp/driver文件夹
version对应/sys/module/mp/sections/__version
srcversion对应/sys/module/mp/srcversion


测试模块,源程序mp.c内容如下:


#include
#include             

static char *info= "a site about linux driver.";          
static int count= 1;   
static int mp= 0;      
   
module_param(mp,int,0);

module_param(count,int,S_IRUSR);    

module_param(info,charp,S_IRUSR);  

MODULE_AUTHOR("ioctrl");
MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)      
{
    int i;
    for(i=0;i    {
       printk(KERN_NOTICE is ");
       printk(info);
        printk("\n");
      }
   return 0;
}

static void hello_exit(void)
{
    printk(KERN_NOTICE"exit!\n");
}

module_init(hello_init);
module_exit(hello_exit);

编译

手工插入,插入时传递参数 count=10,info="驱动开发网站。"
命令如下:
sudo insmod mp.ko count=10 info="驱动开发网站。"
然后用dmesg查看消息
看到如下,共10条记录
[16122.280000] is 驱动开发网站
[16122.280000] is 驱动开发网站
[16122.280000] is 驱动开发网站
[16122.280000] is 驱动开发网站
[16122.280000] is 驱动开发网站
[16122.280000] is 驱动开发网站
[16122.280000] is 驱动开发网站
[16122.280000] is 驱动开发网站
[16122.280000] is 驱动开发网站
[16122.280000] is 驱动开发网站
阅读(1199) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~