撥開(kāi)烏云見(jiàn)天日驅動(dòng)開(kāi)發(fā)之Ubuntu12.04驅動(dòng)開(kāi)發(fā)
現在介紹驅動(dòng)的開(kāi)發(fā),以一個(gè)簡(jiǎn)單的hello進(jìn)行說(shuō)明。首先我在 /home/test 目錄下創(chuàng )建2個(gè)文本文件 hello.c 和Makefile
本文引用地址:http://dyxdggzs.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");
相信有驅動(dòng)基礎的人都能看懂上面的程序。還需要一個(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開(kāi)始)$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
(一個(gè)TAB開(kāi)始)$(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開(kāi)始。完成這些工作之后進(jìn)入test目錄看看有什么內容:
這時(shí)萬(wàn)事俱備,直接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之后的文件內容:
mjl@mjl-machine:~/test$ ls
hello.c hello.mod.c hello.o modules.order
hello.ko hello.mod.o Makefile Module.symvers
安裝驅動(dòng)模塊:
mjl@mjl-machine:~/test$ sudo insmod ./hello.ko
[sudo] password for mjl:
我是在普通用戶(hù)下進(jìn)行的驅動(dòng)安裝,所以需要超級用戶(hù)的密碼,輸入之后用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
但是不管加載還是卸載都沒(méi)有打印信息,來(lái)這里查看
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
究其原因,是因為如果你在字符終端而不是終端模擬器下運行的話(huà),就會(huì )輸出,因為在終端模擬器下時(shí)會(huì )把內核消息輸出到日志文件/var/log/kern.log中。
現在基于ubuntu系統的驅動(dòng)開(kāi)發(fā)需要的平臺已經(jīng)搭建完畢,經(jīng)過(guò)測試可以使用,下一步就是進(jìn)行驅動(dòng)的系統學(xué)習,那是一項長(cháng)期和艱巨的任務(wù),你準備好了嗎?如果在學(xué)習過(guò)程中碰到什么問(wèn)題,歡迎來(lái)論壇forum.eepw.com.cn/forum/p/id/84進(jìn)行提問(wèn),我們會(huì )在這里給你最及時(shí)最準確的解答。
linux相關(guān)文章:linux教程
評論