嵌入式LINUX驱动学习之5.ioctl字符设备驱动编程(一)
说明:一般的字符设备可以使用ioctl函数替代write()和read()函数
一 、内核空间使用的对应函数unlocked_ioct()
1.1函数头文件及原型
//头文件位置:include/linux/fs.h
struct file_operations {
....忽略更多内容.....
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
....忽略更多内容.....
}
1.2 初始化unlocked_ioctl()函数操作接口
#include <linux/fs.h>
static long mycdev_ioctl (struct file * file, unsigned int kcmd, unsigned long kindex){
/* 执行的操作 */
}
struct file_operations fops = {
.owner = THIS_MODULE;
.unlocked_ioctl = mycdev_ioctl;
}
二、用户空间的编程函数:ioctl(2)
// 可查看 :ioctl(2) 或查看内核源代码文件:Documentation/ input/ff.txt
#include <sys/ioctl.h>
int ioctl(int file_descriptor, int request, unsigned long *features);
参数说明:
file_descriptor : 文件描述符
request :一般传无符号整数,用于命令
features :无符号长整形,一般用于参数,
这个参数的好用之处在于:当内核空间执行copy_to_user时,相当于读操作;
当内核空间执行copy_from_user时,相当于写操作;
在ioctl字符设备驱动编程(二)中都有用到。