【发布时间】:2014-09-12 09:31:56
【问题描述】:
我正在编写一个具有多个设备属性的简单 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 并不是一个很好的方法,尤其是因为我收到警告“与字符串文字比较会导致未指定的行为”telling me 使用 strcmp。你知道有什么更好的方法来确定哪个属性负责调用吗?
【问题讨论】:
标签: c linux linux-kernel linux-device-driver