module_param(name, type, perm)这个宏用来给内核模块传参数,其中参数类型有byte, short, ushort, int, uint, long, ulong, charp, bool, invbool
* 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)
#define module_param_named(name, value, type, perm) \
param_check_##type(name, &(value)); \
module_param_cb(name, ¶m_ops_##type, &value, perm); \
__MODULE_PARM_TYPE(name, #type)
//calculation.h
#ifndef _CALCULATION_H
#define _CALCULATION_H
//对导出符号的声明,不能加static修饰
extern int itype;
int addFunc(int a, int b);
int subFunc(int a, int b);
#endif
//calculation.c
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include "calculation.h"
static int __init cal_init(void)
{
printk(KERN_ALERT "calculation init()\n");
printk(KERN_ALERT "itype+1=%d, itype-1=%d\n", addFunc(itype, 1), subFunc(itype, 1));
return 0;
}
static void __exit cal_exit(void)
{
printk(KERN_ALERT "calculation exit()\n");
}
module_init(cal_init);
module_exit(cal_exit);
MODULE_LICENSE("GPL2");
//param.c
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
static int itype = 0;
module_param(itype, int, 0);
static bool btype = 0;
module_param(btype, bool, 0644);
static char ctype = 0;
module_param(ctype, byte, 0);
static char* stype = 0;
module_param(stype, charp, 0644);
static int addFunc(int a, int b)
{
return a+b;
}
static int subFunc(int a, int b)
{
return a-b;
}
EXPORT_SYMBOL(itype);
EXPORT_SYMBOL(addFunc);
EXPORT_SYMBOL(subFunc);
static int __init param_init(void)
{
printk(KERN_ALERT "param_init(void)\n");
printk(KERN_ALERT "itype=%d\n", itype);
printk(KERN_ALERT "btype=%d\n", btype);
printk(KERN_ALERT "ctype=%d\n", ctype);
printk(KERN_ALERT "stype=%s\n", stype);
return 0;
}
static void __exit param_exit(void)
{
printk(KERN_ALERT "param_exit(void)\n");
}
module_init(param_init);
module_exit(param_exit);
MODULE_LICENSE("GPL2");
输出:加载模块时,并给定参数