如何为sysfs属性创建许多类似的函数?

时间:2018-01-31 10:46:43

标签: c linux linux-kernel linux-device-driver sysfs

我有一个通用的AXI从设备,在我的FPGA中有几个I / O寄存器。我想用sysfs接口访问我的寄存器。对于我想要访问的每个寄存器,我创建了一个_show和一个_store函数,但这将是很多代码。

static ssize_t writereg0_store(struct device *dev, struct device_attribute                *attr, const char *buf, size_t count) {
    struct axi_register_driver *drv = dev_get_drvdata(dev);
    u32 val;

    if (kstrtou32(buf, 10, &val) != 0)
        return -EINVAL;

    iowrite32(val, drv->reg_base + WRITE_OFFSET + 0);

    return count;

}

static ssize_t writereg0_show(struct device *dev, struct device_attribute *attr, char *buf) {
    struct axi_register_driver *drv = dev_get_drvdata(dev);

    u32 reg;
    reg = ioread32(drv->reg_base + WRITE_OFFSET + 0);

    return sprintf(buf, "0x%x\n", reg);
}
static DEVICE_ATTR_RW(writereg0);

static ssize_t readreg0_show(struct device *dev, struct device_attribute *attr, char *buf) {
    struct axi_register_driver *drv = dev_get_drvdata(dev);

    u32 reg;
    reg = ioread32(drv->reg_base + 0);

    return sprintf(buf, "0x%x\n", reg);
}
static DEVICE_ATTR_RO(readreg0);

第二个只读寄存器基本相同,除了函数名和地址偏移量:

static ssize_t readreg1_show(struct device *dev, struct device_attribute *attr, char *buf) {
    struct axi_register_driver *drv = dev_get_drvdata(dev);

    u32 reg;
    reg = ioread32(drv->reg_base + OFF_REG1);

    return sprintf(buf, "0x%x\n", reg);
}
static DEVICE_ATTR_RO(readreg1);

创建例如32个寄存器,我要做32次;代码不会很漂亮。

那么创建一堆类似函数的最佳方法是什么,特别是对于给定的sysfs-attribute用例?

2 个答案:

答案 0 :(得分:4)

您的_show_store函数不使用struct device_attribute *attr参数,但此参数恰好包含有关给定文件所代表的特定事物的信息:

// Derive 'device_attribute' structure for a read register's attribute
struct dev_axi_read_reg_attribute {
    struct device_attribute attr;
    int reg_offset; // Offset of the register here
};

// Generic 'show' method, suitable for every read register
static ssize_t read_axi_reg_show(struct device *dev, struct device_attribute *attr, char *buf) {
    struct axi_register_driver *drv = dev_get_drvdata(dev);
    // 'attr' is actually of type `struct dev_axi_read_reg_attribute`.
    // See definition of the macro 'AXI_READ_REG_ATTR' below.
    struct dev_axi_read_reg_attribute* axi_read_attr = container_of(attr,
        struct dev_axi_read_reg_attribute, attr);

    u32 reg;
    // Take offset to the register from the attribute
    reg = ioread32(drv->reg_base + axi_read_attr->reg_offset);

    return sprintf(buf, "0x%x\n", reg);
}

// Macro for declare attribute for read register
#define AXI_READ_REG_ATTR(_name, _reg_offset) \
struct dev_axi_read_reg_attribute dev_attr_##_name = \
    { __ATTR(_name,  S_IRUGO, read_axi_reg_show, NULL), _reg_offset }

// Declare as many attributes as you want
static AXI_READ_REG_ATTR(readreg0, OFF_REG0);
static AXI_READ_REG_ATTR(readreg1, OFF_REG1);
// ...

static struct attribute *axi_register_attrs[] = {
    &dev_attr_readreg0.attr.attr,
    &dev_attr_readreg1.attr.attr,
    // ...
    NULL,
};
ATTRIBUTE_GROUPS(axi_register);

include/linux/device.h中定义的DEVICE_ULONG_ATTR宏中使用了类似的方法。该宏为“long”类型的变量创建了一个属性。

答案 1 :(得分:2)

我可能会遗漏一些东西,但你不想只想制作32个包装器:

static ssize_t readreg_show(struct device *dev, struct device_attribute *attr, char *buf,unsigned long offset) {
    struct axi_register_driver *drv = dev_get_drvdata(dev);

    u32 reg;
    reg = ioread32(drv->reg_base + offset);

    return sprintf(buf, "0x%x\n", reg);
}

并且可能定义32:

static ssize_t readreg1_show(struct device *dev, struct device_attribute *attr, char *buf) {readreg_show(dev,attr,buf,OFF_REG1)}

(每个reg一个)如果你需要不同的功能名称?这可能是MACROd,如果它真的困扰你复制32次,如果偏移都是OFF_REGi。