From 021779ba69a493ff3907cf2958a1818a35524df5 Mon Sep 17 00:00:00 2001
From: Blue Swirl <blauwirbel@gmail.com>
Date: Sun, 7 Feb 2010 15:25:42 +0000
Subject: [PATCH] Change REGISTER_NAMED_NODE to return the phandle

Adjust most callers to take benefit from the phandle.

Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
---
 drivers/adb_kbd.c           |    4 +---
 drivers/adb_mouse.c         |    4 +---
 drivers/cuda.c              |    3 +--
 drivers/escc.c              |    6 ++----
 drivers/floppy.c            |    8 ++++----
 drivers/ide.c               |   33 +++++++++++++++++++--------------
 drivers/pc_kbd.c            |   11 +++--------
 drivers/pc_serial.c         |    8 +++-----
 drivers/pci.c               |   34 ++++++++++++++++++----------------
 include/openbios/bindings.h |   11 +++++------
 modules/bindings.c          |    6 +++++-
 11 files changed, 62 insertions(+), 66 deletions(-)

diff --git a/drivers/adb_kbd.c b/drivers/adb_kbd.c
index 063dc50..688a9de 100644
--- a/drivers/adb_kbd.c
+++ b/drivers/adb_kbd.c
@@ -536,9 +536,7 @@ void *adb_kbd_new (char *path, void *private)
     }
 
         snprintf(buf, sizeof(buf), "%s/keyboard", path);
-	REGISTER_NAMED_NODE( keyboard, buf);
-
-	ph = find_dev(buf);
+        ph = REGISTER_NAMED_NODE(keyboard, buf);
 
 	set_property(ph, "device_type", "keyboard", 9);
 	props[0] = __cpu_to_be32(dev->addr);
diff --git a/drivers/adb_mouse.c b/drivers/adb_mouse.c
index abf813b..77639e1 100644
--- a/drivers/adb_mouse.c
+++ b/drivers/adb_mouse.c
@@ -53,9 +53,7 @@ void adb_mouse_new (char *path, void *private)
 	adb_dev_t *dev = private;
 
         snprintf(buf, sizeof(buf), "%s/mouse", path);
-	REGISTER_NAMED_NODE( mouse, buf);
-
-	ph = find_dev(buf);
+        ph = REGISTER_NAMED_NODE(mouse, buf);
 
 	set_property(ph, "device_type", "mouse", 6);
 	props[0] = __cpu_to_be32(dev->addr);
diff --git a/drivers/cuda.c b/drivers/cuda.c
index 509f3af..61ff491 100644
--- a/drivers/cuda.c
+++ b/drivers/cuda.c
@@ -353,9 +353,8 @@ rtc_init(char *path)
 	char buf[64];
 
         snprintf(buf, sizeof(buf), "%s/rtc", path);
-	REGISTER_NAMED_NODE(rtc, buf);
+        ph = REGISTER_NAMED_NODE(rtc, buf);
 
-	ph = find_dev(buf);
 	set_property(ph, "device_type", "rtc", 4);
 	set_property(ph, "compatible", "rtc", 4);
 
