ioctl 和unlock_ioctl函数讨论

来源:互联网 发布:匡恩网络武汉 编辑:程序博客网 时间:2024/05/23 21:00

http://blog.csdn.NET/cbl709/article/details/7295772  来源网址---谢谢分享----但是笔者可能忽略了函数的返回值类型为long

我在操作的时候发现在编译程序的时候有个警告

“ warning: initialization from incompatible pointer type [enabled by default]
: warning: (near initialization for ‘hello_fops.unlocked_ioctl’) [enabled by default]”

细致之后是发现是因为函数返回值类型是长整型long

今天调一个程序调了半天,发现应用程序的ioctl的cmd参数传送到驱动程序的ioctl发生改变。而根据《Linux设备驱动》这个cmd应该是不变的。因为在kernel 2.6.36 中已经完全删除了struct file_operations 中的ioctl 函数指针,取而代之的是unlocked_ioctl ,所以我怀疑二者是不是兼容的。上网查了一些资料,很多文章只是泛泛谈了一下,说在应用程序中ioctl是兼容的,不必变化。而在驱动程序中这个指针函数变了之后最大的影响是参数中少了inode ,所以应用程序ioctl是兼容的,但驱动程序中我们的ioctl函数必须变化,否则就会发生cmd参数的变化:

原来的驱动程序

static const struct file_operations globalmem_fops=
{
.owner=THIS_MODULE,
.llseek=globalmem_llseek,
.open=globalmem_open,
.read=globalmem_read,
.write=globalmem_write,
.ioctl=globalmem_ioctl,
.release=globalmem_release,
};

int globalmem_ioctl(struct inode* inode,struct file* filp, unsigned int cmd,unsigned long arg)

{

switch (cmd)

  {

   case:XXX:   ...

    ……

  }

}

改变后的

static const struct file_operations globalmem_fops=
{
.owner=THIS_MODULE,
.llseek=globalmem_llseek,
.open=globalmem_open,
.read=globalmem_read,
.write=globalmem_write,
.unlocked_ioctl=globalmem_ioctl,
.release=globalmem_release,
};

long globalmem_ioctl(struct file* filp, unsigned int cmd,unsigned long arg)//没有inode参数!返回值为long!

{

switch (cmd)

  {

   case:XXX:   ...

    ……

  }

}


总结:ioctl变为unlocked_ioctl时候有两个地方发生改变:一是返回值类型变为长整型;二是少了第一个参数inode;

  注意在file_operations中对应的改为unlocked_ioctl

0 0