linux内核驱动02-module param

 一般的程序可以通过main(int argc, char* argv[])传递命令行参数给程序,
 内核允许驱动程序在insmod的时候通过 module_param(name,type,perm) 传递参数给驱动程序
 3个参数:name=要传递的参数变量名,type=变量的数据类型,perm=访问参数的权限
 宏module_param在<linux/moduleparam.h>中定义
 #define module_param(name, type, perm)  module_param_named(name, name, type, perm)
 perm:在 <linux/stat.h>中定义,其中S_IRUGO参数可以被所有人读取,但不能更改
 

 其中module就是指驱动程序,linux驱动程序可以以两种方式加载,一种直接编译进内核中;

第二种就是编译成module,使用驱动程序的时候,通过insmod命令 加载module


param.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>
#include <linux/moduleparam.h>

//参数及默认值
static int times = 1; //打印次数
static char *who =" world!"; //打印内容

//module_param(name,type,perm);
//功能:指定模块参数,用于在加载模块时或者模块加载以后传递参数给模块。
//name:模块参数的名称
//type: 模块参数的数据类型
//perm: 模块参数的访问权限

module_param (times, int, S_IRUGO);
MODULE_PARM_DESC (times, "A integer type for times");

module_param (who, charp, S_IRUGO);
MODULE_PARM_DESC (who, "A character string");

static int __init hello_init (void)
{
	int i;
	for (i=0; i<times; i++){
		printk (KERN_INFO "%d hello  %s\n",i, who);
	}
	return 0;
}

static void __exit hello_exit (void)
{
	printk (KERN_INFO "googbye %s\n",who);
}

module_init (hello_init);
module_exit (hello_exit);

MODULE_LICENSE ("GPL");
MODULE_AUTHOR ("hcl0317");
MODULE_DESCRIPTION ("a simple param driver");
MODULE_SUPPORTED_DEVICE ("param test");


编译以后拷贝 .ko到开发板

带1个参数

insmod param.ko times=10
[ 1639.040000] 0 hello   world!
[ 1639.040000] 1 hello   world!
[ 1639.040000] 2 hello   world!
[ 1639.040000] 3 hello   world!
[ 1639.045000] 4 hello   world!
[ 1639.045000] 5 hello   world!
[ 1639.050000] 6 hello   world!
[ 1639.050000] 7 hello   world!
[ 1639.055000] 8 hello   world!
[ 1639.060000] 9 hello   world! 


rmmod param    
[ 1677.255000] googbye  world!



带2个参数
insmod param.ko  times=5 who=hgfdks
[ 2843.455000] 0 hello  hgfdks
[ 2843.455000] 1 hello  hgfdks
[ 2843.455000] 2 hello  hgfdks
[ 2843.455000] 3 hello  hgfdks
[ 2843.460000] 4 hello  hgfdks


rmmod param    
[ 2870.540000] googbye hgfdks
rmmod: module 'param' not found



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值