組み込みLINUXドライバーが5.ioctlキャラクターデバイスドライバープログラミングを学習(1)
注:一般的なキャラクターデバイスは、write()およびread()関数の代わりにioctl関数を使用できます
1.カーネル空間で使用される対応する関数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;
}
2.ユーザー空間でのプログラミング機能: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字符设备驱动编程(二)中都有用到。