diff --git a/drivers/escc.c b/drivers/escc.c
index b4ed72c..ee29d86 100644
--- a/drivers/escc.c
+++ b/drivers/escc.c
@@ -381,9 +381,8 @@ escc_add_channel(const char *path, const char *node, uint32_t addr,
 
     snprintf(buf, sizeof(buf), "%s/ch-%s", path, node);
 
-    REGISTER_NAMED_NODE(escc, buf);
-
-    activate_device(buf);
+    dnode = REGISTER_NAMED_NODE(escc, buf);
+    activate_dev(dnode);
 
     /* add aliases */
 
@@ -397,7 +396,6 @@ escc_add_channel(const char *path, const char *node, uint32_t addr,
 
     /* add properties */
 
-    dnode = find_dev(buf);
     set_property(dnode, "device_type", "serial",
                  strlen("serial") + 1);
 
diff --git a/drivers/floppy.c b/drivers/floppy.c
index 0091a13..1357caf 100644
--- a/drivers/floppy.c
+++ b/drivers/floppy.c
@@ -1071,10 +1071,9 @@ static void floppy_reset(void)
 }
 
 static void
-ob_floppy_initialize(const char *path)
+ob_floppy_initialize(phandle_t ph)
 {
         int props[3];
-        phandle_t ph = find_dev(path);
 
         set_property(ph, "device_type", "block", sizeof("block"));
 
@@ -1153,11 +1152,12 @@ int ob_floppy_init(const char *path, const char *dev_name,
                    unsigned long io_base, unsigned long mmio_base)
 {
         char nodebuff[128];
+        phandle_t ph;
 
         snprintf(nodebuff, sizeof(nodebuff), "%s/%s", path, dev_name);
         if (!mmio_base) {
-            REGISTER_NAMED_NODE(ob_floppy, nodebuff);
-            ob_floppy_initialize(nodebuff);
+            ph = REGISTER_NAMED_NODE(ob_floppy, nodebuff);
+            ob_floppy_initialize(ph);
         } else {
             // Already in tree and mapped
             REGISTER_NODE_METHODS(ob_floppy, nodebuff);
diff --git a/drivers/ide.c b/drivers/ide.c
index 3636b3c..4760ab1 100644
--- a/drivers/ide.c
+++ b/drivers/ide.c
@@ -1363,6 +1363,7 @@ int ob_ide_init(const char *path, uint32_t io_port0, uint32_t ctl_port0,
 	int io_ports[IDE_MAX_CHANNELS];
 	int ctl_ports[IDE_MAX_CHANNELS];
 	cell props[6];
+        const char *parent_path;
 
 	io_ports[0] = io_port0;
 	ctl_ports[0] = ctl_port0 + 2;
@@ -1417,9 +1418,7 @@ int ob_ide_init(const char *path, uint32_t io_port0, uint32_t ctl_port0,
 
                 snprintf(nodebuff, sizeof(nodebuff), "%s/" DEV_NAME, path,
                          current_channel);
-		REGISTER_NAMED_NODE(ob_ide_ctrl, nodebuff);
-
-		dnode = find_dev(nodebuff);
+                dnode = REGISTER_NAMED_NODE(ob_ide_ctrl, nodebuff);
 
 		props[0]=14; props[1]=0;
 		set_property(dnode, "interrupts",
@@ -1431,6 +1430,10 @@ int ob_ide_init(const char *path, uint32_t io_port0, uint32_t ctl_port0,
 		props[4] = __cpu_to_be32(1); props[5] = __cpu_to_be32(2);
 		set_property(dnode, "reg", (char *)&props, 6*sizeof(cell));
 
+                PUSH_ph(dnode);
+                fword("get-package-path");
+                parent_path = pop_fstr_copy();
+
 		IDE_DPRINTF(DEV_NAME": [io ports 0x%x-0x%x,0x%x]\n",
 		            current_channel, chan->io_regs[0],
 		            chan->io_regs[0] + 7, chan->io_regs[8]);
@@ -1460,10 +1463,9 @@ int ob_ide_init(const char *path, uint32_t io_port0, uint32_t ctl_port0,
 			}
 			IDE_DPRINTF("%s]: %s\n", media, drive->model);
                         snprintf(nodebuff, sizeof(nodebuff),
-                                 "%s/" DEV_NAME "/%s", path, current_channel,
-                                 media);
-			REGISTER_NAMED_NODE(ob_ide, nodebuff);
-			dnode=find_dev(nodebuff);
+                                 "%s/%s", parent_path, media);
+                        dnode = REGISTER_NAMED_NODE(ob_ide, nodebuff);
+
 			set_int_property(dnode, "reg", j);
 
 			/* create aliases */
@@ -1516,6 +1518,7 @@ int macio_ide_init(const char *path, uint32_t addr, int nb_channels)
 	phandle_t dnode;
 	cell props[8];
 	struct ide_channel *chan;
+        const char *parent_path;
 
 	for (i = 0; i < nb_channels; i++, current_channel++) {
 
@@ -1561,9 +1564,7 @@ int macio_ide_init(const char *path, uint32_t addr, int nb_channels)
 
                 snprintf(nodebuff, sizeof(nodebuff), "%s/" DEV_NAME, path,
                          current_channel);
-		REGISTER_NAMED_NODE(ob_ide_ctrl, nodebuff);
-
-		dnode = find_dev(nodebuff);
+                dnode = REGISTER_NAMED_NODE(ob_ide_ctrl, nodebuff);
 
 		set_property(dnode, "compatible", "heathrow-ata", 13);
 
@@ -1599,6 +1600,11 @@ int macio_ide_init(const char *path, uint32_t addr, int nb_channels)
 		props[0] = 0;
 		OLDWORLD(set_property(dnode, "AAPL,bus-id", (char*)props,
 			 1 * sizeof(cell)));
+
+                PUSH_ph(dnode);
+                fword("get-package-path");
+                parent_path = pop_fstr_copy();
+
 		IDE_DPRINTF(DEV_NAME": [io ports 0x%lx]\n",
 		            current_channel, chan->mmio);
 
@@ -1627,10 +1633,9 @@ int macio_ide_init(const char *path, uint32_t addr, int nb_channels)
 			}
 			IDE_DPRINTF("%s]: %s\n", media, drive->model);
                         snprintf(nodebuff, sizeof(nodebuff),
-                                 "%s/" DEV_NAME "/%s", path, current_channel,
-                                 media);
-			REGISTER_NAMED_NODE(ob_ide, nodebuff);
-			dnode = find_dev(nodebuff);
+                                 "%s/%s", parent_path, media);
+                        dnode = REGISTER_NAMED_NODE(ob_ide, nodebuff);
+
 			set_int_property(dnode, "reg", j);
 
 			/* create aliases */
diff --git a/drivers/pc_kbd.c b/drivers/pc_kbd.c
index 249da89..6c75e75 100644
--- a/drivers/pc_kbd.c
+++ b/drivers/pc_kbd.c
@@ -189,17 +189,12 @@ void
 ob_pc_kbd_init(const char *path, const char *dev_name, uint64_t base,
                uint64_t offset, int intr)
 {
-    phandle_t chosen, aliases;
+    phandle_t chosen, aliases, dnode;
     char nodebuff[128];
 
     snprintf(nodebuff, sizeof(nodebuff), "%s/%s", path, dev_name);
-    REGISTER_NAMED_NODE(pc_kbd, nodebuff);
-
-    push_str(nodebuff);
-    fword("find-device");
-
-    push_str(dev_name);
-    fword("device-name");
+    dnode = REGISTER_NAMED_NODE(pc_kbd, nodebuff);
+    activate_dev(dnode);
 
     push_str("keyboard");
     fword("device-type");
diff --git a/drivers/pc_serial.c b/drivers/pc_serial.c
index cb82855..30aca38 100644
--- a/drivers/pc_serial.c
+++ b/drivers/pc_serial.c
@@ -171,14 +171,12 @@ void
 ob_pc_serial_init(const char *path, const char *dev_name, uint64_t base,
                   uint64_t offset, int intr)
 {
-    phandle_t aliases;
+    phandle_t aliases, dnode;
     char nodebuff[128];
 
     snprintf(nodebuff, sizeof(nodebuff), "%s/%s", path, dev_name);
-    REGISTER_NAMED_NODE(pc_serial, nodebuff);
-
-    push_str(nodebuff);
-    fword("find-device");
+    dnode = REGISTER_NAMED_NODE(pc_serial, nodebuff);
+    activate_dev(dnode);
 
     push_str("serial");
     fword("device-type");
diff --git a/drivers/pci.c b/drivers/pci.c
index 7d681f1..fefb1d8 100644
--- a/drivers/pci.c
+++ b/drivers/pci.c
@@ -474,9 +474,8 @@ static inline void pci_decode_pci_addr(pci_addr addr, int *flags,
  *  Nth entry corresponding to Nth "assigned-address" base address entry.
  */
 
-static void pci_set_AAPL_address(const pci_config_t *config)
+static void pci_set_AAPL_address(const pci_config_t *config, phandle_t dev)
 {
-	phandle_t dev = get_cur_dev();
 	cell props[7];
 	int ncells, i;
 
@@ -491,9 +490,9 @@ static void pci_set_AAPL_address(const pci_config_t *config)
 			     ncells * sizeof(cell));
 }
 
-static void pci_set_assigned_addresses(const pci_config_t *config, int num_bars)
+static void pci_set_assigned_addresses(const pci_config_t *config, int num_bars,
+                                       phandle_t dev)
 {
-	phandle_t dev = get_cur_dev();
 	u32 props[32];
 	int ncells;
 	int i;
@@ -521,9 +520,9 @@ static void pci_set_assigned_addresses(const pci_config_t *config, int num_bars)
 			     ncells * sizeof(props[0]));
 }
 
-static void pci_set_reg(const pci_config_t *config, int num_bars)
+static void pci_set_reg(const pci_config_t *config, int num_bars,
+                        phandle_t dev)
 {
-	phandle_t dev = get_cur_dev();
 	u32 props[38];
 	int ncells;
 	int i;
@@ -643,9 +642,9 @@ int ebus_config_cb(const pci_config_t *config)
 }
 
 static void ob_pci_add_properties(pci_addr addr, const pci_dev_t *pci_dev,
-                                  const pci_config_t *config, int num_bars)
+                                  const pci_config_t *config, int num_bars,
+                                  phandle_t dev)
 {
-	phandle_t dev=get_cur_dev();
 	int status,id;
 	uint16_t vendor_id, device_id;
 	uint8_t rev;
@@ -722,9 +721,9 @@ static void ob_pci_add_properties(pci_addr addr, const pci_dev_t *pci_dev,
 					      pci_dev->icells);
 	}
 
-	pci_set_reg(config, num_bars);
-	pci_set_assigned_addresses(config, num_bars);
-	OLDWORLD(pci_set_AAPL_address(config));
+        pci_set_reg(config, num_bars, dev);
+        pci_set_assigned_addresses(config, num_bars, dev);
+        OLDWORLD(pci_set_AAPL_address(config, dev));
 
 #ifdef CONFIG_DEBUG_PCI
 	printk("\n");
@@ -888,6 +887,7 @@ static void ob_scan_pci_bus(int bus, unsigned long *mem_base,
 	uint32_t ccode;
 	uint8_t class, subclass, iface, rev;
 	int num_bars, rom_bar;
+        phandle_t ph;
 
 	activate_device("/");
 	for (devnum = 0; devnum < 32; devnum++) {
@@ -935,11 +935,12 @@ static void ob_scan_pci_bus(int bus, unsigned long *mem_base,
                         if (class == PCI_BASE_CLASS_BRIDGE &&
                             (subclass == PCI_SUBCLASS_BRIDGE_HOST ||
                              subclass == PCI_SUBCLASS_BRIDGE_PCI))
-                            REGISTER_NAMED_NODE(ob_pci_bus_node, config.path);
+                            ph = REGISTER_NAMED_NODE(ob_pci_bus_node,
+                                                     config.path);
                         else
-                            REGISTER_NAMED_NODE(ob_pci_simple_node, config.path);
-
-			activate_device(config.path);
+                            ph = REGISTER_NAMED_NODE(ob_pci_simple_node,
+                                                     config.path);
+                        activate_dev(ph);
 
 			if (htype & PCI_HEADER_TYPE_BRIDGE) {
 				num_bars = 2;
@@ -951,7 +952,8 @@ static void ob_scan_pci_bus(int bus, unsigned long *mem_base,
 
 			ob_pci_configure(addr, &config, num_bars, rom_bar,
                                          mem_base, io_base);
-			ob_pci_add_properties(addr, pci_dev, &config, num_bars);
+                        ob_pci_add_properties(addr, pci_dev, &config, num_bars,
+                                              ph);
 
                         if (class == PCI_BASE_CLASS_BRIDGE &&
                             (subclass == PCI_SUBCLASS_BRIDGE_HOST ||
diff --git a/include/openbios/bindings.h b/include/openbios/bindings.h
index 606d93a..45ad9f5 100644
--- a/include/openbios/bindings.h
+++ b/include/openbios/bindings.h
@@ -107,10 +107,9 @@ typedef struct {
 	void		*func;
 } method_t;
 
-#define REGISTER_NAMED_NODE( name, path )   do { \
-	bind_new_node( name##_flags_, name##_size_, \
-		path, name##_m, sizeof(name##_m)/sizeof(method_t)); \
-	} while(0)
+#define REGISTER_NAMED_NODE(name, path)                                 \
+    bind_new_node(name##_flags_, name##_size_, path, name##_m,          \
+                  sizeof(name##_m) / sizeof(method_t))
 
 #define REGISTER_NODE_METHODS( name, path )   do {			\
 	const char *paths[1];						\
@@ -140,8 +139,8 @@ static const method_t name##_m[]
 extern void 	bind_node( int flags, int size, const char * const *paths, int npaths,
 			   const method_t *methods, int nmethods );
 
-extern void 	bind_new_node( int flags, int size, const char *name,
-			   const method_t *methods, int nmethods );
+phandle_t bind_new_node(int flags, int size, const char *name,
+                        const method_t *methods, int nmethods);
 
 #define INSTALL_OPEN	1	/* install trivial open and close methods */
 
diff --git a/modules/bindings.c b/modules/bindings.c
index 8c3f76e..6882b85 100644
--- a/modules/bindings.c
+++ b/modules/bindings.c
@@ -507,16 +507,20 @@ bind_node( int flags, int size, const char * const *paths, int npaths,
 	activate_dev( save_ph );
 }
 
-void
+phandle_t
 bind_new_node( int flags, int size, const char *name,
 	   const method_t *methods, int nmet )
 {
 	phandle_t save_ph = get_cur_dev();
+        phandle_t ret;
 
 	/* create node */
 	push_str( name );
 	fword("create-node");
+        ret = get_cur_dev();
 	add_methods( flags, size, methods, nmet );
 
 	activate_dev( save_ph );
+
+        return ret;
 }
-- 
1.5.6.5

