Linux驱动开发入门(一)
在Feodra Linux 14编译驱动非常方便,我们可以使用社区提供好的Package,来进行编译工作。编译过程
首先在系统中安装kernel相关的package:
yum -y install kernel-devel kernel-headers
安装成功后yum会返回日志类似如下:
Installed:kernel-devel.i686 0:2.6.35.10-74.fc14Updated:kernel-headers.i686 0:2.6.35.10-74.fc14
Fedora 14会保证内核版本与kernel开发库的代码版本一致,如果需要确认,可以试试看用命令来确认:
uname -a
执行上述命令应该返回结果类似如下:
Linux fedora14 2.6.35.6-45.fc14.i686 #1 SMP Mon Oct 18 23:56:17 UTC 2010 i686 i686 i386 GNU/Linux
与安装的kernel-devel及kernel-headers版本一致即可。
接下来在同一目录下创建两个文件,一个是helloworld.c
#include <linux/module.h> /* Needed by all modules */#include <linux/kernel.h> /* Needed for KERN_INFO */#include <linux/init.h> /* Needed for the macros */static int __init hello_start(void){printk(KERN_INFO "Loading hello module...\n");printk(KERN_INFO "Hello world\n");return 0;}static void __exit hello_end(void){printk(KERN_INFO "Goodbye Mr.\n");}module_init(hello_start);module_exit(hello_end);
另一个为Makefile:
# Comment/uncomment the following line to disable/enable debugging#DEBUG = y# Add your debugging flag (or not) to CFLAGSifeq ($(DEBUG),y)DEBFLAGS = -O -g # "-O" is needed to expand inlineselseDEBFLAGS = -O2endifEXTRA_CFLAGS += $(DEBFLAGS) #-I$(LDDINCDIR)ifneq ($(KERNELRELEASE),)# call from kernel build systemobj-m:= helloworld.oelseKERNELDIR ?= /lib/modules/$(shell uname -r)/buildPWD := $(shell pwd)default:$(MAKE) -C $(KERNELDIR) M=$(PWD) modules #LDDINCDIR=$(PWD)/../include modulesendifclean:rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versionsdepend .depend dep:$(CC) $(CFLAGS) -M *.c > .dependifeq (.depend,$(wildcard .depend))include .dependendif
创建完成后,对代码进行编辑,在上述代码所在目录执行命令如下:
make
编译过程类似如下:
make -C /lib/modules/2.6.35.6-45.fc14.i686/build M=/home/liweinan/projs modules #LDDINCDIR=/home/liweinan/projs/../include modulesmake: Entering directory `/usr/src/kernels/2.6.35.6-45.fc14.i686'CC /home/liweinan/projs/helloworld.oBuilding modules, stage 2.MODPOST 1 modulesCC /home/liweinan/projs/helloworld.mod.oLD /home/liweinan/projs/helloworld.komake: Leaving directory `/usr/src/kernels/2.6.35.6-45.fc14.i686'
helloworld.ko就是我们要的module,安装它试试看:
insmod helloworld.ko
如果安装正确则不会有消息返回。看看dmesg里面的日志:
dmesg | tail
应该有下述日志:
[ 1138.690913] helloworld: module license 'unspecified' taints kernel.[ 1138.690915] Disabling lock debugging due to kernel taint[ 1138.691012] Loading hello module...[ 1138.691014] Hello world
说明安装成功。
常见问题
-1 Invalid module format
这个是在安装模块时可能会遇到的问题,一般是由于使用的kernel library与kernel版本不一致造成,在Fedora 14下通过yum安装,应该会由系统保证环境的一致性,但如果你真的遇到了这个问题,那么可能就需要自己来编译Linux Kernel,Fedora针对内核编译也有十分方便的方法,请参考这篇文档 http://fedoraproject.org/wiki/Docs/CustomKernel
关于Linux内核及驱动开发的一些有用资源
文中所用代码来源在这里:http://www.linuxquestions.org/questions/programming-9/trying-to-compile-hello-world-kernel-module-please-help-439353/
http://kernelnewbies.org/ - 面向内核开发新手的网站,不少有用资源
http://lwn.net/Articles/2.6-kernel-api/ - linux 2.6内核的新版本特性,非常有用,Linux内核每一次微小升级做出的改变,都需要随时了解。
http://lwn.net/Kernel/LDD3/ - Linux驱动开发第三版,注意这本书里面的不少代码在最新的2.6内核已经不可用了。比如在书中的样例代码中经常见到的linux/config.h在最新的2.6内核版本中已经不再存在。遇到问题时多多利用google可以解决不少问题。
页:
[1]