在编写linux设备驱动程序的时候,如果在不参考已有驱动程序的情况下,我们该如何不多不少地用#include包含所需的头文件呢?
下面,来通过一个实验来介绍。
运行环境:Source Insight @ Windows xp
内核代码:Linux 2.6.32
实验代码: 简单ramblock实验
-
#define DEV_MAJOR 251
-
-
static struct gendisk *my_rmbk;
-
static struct request_queue *my_rq;
-
-
DEFINE_SPINLOCK(rmbk_lock);
-
-
#define RAMBLOCK_SIZE (1024*1024)
-
-
const struct block_device_operations rmbk_fops = {
-
.owner = THIS_MODULE,
-
};
-
-
static void do_rmbk_request(struct request_queue *q)
-
{
-
printk("Do_ramdisk_requestn");
-
}
-
-
static int ramdisk_init(void)
-
{
-
/* 注册块设备 */
-
register_blkdev(DEV_MAJOR, "ramdisk");
-
-
/* 分配gendisk结构体*/
-
my_rmbk = alloc_disk(16);
-
-
/* 为块设备准备一个请求队列 */
-
my_rq = blk_init_queue(do_rmbk_request, &rmbk_lock);
-
-
sprintf(my_rmbk, "ramdisk");
-
my_rmbk->major = DEV_MAJOR;
-
my_rmbk->first_minor = 0;
-
my_rmbk->fops = &rmbk_fops;
-
my_rmbk->queue = my_rq;
-
-
set_capacity(my_rmbk, RAMBLOCK_SIZE / 512);
-
-
/* 添加分区信息到内核列表 */
-
add_disk(my_rmbk);
-
-
-
-
return 0;
-
}
-
-
static void ramdisk_exit(void)
-
{
-
del_gendisk(my_rmbk);
-
blk_cleanup_queue(my_rq);
-
put_disk(my_rmbk);
-
unregister_blkdev(DEV_MAJOR, "ramdisk");
-
-
return;
-
}
-
-
module_init(ramdisk_init);
-
module_exit(ramdisk_exit);
-
-
MODULE_LICENSE("GPL");
上面是没有写#include语句的源代码,直接编译程序,看会报告那些函数未知
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:6: warning: data definition has no type or storage class
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:6: warning: type defaults to 'int' in declaration of 'DEFINE_SPINLOCK'
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:6: warning: parameter names (without types) in function declaration
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:10: error: variable 'rmbk_fops' has initializer but incomplete type
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:11: error: unknown field 'owner' specified in initializer
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:11: error: 'THIS_MODULE' undeclared here (not in a function)
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:11: warning: excess elements in struct initializer
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:11: warning: (near initialization for 'rmbk_fops')
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c: In function 'do_rmbk_request':
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:16: error: implicit declaration of function 'printk'
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c: In function 'ramdisk_init':
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:22: error: implicit declaration of function 'register_blkdev'
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:25: error: implicit declaration of function 'alloc_disk'
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:25: warning: assignment makes pointer from integer without a cast
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:28: error: implicit declaration of function 'blk_init_queue'
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:28: error: 'rmbk_lock' undeclared (first use in this function)
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:28: error: (Each undeclared identifier is reported only once
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:28: error: for each function it appears in.)
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:32: error: implicit declaration of function 'sprintf'
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:32: warning: incompatible implicit declaration of built-in function 'sprintf'
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:32: error: dereferencing pointer to incomplete type
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:33: error: dereferencing pointer to incomplete type
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:34: error: dereferencing pointer to incomplete type
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:35: error: dereferencing pointer to incomplete type
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:36: error: dereferencing pointer to incomplete type
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:38: error: implicit declaration of function 'set_capacity'
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:41: error: implicit declaration of function 'add_disk'
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c: In function 'ramdisk_exit':
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:50: error: implicit declaration of function 'del_gendisk'
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:51: error: implicit declaration of function 'blk_cleanup_queue'
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:52: error: implicit declaration of function 'put_disk'
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:53: error: implicit declaration of function 'unregister_blkdev'
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c: At top level:
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:58: warning: data definition has no type or storage class
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:58: warning: type defaults to 'int' in declaration of 'module_init'
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:58: warning: parameter names (without types) in function declaration
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:59: warning: data definition has no type or storage class
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:59: warning: type defaults to 'int' in declaration of 'module_exit'
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:59: warning: parameter names (without types) in function declaration
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:61: error: expected declaration specifiers or '...' before string constant
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:61: warning: data definition has no type or storage class
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:61: warning: type defaults to 'int' in declaration of 'MODULE_LICENSE'
/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:61: warning: function declaration isn't a prototype
make[2]: *** [/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.o] Error 1
make[1]: *** [_module_/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk] Error 2
make[1]: Leaving directory `/home/profiles/2440/linux-2.6.32.2'
make: *** [all] Error 2
第6行 warning
DECLARE_SPINLOCK
第10行 error rmbk_fops 为
struct block_device_operations
第11行 error
.owner = THIS_MODULE
第16行 error
printk
第22行 error
register_blkdev
第25行 error
alloc_disk
第28行 error
blk_init_queue、rmbk_lock
第32行 error
sprintf
第32-36行 error 都是上面引起的连带错误
第38行 error
set_capacity
第41行 error
add_disk
第50行 error
del_gendisk
第51行 error
blk_cleanup_queue
第52行 error
put_disk
第53行 error
unregister_blkdev
第58-59行 warning
module_init,module_exit的警告
第61行 error&warning
MODULE_LICENSE发出的
先说明一下source insight里面经常用到的两个快捷键
Jump to difinition(跳至定义处):
CTRL+
=或者
CTRL+
左键单击关键词
Lookup reference(查找引用):
CTRL+
\ 搜索关键词
步骤:
第6行
DECLARE_SPINLOCK是一个宏,要Lookup reference(查找引用):#define DECLARE_MUTEX
结果:
Spinlock_types.h (e:\friendly2.6.32.2\include\linux):#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
包含:#define
第10行
block_device_operations,Jump to difinition,跳到定义处
怎么查看这个文件的路径呢?
Project->Add and Remove Project Files...->右击被选中的文件->Copy List->Copy selected item(s)
e:\friendly2.6.32.2\include\linux\Blkdev.h
包含:#include
第11行
THIS_MODULE, 同样的,按住鼠标左键,单击这个词
路径:e:\friendly2.6.32.2\include\linux\Module.h
包含:#include
第16行
Jump to difinition: printk
路径: e:\friendly2.6.32.2\include\linux\Kernel.h
包含:#include
第22行
Jump to difinition: register_blkdev
路径: e:\friendly2.6.32.2\block\Genhd.c
包含:???
出现问题了,文件并不在include目录或者子目录中,包含这个文件也没用,而我们用Jump to difinition也只能跳转到Genhd.c
出现int register_blkdev(unsigned int major, const char *name)
这个时候就需要命令Lookup reference(查找引用)。并且搜索的词也是关键,我们需要搜索这个函数的声明,就需要尽量包含整个函数声明的语句。
如int register_blkdev(unsigned int major, const char *name)这整段话搜索的话,很可能不成功,因为包含了变量的名字major, name,不一定能匹配到。
我们应该搜索如"register_blkdev(unsigned int"或者"register_blkdev("这样的关键字,那么就试一试吧。
Fs.h (e:\friendly2.6.32.2\include\linux):extern int register_blkdev(unsigned int, const char *);
Genhd.c (e:\friendly2.6.32.2\block):int register_blkdev(unsigned int major, const char *name)
找到了e:\friendly2.6.32.2\include\linux\Fs.h
那我们就包含#include
第25行
Jump to difinition: alloc_disk
路径: e:\friendly2.6.32.2\block\Genhd.c
找到:struct gendisk *alloc_disk(int minors)
Lookup reference关键字:alloc_disk(int
结果:
Genhd.c (e:\friendly2.6.32.2\block):struct gendisk *alloc_disk(int minors)
Genhd.h (e:\friendly2.6.32.2\include\linux):extern struct gendisk *alloc_disk(int minors);
包含:#include
我们可以在这个文件里看看,应该和一般的硬盘操作有关的函数都在里面,可以找到
set_capacity、add_disk、del_gendisk、put_disk
第28行
Jump to difinition: blk_init_queue
路径: e:\friendly2.6.32.2\block\Blk-core.c
找到:struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Lookup reference关键字:blk_init_queue(request_fn_proc
结果:
Blk-core.c (e:\friendly2.6.32.2\block):struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Blkdev.h (e:\friendly2.6.32.2\include\linux):extern struct request_queue *blk_init_queue(request_fn_proc *, spinlock_t *);
包含:#include ,这里还可以找到blk_cleanup_queue
rmbk_lock,第6行已解决
第32行
Jump to difinition: sprintf
路径: e:\friendly2.6.32.2\lib\Vsprintf.c
找到:int sprintf(char * buf, const char *fmt, ...)
Lookup reference关键字:sprintf(char * buf
结果:
Kernel.h (e:\friendly2.6.32.2\include\linux):extern int sprintf(char * buf, const char * fmt, ...)
Vsprintf.c (e:\friendly2.6.32.2\lib):int sprintf(char * buf, const char *fmt, ...)
包含:#include ,其实和printk在一个文件里面
第32-36行
上面已解决
第38行
set_capacity,第25行已解决
第41行
add_disk,第25行已解决
第50行
del_gendisk,第25行已解决
第51行 error blk_cleanup_queue
第52行
put_disk,第25行已解决
第53行
unregister_blkdev,第28行已解决
第58-59行
Jump to difinition: module_init是找不到的,我们知道它是一个宏
所以只能Lookup reference关键字:#define module_init
结果:
Init.h (e:\friendly2.6.32.2\include\linux):#define module_init(x) __initcall(x);
Init.h (e:\friendly2.6.32.2\include\linux):#define module_init(initfn)
包含:#include , module_exit也包含在里面
第61行
同样的只能Lookup reference关键字:MODULE_LICENSE
结果:
Module.h (e:\friendly2.6.32.2\include\linux):#define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
Nand_ecc.c (e:\friendly2.6.32.2\drivers\mtd\nand):#define MODULE_LICENSE(x) /* x */
Pq.h (e:\friendly2.6.32.2\include\linux\raid):#define MODULE_LICENSE(licence)
包含:#include
添加上头文件
-
#include <linux/spinlock_types.h>
-
#include <linux/blkdev.h>
-
#include <linux/module.h>
-
#include <linux/kernel.h>
-
#include <linux/fs.h>
-
#include <linux/genhd.h>
-
#include <linux/init.h>
再次编译,Finished!
2012-04-20 17:18 发表于百度空间,今搬至CU。
阅读(5183) | 评论(0) | 转发(5) |