设备驱动之三----几个关键函数的实现
2012-12-13 12:34
225 查看
打开
int open(struct inode *inode,struct file *filp);
模块使用计数加1
识别次设备号
硬件操作
检查设备相关错误(诸如设备未就绪或类似的硬件问题)
如果设备是首次打开,则对其初始化
如果有中断操作,申请中断处理程序
关闭
int release(struct inode *inode,struct file *filp);
模块使用计数减1
释放由open分配的,保存在filp>private_data里的所有内容。
硬件操作:
如果申请了中断,则释放中断中中断处理程序。
在最后一次关闭操作时关闭设备。
read/write
ssize_t read(struct file *filp,char __user *buff,sieze_t count,loff_t *offp);
ssize_t write(struct file *filp,const char __user *buff,size_t count,loff_t *offp);
用户空间和内核空间之间的数据拷贝过程
不能简单的用指针操作或者memcpy来进行数据的拷贝
用户空间的数据是可以被换出的,会产生一个页面失效的异常
用户空间的地址无法在内核空间中使用。
用户空间和内核空间之间进行数据拷贝的函数
unsigned long copy_from_user(void *to,const void __user *from,unsigned long count);
unsigned long copy_to_user(void __user *to,const void *from,unsigned long count);
读设备模板
ssize_t xxx_read(struct *filp,char __user *buf,size_t count,loff_t*f_fpos)
{
copy_to_user(buf,---)
}
写设备模板
ssize_t xxx_write(struct file *filp,const char __user *buf,size_t count,loff_t *f_ops)
{
copy_from_user(--,buf,--);
}
ioctl函数
为设备驱动程序执行命令提供了一个特有的入口点
用来设置或者读取设备的属性信息
int ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg);
cmd参数的定义
不推荐使用0x1,0x2之类的值
linux对ioctl()的cmd参数有特殊的定义
设别号(type) 序列号(number) 方向(direction) 数据尺寸(size)
8bit 8bit 2bit 13/14bit
构造命令编号的宏:
_IO(type,nr)用于构造物参数的命令编号;
_IOR(type,nr,datatype)用于构造从驱动程序中读取数据的命令编号;
_IOw(type,nr,datatype)用于写入数据的命令
_IOWR(type,nr,datatype)用于双向传输。
type,nr通过参数传入,而size位字段通过对datatype参数的sizeof获得。
ioctl函数模板
int xxx_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg)
{
-----
switch(cmd)
{
case xxx_cmd1:
----
break;
case xxx_cmd2:
----
break;
default:
return -ENOTTY;
}
return 0;
}
字符设备驱动结构:
int open(struct inode *inode,struct file *filp);
模块使用计数加1
识别次设备号
硬件操作
检查设备相关错误(诸如设备未就绪或类似的硬件问题)
如果设备是首次打开,则对其初始化
如果有中断操作,申请中断处理程序
关闭
int release(struct inode *inode,struct file *filp);
模块使用计数减1
释放由open分配的,保存在filp>private_data里的所有内容。
硬件操作:
如果申请了中断,则释放中断中中断处理程序。
在最后一次关闭操作时关闭设备。
read/write
ssize_t read(struct file *filp,char __user *buff,sieze_t count,loff_t *offp);
ssize_t write(struct file *filp,const char __user *buff,size_t count,loff_t *offp);
用户空间和内核空间之间的数据拷贝过程
不能简单的用指针操作或者memcpy来进行数据的拷贝
用户空间的数据是可以被换出的,会产生一个页面失效的异常
用户空间的地址无法在内核空间中使用。
用户空间和内核空间之间进行数据拷贝的函数
unsigned long copy_from_user(void *to,const void __user *from,unsigned long count);
unsigned long copy_to_user(void __user *to,const void *from,unsigned long count);
读设备模板
ssize_t xxx_read(struct *filp,char __user *buf,size_t count,loff_t*f_fpos)
{
copy_to_user(buf,---)
}
写设备模板
ssize_t xxx_write(struct file *filp,const char __user *buf,size_t count,loff_t *f_ops)
{
copy_from_user(--,buf,--);
}
ioctl函数
为设备驱动程序执行命令提供了一个特有的入口点
用来设置或者读取设备的属性信息
int ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg);
cmd参数的定义
不推荐使用0x1,0x2之类的值
linux对ioctl()的cmd参数有特殊的定义
设别号(type) 序列号(number) 方向(direction) 数据尺寸(size)
8bit 8bit 2bit 13/14bit
构造命令编号的宏:
_IO(type,nr)用于构造物参数的命令编号;
_IOR(type,nr,datatype)用于构造从驱动程序中读取数据的命令编号;
_IOw(type,nr,datatype)用于写入数据的命令
_IOWR(type,nr,datatype)用于双向传输。
type,nr通过参数传入,而size位字段通过对datatype参数的sizeof获得。
ioctl函数模板
int xxx_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg)
{
-----
switch(cmd)
{
case xxx_cmd1:
----
break;
case xxx_cmd2:
----
break;
default:
return -ENOTTY;
}
return 0;
}
字符设备驱动结构:
相关文章推荐
- 驱动-几个关键函数的实现(第三天)
- 嵌入式 globalmem设备驱动ioctl()函数的实现
- 【驱动开发】Windows系统下枚举设备的几个函数
- 25 驱动设备申请及源码实现设备文件创建一体函数(miscdevice)
- Linux字符设备驱动(三)-文件操作函数实现
- 20 字符设备驱动相关的函数和参数及实现(虚拟文件)
- 70 linux网络设备驱动之虚拟网卡及arp应答的实现
- WINCE 设备驱动中实现电源管理
- string.h中的几个函数实现
- URL处理几个关键的函数parse_url、parse_str与http_build_query
- Linux字符设备驱动程序(二)---------实现open,read,write,llseek函数
- 电子商务网站中大数据架构实现个性化的几个关键架构步骤
- TensorFlow入门02:cnn实现MNIST分类的源码及关键函数解析
- 几个关键的MFC函数的区别
- linux设备驱动归纳总结(三):4.ioctl的实现
- 关于驱动开发中mmap函数的实现
- linux驱动开发--copy_to_user 、copy_from_user函数实现内核空间数据与用户空间数据的相互访问
- linux 高级字符设备驱动 ioctl操作介绍 例程分析实现
- 用拖拉实现设备驱动配置(EsayHMI最新驱动配置方式)
- [转]WinCE流设备驱动简介及GPIO驱动的实现