撥開烏云見天日驅(qū)動(dòng)開發(fā)之Ubuntu12.04驅(qū)動(dòng)開發(fā)
現(xiàn)在介紹驅(qū)動(dòng)的開發(fā),以一個(gè)簡單的hello進(jìn)行說明。首先我在 /home/test 目錄下創(chuàng)建2個(gè)文本文件 hello.c 和Makefile
本文引用地址:http://www.ex-cimer.com/article/264572.htm//hello.c程序如下
/*
* File Name: Hello.c
*
* Descriptions:
* This is the my first module.
*
* Author:
* Majunling
* Kernel Version: 3.2.0-23-generic-pae
*
* Update:
* - 2014-10-18 Majunling Creat this file
*/
#include
#include
static int hello_init(void)
{
printk(KERN_ALERT "Hello, worldn");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, worldn");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("Dual BSD/GPL");
相信有驅(qū)動(dòng)基礎(chǔ)的人都能看懂上面的程序。還需要一個(gè)makefile文件,具體如下
//Makefile 文件
# To build modules outside of the kernel tree, we run "make"
# in the kernel source tree; the Makefile these then includes this
# Makefile once again.
# This conditional selects whether we are being included from the
# kernel Makefile or not.
ifeq ($(KERNELRELEASE),)
# Assume the source tree is where the running kernel was built
# You should set KERNELDIR in the environment if it's elsewhere
KERNELDIR ?= /lib/modules/3.2.0-23-generic-pae/build
# The current directory is passed to sub-makes as argument
PWD := $(shell pwd)
modules:
(一個(gè)TAB開始)$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
(一個(gè)TAB開始)$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
.PHONY: modules modules_install clean
else
# called from kernel build system: just declare what our modules are
obj-m := hello.o
endif
此時(shí)需要注意Makefile格式,在module和module_install的下一行要以一個(gè)tab開始。完成這些工作之后進(jìn)入test目錄看看有什么內(nèi)容:
這時(shí)萬事俱備,直接make
mjl@mjl-machine:~/test$ make
make -C /lib/modules/3.2.0-23-generic-pae/build M=/home/mjl/test modules
make[1]: Entering directory `/usr/src/linux-headers-3.2.0-23-generic-pae'
Building modules, stage 2.
MODPOST 1 modules
make[1]: Leaving directory `/usr/src/linux-headers-3.2.0-23-generic-pae'
然后查看make之后的文件內(nèi)容:
mjl@mjl-machine:~/test$ ls
hello.c hello.mod.c hello.o modules.order
hello.ko hello.mod.o Makefile Module.symvers
安裝驅(qū)動(dòng)模塊:
mjl@mjl-machine:~/test$ sudo insmod ./hello.ko
[sudo] password for mjl:
我是在普通用戶下進(jìn)行的驅(qū)動(dòng)安裝,所以需要超級(jí)用戶的密碼,輸入之后用lsmod可以查看安裝的hello模塊。
mjl@mjl-machine:~/test$ lsmod
Module Size Used by
hello 12448 0
vmhgfs 53736 1
vsock 39001 0
刪除模塊使用rmmod進(jìn)行,如下:
mjl@mjl-machine:~/test$ sudo rmmod hello
但是不管加載還是卸載都沒有打印信息,來這里查看
mjl@mjl-machine:~/test$ cat /var/log/syslog |grep world
Oct 18 16:12:20 mjl-machine kernel: [ 3335.231700] Hello, world
Oct 18 16:14:15 mjl-machine kernel: [ 3449.649224] Goodbye, world
究其原因,是因?yàn)槿绻阍谧址K端而不是終端模擬器下運(yùn)行的話,就會(huì)輸出,因?yàn)樵诮K端模擬器下時(shí)會(huì)把內(nèi)核消息輸出到日志文件/var/log/kern.log中。
現(xiàn)在基于ubuntu系統(tǒng)的驅(qū)動(dòng)開發(fā)需要的平臺(tái)已經(jīng)搭建完畢,經(jīng)過測試可以使用,下一步就是進(jìn)行驅(qū)動(dòng)的系統(tǒng)學(xué)習(xí),那是一項(xiàng)長期和艱巨的任務(wù),你準(zhǔn)備好了嗎?如果在學(xué)習(xí)過程中碰到什么問題,歡迎來論壇forum.eepw.com.cn/forum/p/id/84進(jìn)行提問,我們會(huì)在這里給你最及時(shí)最準(zhǔn)確的解答。
linux相關(guān)文章:linux教程
評(píng)論