2012-12-17 46 views
1

我正在学习字符设备驱动编程。我有一些疑惑,并希望在这里澄清它们: -字符设备驱动编程

(a)“设备文件与主号码和次号码相关联,而且在我们的驱动程序模块中,我们定义了一个定义了fops字段的cdev对象根据我们的功能和相同的主要和次要的数字作为我们的设备文件。“

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? 

但你可以在你的首选方式来解释这一点,但我宁愿如果你可以用一个例子来解释它。谢谢!

回答

0

我是角色驱动程序的新手。这只是我可以针对您的问题做出的一个小小总结。欢迎提出建议和编辑。

这些都是你需要知道写字符驱动程序的主要结构:

1)文件操作结构:每个字段在这种结构中指向实现如开放的驱动程序中的功能,读,写, IOCTL。每个打开的文件都通过包含一个名为f_op的字段与某些函数关联,该字段将指向文件操作结构。

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

3)Inode结构:内核用来内部表示文件。这里只有两个参数很重要。结构的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. 

这是我intrepret:

的索引节点是从磁盘& inode对象是intialized读取。

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