ubuntu 下 驅動開發環境

                                   ubuntu 下 驅動開發環境

本文是參考了網上多篇帖子而寫的算不上什麼原創。唯一值得欣慰的只不過在本機上實現罷了。因爲畢竟失敗了幾次。也因爲本人是初學驅動編程很多簡單的問題在我來說是相當的困難的。望有識之士不要笑話。最後,希望本文能給剛學驅動而還沒開頭的人提供一些幫助。

剛看 O'REILLY 寫的《LINUX 設備驅動程序》時。作者一再強調在編寫驅動程序時必須建立內核樹。所謂內核樹,我的理解和網上資料說的一致就是內核源碼的一種邏輯形式。那怎麼建立呢?爲此上網“翻雲覆雨”起來而結果卻是“慘敗而歸“。
爲此託了一天又4個小時(當然包括吃飯睡覺的時間),連個簡單的 hello wrold 都沒實現。(書中p22頁最簡單也最沒用的驅動事列)
不過功夫不負有心人。在今天終於弄明白了怎麼回事。下面就請讓我慢慢道來吧。

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

shana@shana:~$ uname -r
2.6.22-14-generic

如果安裝系統時,自動安裝了源碼。在 /usr/src 目錄下有對應的使用的版本目錄。例如下(我是自己下的)

shana@shana:/usr/src$ ls
linux-headers-2.6.22-14
linux-headers-2.6.22-14-generic
linux-source-2.6.22
linux-source-2.6.22.tar.bz2
shana@shana:/usr/src$

如果沒有源碼。(一般ubuntu 都沒有吧)
查看一下可一下載的源碼包(切記不要使用超級用戶使用此命令否則……會提示沒有此命令)

shana@shana:/usr/src$ apt-cache search linux-source
linux-source - Linux kernel source with Ubuntu patches
xen-source-2.6.16 - Linux kernel source for version 2.6.17 with Ubuntu patches
linux-source-2.6.22 - Linux kernel source for version 2.6.22 with Ubuntu patches
shana@shana:/usr/src$

我選擇了 linux-source-2.6.22 - Linux kernel source for version 2.6.22 with Ubuntu patches 這個~
然後 install 之

shana@shana:/usr/src$ sudo apt-get install linux-source-2.6.22

下載完成後,在/usr/src下,文件名爲:linux-source-2.6.22.tar.bz2,是一個壓縮包,解壓縮既可以得到整個內核的源代碼:

注意 已經切換到超級用戶模式

root@shana:/usr/src#tar jxvf linux-source-2.6.20.tar.bz2

解壓後生成一個新的目錄/usr/src/linux-source-2.6.22,所有的源代碼都在該目錄下。

進入該目錄

開始配置內核 選擇最快的原版的配置(默認)方式 (我是如此)

root@shana:/usr/src/linux-source-2.6.22# make oldconfig

當然你也可以使用 自己喜歡的配置方式 如 menuconfig , xconfig(必須有GTK環境吧)。反正不用剪裁什麼,所以不管那種方式能配置它就行了。

完成後,開始make 吧 這兒比較久 一般有1一個小時吧。(保證空間足夠 我編譯完成後 使用了1.8G)我分區時分給/目錄30G的空間,我沒遇到這問題。倒是我朋友遇到了。

shana@shana:/usr/src/linux-source-2.6.22$ make

shana@shana:/usr/src/linux-source-2.6.22$ make bzImage

當然,第一個make也可以不執行,直接make bzImage。執行結束後,可以看到在當前目錄下生成了一個新的文件: vmlinux, 其屬性爲-rwxr-xr-x。

然後 :

root@shana:/usr/src/linux-source-2.6.22#make modules

root@shana:/usr/src/linux-source-2.6.22#make modules_install

執行結束之後,會在/lib/modules下生成新的目錄/lib/modules/2.6.22-14-generic/
。 在隨後的編譯模塊文件時,要用到這個路徑下的build目錄。至此,內核編譯完成。可以重啓一下系統。

至此 內核樹就建立啦 原來不是很難.....

