14。字符设备框架介绍与实现openwrt入门经典教程
14.字符设备框架介绍与实现
实验目的
掌握编写一个字符设备驱动程序的框架、必须的步骤
1 字符设备驱动程序框架简介
我们在学习 C 语言的时候, 知道每个应用程序的入口函数, 即第一个被执行的函数 是 main 函数,那么,我们自己的驱动程序,哪个函数是入口函数呢?
在写驱动程序时,驱动函数的名字可以任意取,常常为 xxxx_init() ,当实现好这个 xxxx_init()函数以后,内核其实并不知道这个就是我们驱动的入口函数,因此我们要想办法 告诉内核,我们的入口函数是哪个?我们通过 module_init()函数来告诉内核,具体如下。
module_init(drv_init);
通过上面的修饰以后, drv_init()这个函数就变成了我们的驱动程序的入口函数了。当 然,有入口函数,自然还需要一个出口函数,我们通过 module_exit()函数来告诉内核,具 体如下。
module_exit(drv_exit);
从上一章中,我们知道,应用程序是通过 open、 read、 write ...函数来和我们的驱动 程序进行交互的,那么我们的驱动程序是怎么给应用程序提供这些接口的呢?我们在写驱动 程序的时候,我们首先需要定义出一个 file_operations 结构体,该结构体是驱动和应用程 序交互的接口。具体定义如下。
struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int); ssize_t (*read) (struct file *, char __user *, size_t, loff_t *); ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *); ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long,loff_t);
ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long,loff_t); int (*readdir) (struct file *, void *, filldir_t); unsigned int (*poll) (struct file *, struct poll_table_struct *);
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id); int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, loff_t, loff_t, int datasync); int (*aio_fsync) (struct kiocb *, int datasync);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *); ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int); unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long,
unsigned long, unsigned long);
int (*check_flags)(int);
int (*flock) (struct file *, int, struct file_lock *);
ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *,
size_t, unsigned int);
ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t,unsigned
int);
int (*setlease)(struct file *, long, struct file_lock **);
long (*fallocate)(struct file *file, int mode, loff_t offset,loff_t len);
};
我们的驱动程序要给应用程序提供哪些接口,就只需要填充相应的成员即可。比如我们 想提供 open、 close、 read、 write、 ioctl 这三个接口,就应该如下定义。
当 file_operations 结构体定义、设置好以后,我们只需要通过 register_chrdev()函 数将该结构体注册进内核即可。
2 字符设备驱动程序框架实现
经过前面部分的讲解,相信大家一定对如何写一个自己的驱动程序,有所感悟了。接下 来,给大家一个简单的驱动程序的例子,可以用于作为框架模板,以后的驱动都可以基于该 驱动进行修改。
在本实验的 "附件: 字符设备框架程序模板" 文件夹中就有我们提供的模板,大家可 以打开进行阅读,这里也把源码复制出来:
随着后面驱动程序的编写,大家就会熟悉这个模板了,同时熟悉字符设备驱动框架。
/*****************************
*
* 驱动程序模板
* 版本: V1
* 使用方法(末行模式下) :
* :%s/xxx/"你的驱动名称"/g
*
*******************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include #include #include
#include #include
#include
#include
#include #include
#include #include #include #include #include #include #include #include
/**************** 基本定义 **********************/
//内核空间缓冲区定义
#if 0
#define KB_MAX_SIZE 20 #define kbuf[KB_MAX_SIZE];
#endif
//加密函数参数内容: _IOW(IOW_CHAR , IOW_NUMn , IOW_TYPE)
//加密函数用于 xxx_ioctl 函数中
//使用举例: ioctl(fd , _IOW("L",0x80,long) , 0x1);
//#define NUMn xxx , if you need!
#define IOW_CHAR "L"
#define IOW_TYPE long
#define IOW_NUM1 0x80
//初始化函数必要资源定义
//用于初始化函数当中
//device number;
dev_t dev_num;
//struct dev
struct cdev xxx_cdev;
//auto "mknode /dev/xxx c dev_num minor_num"
struct class *xxx_class = NULL;
struct device *xxx_device = NULL;
/**************** 结构体 file_operations 成员函数 *****************/
//open
static int xxx_open(struct inode *inode, struct file *file)
{
printk("xxx drive open... ");
return 0;
}
//close
static int xxx_close(struct inode *inode , struct file *file)
{
printk("xxx drive close... ");
return 0;
}
//read
static ssize_t xxx_read(struct file *file, char __user *buffer,
size_t len, loff_t *pos)
{
int ret_v = 0;
printk("xxx drive read... ");
return ret_v;
}
//write
static ssize_t xxx_write( struct file *file , const char __user *buffer,
size_t len , loff_t *offset )
{
int ret_v = 0;
printk("xxx drive write... ");
return ret_v;
}
//unlocked_ioctl
static int xxx_ioctl (struct file *filp , unsigned int cmd , unsigned long arg)
{
int ret_v = 0;
printk("xxx drive ioctl... ");
switch(cmd)
{
//常规:
//cmd 值自行进行修改
case 0x1:
{
if(arg == 0x1) //第二条件;
{
}
}
break;
//带密码保护:
//请在"基本定义"进行必要的定义
case _IOW(IOW_CHAR,IOW_NUM1,IOW_TYPE):
{
if(arg == 0x1) //第二条件
{
}
}
break;
default: break;
}
return ret_v;
}
/***************** 结构体: file_operations ************************/
//struct
static const struct file_operations xxx_fops = {
.owner = THIS_MODULE,
.open = xxx_open,
.release = xxx_close,
.read = xxx_read,
.write = xxx_write, .unlocked_ioctl = xxx_ioctl,
};
/************* functions: init , exit*******************/
//条件值变量,用于指示资源是否正常使用
unsigned char init_flag = 0;
unsigned char add_code_flag = 0;
//init
static __init int xxx_init(void)
{
int ret_v = 0;
printk("xxx drive init... ");
//函数 alloc_chrdev_region 主要参数说明:
//参数 2 : 次设备号
//参数 3 : 创建多少个设备
if( ( ret_v = alloc_chrdev_region(&dev_num,0,1,"xxx") ) < 0 )
{
goto dev_reg_error;
}
init_flag = 1; //标示设备创建成功;
printk("The drive info of xxx: major: %d minor: %d ", MAJOR(dev_num),MINOR(dev_num));
cdev_init(&xxx_cdev,&xxx_fops);
if( (ret_v = cdev_add(&xxx_cdev,dev_num,1)) != 0 )
{
goto cdev_add_error;
}
xxx_class = class_create(THIS_MODULE,"xxx");
if( IS_ERR(xxx_class) )
{
goto class_c_error;
}
xxx_device = device_create(xxx_class,NULL,dev_num,NULL,"xxx");
if( IS_ERR(xxx_device) )
{
goto device_c_error;
}
printk("auto mknod success! ");
//------------ 请在此添加您的初始化程序 --------------//
//如果需要做错误处理,请: goto xxx_error;
add_code_flag = 1;
//---------------------- END ---------------------------//
goto init_success;
dev_reg_error:
printk("alloc_chrdev_region failed ");
return ret_v;
cdev_add_error:
printk("cdev_add failed ");
unregister_chrdev_region(dev_num, 1);
init_flag = 0; return ret_v;
class_c_error:
printk("class_create failed ");
cdev_del(&xxx_cdev);
unregister_chrdev_region(dev_num, 1);
init_flag = 0;
return PTR_ERR(xxx_class);
device_c_error:
printk("device_create failed ");
cdev_del(&xxx_cdev);
unregister_chrdev_region(dev_num, 1);
class_destroy(xxx_class);
init_flag = 0;
return PTR_ERR(xxx_device);
//------------------ 请在此添加您的错误处理内容 ----------------//
xxx_error:
add_code_flag = 0;
return -1;
//-------------------- END -------------------//
init_success:
printk("xxx init success! ");
return 0;
}
//exit
static __exit void xxx_exit(void)
{
printk("xxx drive exit... ");
if(add_code_flag == 1)
{
//---------- 请在这里释放您的程序占有的资源 ---------//
printk("free your resources... ");
printk("free finish ");
//---------------------- END -------------------//
}
if(init_flag == 1)
{
//释放初始化使用到的资源;
cdev_del(&xxx_cdev);
unregister_chrdev_region(dev_num, 1);
device_unregister(xxx_device);
class_destroy(xxx_class);
}
}
/**************** module operations**********************/
//module loading module_init(xxx_init); module_exit(xxx_exit);
//some infomation
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("from Jafy"); MODULE_DESCRIPTION("xxx drive");
/********************* The End ***************************/