Chinaunix首页 | 论坛 | 博客
  • 博客访问: 457962
  • 博文数量: 134
  • 博客积分: 3056
  • 博客等级: 中校
  • 技术积分: 1150
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-14 15:53
文章分类
文章存档

2013年(1)

2010年(133)

我的朋友

分类: LINUX

2010-07-27 11:04:37

1. 一个带参数的模块hello world,源码如下:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/config.h>

MODULE_LICENSE("GPL");

static int howmany = 1;
MODULE_PARM(howmany, "i");

static int hello_init(void)
{
    int i;

    for(i = 0; i < howmany; i++)
    printk(KERN_ALERT "Hello, Michael Yao\n");
    return 0;
}

static void hello_exit(void)
{
    printk(KERN_ALERT "Goodbye,\nLove you!\n");
}


2. 我的Makefile文件如下:

TARGET := helloworld
INCLUDE := -I/home/michael/work/project/org_source/linux-2.4.x/include/ #开发板内核源码所在目录

CFLAGS := -O2 -Wall -DMODULE -DUSEFIFO -D__KERNEL__ -DLINUX\
     -mno-abicalls -fno-pic -mips32 -DMODULE -mlong-calls
CC := /opt/buildroot-gdb/bin/mipsel-linux-gcc #交叉编译器所在位置


${TARGET}.o: ${TARGET}.c
    $(CC) $(CFLAGS) ${INCLUDE} -c ${TARGET}.c

install:
    $(shell cp ./helloworld.o /tftpboot) #我已经安装并启动tftp系统服务


clean:
    rm -rf *.o *~core .depend .*.cmd *.ko *.mod.c .tmp_versions


3. 在开发板/tmp目录下,在minicom中输入命令'tftp -gr helloworld.o 192.168.1.222'。然后在开发板终端输入

insmod helloworld.o howmany=2


终端里输出是:

Hello, Michael Yao
Hello, Michael Yao


4. 说明linux2.4内核中,include/linux/module.h中定义的宏 MODULE_PARM(var,type) 用于向模块传递命令行参数。var为接受参数值的变量名,type为采取如下格式的字符串[min[-max]]{b,h,i,l,s}。min及max 用于表示当参数为数组类型时,允许输入的数组元素的个数范围;b:byte;h:short;i:int;l:long;s:string。
如:

static int myint = 9;

static char *mystr = "test"; //注意需要传递参数的默认值

MODULE_PARM(myint, "i");

MODULE_PARM(mystr, "s");


在装载内核模块时,用户就可以向模块传递一些参数:

insmod modname myint=10,如果用户未指定参数,myint将使用模块内定义的缺省值9
阅读(854) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~