寫一個 最簡單 最沒用的驅動吧
我在 /home/shana/linux_q/ 目錄下創建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.20/build
PWD := $(shell pwd)

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

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

如果以上你都完成了在 make 時出現這樣的錯誤

shana@shana:~/linux_驅動開發$ make
make: 沒有什麼可以做的爲 `modules'。

原因很簡單 你肯定是從我這直接複製的吧~~~呵呵,Makefile格式錯誤啦~
解決辦法就是 你把如 $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install 移動到行首然後按Tab 鍵自動對齊
這時裏邊的變量都變成綠色了~然後在 make 吧

shana@shana:~/linux_驅動開發$ make
make -C /lib/modules/2.6.22-14-generic/build M=/home/shana/linux_驅動開發 modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.22-14-generic'
CC [M] /home/shana/linux_驅動開發/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/shana/linux_驅動開發/hello.mod.o
LD [M] /home/shana/linux_驅動開發/hello.ko
make[1]: Leaving directory `/usr/src/linux-headers-2.6.22-14-generic'
shana@shana:~/linux_驅動開發$


shana@shana:~/linux_驅動開發$ ls -l
總用量 124
-rw-r--r-- 1 shana shana 303 2008-03-16 10:43 hello.c
-rw-r--r-- 1 shana shana 49039 2008-03-16 12:11 hello.ko
-rw-r--r-- 1 shana shana 687 2008-03-16 12:11 hello.mod.c
-rw-r--r-- 1 shana shana 25840 2008-03-16 12:11 hello.mod.o
-rw-r--r-- 1 shana shana 24360 2008-03-16 12:11 hello.o
-rw-r--r-- 1 shana shana 8344 2008-03-16 09:17 linux_qudong_qu.txt
-rw-r--r-- 1 shana shana 266 2008-03-16 12:09 Makefile
-rw-r--r-- 1 shana shana 0 2008-03-16 12:11 Module.symvers
shana@shana:~/linux_驅動開發$


然後加載模塊 (超級用戶)

root@shana:/home/shana/linux_驅動開發# insmod ./hello.ko

按照書上的例子 會在終端顯示 hello , world 但是運行後什麼都沒有出現 (原因不解)

root@shana:/home/shana/linux_驅動開發# insmod ./hello.ko
root@shana:/home/shana/linux_驅動開發#

查看加載模塊

root@shana:/home/shana/linux_驅動開發# lsmod
Module Size Used by
hello 2560 0

已經加載上咯~~

刪除模塊

root@shana:/home/shana/linux_驅動開發# rmmod hello
root@shana:/home/shana/linux_驅動開發#

那程序的輸出在那呢?書中說明 如果不出現在終端 則會寫進 syslog 文件中

shana@shana:~/linux_驅動開發$ cat /var/log/syslog |grep world
Mar 16 12:14:53 shana kernel: [ 5937.529297] Hello, world
Mar 16 12:16:05 shana kernel: [ 6009.439036] Goodbye, cruel world
shana@shana:~/linux_驅動開發$

至此 全部工作都完成了。是否對你有用呢?

Preface:
1.筆者剛學linux驅動開發,看到網上很多說要現建立內核樹。在筆者到官網下載源碼時,源碼下面有如下說明: If you are simply trying to build third-party modules for your kernel, you do not want this package. Install the appropriate linux-headers package instead. 意思是,如果你只是想爲內核編譯第三方的模塊,那麼,你不需下載此源碼包。安裝內核頭文件包或許會更適合你。

2.如果你的ubuntu是保持更新的,你的系統是安裝有內核頭文件包的,不信你到/usr/src目錄下查看,是不是有linux-headers-2.6.xx-xx-generic的文件夾呢,呵呵。我現在可以說,你可以在此開發你的驅動程序了。

3.筆者也是初學,有見笑的,大家多指正,emial: [email protected]; 轉載註明出處:http://sites.google.com/site/tonychen123site,作者:tony

