2.6驱动移植系列之Getting started(1)-- 驱动移..
时间:2007-03-12 来源:lanttor.guo
联系邮件: [email protected]
Driver porting: hello world
[Posted February 4, 2003 by corbet]
This article is part of the LWN Porting Drivers to 2.6 series.
Your editor is currently in the middle of porting the example source from Linux Device Drivers, Second Edition to the 2.5 kernel. This work is, of course, just the beginning of the rather larger job of updating the whole book. This article is the first in what will, hopefully, be a series describing what is required to make this code work again. The series will thus, with luck, be useful as a guide to how to port drivers to the new kernel API.
你的这位编辑(这里指corbet本人,谦称)正在将LDD2(2nd)上的样例代码移植到kernel2.5。这个工作,是更新整个书(指的是更新LDD2,现在已出版LDD3)的开始。希望从这篇文章开始(以及后面一系列的由corbet撰写的文章),我们使LDD2上的代码重新工作起来。这一系列文章可以作为将驱动移植到新kernel API(kernel 2.6)的指南。
The obvious place to start in this sort of exercise, of course, is the classic "hello world" program, which, in this context, is implemented as a kernel module. The 2.4 version of this module looked like:
进行这种练习(这里指驱动的编写和移植)最容易的时机,就是从大名鼎鼎的”hello world”程序开始。这里,该程序被实现为一个内核模块(kernel module)。2.4版本的模块看起来是这样的:
#define MODULE
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{
printk(KERN_INFO "Hello, world\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye cruel world\n");
}
One would not expect that something this simple and useless would require much in the way of changes, but, in fact, this module will not quite work in a 2.5 kernel. So what do we have to do to fix it up?
没有人期望这么一个简单而无用的程序还需要很多修改,可是实际上,这个模块的确不能在2.5kernel上工作。我们如何修改它呢?
The first change is relatively insignificant; the first line:
#define MODULE
is no longer necessary, since the kernel build system (which you really should use now, see the next article) defines it for you.
第一处修改相对不是很重要,第一行:
#define MODULE
不再需要,因为内核构建系统(指编译内核和安装内核)将会定义这个宏。
The biggest problem with this module, however, is that you have to explicitly declare your initialization and cleanup functions with module_init and module_exit, which are found in <linux/init.h>. You really should have done that for 2.4 as well, but you could get away without it as long as you used the names init_module and cleanup_module. You can still sort of get away with it (though you may have to ignore some compiler warnings), but the new module code broke this way of doing things once, and could do so again. It's really time to bite the bullet and do things right.
这个模块的最大修改处,是你必须显示的声明模块的初始化函数和清除函数。为了达到这一目的,你需要使用<linux/init.h>中定义的module_init和module_exit。在2.4kernel里,你的确可以使用这两个宏函数,但是你也可以不用它们,而用init_module和cleanup_module作为初始化函数名和清除函数名(关于这一点,可以参考LDD2)。但是新的模块代码(在2.6kernel里)必须遵循新的约定,只能使用module_init和module_exit。
With these changes, "hello world" now looks like:
根据上述的修改,”hello world”模块现在看起来应该是:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
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);
This module will now work - the "Hello, world" message shows up in the system log file. What also shows up there, however, is a message reading "hello: module license 'unspecified' taints kernel." "Tainting" of the kernel is (usually) a way of indicating that a proprietary module has been inserted, which is not really the case here. What's missing is a declaration of the license used by the module:
MODULE_LICENSE("Dual BSD/GPL");
这个模块现在可以开始工作-“Hello, World”模块的输出信息在系统日志文件里显示。当你查看这个系统日志文件的时候,会发行这样的一行信息:"hello: module license 'unspecified' taints kernel."。内核被污染(Tainting)表明了一个私有的模块被插入了内核,这是不运行发生的。所以我们修改的“Hello, World”模块还少了一句声明:
MODULE_LICENSE("Dual BSD/GPL");
MODULE_LICENSE is not exactly new; it was added to the 2.4.10 kernel. Some older code may still lack MODULE_LICENSE calls, however. They are worth adding; in addition to avoiding the "taints kernel" message, a license declaration gives your module access to GPL-only kernel symbols. Assuming, of course, that the module is GPL-licensed.
MODULE_LICENSE并不是新引入的,在2.4.10kernel里已经有了。一项旧代码可能仍然缺少MODULE_LICENSE的声明。这个声明需要加入,除了可以避免“污染内核”这样的信息输出,这项声明给予你的模块可以访问GPL-only内核符号的权利。
With these changes, "hello world" works as desired. At least, once you have succeeded in building it properly; that is the subject of the next article.
有了这些修改,"hello world"模块可以按照所期望的工作了。当然之前,你应该成功的编译和构建好模块。