最近写linux驱动,被网上的教程狠狠坑了一把.
linux内核为3.13, 网上博客都是基于2.6.x的.
大部分都提及: file_operations 的 ioctl 已经废弃,改由使用unlocked_ioctl 或者compat_ioctl
此时编译会失败,一般提示: error:unknown field 'ioctl' specified in initializer
把ioctl修改下就好:
struct file_operations vppgo_io_fops = {
.open = test_io_open,
.release = test_io_close,
//.ioctl = test_io_ioctl, //#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35))
.unlocked_ioctl = test_io_ioctl,
};
但是还有一点: ioctl函数指针,参数个数减少,这个是国内大部分教程都没有写明的,搜索了好久,才google出来,详细见这个链接.
int ioctl(struct inode *i, struct file *f, unsigned int cmd, unsigned long arg);
从 2.6.35开始,变为:
long ioctl(struct file *f, unsigned int cmd, unsigned long arg);
存在的坑还在于,由于是函数指针类型,在编译的时候参数错了也不会报错.
因此在实际使用时,就会发现ioctl到驱动的参数值对不上.
还是要多多看看头文件啊,确定下参数形式...
ioctl相关的file_operations位于中.
/usr/src/linux-source-3.13.0/include/linux/fs.h