#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <asm/uaccess.h>
// 定义设备文件名
#define DEVICE_NAME "wordcount"
// 描述与设备文件触发的事件对应的回调函数指针
static struct file_operations dev_fops =
{ .owner = THIS_MODULE };
// 描述设备文件的信息
static struct miscdevice misc =
{ .minor = MISC_DYNAMIC_MINOR, .name = DEVICE_NAME, .fops = &dev_fops };
// 初始化Linux驱动
static int __init word_count_init(void)
{
int ret;
// 建立设备文件
ret = misc_register(&misc);
// 输出日志信息
printk("word_count_init_success,ret=%d\n",ret);
return ret;
}
// 卸载Linux驱动
static void __exit word_count_exit(void)
{
// 删除设备文件
misc_deregister(&misc);
// 输出日志信息
printk("word_count_init_exit_success\n");
}
// 注册初始化Linux驱动的函数
module_init( word_count_init);
// 注册卸载Linux驱动的函数
module_exit( word_count_exit);
MODULE_AUTHOR("lining");
MODULE_DESCRIPTION("statistics of word count.");
MODULE_ALIAS("word count module.");
MODULE_LICENSE("GPL");
[root@localhost word_count]# rmmod word_count
ERROR: Module word_count does not exist in /proc/modules
[root@localhost word_count]# insmod word_count.ko
[root@localhost word_count]# dmesg | grep word_count
word_count: no version for "struct_module" found: kernel tainted.
word_count_init_success
word_count_init_exit_success
word_count_init_success
word_count_init_exit_success
word_count_init_success
word_count_init_exit_success
word_count_init_success
word_count_init_exit_success
word_count_init_success,ret=0
[root@localhost word_count]# modinfo word_count.ko
filename: word_count.ko
license: GPL
alias: word count module.
description: statistics of word count.
author: lining
srcversion: B604BC7626027EB7564FDA5
depends:
vermagic: 2.6.18-53.el5xen SMP mod_unload 686 REGPARM 4KSTACKS gcc-4.1
[root@localhost word_count]#