Linux sysfs 下创建节点 实现echo cat功能

device_create_file
DEVICE_ATTR

**DEVICE_ATTR宏展开**
#define DEVICE_ATTR(_name, _mode, _show, _store) \
    struct device_attribute dev_attr_##_name = { \
        .attr = {.name = __stringify(_name), .mode = _mode },   \
        .show   = _show,                                        \
        .store  = _store,                                       \
    }

device_create_file要在probe中调用。

err = device_create_file(&dev->dev, &dev_attr_test);
if (err) {
        dev_err(&dev->dev, "sys file creation failed\n");
        return -ENODEV;
}

dev_attr_test的实现
static DEVICE_ATTR(test, 0664, test_show, test_store);
static ssize_t test_show(struct device *dev,
                   struct device_attribute *attr, char *buf)
{

    int temp[3];
    struct test_data *gpio_data_ptr = dev_get_drvdata(dev);
    temp[0] = gpio_get_value(gpio_data_ptr->gpio0);
    temp[1] = gpio_get_value(gpio_data_ptr->gpio1);
    temp[2] = gpio_get_value(gpio_data_ptr->gpio2);

    return snprintf(buf, 4, "%d%d%d", temp[0], temp[1],
                    temp[2]);
}

static ssize_t test_store(struct device *dev,

           struct device_attribute *attr, const char *buf, size_t size)

{
    struct test *gpio_data_ptr = dev_get_drvdata(dev);
    gpio_set_value(gpio_data_ptr->gpio0,  (buf[0] == '1') ? 1 : 0);
    gpio_set_value(gpio_data_ptr->gpio1,  (buf[1] == '1') ? 1 : 0);
    gpio_set_value(gpio_data_ptr->gpio2,  (buf[2] == '1') ? 1 : 0)
    return size;
}

static DEVICE_ATTR(test, 0664, test_show, test_store);
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值