有可能具有平台属性吗?

发布于 2025-01-21 12:14:14 字数 3271 浏览 3 评论 0原文

与其具有设备驱动程序属性 - 在对代码进行了简短的调查之后,似乎总是在驱动程序模块中引用全局变量,不如说show(struct kobject *,struct kobject *,struct kobj_attribute *,char *buf )store(struct kobject *,struct kobj_attribute *,const char *buf,size_t count)方法在,例如void *platform_get_get_drvdata(const struct struct struct struct struct ocked) platform_device *)?

show()store()函数的参数之间是否存在任何关联- > kobj ?

我可以每次调用probe()函数时可以创建单独的sysfs条目,这将是不错的(在我的情况下)具有与该SYSFS条目和探测实例相对应的属性,而不是与全球驱动程序。


编辑

这就是我想出的。我没有使用方便的“ attribute_groups”宏(和其他)来声明静态全局变量,而是分配了所有内容:

struct am_attributes {
    void * var;
    struct kobj_attribute attr;
}

static const struct attribute_group **am_attribute_groups(struct am_instance *instance, struct device *dev)
{
    struct am_attribute *const am_ms_delay_attr = devm_kzalloc(dev, sizeof(struct am_attribute), GFP_KERNEL);
    struct am_attribute *const am_enable_attr = devm_kzalloc(dev, sizeof(struct am_attribute), GFP_KERNEL);
    struct attribute **const am_attrs = devm_kzalloc(dev, 3 * sizeof(struct attribute *), GFP_KERNEL);
    struct attribute_group *const am_group = devm_kzalloc(dev, sizeof(struct attribute_group), GFP_KERNEL);
    struct attribute_group **const am_groups = devm_kzalloc(dev, 2 * sizeof(struct attribute_group *), GFP_KERNEL);

    if (am_ms_delay_attr && am_enable_attr && am_attrs && am_group && am_groups)
    {
        struct kobj_attribute ms_pause = __ATTR_RW(autowrap_ms_pause);
        struct kobj_attribute enable = __ATTR_RW(autowrap_enable);

        /* Create the attributes for this probe instance. */
        am_ms_delay_attr->var = &instance->autowrap_ms_delay;
        am_ms_delay_attr->attr = ms_pause;

        am_enable_attr->var = &instance->autowrap_enable;
        am_enable_attr->attr = enable;

        am_attrs[0] = &am_ms_delay_attr->attr.attr;
        am_attrs[1] = &am_enable_attr->attr.attr;
        am_attrs[2] = NULL;

        am_group->attrs = am_attrs;

        am_groups[0] = am_group;
        am_groups[1] = NULL;

        return (const struct attribute_group **)am_groups;
    }
    else
    {
        return NULL;
    }
}

在我所做的探针函数中:

...

instance->class->dev_groups = am_attribute_groups(instance, dev);

if (instance->class->dev_groups == NULL)
{
    pr_warn("Couldn't allocate SYSFS attributes.");
    ...

最后,在show> show()store()函数我使用container_of宏来“ rewind” attry参数get struct struct am_attribute pointer和<<代码> void * var;指针。 (然后我派什么):

#define to_am_attribute(x) container_of(x, struct am_attribute, attr)

static ssize_t autowrap_ms_pause_show(struct kobject *kobj,
                                    struct kobj_attribute *attr,
                                    char *buf)
{
    struct am_attribute *am_attr = to_am_attribute(attr);
    (void)kobj;

    return sprintf(buf, "%d\n", *(int *)(am_attr->var));
}

现在的问题是: 这样做是什么样的麻烦? (或“这是合理的?”)

Rather than having device driver attributes - which, after a short survey of the code, seems to always reference global variable in the driver module - is it possible to have the show(struct kobject *, struct kobj_attribute *, char *buf) and store(struct kobject *, struct kobj_attribute *, const char *buf, size_t count) methods operate on, say, instance data referenced by void *platform_get_drvdata(const struct platform_device *)?

Is there any association between the parameters given to the show() and store() functions and, say (struct device *)x->kobj?

I can create separate SYSFS entries each time my probe() function is called, and it would be nice (in my case) to have attributes that correspond to that SYSFS entry and the probe instance, rather than to the driver globally.


EDIT

This is what I came up with. Rather than using the handy 'ATTRIBUTE_GROUPS' macro (and others) which declare static global variables, I allocated everything:

struct am_attributes {
    void * var;
    struct kobj_attribute attr;
}

static const struct attribute_group **am_attribute_groups(struct am_instance *instance, struct device *dev)
{
    struct am_attribute *const am_ms_delay_attr = devm_kzalloc(dev, sizeof(struct am_attribute), GFP_KERNEL);
    struct am_attribute *const am_enable_attr = devm_kzalloc(dev, sizeof(struct am_attribute), GFP_KERNEL);
    struct attribute **const am_attrs = devm_kzalloc(dev, 3 * sizeof(struct attribute *), GFP_KERNEL);
    struct attribute_group *const am_group = devm_kzalloc(dev, sizeof(struct attribute_group), GFP_KERNEL);
    struct attribute_group **const am_groups = devm_kzalloc(dev, 2 * sizeof(struct attribute_group *), GFP_KERNEL);

    if (am_ms_delay_attr && am_enable_attr && am_attrs && am_group && am_groups)
    {
        struct kobj_attribute ms_pause = __ATTR_RW(autowrap_ms_pause);
        struct kobj_attribute enable = __ATTR_RW(autowrap_enable);

        /* Create the attributes for this probe instance. */
        am_ms_delay_attr->var = &instance->autowrap_ms_delay;
        am_ms_delay_attr->attr = ms_pause;

        am_enable_attr->var = &instance->autowrap_enable;
        am_enable_attr->attr = enable;

        am_attrs[0] = &am_ms_delay_attr->attr.attr;
        am_attrs[1] = &am_enable_attr->attr.attr;
        am_attrs[2] = NULL;

        am_group->attrs = am_attrs;

        am_groups[0] = am_group;
        am_groups[1] = NULL;

        return (const struct attribute_group **)am_groups;
    }
    else
    {
        return NULL;
    }
}

and in my probe function I'm doing:

...

instance->class->dev_groups = am_attribute_groups(instance, dev);

if (instance->class->dev_groups == NULL)
{
    pr_warn("Couldn't allocate SYSFS attributes.");
    ...

Finally, in the show() and store() functions I use the container_of macro to "rewind" the attr parameter to get struct am_attribute pointer and the void * var; pointer. (which I then cast):

#define to_am_attribute(x) container_of(x, struct am_attribute, attr)

static ssize_t autowrap_ms_pause_show(struct kobject *kobj,
                                    struct kobj_attribute *attr,
                                    char *buf)
{
    struct am_attribute *am_attr = to_am_attribute(attr);
    (void)kobj;

    return sprintf(buf, "%d\n", *(int *)(am_attr->var));
}

The question now is: What sort of trouble am I asking for by doing this? (or "is this reasonable?")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文