Linux裝置驅動程式(0)

2021-07-22 11:48:02 字數 1537 閱讀 7487

環境:

(沒有使用網上教程編譯2.6.xx的標準核心來建立核心目錄樹,而是直接在自己的虛擬機器上面進行測試,其實3.19.0-25-generic 應該屬於核心目錄樹)

編寫書上的測試驅動程式:

#include 

2#include

34 module_license("dual bsd/gpl");

56static

int hello_init(void)

7 1112

static

void hello_exit(void)

13 1617 module_init(hello_init);

18 module_exit(hello_exit);

makefile編寫

obj-m := hello.o  //從目標檔案hello.o中構造出乙個模組名為hello.ko的模組

2 kerneldir := /lib/modules/3.19

.0-25-generic/build

3 pwd := $(shell pwd)

4 modules:

5 $(make) -c $(kerneldir) m=$(pwd) modules

kerneldir:= /lib/modules/3.19.0-25-generic/build 其中,/lib/modules/3.19.0-25-generic/目錄下,存在已經安裝的模組目錄,且在這個目錄下有個符號鏈結build,它指向核心的源**目錄:

lrwxrwxrwx 1 root root 40 nov 25 2015 /lib/modules/3.19.0-25-generic/build -> /usr/src/linux-headers-3.19.0-25-generic

pwd:= $(shell pwd) //當前目錄(一般是hello.c所在的目錄)

4-5line :命令首先改變目錄到-c選項指定的位置(即核心源**目錄),其中儲存有核心的頂層makefile檔案,m=選項是讓該makefile在構造modules目標之前返回到模組源**目錄,然後,modules目標指向obj-m變數中設定的模組,從而核心的makefile負責真正構造模組

測試:

make後生成的檔案如上圖,其中生成了hello.ko模組,接著裝載模組和解除安裝模組

但是沒有向終端輸出,而是輸出到/var/log/kernel.log日誌檔案中:如下圖

使用命令:cat /var/log/kern.log | tail -n 10 輸出最後10行

linux裝置驅動程式 字元裝置驅動程式

先留個 有一起學習驅動程式的加qq295699450 字元裝置驅動 這篇比較惱火。載入成功,但是讀不出來資料,有知道怎麼回事的,留個言,一起討論下 資料結構 struct scull mem struct scull dev dev 整個驅動程式 如下 include include include...

Linux裝置驅動程式 字元裝置驅動程式

1.檢視主裝置號,次裝置號 進入 dev目錄執行ls l,第四,五列分別為主次裝置號,10,180,1,5,這些是主裝置號,而60,63這些就是次裝置號 130 shell android dev ls l crw rw r system radio 10,60 1969 12 31 21 00 a...

Linux裝置驅動程式

linux系統中的裝置分為字元裝置 char device 塊裝置 block 和網路裝置 net device 三種,字元裝置是指在訪問時沒有快取,能夠像檔案一樣被訪問的裝置,字元裝置驅動程式至少要實現open close read和write系統呼叫。多數的linux裝置驅動程式可以在核心模組發...