mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH] net: Set mac-address property
Date: Tue,  4 Jun 2013 09:47:17 +0200	[thread overview]
Message-ID: <1370332037-10575-1-git-send-email-s.hauer@pengutronix.de> (raw)

Linux normally has no idea how to retrieve MAC Addresses, but instead
expects the MAC address in the devicetree. This patch adds the MAC
address to the devicetree for Linux if we find a valid one in barebox.
This mechanism is limited of course to devices barebox has a driver for
and which are probed themselves from the devicetree.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 include/net.h |  1 +
 net/eth.c     | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/include/net.h b/include/net.h
index fa3c05a..a4cfec7 100644
--- a/include/net.h
+++ b/include/net.h
@@ -52,6 +52,7 @@ struct eth_device {
 
 	struct device_d dev;
 	struct device_d *parent;
+	char *nodepath;
 
 	struct list_head list;
 
diff --git a/net/eth.c b/net/eth.c
index bea7b12..7240b4d 100644
--- a/net/eth.c
+++ b/net/eth.c
@@ -273,6 +273,52 @@ static int eth_set_ethaddr(struct device_d *dev, struct param_d *param, const ch
 	return 0;
 }
 
+#ifdef CONFIG_OFDEVICE
+static int eth_of_fixup(struct device_node *root)
+{
+	struct eth_device *edev;
+	struct device_node *node;
+	int ret;
+
+	/*
+	 * Add the mac-address property for each network device we
+	 * find a nodepath for and which has a valid mac address.
+	 */
+	list_for_each_entry(edev, &netdev_list, list) {
+		if (!edev->nodepath) {
+			dev_dbg(&edev->dev, "%s: no node to fixup\n", __func__);
+			continue;
+		}
+
+		if (!is_valid_ether_addr(edev->ethaddr)) {
+			dev_dbg(&edev->dev, "%s: no valid mac address, cannot fixup\n",
+					__func__);
+			continue;
+		}
+
+		node = of_find_node_by_path(root, edev->nodepath);
+		if (!node) {
+			dev_dbg(&edev->dev, "%s: fixup node %s not found\n",
+					__func__, edev->nodepath);
+			continue;
+		}
+
+		ret = of_set_property(node, "mac-address", edev->ethaddr, 6, 1);
+		if (ret)
+			pr_err("Setting mac-address property of %s failed with: %s\n",
+					node->full_name, strerror(-ret));
+	}
+
+	return 0;
+}
+
+static int eth_register_of_fixup(void)
+{
+	return of_register_fixup(eth_of_fixup);
+}
+late_initcall(eth_register_of_fixup);
+#endif
+
 int eth_register(struct eth_device *edev)
 {
 	struct device_d *dev = &edev->dev;
@@ -316,6 +362,10 @@ int eth_register(struct eth_device *edev)
 	if (found)
 		register_preset_mac_address(edev, ethaddr);
 
+	if (IS_ENABLED(CONFIG_OFDEVICE) && edev->parent &&
+			edev->parent->device_node)
+		edev->nodepath = xstrdup(edev->parent->device_node->full_name);
+
 	if (!eth_current)
 		eth_current = edev;
 
@@ -328,6 +378,10 @@ void eth_unregister(struct eth_device *edev)
 		eth_current = NULL;
 
 	dev_remove_parameters(&edev->dev);
+
+	if (IS_ENABLED(CONFIG_OFDEVICE) && edev->nodepath)
+		free(edev->nodepath);
+
 	unregister_device(&edev->dev);
 	list_del(&edev->list);
 }
-- 
1.8.2.rc2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

                 reply	other threads:[~2013-06-04  7:47 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1370332037-10575-1-git-send-email-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox