當前位置:才華齋>IT認證>Linux認證>

最簡單的Linux驅動程式

Linux認證 閱讀(1.39W)

你正在學習linux嗎?你知道最簡單的Linux驅動程式是怎麼樣的嗎?下面yjbys小編為大家提供最簡單的'Linux驅動程式,希望對大家有所幫助!

最簡單的Linux驅動程式

#include

#include //兩個必須標頭檔案

MODULE_LICENSE("Dual BSD/GPL");//GPL協議

static char *whom = "world";//insmod傳入引數是用到

static int howmany = 1;//insmod傳入引數是用到

module_param(howmany,int,S_IRUGO);//傳入引數要用module_param巨集來宣告。這樣我們就可以使用insmod命令

//時傳入引數,其實是模組讓這些引數對insmod命令可見

module_param(whom,charp,S_IRUGO);

unsigned int i;

static int hello_init(void)

{

printk(KERN_ALERT "Hello,worldn");

for(i=0;i

printk(KERN_ALERT "hello %sn",whom);

return 0;

}

static void hello_exit(void)

{

printk(KERN_ALERT "Goodbye, cruel worldn");

}

module_init(hello_init);

module_exit(hello_exit);

  下面是Makefile檔案

obj-m += hello.o//obj-m編譯成模組

KERDIR = /work//核心所在目錄

modules:

make -C $(KERDIR) M=`pwd` modules//-C表示進入到後面目錄裡編譯,M=` `指定編譯好的檔案所在目錄

clean:

rm -rf *.o *~core nd * * *.c *_versions

  下面是載入模組時情況

[root@cgyl2010 ~]#insmod howmany=10 whom="Mom"

Hello,world

hello Mom

hello Mom

hello Mom

hello Mom

hello Mom

hello Mom

hello Mom

hello Mom

hello Mom

hello Mom

這樣就可以通過insmoa命令傳進來的引數來控制我們要實現的現象(這裡是列印情況),有時候會比較方便。