用“dev_groups”replace弃用的“dev_attrs”属性

我试图编译一个Linux设备驱动程序(内核模块),然而这个模块最后在​​2013年4月更新,当然它不会在最近的(3.13)内核上编译,这里是错误:

als_sys.c:99:2: error: unknown field 'dev_attrs' specified in initializer 

我已经search了,但是我发现的所有东西都是补丁,关于更新旧模块没有明确的“教程”,我唯一明白的是我需要使用dev_groups ,但是它不接受相同的值dev_attrs和我不知道如何适应现有的代码。

代码(其中的一些代码可以在这里find):

 # als_sys.c static ssize_t illuminance_show(struct device *dev, struct device_attribute *attr, char *buf) { struct als_device *als = to_als_device(dev); int illuminance; int result; result = als->ops->get_illuminance(als, &illuminance); if (result) return result; if (!illuminance) return sprintf(buf, "0\n"); else if (illuminance == -1) return sprintf(buf, "-1\n"); else if (illuminance < -1) return -ERANGE; else return sprintf(buf, "%d\n", illuminance); } # truncated - also "adjustment_show" is similar to this function so # I didn't copy/paste it to save some space in the question static struct device_attribute als_attrs[] = { # that's what I need to modify, but __ATTR(illuminance, 0444, illuminance_show, NULL), # I have no clue what to __ATTR(display_adjustment, 0444, adjustment_show, NULL), # put here instead __ATTR_NULL, }; # truncated static struct class als_class = { .name = "als", .dev_release = als_release, .dev_attrs = als_attrs, # line 99, that's where it fails }; 

编辑

正如在下面的答案中提到的,我改变了这样的代码:

 static struct device_attribute als_attrs[] = { __ATTR(illuminance, 0444, illuminance_show, NULL), __ATTR(display_adjustment, 0444, adjustment_show, NULL), __ATTR_NULL, }; static const struct attribute_group als_attr_group = { .attrs = als_attrs, }; static struct class als_class = { .name = "als", .dev_release = als_release, .dev_groups = als_attr_group, # line 103 - it fails here again }; 

但是我仍然得到另一个错误:

als_sys.c:103:2: error: initializer element is not constant

我发现这个问题是关于相同的错误,但是它的答案是关于一个属性,我不知道如何适应多个。

感谢您的帮助,祝您有美好的一天。

的确,在3.13中, dev_attrsdev_groups所取代。 在3.12中,它们都是以struct的形式呈现的。 看看3.12版本和3.13版本 。

无论如何,不​​应该有一个问题,因为简单的搜索attribute_group给你很多的例子。

简而言之,您必须将dev_attrs嵌入到dev_group中:

 static const struct attribute_group als_attr_group = { .attrs = als_attrs, }; 

然后在struct类中使用该属性组。

还有一个方便的宏ATTRIBUTE_GROUPS 。 请参阅示例用法https://lkml.org/lkml/2013/10/23/218

编辑

从属性组中删除const声明,如下所示:

 static struct attribute_group als_attr_group = { .attrs = als_attrs, }; 

因为你不能用类似0xff'c'东西来初始化const struct。 在这里看到更多详细信息 。