第一个驱动程序

本文就来写一个最简单的驱动,以便对驱动有个直观的认识。

1.不带参数的hello world驱动

[weishusheng@localhost holle-world]$ vim hello.c

#include<linux/init.h>
#include<linux/module.h>
MODULE_LICENSE("GPL");

static int hello_init(void)

 {
 printk(KERN_ALERT "hello %s! ",whom);
 return 0;
 }
 
 static void hello_exit(void)
 {
 printk(KERN_ALERT "Goodbye, world! ");

 }
module_init(hello_init);
module_exit(hello_exit);

 linux/init.h  与linux/module.h是模块必须包含的,MODULE_LICENSE("GPL")告诉内核该模块使用自由许可证,没有这句,内核在装载模块时会抱怨。

module_init()与module_exit()是两个宏,不是函数,通过这两个宏,内核分别在内核装载模块和卸载模块时调用。

接下来编写Makefile

[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! ",whom);
27 return 0;
28 }
29
30 static void hello_exit(void)
31 {
32 printk(KERN_ALERT "Goodbye, world! ");
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查看了,步骤和上面一样。

原文地址:https://www.cnblogs.com/thinkinglife/p/3968597.html