【轉】第一個Linux核心驅動程式

來源:互聯網
上載者:User

標籤:

原文網址:http://blog.csdn.net/nexttake/article/details/8181008

剛看 O’REILLY 寫的《LINUX 裝置驅動程式》時。作者一再強調在編寫驅動程式時必須 建立核心樹。所謂核心樹,我的理解和網上資料說的一致就是核心源碼的一種邏輯形式。

先查看自己OS使用的核心版本
[email protected]:~$ uname -r
2.6.22-14-generic           /*  這是我顯示的結果 */
如果安裝系統時,自動安裝了源碼。在 /usr/src 目錄下有對應的使用的版本目錄。例如下(我是自己下的)
[email protected]:/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         /*  這是我下的源碼 包 */
[email protected]:/usr/src$ 
如果沒有源碼。(一般ubuntu 都沒有吧)
查看一下可一下載的源碼包(切記不要使用超級使用者使用此命令否則……會提示沒有此命令)
[email protected]:/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
[email protected]:/usr/src$ 
我選擇了   linux-source-2.6.22 - Linux kernel source for version 2.6.22 with Ubuntu patches   這個~
然後 install 之
[email protected]:/usr/src$sudo  apt-get install linux-source-2.6.22 (相當於下載這個版本的核心源碼)
下載完成後,在/usr/src下,檔案名稱為:linux-source-2.6.22.tar.bz2,是一個壓縮包,解壓縮既可以得到整個核心的原始碼:

可以使用apt-get purgelinux-source-2.6.22 卸載。
注意 已經切換到超級使用者模式
[email protected]:/usr/src#sudo tar jxvf linux-source-2.6.20.tar.bz2
解壓後產生一個新的目錄/usr/src/linux-source-2.6.22,所有的原始碼都在該目錄下。
---------------------------------------------------------------------------------------------------------------------------------------------------

進入該目錄
開始配置核心 選擇最快的原版的配置(預設)方式 (我是如此)
[email protected]:/usr/src/linux-source-2.6.22# make oldconfig或者 make defconfig
當然你也可以使用 自己喜歡的配置方式 如  menuconfig , xconfig(必須有GTK環境吧)。反正不用剪裁什麼,所以不管那種方式能配置它就行了。
完成後,開始make 吧 這兒比較久 一般有1一個小時吧。(保證空間足夠 我編譯完成後 使用了1.8G) 我分區時分給/目錄30G的空間,我沒遇到這問題。倒是我朋友遇到了。
[email protected]:/usr/src/linux-source-2.6.22$ make
[email protected]:/usr/src/linux-source-2.6.22$  make bzImage
當然,第一個make也可以不執行,直接make bzImage。執行結束後,可以看到在目前的目錄下產生了一個新的檔案: vmlinux, 其屬性為-rwxr-xr-x。
然後 :
[email protected]:/usr/src/linux-source-2.6.22#make modules                /*   編譯 模組 */
[email protected]:/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  檔案

[html] view plaincopy 
  1. obj-m := hello.o  
  2. KERNELDIR := /lib/modules/2.6.38-8-generic/build/  
  3. PWD := $(shell pwd)  
  4. modules:$(MAKE) -C $(KERNELDIR) M=$(PWD) modules  
  5. modules_install:$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install  


其實如果使用這個命令編譯,Makefile一行即可

[html] view plaincopy 
  1. obj-m := hello.o  


[email protected]:~/linux_驅動開發$make -C /lib/modules/2.6.38-8-generic/build/  M=/home/yzy/mydriver  
[email protected]:~/linux_驅動開發$ ls -l
總用量 124
-rw-r--r-- 1 shana shana   303 2008-03-16 10:43 hello.c
-rw-r--r-- 1 shana shana 4Array03Array 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 0Array:17 linux_qudong_qu.txt
-rw-r--r-- 1 shana shana   266 2008-03-16 12:0Array Makefile
-rw-r--r-- 1 shana shana     0 2008-03-16 12:11 Module.symvers
[email protected]:~/linux_驅動開發$ 
然後載入模組 (超級使用者)
[email protected]:/home/shana/linux_驅動開發# insmod ./hello.ko 
按照書上的例子 會在終端顯示 hello , world 但是運行後什麼都沒有出現 (原因不解)
[email protected]:/home/shana/linux_驅動開發# insmod ./hello.ko   或者sudo -s先切換到root使用者
[email protected]:/home/shana/linux_驅動開發# 
查看載入模組
[email protected]:/home/shana/linux_驅動開發# lsmod
Module                  Size  Used by
hello                   2560  0 
已經載入上咯~~
刪除模組
[email protected]:/home/shana/linux_驅動開發# rmmod hello
[email protected]:/home/shana/linux_驅動開發# 

 

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
那程式的輸出在那呢?書中說明 如果不出現在終端 則會寫進 syslog 檔案中
[email protected]:~/linux_驅動開發$  dmesg
[ 2266.586863] Hello, world\n
[ 2389.281426] Goodbye, cruel world\n
 
至此 全部工作都完成了。

【轉】第一個Linux核心驅動程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.