试试第一个hello world模块添加:
1,在添加drivers/char/hello.c
/*********************************************************************************
* Copyright: (C) 2014 zhouguangfeng<zhouguangfeng91@gmail.com>
* All rights reserved.
*
* Filename: hello.c
* Description: This file
*
* Version: 1.0.0(08/09/2014)
* Author: zhouguangfeng <zhouguangfeng91@gmail.com>
* ChangeLog: 1, Release initial version on "08/09/2014 06:23:18 PM"
*
********************************************************************************/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
MODULE_LICENSE ("Dual BSD/GPL");
static int __init hello_init (void)
{
printk (KERN_INFO "Hello world
");
return 0;
}
static void __exit hello_exit (void)
{
printk (KERN_INFO "Goodbye world
");
return;
}
module_init (hello_init);
module_exit (hello_exit);
其中 printk是内核特有的打印函数,类似与printf,只是它不依赖库文件,KERN_INFO为
打印优先级
2,修改drivers/char/Kconfig文件,添加在make menuconfig的选项
#
# Character device configuration
#
menu "Character devices"
source "drivers/tty/Kconfig"
# add by zhouguangfeng 2014.8.9 for "Hello world"
config FL_HELLO
tristate "CCTE2440 Hello Driver"
depends on ARCH_S3C2440
help
FL2440 Hello Module.
3,修改drivers/char/Makefile,编译添加的hello.c
obj-$(CONFIG_FL_HELLO) += hello.o
4,make menuconfig选项
[*] Enable loadable module support --->
[*] Module unloading //需要支持,否则已经加载的模块无法卸载载
Device Drivers --->
Character devices --->
<M> FL2440 Hello Driver //刚才添加的Kconfig选项,这里编译为模块
5,make之后,下载内核与文件系统,挂在hello.ko模块
Copyright (C) 2014 zhouguangfeng<zhouguangfeng@gmail.com>
dm9000 dm9000 eth0: link up, 100Mbps, full-duplex, lpa 0xCDE1
zhouxiaoxing login: root
>: ls
apps data etc info lib mnt root sys usr
bin dev hello init linuxrc proc sbin tmp var
>: ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1): 56 data bytes
64 bytes from 192.168.1.1: seq=0 ttl=64 time=2.592 ms
64 bytes from 192.168.1.1: seq=1 ttl=64 time=0.685 ms
64 bytes from 192.168.1.1: seq=2 ttl=64 time=0.658 ms
--- 192.168.1.1 ping statistics ---
9 packets transmitted, 9 packets received, 0% packet loss
round-trip min/avg/max = 0.658/0.896/2.592 ms
>: tftp -gr hello.ko 192.168.1.3
hello.ko 100% |*******************************| 2365 0:00:00 ETA
>: ls
apps dev hello.ko lib proc sys var
bin etc info linuxrc root tmp
data hello init mnt sbin usr
>: insmod hello.ko
Hello world
>: rmmod hello
Goodbye world