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] param: Add dev_add_param_mac
Date: Fri,  4 Apr 2014 09:06:56 +0200	[thread overview]
Message-ID: <1396595216-15226-1-git-send-email-s.hauer@pengutronix.de> (raw)

This adds a convenience function to register a MAC address device
parameter. The only current user is converted to use it.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 include/param.h | 13 +++++++++
 lib/parameter.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 net/eth.c       | 14 ++--------
 3 files changed, 103 insertions(+), 11 deletions(-)

diff --git a/include/param.h b/include/param.h
index e8458b5..eac3372 100644
--- a/include/param.h
+++ b/include/param.h
@@ -54,6 +54,11 @@ struct param_d *dev_add_param_ip(struct device_d *dev, const char *name,
 		int (*get)(struct param_d *p, void *priv),
 		IPaddr_t *ip, void *priv);
 
+struct param_d *dev_add_param_mac(struct device_d *dev, const char *name,
+		int (*set)(struct param_d *p, void *priv),
+		int (*get)(struct param_d *p, void *priv),
+		u8 *mac, void *priv);
+
 int dev_add_param_fixed(struct device_d *dev, char *name, const char *value);
 
 void dev_remove_param(struct param_d *p);
@@ -126,6 +131,14 @@ static inline struct param_d *dev_add_param_ip(struct device_d *dev, const char
 	return NULL;
 }
 
+static inline struct param_d *dev_add_param_mac(struct device_d *dev, const char *name,
+		int (*set)(struct param_d *p, void *priv),
+		int (*get)(struct param_d *p, void *priv),
+		u8 *mac, void *priv)
+{
+	return NULL;
+}
+
 static inline int dev_add_param_fixed(struct device_d *dev, char *name, char *value)
 {
 	return 0;
diff --git a/lib/parameter.c b/lib/parameter.c
index 9dfde3f..baa2b15 100644
--- a/lib/parameter.c
+++ b/lib/parameter.c
@@ -542,6 +542,93 @@ struct param_d *dev_add_param_ip(struct device_d *dev, const char *name,
 
 	return &pi->param;
 }
+
+struct param_mac {
+	struct param_d param;
+	char *mac;
+	u8 mac_str[sizeof("xx:xx:xx:xx:xx:xx")];
+	const char *format;
+	int (*set)(struct param_d *p, void *priv);
+	int (*get)(struct param_d *p, void *priv);
+};
+
+int string_to_ethaddr(const char *str, u8 enetaddr[6]);
+void ethaddr_to_string(const u8 enetaddr[6], char *str);
+
+static inline struct param_mac *to_param_mac(struct param_d *p)
+{
+	return container_of(p, struct param_mac, param);
+}
+
+static int param_mac_set(struct device_d *dev, struct param_d *p, const char *val)
+{
+	struct param_mac *pm = to_param_mac(p);
+	char mac_save[6];
+	int ret;
+
+	if (!val)
+		return -EINVAL;
+
+	memcpy(mac_save, pm->mac, 6);
+
+	ret = string_to_ethaddr(val, pm->mac);
+	if (ret)
+		goto out;
+
+	if (!pm->set)
+		return 0;
+
+	ret = pm->set(p, p->driver_priv);
+	if (ret)
+		goto out;
+
+	return 0;
+out:
+	memcpy(pm->mac, mac_save, 6);
+
+	return ret;
+}
+
+static const char *param_mac_get(struct device_d *dev, struct param_d *p)
+{
+	struct param_mac *pm = to_param_mac(p);
+	int ret;
+
+	if (pm->get) {
+		ret = pm->get(p, p->driver_priv);
+		if (ret)
+			return NULL;
+	}
+
+	ethaddr_to_string(pm->mac, p->value);
+
+	return p->value;
+}
+
+struct param_d *dev_add_param_mac(struct device_d *dev, const char *name,
+		int (*set)(struct param_d *p, void *priv),
+		int (*get)(struct param_d *p, void *priv),
+		u8 *mac, void *priv)
+{
+	struct param_mac *pm;
+	int ret;
+
+	pm = xzalloc(sizeof(*pm));
+	pm->mac = mac;
+	pm->set = set;
+	pm->get = get;
+	pm->param.driver_priv = priv;
+	pm->param.value = pm->mac_str;
+
+	ret = __dev_add_param(&pm->param, dev, name,
+			param_mac_set, param_mac_get, 0);
+	if (ret) {
+		free(pm);
+		return ERR_PTR(ret);
+	}
+
+	return &pm->param;
+}
 #endif
 
 /**
diff --git a/net/eth.c b/net/eth.c
index 1f48f2d..3ced3cd 100644
--- a/net/eth.c
+++ b/net/eth.c
@@ -257,17 +257,9 @@ int eth_rx(void)
 	return eth_current->recv(eth_current);
 }
 
-static int eth_set_ethaddr(struct device_d *dev, struct param_d *param, const char *val)
+static int eth_set_ethaddr(struct param_d *param, void *priv)
 {
-	struct eth_device *edev = dev_to_edev(dev);
-
-	if (!val)
-		return dev_param_set_generic(dev, param, NULL);
-
-	if (string_to_ethaddr(val, edev->ethaddr) < 0)
-		return -EINVAL;
-
-	dev_param_set_generic(dev, param, val);
+	struct eth_device *edev = priv;
 
 	edev->set_ethaddr(edev, edev->ethaddr);
 
@@ -345,7 +337,7 @@ int eth_register(struct eth_device *edev)
 	dev_add_param_ip(dev, "serverip", NULL, NULL, &edev->serverip, edev);
 	dev_add_param_ip(dev, "gateway", NULL, NULL, &edev->gateway, edev);
 	dev_add_param_ip(dev, "netmask", NULL, NULL, &edev->netmask, edev);
-	dev_add_param(dev, "ethaddr", eth_set_ethaddr, NULL, 0);
+	dev_add_param_mac(dev, "ethaddr", eth_set_ethaddr, NULL, edev->ethaddr, edev);
 
 	if (edev->init)
 		edev->init(edev);
-- 
1.9.1


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

                 reply	other threads:[~2014-04-04  7:07 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=1396595216-15226-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