From: Sascha Hauer <s.hauer@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 06/23] param: Add dev_add_param_string
Date: Mon, 21 Jul 2014 17:14:30 +0200 [thread overview]
Message-ID: <1405955687-27433-7-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1405955687-27433-1-git-send-email-s.hauer@pengutronix.de>
This function allows drivers to create a string parameter and
provides access to it via a pointer. Using regular dev_add_param
only allows access via dev_get_param().
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
include/param.h | 13 ++++++++++
lib/parameter.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 89 insertions(+)
diff --git a/include/param.h b/include/param.h
index 8f200df..9856a2e 100644
--- a/include/param.h
+++ b/include/param.h
@@ -31,6 +31,11 @@ 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_string(struct device_d *dev, const char *name,
+ int (*set)(struct param_d *p, void *priv),
+ int (*get)(struct param_d *p, void *priv),
+ char **value, void *priv);
+
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),
@@ -95,6 +100,14 @@ static inline int dev_add_param(struct device_d *dev, char *name,
return 0;
}
+static inline struct param_d *dev_add_param_string(struct device_d *dev, const char *name,
+ int (*set)(struct param_d *p, void *priv),
+ int (*get)(struct param_d *p, void *priv),
+ char **value, void *priv)
+{
+ return NULL;
+}
+
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),
diff --git a/lib/parameter.c b/lib/parameter.c
index 13062bc..84a632c 100644
--- a/lib/parameter.c
+++ b/lib/parameter.c
@@ -201,6 +201,82 @@ int dev_add_param_fixed(struct device_d *dev, char *name, const char *value)
return 0;
}
+struct param_string {
+ struct param_d param;
+ char **value;
+ int (*set)(struct param_d *p, void *priv);
+ int (*get)(struct param_d *p, void *priv);
+};
+
+static inline struct param_string *to_param_string(struct param_d *p)
+{
+ return container_of(p, struct param_string, param);
+}
+
+static int param_string_set(struct device_d *dev, struct param_d *p, const char *val)
+{
+ struct param_string *ps = to_param_string(p);
+ int ret;
+ char *value_save = *ps->value;
+
+ if (!val)
+ return -EINVAL;
+
+ *ps->value = xstrdup(val);
+
+ if (!ps->set)
+ return 0;
+
+ ret = ps->set(p, p->driver_priv);
+ if (ret) {
+ free(*ps->value);
+ *ps->value = value_save;
+ } else {
+ free(value_save);
+ }
+
+ return ret;
+}
+
+static const char *param_string_get(struct device_d *dev, struct param_d *p)
+{
+ struct param_string *ps = to_param_string(p);
+ int ret;
+
+ if (ps->get) {
+ ret = ps->get(p, p->driver_priv);
+ if (ret)
+ return NULL;
+ }
+
+ return *ps->value;
+}
+
+struct param_d *dev_add_param_string(struct device_d *dev, const char *name,
+ int (*set)(struct param_d *p, void *priv),
+ int (*get)(struct param_d *p, void *priv),
+ char **value, void *priv)
+{
+ struct param_string *ps;
+ struct param_d *p;
+ int ret;
+
+ ps = xzalloc(sizeof(*ps));
+ ps->value = value;
+ ps->set = set;
+ ps->get = get;
+ p = &ps->param;
+ p->driver_priv = priv;
+
+ ret = __dev_add_param(p, dev, name, param_string_set, param_string_get, 0);
+ if (ret) {
+ free(ps);
+ return ERR_PTR(ret);
+ }
+
+ return &ps->param;
+}
+
struct param_int {
struct param_d param;
int *value;
--
2.0.1
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next prev parent reply other threads:[~2014-07-21 15:15 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-21 15:14 USB Gadget updates Sascha Hauer
2014-07-21 15:14 ` [PATCH 01/23] USB: gadget: Update to 3.15 Sascha Hauer
2014-07-21 15:14 ` [PATCH 02/23] USB: gadget: dequeue descriptor before freeing it Sascha Hauer
2014-07-21 15:14 ` [PATCH 03/23] USB: gadget: composite: Break out of potential endless loop Sascha Hauer
2014-07-21 15:14 ` [PATCH 04/23] USB: gadget: fsl_udc: Warn about freeing queued descriptors Sascha Hauer
2014-07-21 15:14 ` [PATCH 05/23] USB: gadget: usbserial: Always enable console Sascha Hauer
2014-07-21 15:14 ` Sascha Hauer [this message]
2014-07-21 15:14 ` [PATCH 07/23] USB: gadget: specify vendor/product id with device parameters Sascha Hauer
2014-07-21 15:14 ` [PATCH 08/23] USB: gadget: DFU: remove unused code Sascha Hauer
2014-07-21 15:14 ` [PATCH 09/23] USB: gadget: DFU: Use usb_assign_descriptors/usb_free_all_descriptors Sascha Hauer
2014-07-21 15:14 ` [PATCH 10/23] USB: gadget: DFU: Move locally used defines/structs to dfu driver Sascha Hauer
2014-07-21 15:14 ` [PATCH 11/23] Add function to parse a string in dfu format Sascha Hauer
2014-07-21 15:14 ` [PATCH 12/23] USB: gadget: DFU: Move stuff to dfu_bind Sascha Hauer
2014-07-21 15:14 ` [PATCH 13/23] USB: gadget: DFU: use usb_gstrings_attach Sascha Hauer
2014-07-21 15:14 ` [PATCH 14/23] USB: gadget: DFU: free resources when usb_gadget_poll fails Sascha Hauer
2014-07-21 15:14 ` [PATCH 15/23] USB: gadget: DFU: return -EINTR when interrupted Sascha Hauer
2014-07-21 15:14 ` [PATCH 16/23] USB: gadget: DFU: register as USB function Sascha Hauer
2014-07-21 15:14 ` [PATCH 17/23] USB: gadget: DFU: drop app idle state Sascha Hauer
2014-07-21 15:14 ` [PATCH 18/23] Add release string Sascha Hauer
2014-07-21 15:14 ` [PATCH 19/23] USB: gadget: Add Android fastboot support Sascha Hauer
2014-07-22 7:25 ` Holger Schurig
2014-07-22 9:23 ` Sascha Hauer
2014-07-21 15:14 ` [PATCH 20/23] USB: gadget: Add a multi function gadget Sascha Hauer
2014-07-21 15:14 ` [PATCH 21/23] USB: gadget: fsl_udc: Be more tolerant in fsl_ep_dequeue Sascha Hauer
2014-07-21 15:14 ` [PATCH 22/23] Documentation: Add documentation for USB serial console Sascha Hauer
2014-07-21 15:14 ` [PATCH 23/23] Documentation: Add documentation for Fastboot and Composite Multifunction Gadget 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=1405955687-27433-7-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