gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:22:09 27 4
gpt4 key购买 nike

我的 FPGA 中有一个带有多个 I/O 寄存器的通用 AXI 从设备。我想使用 sysfs 接口(interface)访问我的寄存器。对于我想要访问的每个寄存器,我都创建了一个 _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 用例?

最佳答案

您的_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);

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

关于c - 如何为 sysfs 属性创建许多类似的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48540242/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com