【问题标题】:Sharing variable between nodes in a device tree在设备树中的节点之间共享变量
【发布时间】:2021-05-31 10:43:00
【问题描述】:

我正在尝试找到一种方法来从 node_1 访问此设备树中 node_0 中的变量(参见下面的代码):

/ {
    amba_pl: amba_pl@0 {
      node_0: node@a8000000 {
         node_id = <0x0>;
         node0State = <0x0>;
      };
   };

      node_1: node@a9000000 {
         node1State = <node_0's node0State>;
      };
   };
};

主要目标是能够在内核模块之间共享状态。我知道我可以在写入节点中 EXPORT_SYMBOL(variable) ,然后在读取节点中使用 extern *variable ,但想看看我是否可以在设备树本身中完成此操作。 node_0 将始终是唯一设置 nodeState 的节点,而 node_1 只会读取。这可能吗?

【问题讨论】:

  • 突然间我知道在 ACPI 中很容易,但在 DT 中可能你可以使用 C 预处理。我不确定在运行时是否可行(ACPI 再次可以做到这一切)。

标签: linux-kernel linux-device-driver kernel-module device-tree petalinux


【解决方案1】:

你可以存储一个指向包含node0state的节点的phandle:

/ {
    amba_pl: amba_pl@0 {
      node_0: node@a8000000 {
         node_id = <0x0>;
         node0State = <0x0>;
      };
   };

      node_1: node@a9000000 {
         stateNode = <&node_0>;
      };
   };
};

在驱动代码中,如果struct device_node *mynode;引用node_1节点,则stateNodephandle属性引用的另一个节点的node0state属性可以如下访问:

    int rc = 0;
    struct device_node *np;
    u32 node0state;

    np = of_parse_phandle(mynode, "stateNode", 0);
    if (!np) {
        // error node not found
        rc = -ENXIO;
        goto error1;
    }
    
    // Do whatever is needed to read "node0state".  Here is an example.
    rc = of_property_read_u32(np, "node0state", &node0state);
    if (rc) {
        // error
        of_node_put(np);  // decrement reference to the node
        goto error2;
    }

    // Finished with the node.
    of_node_put(np);  // decrement reference to the node

【讨论】:

  • 这正是我想要的。那么 stateNode 可以被 node_1 修改吗?此外,node0State 是否可以由 node_0 修改?我一直在使用 of_property_read_u32() 来获取字段值,但看起来 of.h 中没有 of_property_write_u32()
  • @plasmaphase,你不能写 OF——它只是为了获取数据。但您可以使用叠加层对其进行修改。
猜你喜欢
  • 2016-03-01
  • 2015-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-19
  • 1970-01-01
  • 1970-01-01
  • 2019-12-03
相关资源
最近更新 更多