文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>驱动程序Hello,world in 2.6 kernel

驱动程序Hello,world in 2.6 kernel

时间:2006-12-18  来源:todaygood

驱动程序Hello,world  in 2.6 kernel
 
文件: helloDriverIn2.6.tar.bz2
大小: 62KB
下载: 下载

#include <linux/init.h>  //注意如果没有包含这个头文件,则编译时产生警告,//insmod hello.ko后也没有输出。
#include <linux/module.h>
 
MODULE_LICENSE("Dual BSD/GPL");
 
static int hello_init(void)
{
        printk(KERN_ALERT"hello,world\n");
        return 0;
 
}
 
 
static void hello_exit(void)
{
 
        printk(KERN_ALERT"Goodbye,cruel world\n");
 
}
 
module_init(hello_init);
module_exit(hello_exit);


说明: 与2.4 内核驱动的不同之处在于:
 1、 使用新的入口

必须包含

module_init(your_init_func);

module_exit(your_exit_func);

老版本:
        int init_module(void);

       void cleanup_module(voi);

2.4中两种都可以用,对如后面的入口函数不必要显示包含任何头文件。

2、 GPL

MODULE_LICENSE("Dual BSD/GPL");

老版本:MODULE_LICENSE("GPL");

Makefile 的编写

 

ifneq ($(KERNELRELEASE),)
obj-m := hello.o
 
else
 
        KERNELDIR ?= /lib/modules/$(shell uname -r)/build
        PWD :=$(shell pwd)
default:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
 
endif


make 之后生成了如下文件:
[root@hujunlinux drivers]# ls
hello.c  hello.ko  hello.mod.c  hello.mod.o  hello.o  makefile

这里说一下
 在 结构体的初试化时,2.6的kernel ,gcc开始采用ANSI C的struct结构体的初始化形式:

static struct some_structure = {

.field1 = value,

.field2 = value,

...

};

老版本:非标准的初试化形式

static struct some_structure = {

field1: value,

field2: value,

...

};

试验了一下:
                                               
                                                                              
                                                                              

#include <stdio.h>
                                                                              
int main()
{
    typedef struct my_struct
        {
                int a;
                int b;
        } mystruct ;
                                                                              
        mystruct A={
                    .a=2,
                    .b=3
                  };
   printf("A.a=%d,A.b=%d\n",A.a,A.b);
                                                                              
   return 0;
 } 

//模块参数 练习



#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
 
 
MODULE_LICENSE("Dual BSD/GPL");
 
static char *whom ="world";
static int howmany =1;
 
module_param(howmany,int,S_IRUGO);
module_param(whom,charp,S_IRUGO);
 
 
static int hello_init(void)
{
        int i=howmany;
        for(;i>0;i--)
        {
                printk(KERN_ALERT"hello,%s\n",whom);
        }
        return 0;
 
}
 
 
static void hello_exit(void)
{
 
        printk(KERN_ALERT"Goodbye,cruel world\n");
 
}
 
module_init(hello_init);
module_exit(hello_exit);

相关阅读 更多 +
排行榜 更多 +
像素太空杀

像素太空杀

飞行射击 下载
忍者手势

忍者手势

飞行射击 下载
勇士X地下城大冒险

勇士X地下城大冒险

角色扮演 下载