inode获取设备号

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

当然是从代表一个设备文件的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, };
 







  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值