下面是一个最简单的字符驱动程序框架,下面我将一一进行分析里面的内容。
#include <linux/fs.h>
#include <linux/init.h>
/* 定义一个open函数 */
static int first_drv_open(struct inode *inodep, struct file *filep)
{
return 0;
}
/* 定义一个write函数 */
static ssize_t first_drv_write(struct file *filep, const char __user * buf, size_t len, loff_t *ppos)
{
return 0;
}
/* 把自己定义的函数接口集合起来,方便系统使用 */
static const struct file_operations first_drv_file_operation = {
.open = first_drv_open,
.write = first_drv_write,
};
/* 把集合起来的函数接口告诉系统,同时使用111作为该设备的字符设备号 */
static int __init first_drv_init(void)
{
register_chrdev(111,"first_drv",&first_drv_file_operation);
return 0;
}
/* 从系统中卸载掉字符设备号为111的设备 */
static void __exit first_drv_exit(void)
{
unregister_chrdev(111,"first_drv");
}
/* 声明段属性 */
module_init(first_drv_init);
module_exit(first_drv_exit);
MODULE_LICENSE("GPL");
其中最前面的头文