我正在编写具有多个设备属性的简单Linux驱动程序。现在,无论读取或写入哪个属性,最终都将在设备内存的某个位置读取或写入。只有定义精确位置的偏移量才能从一个属性更改为另一个属性。使用几行代码可以更轻松地解释这一点:
/* General read function evoked by attributes */
static const ssize_t foo_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
u32 offset;
if (attr->attr.name == "control")
offset = OFFSET_CTRL;
else if (attr->attr.name == "status")
offset = OFFSET_STATUS;
u32 data = ioread32(dev_mem + offset);
...
}
...
/* declaring attributes, all linking to the same function */
static DEVICE_ATTR(control, S_IWUGO | S_IRUGO, foo_show, foo_set);
static DEVICE_ATTR(status, S_IRUGO, foo_show, NULL);
现在,您可能会猜想使用attr->attr.name == foo
不是真正的好方法,特别是因为我收到警告“与字符串文字比较导致未指定的行为”,告诉我使用strcmp
。您是否知道任何更好的方法来确定哪个属性负责调用?
在现有驱动程序中似乎完成此操作的方法是直接与global属性进行比较。
static DEVICE_ATTR(control, S_IWUGO | S_IRUGO, foo_show, foo_set);
static DEVICE_ATTR(status, S_IRUGO, foo_show, NULL);
static const ssize_t foo_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
u32 offset;
if (attr == &dev_attr_control)
offset = OFFSET_CTRL;
else if (attr == &dev_attr_status)
offset = OFFSET_STATUS;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句