【嵌入式Linux驱动开发】三、字符设备驱动(二)

2019-07-13 00:43发布

1. open函数 int  open(struct  inode *inode, struct  file *fp);
  • 模块使用计数加1;
  • 识别次设备号;
  • 硬件操作:检查设备相关错误;如果设备是首次打开,则对其初始化;如果有中断操作,申请中断处理程序。
2. release函数
int  release(struct  inode *inode, struct  file *fp);
  • 模块使用计数减1;
  • 释放由open分配的,保存在file->private_data里的内容;
  • 硬件操作:如果申请了中断,则释放中断处理程序;在最后一次关闭操作时关闭设备。
3. read/write函数 ssize_t  read(struct  file *fp, char  __user *buf, size_t  count, loff_t *offp); ssize_t  write(struct  file *fp, const  char  __user *buf, size_t  count, loff_t *offp); 用户空间和内核空间之间的数据传递: 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 file *fp, char __user *buf, size_t count, loff_t *f_pos) { ... copy_to_user(buf, ..., ...); ... } //写设备模版 ssize_t xxx_write(struct file *fp, const char __user *buf, size_t count, loff_t *f_pos) { ... copy_from_user(..., buf, ...); ... }4. ioctl函数
  • 为设备驱动程序执行“命令”提供了一个特有的入口点。
  • 用来设置或者读取设备的属性信息。
int  ioctl(srutct  inode *inode, struct  file *fp, unsigned  int  cmd, unsigned  long  arg); cmd参数的定义: 设备类型(type) 序列号(number) 方向(direction) 数据尺寸(size) 8bit 8bit 2bit 13/14bit type、number位字段通过参数传入,而size位字段通过对datatype参数取sizeof获得。
构造命令编号的宏:
  • _IO(type, number, )用于构造无参数的命令编号;
  • _IOR(type, number, datatype)用于构造从驱动程序中读取数据的命令编号;
  • _IOW(type, number, datatype)用于写入数据的命令;
  • _IORW(type, number, datatype)用于双向传输;
//ioctl函数模版 int xxx_ioctl(struct inode *inode, struct file *fp, unsigned int cmd, unsigned long arg) { ... switch(cmd) { case XXX_CMD1: ... break; case XXX_CMD2: ... break; default: //不能支持的命令 return -ENOTTY; } return 0; }5. 字符设备驱动结构