本文就來寫一個最簡單的驅動,以便對驅動有個直觀的認識。linux
1.不帶參數的hello world驅動shell
[weishusheng@localhost holle-world]$ vim hello.cvim
#include<linux/init.h>
#include<linux/module.h>
MODULE_LICENSE("GPL");函數
static int hello_init(void)ui
{
printk(KERN_ALERT "hello %s!\n",whom);
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, world!\n");
}
module_init(hello_init);
module_exit(hello_exit);spa
linux/init.h 與linux/module.h是模塊必須包含的,MODULE_LICENSE("GPL")告訴內核該模塊使用自由許可證,沒有這句,內核在裝載模塊時會抱怨。ip
module_init()與module_exit()是兩個宏,不是函數,經過這兩個宏,內核分別在內核裝載模塊和卸載模塊時調用。開發
接下來編寫Makefilecmd
[weishusheng@localhost holle-world]$ vim Makefile 原型
1 #ARCH=x86
2 ARCH=arm920t
3 PROJ=fl2440
4 PWD=$(shell pwd)
5
6 ifneq ("${ARCH}", "x86")
7 CROSS_COMPILE ?= /opt/buildroot-2011.11/${ARCH}/usr/bin/arm-linux-
8 KERNEL_DIR = ${PWD}/../../kernel/linux-3.0-Dm9000
9 else
10 KERNEL_DIR = /lib/modules/$(shell uname -r)/build
11 endif
12
13
14 obj-m += hello.o
15 #obj-m += workqueue.o
16
17 all:
18 make -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules
19 @make clear
20
21 clear:
22 @rm -f *.o *.cmd *.mod.c
23 @rm -rf *~ core .depend .tmp_versions Module.symvers modules.order -f
24 @rm -f .*ko.* *ko.* .*.o.cmd
25
26 clean:
27 rm -f *.ko
接下來編譯
[weishusheng@localhost holle-world]$ ls
hello.c Makefile
[weishusheng@localhost holle-world]$ make
make -C /home/weishusheng/driver/holle-world/../..//kernel/linux-3.0-Dm9000 SUBDIRS=/home/weishusheng/driver/holle-world modules
make[1]: Entering directory `/home/weishusheng/kernel/linux-3.0-Dm9000'
CC [M] /home/weishusheng/driver/holle-world/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/weishusheng/driver/holle-world/hello.mod.o
LD [M] /home/weishusheng/driver/holle-world/hello.ko
make[1]: Leaving directory `/home/weishusheng/kernel/linux-3.0-Dm9000'
make[1]: Entering directory `/home/weishusheng/driver/holle-world'
make[1]: Leaving directory `/home/weishusheng/driver/holle-world'
[weishusheng@localhost holle-world]$ ls
hello.c hello.ko Makefile
此時已經生成hello.ko,把hello.ko經過tftp下到開發板,insmod,而後用dmesg可查看到
>: insmod hello.ko
>: dmesg
...
...
...
hello world!
>:
2.帶參數的hello world程序
[weishusheng@localhost holle-world]$ vim hello.c
1 /*********************************************************************************
2 * Copyright: (C) 2014 weishusheng
3 * All rights reserved.
4 *
5 * Filename: hello.c
6 * Description: This file use fot build my first module
7 *
8 * Version: 1.0.0(09/02/2014)
9 * Author: weishusheng <642613208@qq.com>
10 * ChangeLog: 1, Release initial version on "09/02/2014 01:18:16 PM"
11 *
12 ********************************************************************************/
13 #include<linux/init.h>
14 #include<linux/module.h>
15 #include<linux/moduleparam.h>
16 MODULE_LICENSE("GPL");
17 static char *whom = "world";
18 static int howmany = 1;
19 module_param(howmany, int, S_IRUGO);
20 module_param(whom, charp, S_IRUGO);
21
22 static int hello_init(void)
23 {
24 int i;
25 for(i=0;i<howmany;i++)
26 printk(KERN_ALERT "hello %s!\n",whom);
27 return 0;
28 }
29
30 static void hello_exit(void)
31 {
32 printk(KERN_ALERT "Goodbye, world!\n");
33
34 }
35
36
37 module_init(hello_init);
38 module_exit(hello_exit);
module_param用於聲明參數,須要包含 #include<linux/moduleparam.h>頭文件,module_param原型爲module_param(name,type,perm),name是變量名字,type是變量類型,perm是訪問許可值,通常設爲S_IRUGO。Makefile可用上面那個,同樣可用編譯。
後面就是make,insmod,dmesg查看了,步驟和上面同樣。