inode获取设备号

来源:互联网 发布:windows 网络重置 编辑:程序博客网 时间:2024/06/10 21:40

当我们打开一个设备文件时,我们如何从该设备文件获取到设备文件的设备号呢?

当然是从代表一个设备文件的struct inode中找到代表设备文件的dev_t啦!




struct inode

struct inode {...<span style="font-size:18px;color:#ff0000;">dev_ti_rdev;  /*设备号*/</span>...const struct inode_operations*i_op;const struct file_operations*i_fop;/* former ->i_op->default_file_ops */... struct list_headi_devices;union {struct pipe_inode_info*i_pipe;struct block_device*i_bdev;struct cdev*i_cdev;  /*此处指向的内容的次设备号是0*/};....}



struct file_operations {struct module *owner;l ...<span style="font-size:18px;color:#ff0000;">int (*open) (struct inode *, struct file *);</span>int (*flush) (struct file *, fl_owner_t id);int (*release) (struct inode *, struct file *);...};


struct cdev *i_cdev

struct cdev is the kernel’s internal structure that represents char devices; this
field contains a pointer to that structure when the inode refers to a char device

file.

struct cdev 是内核内部的结构,其作用是用来表示一字符设备(即c前缀的原因),该域包含了一个指针,当该节点指向一个设备文件时,该指针指向那个设备文件的节点。


<pre name="code" class="plain">static int chery_drv_open(struct inode *inode, struct file *file){dev_t cdev = inode->i_cdev->dev;printk("minor %d\n",MINOR(inode->i_rdev<span style="font-family: Arial, Helvetica, sans-serif;">));</span>
return 0;}

static struct file_operations chery_drv_fops = { .owner = THIS_MODULE, /* 这是一个宏,指向编译模块时自动创建的__this_module变量 */ .open = chery_drv_open, };







0 0
原创粉丝点击