Linux 最簡單的驅動程式hello world

2021-09-30 15:42:13 字數 1728 閱讀 1831

最近開始學linux驅動,就從最簡單的開始練起,就從ldd-3裡的第乙個驅動程式hello world練起。

the following code is a complete "hello world" module:

#include

#include

module_license("dual bsd/gpl");

static int hello_init(void)

static void hello_exit(void)

module_init(hello_init);

module_exit(hello_exit);

儲存為 hello.c

再寫乙個makefile,**如下:

obj-m := hello.o

kerneldir := /usr/src/kernels/2.6.9-42.el-smp-i686

pwd := $(shell pwd)

all:

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

.phony: clean

clean:

rm -rf *.o *.ko

載入驅動:

螢幕沒有顯示。

檢視驅動,lsmodhello已在,說明已經載入進去了。

網上檢視,尋求解決之法,

說可以在/var/log/message中顯示hello world!,但還是沒有、

最後使用dmesg命令,看到了顯示。

解除安裝驅動:

rmmod hello

途中問題:

載入驅動時遇到 insmod: error inserting 'hello.ko': -1 invalid module format 錯誤

原因:使用的核心和編譯的核心版本不一樣。

我使用的pc核心版本如下:

在/usr/src/kernels/下有三個核心,選擇和上面系統一樣的核心。

修改 makefile中的 kerneldir  為

kerneldir := /usr/src/kernels/2.6.9-42.el-smp-i686

或者為 kerneldir :=

/lib/modules/$(shell uname -r)/build

cd /lib/modules/$(uname -r)/build/

這個目錄實際上指向了:/usr/src/kernels/2.6.9-42.el-smp-i686

最簡單的字元裝置驅動程式

首先,先理清一下簡單字元裝置驅動程式的思路 1 申請裝置號 動態申請 int alloc chrdev region dev t dev,unsignedbaseminor,unsignedcount,const char name 靜態申請 int register chrdev region d...

linux 驅動程式 高階字元驅動程式

ioctl方法 驅動程式的原型實現 int ioctl struct inode inode,struct file filp,unsigned int cmd,unsigned long arg ioctl 命令選擇 位段結構 number direction ioc read ioc write...

簡單的驅動程式分析

一 底層led驅動程式 最簡單的驅動框架 1.首先先要實現led open led write led read 這幾個函式,上層應用程式呼叫時進行哪些對應的操作。2.上面幾個函式如何告訴給核心,之前說過驅動屬於核心的一部分,需一起使用。核心系統呼叫時最後會呼叫相應的驅動程式,所以實現這幾個函式後,...