字符设备驱动编程

时间:2012-12-17 21:08:57

标签: linux-device-driver

我正在研究字符设备驱动程序编程。我有些疑惑,希望在这里澄清一下: -

(a)“设备文件与主编号和次编号相关联。同样在我们的驱动程序模块中,我们定义了一个cdev对象,其fops字段根据我们的函数定义,并且与我们的设备相同的主要和次要编号文件。”

1. I want to know what exactly happens when a function is called on the device file. 
Here is what I think. Suppose, I make a file called mydevfile using mknod(). Now 
when I call open(mydevfile, O_RDWR), the kernel is searched for a cdev object with
same minor and major number. When found, the cdev 's fops is searched for function
for open() (say dev_open()). It is written that the dev_open() should have first 
argument inode* and second argument file*. My question is how are these parameters 
passed to the dev_open() function?

2. I learnt that inode is associated with a file on disk. Which file is it associated
with here? Also inode has a pointer to corresponding cdev. Now if we have already
got the cdev  by searching major and minor number from mydevfile, why do we need 
inode? W

3. What does the file*(i.e. the second argument) point to in this case?

您可以自由地以您的首选方式解释这一点,但我希望您能够使用示例来解释它。谢谢!

1 个答案:

答案 0 :(得分:0)

我是角色车手的新手。这只是我能为您提出的问题的一个小小的总结。欢迎提出建议和修改。

这些是编写角色驱动程序时需要了解的主要结构:

1)文件操作结构:该结构中的每个字段指向驱动程序中的函数,该函数实现例如open,read,write,ioctl。每个打开的文件都通过包含一个名为f_op的字段与一些函数相关联,该字段将指向文件操作结构。

2)文件结构:它代表一个打开的文件。它不是特定于驱动程序,每个打开的文件在内核空间中都有一个文件结构。它由内核在open& amp;创建时创建。传递给对文件进行操作的任何函数,直到最后一次关闭。 struct fileoperations * f_op;

3)Inode结构:内核用于在内部表示文件。这里只有两个参数很重要,即a。 struct cdev * i_cdev和b.dev_t i_rdev

a. struct cdev *i_cdev: kernel's internal structure to represent the char devices.
b. dev_t i_rdev: contains the actual device numbers.

这就是我的意思:

从磁盘读取Inode; inode对象是初始化的。

ext2_readinode()----> init_special_inode()---->这会将inode对象的i_rdev字段初始化为次要和主要数量的设备文件。