4.環境本來就有了看來,所以我的文章,本來應該叫驅動開發最簡單例子才合適……另,我要示例的是最簡單,也是個最沒用的驅動演示,名字叫tst,包含源碼tst.c及makefile文件,編譯出來的模塊名叫tst.ko

Content:

1)建立你的實驗目錄並創建tst.c源文件:
代碼:
tony@ubuntu:~/mini2440$ mkdir mtst; cd mtst
tony@ubuntu:~/mini2440/mtst$ vim tst.c

======================================



#include <linux/module.h>   
#include <linux/kernel.h>   
#include <linux/init.h>     

static int __init tst_start(viod)
{
    printk(KERN_INFO "Loading my so simple module.../n");
    printk(KERN_INFO "I love Ubuntu/n");
    return 0;
}

static void __exit tst_end(void)
{
    printk(KERN_INFO "From tst module: Goodbye, Tony./n");
}

module_init(tst_start);
module_exit(tst_end);




2)創建Makefile文件:

代 碼:
tony@ubuntu:~/mini2440/mtst$ vim Makefile
=================================================================
obj-m = tst.o

K_DIR = $(shell uname -r)
PWD = $(shell pwd)

all:
    make -C /lib/modules/$(K_DIR)/build M=$(PWD) modules
clean:
    make -C /lib/modules/$(K_DIR)/build M=$(PWD) clean

# end #


3) 編譯

代碼:
tony@ubuntu:~/mini2440/mtst$ sudo make Note:好像要用sudo才行!
make -C /lib/modules/2.6.28-12-generic/build M=/home/tony/mini2440/mtst modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.28-12-generic'
  CC [M]  /home/tony/mini2440/mtst/tst.o
/home/tony/mini2440/mtst/tst.c:19: warning: function declaration isn’t a prototype
  Building modules, stage 2.
  MODPOST 1 modules        Note:生成一個module!
  CC      /home/tony/mini2440/mtst/tst.mod.o
  LD [M]  /home/tony/mini2440/mtst/tst.ko
make[1]: Leaving directory `/usr/src/linux-headers-2.6.28-12-generic'
tony@ubuntu:~/mini2440/mtst$

4) 測試
代碼:
tony@ubuntu:~/mini2440/mtst$ su     Note:加載模塊要有root特權                                           
Password:                                                                                     
root@ubuntu:/home/tony/mini2440/mtst# insmod tst.ko  Note:加載模塊要有root特權
root@ubuntu:/home/tony/mini2440/mtst# dmesg | tail   
[   25.464531] r8169: eth0: link up             
[   25.464534] r8169: eth0: link up             
[   54.931357] UDF-fs: No VRS found             
[   54.981587] ISO 9660 Extensions: Microsoft Joliet Level 3
[   55.013712] ISOFS: changing to secondary root           
[ 2932.746291] module init                                 
[ 3138.097884] Cleanup module                             
[ 7444.574136] tst: module license 'unspecified' taints kernel.
[ 7444.575117] Loading my so simple module...   Note:注意這兩行,就是我們的模塊加載信息 
[ 7444.575121] I love Ubuntu                                           
root@ubuntu:/home/tony/mini2440/mtst# lsmod | grep tst
tst                     9344  0
root@ubuntu:/home/tony/mini2440/mtst# rmmod tst.ko
root@ubuntu:/home/tony/mini2440/mtst# lsmod | grep tst
root@ubuntu:/home/tony/mini2440/mtst# dmesg | tail
[   25.464534] r8169: eth0: link up
[   54.931357] UDF-fs: No VRS found
[   54.981587] ISO 9660 Extensions: Microsoft Joliet Level 3
[   55.013712] ISOFS: changing to secondary root
[ 2932.746291] module init
[ 3138.097884] Cleanup module
[ 7444.574136] tst: module license 'unspecified' taints kernel.
[ 7444.575117] Loading my so simple module...
[ 7444.575121] I love Ubuntu
[ 7559.570808] From tst module: Goodbye, Tony.    Note:呵呵,模塊正常卸載
root@ubuntu:/home/tony/mini2440/mtst#
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章