From: Sascha Hauer <s.hauer@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 03/21] param: Add integer and boolean parameter helpers
Date: Sun, 7 Apr 2013 16:00:37 +0200 [thread overview]
Message-ID: <1365343255-26497-4-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1365343255-26497-1-git-send-email-s.hauer@pengutronix.de>
This adds convenience functions for directly registering integers
and bools as device parameter. This way driver no longer have to
fiddle with string handling. The format used to print the parameter
is passed to the functions to be able to print parameters in a
flexible way.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
include/param.h | 37 +++++++++++++
lib/parameter.c | 161 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 198 insertions(+)
diff --git a/include/param.h b/include/param.h
index 2a1b4fa..ea22215 100644
--- a/include/param.h
+++ b/include/param.h
@@ -16,6 +16,7 @@ struct param_d {
char *name;
char *value;
struct device_d *dev;
+ void *driver_priv;
struct list_head list;
};
@@ -29,6 +30,19 @@ int dev_add_param(struct device_d *dev, const char *name,
const char *(*get)(struct device_d *, struct param_d *p),
unsigned long flags);
+struct param_d *dev_add_param_int(struct device_d *dev, const char *name,
+ int (*set)(struct param_d *p, void *priv),
+ int (*get)(struct param_d *p, void *priv),
+ int *value, const char *format, void *priv);
+
+struct param_d *dev_add_param_bool(struct device_d *dev, const char *name,
+ int (*set)(struct param_d *p, void *priv),
+ int (*get)(struct param_d *p, void *priv),
+ int *value, void *priv);
+
+struct param_d *dev_add_param_int_ro(struct device_d *dev, const char *name,
+ int value, const char *format);
+
int dev_add_param_fixed(struct device_d *dev, char *name, char *value);
void dev_remove_param(struct device_d *dev, char *name);
@@ -41,6 +55,7 @@ int dev_param_set_generic(struct device_d *dev, struct param_d *p,
/* Convenience functions to handle a parameter as an ip address */
int dev_set_param_ip(struct device_d *dev, char *name, IPaddr_t ip);
IPaddr_t dev_get_param_ip(struct device_d *dev, char *name);
+
#else
static inline const char *dev_get_param(struct device_d *dev, const char *name)
{
@@ -65,6 +80,28 @@ static inline int dev_add_param(struct device_d *dev, char *name,
return 0;
}
+static inline struct param_d *dev_add_param_int(struct device_d *dev, const char *name,
+ int (*set)(struct param_d *p, void *priv),
+ int (*get)(struct param_d *p, void *priv),
+ int *value, const char *format, void *priv)
+{
+ return NULL;
+}
+
+struct param_d *dev_add_param_bool(struct device_d *dev, const char *name,
+ int (*set)(struct param_d *p, void *priv),
+ int (*get)(struct param_d *p, void *priv),
+ int *value, void *priv)
+{
+ return NULL;
+}
+
+static inline struct param_d *dev_add_param_int_ro(struct device_d *dev, const char *name,
+ int value, const char *format);
+{
+ 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 70f035a..3e78c96 100644
--- a/lib/parameter.c
+++ b/lib/parameter.c
@@ -27,6 +27,7 @@
#include <net.h>
#include <malloc.h>
#include <driver.h>
+#include <linux/err.h>
struct param_d *get_param_by_name(struct device_d *dev, const char *name)
{
@@ -218,6 +219,166 @@ int dev_add_param_fixed(struct device_d *dev, char *name, char *value)
return 0;
}
+struct param_int {
+ struct param_d param;
+ int *value;
+ const char *format;
+#define PARAM_INT_FLAG_BOOL (1 << 0)
+ unsigned flags;
+ int (*set)(struct param_d *p, void *priv);
+ int (*get)(struct param_d *p, void *priv);
+};
+
+static inline struct param_int *to_param_int(struct param_d *p)
+{
+ return container_of(p, struct param_int, param);
+}
+
+static int param_int_set(struct device_d *dev, struct param_d *p, const char *val)
+{
+ struct param_int *pi = to_param_int(p);
+ int value_save = *pi->value;
+ int ret;
+
+ if (!val)
+ return -EINVAL;
+
+ *pi->value = simple_strtol(val, NULL, 0);
+ if (pi->flags & PARAM_INT_FLAG_BOOL)
+ *pi->value = !!*pi->value;
+
+ if (pi->set) {
+ ret = pi->set(p, p->driver_priv);
+ if (ret) {
+ *pi->value = value_save;
+ return ret;
+ }
+ }
+
+ return 0;
+}
+
+static const char *param_int_get(struct device_d *dev, struct param_d *p)
+{
+ struct param_int *pi = to_param_int(p);
+ int ret;
+
+ if (pi->get) {
+ ret = pi->get(p, p->driver_priv);
+ if (ret)
+ return NULL;
+ }
+
+ free(p->value);
+ p->value = asprintf(pi->format, *pi->value);
+
+ return p->value;
+}
+
+/**
+ * dev_add_param_int - add an integer parameter to a device
+ * @param dev The device
+ * @param name The name of the parameter
+ * @param set set function
+ * @param get get function
+ * @param value pointer to the integer containing the value of the parameter
+ * @param format the printf format used to print the value
+ * @param priv user private data, will be passed to get/set
+ *
+ * The get function can be used as a notifier when the variable is about
+ * to be read.
+ * The set function can be used as a notifer when the variable is about
+ * to be written. Can also be used to limit the value.
+ */
+struct param_d *dev_add_param_int(struct device_d *dev, const char *name,
+ int (*set)(struct param_d *p, void *priv),
+ int (*get)(struct param_d *p, void *priv),
+ int *value, const char *format, void *priv)
+{
+ struct param_int *pi;
+ struct param_d *p;
+ int ret;
+
+ pi = xzalloc(sizeof(*pi));
+ pi->value = value;
+ pi->format = format;
+ pi->set = set;
+ pi->get = get;
+ p = &pi->param;
+ p->driver_priv = priv;
+
+ ret = __dev_add_param(p, dev, name, param_int_set, param_int_get, 0);
+ if (ret) {
+ free(pi);
+ return ERR_PTR(ret);
+ }
+
+ return &pi->param;
+}
+
+/**
+ * dev_add_param_bool - add an boolean parameter to a device
+ * @param dev The device
+ * @param name The name of the parameter
+ * @param set set function
+ * @param get get function
+ * @param value pointer to the integer containing the value of the parameter
+ * @param priv user private data, will be passed to get/set
+ *
+ * The get function can be used as a notifier when the variable is about
+ * to be read.
+ * The set function can be used as a notifer when the variable is about
+ * to be written. Can also be used to limit the value.
+ */
+struct param_d *dev_add_param_bool(struct device_d *dev, const char *name,
+ int (*set)(struct param_d *p, void *priv),
+ int (*get)(struct param_d *p, void *priv),
+ int *value, void *priv)
+{
+ struct param_int *pi;
+ struct param_d *p;
+
+ p = dev_add_param_int(dev, name, set, get, value, "%d", priv);
+ if (IS_ERR(p))
+ return p;
+
+ pi = to_param_int(p);
+ pi->flags |= PARAM_INT_FLAG_BOOL;
+
+ return p;
+}
+
+struct param_int_ro {
+ struct param_d param;
+ char *value;
+};
+
+/**
+ * dev_add_param_int_ro - add a read only integer parameter to a device
+ * @param dev The device
+ * @param name The name of the parameter
+ * @param value The value of the parameter
+ * @param format the printf format used to print the value
+ */
+struct param_d *dev_add_param_int_ro(struct device_d *dev, const char *name,
+ int value, const char *format)
+{
+ struct param_int *piro;
+ int ret;
+
+ piro = xzalloc(sizeof(*piro));
+
+ ret = __dev_add_param(&piro->param, dev, name, NULL, NULL, 0);
+ if (ret) {
+ free(piro);
+ return ERR_PTR(ret);
+ }
+
+ piro->param.value = asprintf(format, value);
+
+ return &piro->param;
+}
+
/**
* dev_remove_param - remove a parameter from a device and free its
* memory
--
1.8.2.rc2
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next prev parent reply other threads:[~2013-04-07 14:01 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-04-07 14:00 [PATCH] Add device parameter convenience helpers Sascha Hauer
2013-04-07 14:00 ` [PATCH 01/21] param: Add dev member to struct param_d Sascha Hauer
2013-04-07 14:00 ` [PATCH 02/21] param: refactor __dev_add_param Sascha Hauer
2013-04-07 14:00 ` Sascha Hauer [this message]
2013-04-07 14:00 ` [PATCH 04/21] param: Add ip address convenience function Sascha Hauer
2013-04-07 14:00 ` [PATCH 05/21] net: ksz8864: Use dev_add_param_bool for enable parameter Sascha Hauer
2013-04-07 14:00 ` [PATCH 06/21] net: store ethernet device parameters in device Sascha Hauer
2013-04-07 14:00 ` [PATCH 07/21] netconsole: use dev_add_param_* helpers Sascha Hauer
2013-04-07 14:00 ` [PATCH 08/21] param: remove now unused dev_[gs]et_param_ip Sascha Hauer
2013-04-07 14:00 ` [PATCH 09/21] treewide: Use dev_add_param_int_ro where possible Sascha Hauer
2013-04-07 14:00 ` [PATCH 10/21] mci: Use dev_add_param_int for probe parameter Sascha Hauer
2013-04-07 14:00 ` [PATCH 11/21] ata: Use dev_add_param_bool " Sascha Hauer
2013-04-07 14:00 ` [PATCH 12/21] fb: Use dev_add_param_bool for enable parameter Sascha Hauer
2013-04-07 14:00 ` [PATCH 13/21] fb: imxfb: Use dev_add_param_int for alpha parameter Sascha Hauer
2013-04-07 14:00 ` [PATCH 14/21] fb: imx-ipu-fb: " Sascha Hauer
2013-04-07 14:00 ` [PATCH 15/21] param: pass param to dev_remove_param Sascha Hauer
2013-04-07 14:00 ` [PATCH 16/21] pwm: Use dev_add_param_int for pwm parameters Sascha Hauer
2013-04-07 14:00 ` [PATCH 17/21] ARM: i.MX: iim: Use dev_add_param_bool for parameters Sascha Hauer
2013-04-07 14:00 ` [PATCH 18/21] console: Use dev_add_param_int for baudrate parameter Sascha Hauer
2013-04-07 14:00 ` [PATCH 19/21] mtd: Nand: Use dev_add_param_bool for erasebad parameter Sascha Hauer
2013-04-07 22:35 ` Alexander Aring
2013-04-08 8:11 ` Sascha Hauer
2013-04-08 10:15 ` Alexander Aring
2013-04-07 14:00 ` [PATCH 20/21] USB gadget at91: Use dev_add_param_bool for vbus parameter Sascha Hauer
2013-04-07 14:00 ` [PATCH 21/21] ARM: MXS: ocotp: Use dev_add_param_bool for parameter Sascha Hauer
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=1365343255-26497-4-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