You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by an...@apache.org on 2018/11/23 16:16:53 UTC

[mynewt-core] 21/26: hw/bus: Add init argument to node creation function

This is an automated email from the ASF dual-hosted git repository.

andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git

commit 79e89e6c9f675c3862fc3ab8b86c43fc787c3256
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Mon Nov 19 11:40:44 2018 +0100

    hw/bus: Add init argument to node creation function
    
    Since bus nodes are os_dev, they may need some extra argument to be
    passed in init function. However, os_dev custom argument is already
    used by bus driver thus we need other way to pass driver-specific arg.
    
    To make this possible, bus_XXX_node_create functions have extra argument
    which is stored inside node and passed to init function later.
    
    Hack warning: to avoid using extra 4 bytes of memory for each node to
    store extra arg for a brief time we'll just reuse the same pointer (via
    union) as used by parent_bus. It's ok to make it like this since we need
    to store arg until init function is called and also parent_bus is used
    1st time in init function so we can just "exchange" contents there.
---
 hw/bus/i2c/include/bus/i2c.h    |  5 ++++-
 hw/bus/include/bus/bus_driver.h | 10 +++++++++-
 hw/bus/spi/include/bus/spi.h    |  5 ++++-
 hw/bus/src/bus.c                |  5 ++++-
 4 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/hw/bus/i2c/include/bus/i2c.h b/hw/bus/i2c/include/bus/i2c.h
index 47050f2..d7a3530 100644
--- a/hw/bus/i2c/include/bus/i2c.h
+++ b/hw/bus/i2c/include/bus/i2c.h
@@ -147,10 +147,13 @@ bus_i2c_node_init_func(struct os_dev *odev, void *arg);
  */
 static inline int
 bus_i2c_node_create(const char *name, struct bus_i2c_node *node,
-                    const struct bus_i2c_node_cfg *cfg)
+                    const struct bus_i2c_node_cfg *cfg, void *arg)
 {
+    struct bus_node *bnode = (struct bus_node *)node;
     struct os_dev *odev = (struct os_dev *)node;
 
+    bnode->init_arg = arg;
+
     return os_dev_create(odev, name, OS_DEV_INIT_PRIMARY, 1,
                          bus_i2c_node_init_func, (void *)cfg);
 }
diff --git a/hw/bus/include/bus/bus_driver.h b/hw/bus/include/bus/bus_driver.h
index d3b8302..df6f8aa 100644
--- a/hw/bus/include/bus/bus_driver.h
+++ b/hw/bus/include/bus/bus_driver.h
@@ -102,7 +102,15 @@ struct bus_dev {
 struct bus_node {
     struct os_dev odev;
     struct bus_node_callbacks callbacks;
-    struct bus_dev *parent_bus;
+
+    /*
+     * init_arg is valid until os_dev is initialized (bus_node_init_func called)
+     * parent_bus is valid afterwards
+     */
+    union {
+        struct bus_dev *parent_bus;
+        void *init_arg;
+    };
 
 #if MYNEWT_VAL(BUS_DEBUG)
     uint32_t nodemagic;
diff --git a/hw/bus/spi/include/bus/spi.h b/hw/bus/spi/include/bus/spi.h
index 941bf98..da8d1ed 100644
--- a/hw/bus/spi/include/bus/spi.h
+++ b/hw/bus/spi/include/bus/spi.h
@@ -101,10 +101,13 @@ bus_spi_node_init_func(struct os_dev *odev, void *arg);
 
 static inline int
 bus_spi_node_create(const char *name, struct bus_spi_node *node,
-                    const struct bus_spi_node_cfg *cfg)
+                    const struct bus_spi_node_cfg *cfg, void *arg)
 {
+    struct bus_node *bnode = (struct bus_node *)node;
     struct os_dev *odev = (struct os_dev *)node;
 
+    bnode->init_arg = arg;
+
     return os_dev_create(odev, name, OS_DEV_INIT_PRIMARY, 1,
                          bus_spi_node_init_func, (void *)cfg);
 }
diff --git a/hw/bus/src/bus.c b/hw/bus/src/bus.c
index 6176c41..e8d0a9f 100644
--- a/hw/bus/src/bus.c
+++ b/hw/bus/src/bus.c
@@ -128,6 +128,7 @@ bus_node_init_func(struct os_dev *odev, void *arg)
     struct bus_node *bnode = (struct bus_node *)odev;
     struct bus_node_cfg *node_cfg = arg;
     struct os_dev *parent_odev;
+    void *init_arg;
 
     parent_odev = os_dev_lookup(node_cfg->bus_name);
     if (!parent_odev) {
@@ -136,13 +137,15 @@ bus_node_init_func(struct os_dev *odev, void *arg)
 
     BUS_DEBUG_POISON_NODE(bnode);
 
+    /* We need to save init_arg here since it will be overwritten by parent_bus */
+    init_arg = bnode->init_arg;
     bnode->parent_bus = (struct bus_dev *)parent_odev;
 
     odev->od_handlers.od_open = bus_node_open_func;
     odev->od_handlers.od_close = bus_node_close_func;
 
     if (bnode->callbacks.init) {
-        bnode->callbacks.init(bnode, arg);
+        bnode->callbacks.init(bnode, init_arg);
     }
 
     return 0;