linux驅動源碼
Linux內核源碼路徑:/usr/src/linux(這個源碼是從kernel.org網站download的2.4.18版本)
按照《linux設備驅動開發詳解》一書中的步驟實現經典例子"hello,world!"的例子。
具體步驟如下:
=============================================
1.源碼如下:
/*
* hello.c -- the example of printf "hello world!" in the screen of driver program
*/
#include <linux/init.h>
#include <linux/mole.h>
MODULE_LICENSE("Dual BSD/GPL");/* declare the license of the mole ,it is necessary */
static int hello_init(void)
{
printk(KERN_ALERT "Hello World enter!\n");
return 0;
}
static int hello_exit(void)
{
printk(KERN_ALERT "Hello world exit!\n");
}
mole_init(hello_init); /* load the mole */
mole_exit(hello_exit); /* unload the mole */
進入目錄:
[root@Alex_linux /]#cd /work/jiakun_test/moletest
[root@Alex_linux moletest]# vi hello.c
然後拷入上面書上的源碼。
2.編譯代碼:
1>.首先我在2.4內核的虛擬機上進行編譯,編譯過程如下:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
其中-I選項指定內河源碼,也就是內核源碼樹路徑。編譯結果:
hello.c:1:22: net/sock.h: No such file or directory
hello.c: In function `hello_init':
hello.c:6: warning: implicit declaration of function `printk'
hello.c:6: `KERN_ALERT' undeclared (first use in this function)
hello.c:6: (Each undeclared identifier is reported only once
hello.c:6: for each function it appears in.)
hello.c:6: parse error before string constant
hello.c: In function `hello_exit':
hello.c:11: `KERN_ALERT' undeclared (first use in this function)
hello.c:11: parse error before string constant
hello.c: At top level:
hello.c:13: warning: type defaults to `int' in declaration of `mole_init'
hello.c:13: warning: parameter names (without types) in function declaration
hello.c:13: warning: data definition has no type or storage class
hello.c:14: warning: type defaults to `int' in declaration of `mole_exit'
hello.c:14: warning: parameter names (without types) in function declaration
hello.c:14: warning: data definition has no type or storage class
在網上查詢有網友提示沒有引入kernel.h
解決:vi hello.c
在第一行加入:#include <linux/kernel.h>
再次編譯仍然報KERN_ALERT沒有聲明
修改編譯條件-I,再次編譯:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
[root@Alex_linux moletest]#ls
hello.c hello.o Makefile
[root@Alex_linux moletest]#
2>.接著我嘗試在2.6內核的虛擬機上進行編譯
編譯過程如下:
[root@JiaKun moletest]# ls
hello.c makefile
[root@JiaKun moletest]# vi hello.c
[root@JiaKun moletest]# make
make -C /mylinux/kernel/2.4.18-rmk7 M=/home/alex/test/moletest moles
make: *** /mylinux/kernel/2.4.18-rmk7: No such file or directory. Stop.
make: *** [moles] Error 2
[root@JiaKun moletest]# vi makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
scripts/Makefile.build:17: /home/alex/test/moletest/Makefile: No such file or directory
make[2]: *** No rule to make target `/home/alex/test/moletest/Makefile'. Stop.
make[1]: *** [_mole_/home/alex/test/moletest] Error 2
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
make: *** [moles] Error 2
[root@JiaKun moletest]# mv makefile Makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
CC [M] /home/alex/test/moletest/hello.o
Building moles, stage 2.
MODPOST
CC /home/alex/test/moletest/hello.mod.o
LD [M] /home/alex/test/moletest/hello.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
[root@JiaKun moletest]# ls
hello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile Mole.symvers
3.執行代碼,載入驅動模塊:
2.4內核載入模塊:
insmod ./hello.o
但是此時並沒有輸出printk列印的信息。但是可以在/var/log/messages 中看到列印的信息,這是由於KERN_ALERT優先順序不夠高。這里
需要修改為:KERN_EMERG。再次編譯,載入模塊即可以看到結果
2.6內核載入模塊:
[root@JiaKun moletest]# insmod hello.ko
[root@JiaKun moletest]#
Message from syslogd@ at Sat Jul 26 19:52:44 2008 ...
JiaKun kernel: Hello, world
有的朋友可能會出現insmod命令找不到的錯誤,這可能有下面幾個原因:
<1> 你的系統沒有安裝mole-init-tools工具,關於此問題,只需安裝即可,但是一般裝完系統是有這個命令的。
<2> 環境變數沒有添加導致不能使用該命令。使用echo $PATH即可查看PATH環境變數,發現沒有/sbin這個路徑,所以你當然不能使用insmod這個命令了。解決的方法很簡單,只需在命令行輸入:
PATH = "$PATH:/sbin"即可添加。(insmod在/sbin這個目錄下,你可以使用whereis insmod查看)。
<3> insmod這個命令需要在root許可權下才能使用。
載入完成後你可以輸入lsmod查看hello這個模塊哦。
4.卸載驅動模塊:rmmod hello.
載入模塊後就可在屏幕上看到如下信息:Hello world enter.
卸載時就可在屏幕上看到如下信息:hello world exit.
[root@JiaKun moletest]# rmmod hello.ko
[root@JiaKun moletest]#
Message from syslogd@ at Sat Jul 26 19:52:58 2008 ...
JiaKun kernel: Goodbye, cruel world
另外,如果有多個文件,則按下列方式編寫Makefile文件(file1.c、file2.c):
obj -m := molename.o
mole-objs := file1.o file2.o
轉載僅供參考,版權屬於原作者。祝你愉快,滿意請採納哦
2. 哪位知道HDMI的驅動源碼在linux源碼包的哪個目錄下,具體是 xxx.c
應該都在這了,hdmi.c
3. Linux內核源碼在哪裡
一般在Linux系統中的/usr/src/linux*.*.*(*.*.*代表的是內核版本,如2.4.23)目錄下就是內核源代碼(如果沒有類似目錄,是因為還沒安裝內核代碼)。另外還可從互連網上免費下載。注意,不要總到http://www.kernel.org/去下載,最好使用它的鏡像站點下載。請在http://www.kernel.org/mirrors/里找一個合適的下載點,再到pub/linux/kernel/v2.6/目錄下去下載2.4.23內核。
代碼目錄結構
在閱讀源碼之前,還應知道Linux內核源碼的整體分布情況。現代的操作系統一般由進程管理、內存管理、文件系統、驅動程序和網路等組成。Linux內核源碼的各個目錄大致與此相對應,其組成如下(假設相對於Linux-2.4.23目錄):
1.arch目錄包括了所有和體系結構相關的核心代碼。它下面的每一個子目錄都代表一種Linux支持的體系結構,例如i386就是Intel CPU及與之相兼容體系結構的子目錄。PC機一般都基於此目錄。
2.include目錄包括編譯核心所需要的大部分頭文件,例如與平台無關的頭文件在include/linux子目錄下。
3.init目錄包含核心的初始化代碼(不是系統的引導代碼),有main.c和Version.c兩個文件。這是研究核心如何工作的好起點。
4.mm目錄包含了所有的內存管理代碼。與具體硬體體系結構相關的內存管理代碼位於arch/*/mm目錄下。
5.drivers目錄中是系統中所有的設備驅動程序。它又進一步劃分成幾類設備驅動,每一種有對應的子目錄,如音效卡的驅動對應於drivers/sound。
6.ipc目錄包含了核心進程間的通信代碼。
7.moles目錄存放了已建好的、可動態載入的模塊。
8.fs目錄存放Linux支持的文件系統代碼。不同的文件系統有不同的子目錄對應,如ext3文件系統對應的就是ext3子目錄。
Kernel內核管理的核心代碼放在這里。同時與處理器結構相關代碼都放在arch/*/kernel目錄下。
9.net目錄里是核心的網路部分代碼,其每個子目錄對應於網路的一個方面。
10.lib目錄包含了核心的庫代碼,不過與處理器結構相關的庫代碼被放在arch/*/lib/目錄下。
11.scripts目錄包含用於配置核心的腳本文件。
12.documentation目錄下是一些文檔,是對每個目錄作用的具體說明。
一般在每個目錄下都有一個.depend文件和一個Makefile文件。這兩個文件都是編譯時使用的輔助文件。仔細閱讀這兩個文件對弄清各個文件之間的聯系和依託關系很有幫助。另外有的目錄下還有Readme文件,它是對該目錄下文件的一些說明,同樣有利於對內核源碼的理解。
在閱讀方法或順序上,有縱向與橫向之分。所謂縱向就是順著程序的執行順序逐步進行;所謂橫向,就是按模塊進行。它們經常結合在一起進行。對於Linux啟動的代碼可順著Linux的啟動順序一步步來閱讀;對於像內存管理部分,可以單獨拿出來進行閱讀分析。實際上這是一個反復的過程,不可能讀一遍就理解。
4. linux驅動中內核源碼樹和載入模塊的問題
恩? 理解不全,要重新編譯一個內核來使用,要注意兩點:
a,安裝 LKMs 到 /lib/moles 的子目錄下;
b,將新的內核映像拷貝到 /boot 分區中,並配置 grub/lilo;
centos/redhat 發行版中的內核版本有自己很多的補丁修改,不同於主流的
從 kernel.org 中下載下來的內核。你只是簡單的修改版本名稱那是不行的,
mod version 內部還是會被認為是兩個不同的內核(實際上也是不同的)。
在 centos/redhat 上為本機運行著的內核開發驅動,實際上可以不用去下載
任何版本的內核代碼樹,你只需要安裝 相對應的 kernel-devel rpm 包即可。
當然,為了更方便,你可以直接從 centos 官網下載合適的源碼包來重新編
譯後再開發你的驅動程序。
更多問題,可以上巨立安郵件列表中進行交流。
巨立安技術是你在arm/x86下學習 linux 開發的上佳指導。
5. 請問各位大神,做linux驅動移植時,內核沒有的一些外設的驅動源碼一般是去哪裡找呢
linux內核源碼,能支持的驅動已經是很豐富的了。
如果沒有具體型號的外設驅動,那麼,就參考linux內核已有的驅動源碼,來修改。
比如:網卡驅動
A板卡,網路晶元是DM9000,在linux-x.x.x/drivers/net目錄下有dm9000.c驅動;
現在要做網路晶元是CS8900,沒有驅動,就可以參考DM9000驅動,
對照晶元手冊,修改驅動,編譯通過,調試等。
6. linux-3.18.1內核源代碼的block設備驅動源碼的這些.c文件分別負責什麼功能,來個大神給我解釋一下。
每個c文件頭里拍世有說明啊弊謹
比如cciss是 HP Smart Array block driver,是一個比較舊襲卜肢的HP RAID控制器塊驅動
7. AT070TN92 linux驅動源碼
是去外設晶元的官網還是一些著名論壇?還是要自己寫。。。。求解答。。
8. linux 2.6.32提供驅動程序源碼嗎為什麼都是 .ko文件這些文件能打開嗎
.ko文件是編譯好的驅動模塊,打不開,但可以動態加棗拆載進內核使用。linux系統是開源的,內核源帶隱代碼蠢岩廳都是可以下載到的
9. 驅動開發必須使用開發板廠家提供的Linux源碼嗎
驅動開發環境
要進行linux驅動開發我們首先要有linux內核的源碼樹,並且這個linux內核的源碼樹要和開發板中的內核源碼樹要一直;
比如說我們開發板中用的是linux kernel內核版本為2.6.35.7,在我們ubuntu虛擬機上必須要有同樣版本的源碼樹,
我們再編譯好驅動的的時候,使用modinfo XXX命令會列印出一個版本號,這個版本號是與使用的源碼樹版本有關,如果開發板中源碼樹中版本與
modinfo的版本信息不一致使無法安裝驅動的;
我們開發板必須設置好nfs掛載;這些在根文件系統一章有詳細的介紹;
10. linux的U盤驅動源碼
根據你的描述,估計FUSE不適合你。
參考以下幾個文件吧(我的系統是DEBIAN LENNY)
localhost:/home/aaa/program# apt-cache search libusb
libusb-0.1-4 - userspace USB programming library
libusb-1.0-0 - userspace USB programming library
libusb-1.0-0-dev - userspace USB programming library development files
libusb-dev - userspace USB programming library development files
用apt-get source libusb下載過來的內如如下所示:
total 1688
-rw-r--r-- 1 500 500 2661 2005-02-14 acinclude.m4
-rw-r--r-- 1 500 500 244051 2006-03-04 aclocal.m4
drwxrwxrwx 2 500 500 4096 2006-03-04 apidocs
-rw-r--r-- 1 500 500 130 2004-04-22 AUTHORS
-rw-r--r-- 1 500 500 16833 2006-03-04 bsd.c
-rw-r--r-- 1 500 500 189 2004-01-28 ChangeLog
-rwxr-xr-x 1 500 500 3642 2004-03-12 compile
-rwxr-xr-x 1 500 500 42037 2004-04-12 config.guess
-rw-r--r-- 1 500 500 2467 2006-03-04 config.h.in
-rwxr-xr-x 1 500 500 30221 2004-04-12 config.sub
-rwxr-xr-x 1 500 500 746195 2006-03-04 configure
-rw-r--r-- 1 500 500 6777 2006-03-04 configure.in
-rw-r--r-- 1 500 500 26428 2004-01-28 COPYING
-rw-r--r-- 1 500 500 35685 2006-03-04 darwin.c
drwxr-xr-x 3 root root 4096 02-19 18:51 debian
-rwxr-xr-x 1 500 500 14841 2004-03-12 depcomp
-rw-r--r-- 1 500 500 15021 2006-03-04 descriptors.c
drwxrwxrwx 2 500 500 4096 02-19 18:49 doc
-rw-r--r-- 1 500 500 44099 2006-03-04 Doxyfile
-rw-r--r-- 1 500 500 44154 2006-02-07 Doxyfile.in
-rw-r--r-- 1 500 500 759 2004-01-28 error.c
-rw-r--r-- 1 500 500 716 2004-01-28 error.h
-rw-r--r-- 1 500 500 2043 2006-03-04 INSTALL.libusb
-rw-r--r-- 1 500 500 2063 2004-01-28 INSTALL.libusb.in
-rwxr-xr-x 1 500 500 9208 2004-03-12 install-sh
-rw-r--r-- 1 500 500 1267 2004-01-28 libusb-config.in
-rw-r--r-- 1 500 500 196 2005-02-15 libusb.pc.in
-rw-r--r-- 1 500 500 1290 2006-03-04 libusb.spec
-rw-r--r-- 1 500 500 1293 2006-03-04 libusb.spec.in
-rw-r--r-- 1 500 500 2248 2004-01-28 LICENSE
-rw-r--r-- 1 500 500 19148 2006-03-04 linux.c
-rw-r--r-- 1 500 500 3146 2005-02-03 linux.h
-rw-r--r-- 1 500 500 183730 2004-04-12 ltmain.sh
-rw-r--r-- 1 500 500 2220 2006-03-04 Makefile.am
-rw-r--r-- 1 500 500 34139 2006-03-04 Makefile.in
-rwxr-xr-x 1 500 500 10678 2004-03-12 missing
-rw-r--r-- 1 500 500 8 2004-01-28 NEWS
-rw-r--r-- 1 500 500 2546 2006-03-04 README
-rw-r--r-- 1 500 500 2556 2006-03-04 README.in
drwxrwxrwx 2 500 500 4096 2006-03-04 tests
-rw-r--r-- 1 500 500 6443 2006-03-04 usb.c
-rw-r--r-- 1 500 500 8367 2006-03-04 usb.h.in
-rw-r--r-- 1 500 500 1771 2006-03-04 usbi.h
-rw-r--r-- 1 500 500 13511 2006-03-04 usbpp.cpp
-rw-r--r-- 1 500 500 24428 2005-02-10 usbpp.h
可以從usb.c, usb.h.in入手來研究。