ubuntu8.10内核驱动模块编程环境配置

一 篇很 棒的 文章,对于模块,驱动的換境很 不错
转 http://hi.baidu.com/zhouhanqing/blog/item/7f283eca2ea9ce17bf09e6bf.html

本文是参考了网上多篇帖子而写的算不上什么原创。唯一值得欣慰的只不过在本机上实现罢了。因为毕竟失败了几次。也因为本人是初学驱动编程 很多简单的问题在我来说是相当的困难的。望有识之士不要笑话。最后,希望本文能给刚学驱动而还没开头的人提供一些帮助。

刚看 O'REILLY 写的《LINUX 设备驱动程序》时。作者一再强调在编写驱动程序时必须 建立内核树。所谓内核树,我的理解和网上资料说的一致就是内核源码的一种逻辑形式。那怎么建立呢?为此上网“翻云覆雨”起来而结果却是“惨败而归“。
为此托了一天又4个小时(当然包括吃饭睡觉的时间),连个简单的 hello wrold 都没实现。(书中p22页最简单也最没用的驱动事列)
不过功夫不负有心人。在今天终于弄明白了怎么回事。下面就请让我慢慢道来吧。

先查看自己OS使用的内核版本

zhouhanqing@ubuntu:~$ uname -r
2.6.27-11-generic
/* 这是我显示的结果 */

如果安装系统时,自动安装了源码。在 /usr/src 目录下有对应的使用的版本目录。例如下(我是自己下的)



如果没有源码。(一般ubuntu 都没有吧)
查看一下可一下载的源码包(切记不要使用超级用户使用此命令否则……会提示没有此命令)


我选择了 linux-2.6.27.11 这个(少了general,目前不知道有啥区别)~
然后 install 之 ,现在ubuntu都可以右键直接解压
我把源码放在了/home/zhouhanqing下解压后生成一个新的目录/home/zhouhanqing/linux-2.6.27.11,所有的源代码都在该目录下。

进入该目录
进入后,安全起见,先 make mrproper 一下
step1:
开始配置内核
apt-get install libncurses5-dev (meke menuconfig要调用的)
zhouhanqing@ubuntu:~/linux-2.6.27.11$ make menuconfig


如果连基本的编程库(gcc, make等)也没有的话

apt-get install build-essential

如果上面的东西都装完了,恭喜你,终于可以make menuconfig了

但先别急,相信你现在的系统正用得很爽

上网,声音,显示···什么问题都没有
为此,你最好把现在内核用的.config文件拷贝到~/linux-2.6.27.11/下,当模板。在这里将 /boot/下的config-2.6.27-11-generic拷贝到~/linux-2.6.27.11/(你刚下载下来解压的 Linux源码下,把名字改为.config)


当你make menuconfig后,选倒数第二项:Load an alternate configuration file

把.config加载进来,这样你就能在原来内核的基础之上修改了

当然你也可以使用 自己喜欢的配置方式 如oldconfig , xconfig(必须有GTK环境吧)。反正不用剪裁什么,所以不管那种方式能配置它就行了。

Step2: make
让他编译吧,耐心等待。
Step3: make bzImage
Step4: make modules
Step5: make modules_install
Step6: make install
重新启动
至此 内核树就建立啦 原来不是很难.....

写一个 最简单 最没用的驱动吧
我在 /home/zhouhanqing/lHandyDriver/ 目录下创建2个文本文件 hello.c Makefile

//hello.c
#include <linux/init.h>
#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);

程序我就不解释了……

Makefile 文件

obj-m := hello.o
KERNELDIR := /lib/modules/2.6.27。11/build
PWD := $(shell pwd)

modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

如果以上你都完成了在 make 时出现这样的错误

zhouhanqing@ubuntu:~/linux_驱动开发$ make
make: 没有什么可以做的为 `modules'。

原因很简单 你肯定是从我这直接复制的吧~~~呵呵,Makefile格式错误啦~
解决办法就是 你把如 $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install 移动到行首 然后按Tab 键自动对齐
这时里边的变量都变成绿色了~然后在 make 吧
另外注意在Makefile中obj-m := hello.o 必须与.c文件的名字相同,大小写一样,这里.c应该是hello.c,而不是Hello.c或其他。
zhouhanqing@ubuntu:~/HandyDriver$ make
make -C /lib/modules/2.6.27.11/build M=/home/zhouhanqing/HandyDriver modules
make[1]: 正在进入目录 `/home/zhouhanqing/linux-2.6.27.11'
CC [M] /home/zhouhanqing/HandyDriver/Hello.o
Building modules, stage 2.
MODPOST 1 modules
CC      /home/zhouhanqing/HandyDriver/Hello.mod.o
LD [M] /home/zhouhanqing/HandyDriver/Hello.ko
make[1]:正在离开目录 `/home/zhouhanqing/linux-2.6.27.11'
zhouhanqing@ubuntu:~/HandyDriver$ ls -l
总用量 160
-rw-r--r-- 1 zhouhanqing zhouhanqing   302 2009-03-25 15:48 Hello.c
-rw-r--r-- 1 zhouhanqing zhouhanqing   302 2009-03-25 15:48 Hello.c~
-rw-r--r-- 1 zhouhanqing zhouhanqing 62986 2009-03-25 16:00 Hello.ko
-rw-r--r-- 1 zhouhanqing zhouhanqing   690 2009-03-25 16:00 Hello.mod.c
-rw-r--r-- 1 zhouhanqing zhouhanqing 36200 2009-03-25 16:00 Hello.mod.o
-rw-r--r-- 1 zhouhanqing zhouhanqing 27748 2009-03-25 16:00 Hello.o
-rw-r--r-- 1 zhouhanqing zhouhanqing   200 2009-03-25 16:00 Makefile
-rw-r--r-- 1 zhouhanqing zhouhanqing   200 2009-03-25 15:46 Makefile~
-rw-r--r-- 1 zhouhanqing zhouhanqing   291 2009-03-25 16:00 Module.markers
-rw-r--r-- 1 zhouhanqing zhouhanqing    46 2009-03-25 16:00 modules.order
-rw-r--r-- 1 zhouhanqing zhouhanqing     0 2009-03-25 16:00 Module.symvers

然后加载模块 (超级用户)

zhouhanqing@ubuntu:~/HandyDriver$ sudo insmod Hello.ko


按照书上的例子 会在终端显示 hello , world 但是运行后什么都没有出现 (printk优先级问题)

zhouhanqing@ubuntu:~/HandyDriver$ sudo insmod Hello.ko

查看加载模块

zhouhanqing@ubuntu:~/HandyDriver$ lsmod
Module                  Size Used by
Hello                   9472 0

已经加载上咯~~

删除模块

zhouhanqing@ubuntu:~/HandyDriver$sudo rmmod Hello


那程序的输出在那呢?书中说明 如果不出现在终端 则会写进 syslog 文件中

zhouhanqing@ubuntu:~/HandyDriver$ cat /var/log/syslog|grep world
Mar 25 16:05:04 ubuntu kernel: [ 1386.173320] Hello, world
Mar 25 16:06:10 ubuntu kernel: [ 1452.712873] Goodbye, cruel world

转自:http://hi.baidu.com/zhouhanqing/blog/item/7f283eca2ea9ce17bf09e6bf.html

猜你喜欢

转载自yiranwuqing.iteye.com/blog/910569