Character device driver internal implementation

As long as the file exists, there will be a unique corresponding inode number, and there will be a corresponding struct inode structure. Opening a device file through open() at the application layer will generate an inode number correspondingly, and the file can be found through the inode number The inode structure, inode structure

 

struct inode {
    umode_t            i_mode;
    unsigned short        i_opflags;
    kuid_t            i_uid;
    kgid_t            i_gid;
    unsigned int        i_flags;

    dev_t            i_rdev;
    loff_t            i_size;
}


Character device driver object structure cdev

struct cdev {

    struct kobject kobj;

    struct module *owner;

    const struct file_operations *ops;

    struct list_head list;

    dev_t dev;

    unsigned int count;

}

Find the operation method structure pointer in the driver object structure, define and initialize an operation method structure variable in the driver program, and realize specific functions such as opening and closing, reading and writing, etc.

struct file_operations fops={

    .open=mycdev_open,

    .unlocked_ioctl=mycdev_ioctl,

    .release=mycdev_close,

};

The route of the open function callback to the open operation method in the driver:

open()--->sys_open()--->struct inode structure--->struct cdev structure--->struct file_operations structure--->mycdev_open

Guess you like

Origin blog.csdn.net/m0_53451387/article/details/131235251