linux 创建sysfs节点示例

3 篇文章 0 订阅

#include <linux/module.h>
#include <linux/slab.h>
#include <linux/kobject.h>
#include <linux/platform_device.h>

static ssize_t node1_show(struct device *dev,
        struct device_attribute *attr, char *buf)
{
    ssize_t rc = 0;
    snprintf(buf, 32, "%s\n", node1_value);
    rc = strlen(buf);
    return rc;
}

static ssize_t node2_show(struct device *dev,
        struct device_attribute *attr, char *buf)
{
    ssize_t rc = 0;
    snprintf(buf, 8, "%d\n", node2_value);
    rc = strlen(buf);
    return rc;
}

static ssize_t node1_store(struct device *dev,
        struct device_attribute *attr, const char *buf, size_t count)
{
    printk("buf = %s,echo debug buf",buf);
 
    return count;
}
 

static DEVICE_ATTR(node1, 0666, node1_show, node1_store);
static DEVICE_ATTR(node2, 0666, node2_show, NULL);

static struct attribute *all_node_attrs[] = {
        &dev_attr_node1.attr,
        &dev_attr_node2.attr,
        ....
        NULL
};

static struct attribute_group all_node_attr_group = {
        .attrs = all_node_attrs,
};

int32_t example_init(void)
{
    int32_t rc = 0;
    struct kobject *all_node_device = NULL;

    CDBG("%s %d\n", __func__, __LINE__);

    node1_value = 0;
    node2_value= 0;

    all_node_device = kobject_create_and_add("temp_node", NULL);
    if (all_node_device == NULL) {
        pr_err("%s: subsystem_register failed\n", __func__);
        rc = -ENOMEM;
        return rc;
    }
    rc = sysfs_create_group(all_node_device, &all_node_attr_group);
    if (rc) {
        pr_err("%s: sysfs_create_file failed\n", __func__);
        kobject_del(all_node_device);
    }

    return 0;
}

static void __exit example_exit(void)
{
     kobject_put(all_node_device);
}

module_init(example_init);
module_exit(example_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("xxxx")

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值