I have a question about Linux device file and drivers, with my current understanding as the following:
When a user calls
open()on some device file, at some point of time kernel will askinodeof the device file for theread()/write()function.The device file was created by
udevearlier, based on devices in/sysfolder, meaning theinodeof the device file should havei_fopfield pointed to functions that know how to talk to the device, such asread/write/mmap/ioctl/poll. And that means each device file'sinode->i_fopfield should point to a differentfile_operationsstruct.
If so, the device driver will be providing these read()/write() functions, maybe the complete file_operations struct, including read/write/mmap/ioctl, etc.
now , ULK says (in the description of open() syscall for device files) "sets the i_fop field of the inode object to the address of either the def_blk_fops or the def_chr_fops file operation table, according to the type of device file." And that means all block device files have the same read()/write() function, but then how can a user talk to different devices?
I also checked the device_driver struct, and indeed, there are no place to store file access functions, so how exactly an open() syscall performs its job using a device specific driver then? And where do device specific operation function live, if not in device_driver?
device_driverprovide in this process, since thecdevstruct already havefile_operations? - QnAdevice_driverstructure's definition you will find that this structure describes completely different things than the file operations. It has e.g.probefunction which binds the device with a driver. - Tsyvarev