Linux下的sys节点小析

先看使用的demo

static ssize_t ts_info_show(struct device *dev,struct device_attribute *attr, char *buf)
{
	return 0;
}

static DEVICE_ATTR(ts_info, 0400, ts_info_show, NULL);
 
static struct attribute *g[] = {
        &dev_attr_ts_info.attr,
        NULL,
};
 
static const struct attribute_group gg = {
	.attrs	= g,
};

sysfs_create_group(&kobj,&gg);

 

kernel4.14/include/linux/device.h

#define DEVICE_ATTR(_name, _mode, _show, _store) \
	struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)

kernel4.14/include/linux/sysfs.h

#define __ATTR(_name, _mode, _show, _store) {                           \
        .attr = {.name = __stringify(_name),                            \
                 .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },             \
        .show   = _show,                                                \
        .store  = _store,                                               \
}

比如这个static DEVICE_ATTR(ts_info, 0664, ts_info_show, NULL);

按上面的规则展开来就是

struct device_attribute dev_attr_ts_info={
        .attr = {
        	     .name = "ts_info",  
                 .mode = 0400, 
                 };
        .show   = ts_info_show, 
        .store  = NULL,        
}

kernel4.14/include/linux/device.h

struct device_attribute {
        struct attribute        attr;
        ssize_t (*show)(struct device *dev, struct device_attribute *attr,char *buf);
        ssize_t (*store)(struct device *dev, struct device_attribute *attr,const char *buf, size_t count);
};

 kernel4.14/include/linux/sysfs.h

struct attribute {
        const char              *name;
        umode_t                 mode;
}

代码里调用

static struct attribute * g[] = {
        &dev_attr_ts_info.attr,
        NULL,
 };

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值