在设备树中的节点之间共享变量

时间:2021-03-01 21:40:16

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

我正在尝试找到一种方法来从 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 将只读取。这可能吗?

1 个答案:

答案 0 :(得分:0)

您可以存储一个引用包含 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 节点,那么 node0state phandle 属性引用的另一个节点的 stateNode 属性可以访问为如下:

    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
相关问题