Here is my understanding in opening to a file for reading/writing.
In the application layer, I can invoke the fopen()
function.
The fwrite()
function will invoke a system call open()
.
After the OS receives the open()
call, it will pass the command to VFS(virtual file system).
VFS looks up the file name, including any directories needed and does the necessary access checks.
If this is in RAM cache then no disk access is needed. If not, the VFS sends a read request to the specific file system which is probably EXT4.
Then the EXT4 file system driver will determine in what disk block that directory is located in. It will then send a read command to the disk device driver.
So now let's say I want to read an i2c device A attached to the board. And the file directory is /dev/i2c/A
Is there a major number for all devices? For example, Linux OS sets 180 as the major number for USB. So at the device side, is there a major number 180 in each USB device?
If the answer to 1st question is NO, then how can the Linux OS determine which type of device A is, is it just according to the file directory?
I think the answer to 2nd question maybe: at the boot initialization stage, there are some certain codes that have already mount that port to the file system using something like export()? So in fact, right after the booting stage, file directory /dev/i2c/A exists there and it is bind with a major number for i2c devices. So when I want to open dev/i2c/A, the OS will find the right i2c driver for me, not the SPI or USB driver.I'm not sure about this part, i need more information on this.
The above situation happens when the device is mounted to the file system right after the booting stage. so what happened if I have a usb, how can this usb be mounted to the file system with the right major number 180 after it is plugged in? And I guess there is a irq when the usb is plugged in before the mounting stage starts?
/dev/
tree. The question of what driver is selected for a device is completely different: i.e. identification of peripherals by heuristic probing, or with help from a "plug and play" system. – Kaz