Linux 驅動開發核心模組的新增

2021-07-12 04:15:02 字數 3667 閱讀 4940

寫這篇部落格的目的是做一下筆記,便於自己查閱。

首先給出核心模組源**,當然是最最簡單的helloworld。

#include 

#include 

module_license(

"gpl"

);  //gpl 開源協議

static

inthello_init(void)     

static

voidhello_exit(void)     

module_init(hello_init);  //必須使用的,模組載入函式。向核心註冊

module_exit(hello_exit);  //必須使用的,模組解除安裝函式註冊

需要的makefile

ifneq ($(kernelrelease),)    //判斷kernelrelease不能與null

obj-m:=hello.o   //目標模組的名稱

else

#generate the path

current_path:=$(shell pwd)  //獲得當前目錄的名稱並賦值給current_path變數

#the absolute path  // 是用絕對路徑

linux_kernel_path:=/lib/modules/$(shell uname -r)/build      //核心模組所依賴的核心編譯路徑,shell uname -r獲得當前核心版本號

#complie object

default:

make -c $(linux_kernel_path) m=$(current_path) modules //make -c 表示到$(linux_kernel_path)使用它的makefile編譯。

//m=$(current_path)表示你的核心源**的位置

// modules :編譯成為模組

clean:

make -c $(linux_kernel_path) m=$(current_path) clean

endif

編譯一次核心模組需要兩次執行上述makefile,第一次執行的時候kernelrelease是沒有值得,第二次執行時kernelrelease是有值得。需要

注意此處

如果多個c檔案進行的編譯乙個核心模組。makefile則需要寫為如下格式

ifneq ($(kernelrelease),)    //判斷kernelrelease不能與null

obj-m:=hello.o   //目標模組的名稱

hello.o-objs :=file1.o file2.o file3.o  ...  //(可以最近多個)       ------>如果乙個原始檔可以用這個方式生產與檔名不同的核心模

else

#generate the path

current_path:=$(shell pwd)  //獲得當前目錄的名稱並賦值給current_path變數

#the absolute path  // 是用絕對路徑

linux_kernel_path:=/lib/modules/$(shell uname -r)/build      //核心模組所依賴的核心編譯路徑,shell uname -r獲得當前核心版本號

#complie object

default:

make -c $(linux_kernel_path) m=$(current_path) modules //make -c 表示到$(linux_kernel_path)使用它的makefile編譯。

//m=$(current_path)表示你的核心源**的位置

// modules :編譯成為模組

clean:

make -c $(linux_kernel_path) m=$(current_path) clean

endif

編譯得到hello.ko,然後insmod hello.ko載入模組,rmmod hello解除安裝模組。

檢視核心模組l

smod

modprobe hello:也是載入乙個模組,但是不同於insmod的是,它會在/lib/modules/$<$version>/modules.dep檢視要載入的模組,看它

是否依賴於其他的模組,如果有modprobe會先找到那些被依賴模組並先安裝它們。

--------------------------------華麗的分割線---------------------------------------

核心引數:

1.定義模組引數的方法:

module_param(name, type, perm);

其中,name:表示引數的名字;

type:表示引數的型別;

perm:表示引數的訪問許可權;

static int num=10;

module_param(num,int,s_irugo);

static int hello_init(void)

static void   hello_exit(void)

module_init(hello_init);

module_exit(hello_exit);

module_license("gpl");

module_description("a ****** module");

module_alias("hello");

shell    $ insmod hello.ko  num=100 即可改變num的值。

-------------------------------------------華麗分割線-----------------------------------

核心模組符號匯出

export_symbol(函式名/變數名

即當乙個核心模組呼叫另外乙個核心模組的函式或者變數時,需要再被呼叫的核心模組中將變數或者函式使用exprot_symbol 將對應函式或者變數匯出,才能

正常使用。關於這部分內容網路上有許多博文,具體使用時請參考其他博文吧。

----------------------------華麗分割線----------------------------------------------

核心列印&級別。

/proc/sys/kernel/printk中定義著其他控制台和其他log檔案的列印級別。

其他的就不再贅述了,網上有很多博文了。

-----------------------------------華麗結束線-----------------------------------

linux的驅動開發 核心模組的編譯

1.編譯核心模組的編譯器 qquad 編譯器 gcc 交叉編譯器 2.編寫編譯核心模組的makefile qquad 內部編譯 核心模組的原始檔放在核心原始碼中進行編譯,需要修改核心中的kconfig,makefile,make menuconfig qquad 靜態編譯 將核心模組編譯進uimag...

Linux 驅動開發 核心模組設計筆記 0

1.模組化程式設計原因 linux 核心龐大,需要大量元件,一起整合會導致映象檔案過大 核心模組操作命令 載入 insmod ko解除安裝 rmmod ko檢視 lsmod grep ko核心模組程式設計標頭檔案必備 include include核心模組函式出入口 module init func...

linux 2 6 35核心移植 USB驅動的新增

實驗目的 在fs2410開發板上進行linux 2.6.22.6核心的移植,這個部分完成usb裝置驅動的新增,完成相應的功能。實驗環境 1 ubuntu 10.10發行版 2 u boot 2010.03 3 fs2410平台 4 交叉編譯器 arm none linux gnueabi gcc 4...