1.1 相关函数
printk函数
例:printk(KERN_EMERG “HELLO WORLD enter!\n”);
只有当日志级别小于控制台级别时,才会在控制台打印出来
日志级别有8种:
#define KERN_EMERG “<0>” /* system is unusable /
#define KERN_ALERT “<1>” / action must be taken immediately*/
#define KERN_CRIT “<2>” /* critical conditions /
#define KERN_ERR “<3>” / error conditions /
#define KERN_WARNING “<4>” / warning conditions /
#define KERN_NOTICE “<5>” / normal but significant condition*/
#define KERN_INFO “<6>” /* informational /
#define KERN_DEBUG “<7>” / debug-level messages */
1.2 编译
makefile里面的clean,all后面必须有Tab键。生成的KO文件就是驱动文件。
1.3 加载、卸载、查看模块
– insmod加载模块命令
– lsmod查看模块命令
– rmmod卸载模块命令
#include <linux/init.h> /*hello_init()、hello_init()在此头文件中*/
#include <linux/module.h> /*MODULE_LICENSE()在此头文件中*/
MODULE_LICENSE("Dual BSD/GPL"); /*没有版本限制,遵循GPL协议。必须有*/
MODULE_AUTHOR("TOPEET");
static int hello_init(void)
{
printk(KERN_EMERG "HELLO WORLD enter!\n"); /*printk():内核的打印
KERN_EMERG:紧急权限*/
return 0;
}
static void hello_exit(void)
{
printk(KERN_EMERG "HELLO WORLD exit!\n");
}
module_init(hello_init); /*入口函数,输入是函数名*/
module_exit(hello_exit); /*出口函数*/