Reputation: 3423
I am studying character device driver programming. I had some doubts and hope to clarify them here:-
(a) "A device file is associated with a major number and a minor number. Also in our driver module, we define a cdev object with its fops field defined according to our functions and same major and minor numbers as our device file."
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?
You are free to explain this in your preferred way, but I would prefer if you could explain it using an example. Thanks!
Upvotes: 1
Views: 317
Reputation: 1285
I am a newcomer to character drivers. This is just a small summary of what I can make out for your questions. Suggestions and edits are welcomed.
These are the main structures you need to know for writing the character drivers:
1) File operation structure: each field in this structure points to the function in the driver that implements eg open, read,write,ioctl. Each open file is associated with some functions by including a field called f_op which will point to the file operation structure.
2) File structure: it represents an open file. it is not specific to drivers and each open file will have a file structure in the kernel space. It is created by the kernel on open & passed to any function that operates on the file until the last close. struct fileoperations *f_op;
3) Inode structure: used by the kernel to internally represent the files. Only two parameters are important here viz a. struct cdev *i_cdev and 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.
this is what I intrepret:
Inode is read from disk & inode object is intialized.
ext2_readinode()----> init_special_inode()----> this will intialize the i_rdev field of inode object to minor and major numbers of device files.
Upvotes: 0