【问题标题】:Confusion regarding how exactly file operations get set up for a Linux Character Device Driver关于如何为 Linux 字符设备驱动程序设置文件操作的困惑
【发布时间】:2020-07-12 17:44:57
【问题描述】:

我目前正在阅读 Oreilly 的 Linux 设备驱动程序第 3 卷,我有一个关于为特定设备驱动程序设置文件操作的问题。

我目前的理解是,在Linux的字符设备驱动中,需要在设备驱动初始化函数中分配初始化的cdev结构,并传递给int cdev_add(struct cdev *dev, dev_t num, unsigned int count);

一旦这一切完成,内核现在将struct file_operations 中指定的文件操作与添加的struct cdev 相关联。

当我试图了解设备驱动程序open(struct inode *, struct file *) 函数中发生的事情时,我感到困惑。我们得到了一个文件指针和一个 inode 指针。这两个结构都包含struct file_operations * 字段,inode -> i_cdev -> opsfile -> f_op 中的一个。

这些字段是否在每个设备上引用相同的 file_operations 结构?

我也意识到我可以更改file_operations 结构的值。如果我决定编辑结构中的字段,退出open() 函数后是否会反映更改?如果是这样,这个功能的意义何在?

对不起,如果我在这篇文章中要求太多。任何帮助将不胜感激!

【问题讨论】:

  • 当它到达open() 函数时,file->f_opinode->i_cdev->ops 应该从您的驱动程序指向同一个struct file_operations。在open() 中修改struct file_operations 的内容不是一个好主意,但是可以使用fops_get()replace_fops() 的组合将file->f_op 切换为指向不同的struct file_operations。有关示例,请参见“drivers/char/misc.c”中的misc_open()
  • 另外,LDDv3 的书很老了,所以写这本书的时候fops_get()replace_fops() 函数可能不存在。

标签: c linux linux-kernel linux-device-driver device-driver


【解决方案1】:

上面 Ian Abbott 的评论基本上回答了我的问题。

当它到达open() 函数时,file->f_opinode->i_cdev->ops 应该从您的驱动程序指向同一个struct file_operations。在open() 中修改struct file_operations 的内容不是一个好主意,但是可以使用fops_get()replace_fops() 的组合将file->f_op 切换为指向不同的struct file_operations。有关示例,请参见“drivers/char/misc.c”中的misc_open()。 – Ian Abbott 10 小时

谢谢!!

【讨论】:

    猜你喜欢
    • 2018-07-24
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 2013-03-26
    相关资源
    最近更新 更多