日期:2014-05-16 浏览次数:20656 次
想学习一下驱动,于是找了一个实例HelloWorld,编写自己的第一个驱动程序
安装ubuntu系统,打开终端,以root权限进入,命令如下
bory@borya:~$ sudo -s
root@borya:~# apt-cache search linux-source linux-source - Linux kernel source with Ubuntu patches linux-source-3.0.0 - Linux kernel source for version 3.0.0 with Ubuntu patches
接着下载内核源码包
root@borya:~# apt-get install linux-source-3.0.0
root@borya:~# cd /usr/src/ root@borya:/usr/src# ls linux-headers-3.0.0-12 linux-source-3.0.0 vboxguest-4.1.16 linux-headers-3.0.0-12-generic linux-source-3.0.0.tar.bz2 root@borya:/usr/src# tar jxvf linux-source-3.0.0.tar.bz2
root@borya:/usr/src/linux-source-3.0.0# make oldconfig
root@borya:/usr/src/linux-source-3.0.0# make bzImage
继续编译modules,也需要1个多小时
root@borya:/usr/src/linux-source-3.0.0# make modules
最后是安装modules
root@borya:/usr/src/linux-source-3.0.0# make modules_install
在自己任意工作目录下,新建一个目录test。
编写hello.c
#include "linux/init.h" #include "linux/module.h" static int hello_init(void) { printk(KERN_ALERT "Hello World linux_driver_module\n"); return 0; } static void hello_exit(void) { printk(KERN_ALERT "This is first step linux_driver_module\n"); } module_init(hello_init); module_exit(hello_exit);
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
bory@borya:~/driver/hello$ make
bory@borya:~/driver/hello$ insmod ./hello.ko
root@borya:~/driver/hello$rmmod ./hello.ko