summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlrich Ölmann <u.oelmann@pengutronix.de>2018-12-18 14:57:49 +0100
committerRoland Hieber <rhi@pengutronix.de>2019-01-07 15:46:04 +0100
commit44122040c53042f68e93657450ef5e5ceaa48e4a (patch)
tree7cd36c899e9634f67e1012cf7fd6dcd4ea097d8e
parentd32a8de0e9d23db601f06676ff432e495ae1156e (diff)
downloaddt-utils-44122040c53042f68e93657450ef5e5ceaa48e4a.tar.gz
dt-utils-44122040c53042f68e93657450ef5e5ceaa48e4a.tar.xz
state: refactor variable type
This ports the following barebox commits and adjusts the handling of type information of state variables in barebox-state: | commit 7b3d284f4bad78d61e9f5d32ec5aa1efc19ce733 | Author: Sascha Hauer <s.hauer@pengutronix.de> | Date: Mon Apr 3 22:51:58 2017 +0200 | | state: remove unused variable type | | enum state_variable_type is never used. Remove it. | | Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de> | commit 7126dffd0be98a0f4c80bc13bc7df601526f056f | Author: Daniel Schultz <d.schultz@phytec.de> | Date: Fri Nov 3 11:48:26 2017 +0100 | | common: state: Add variable_type to state_variable | | Add a pointer in state_variable to the corresponding variable_type array | element. | | Signed-off-by: Daniel Schultz <d.schultz@phytec.de> | Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de> | commit dc74265d2ac074a18609d51bad1fdbd2bbcdd608 | Author: Daniel Schultz <d.schultz@phytec.de> | Date: Fri Nov 3 11:48:27 2017 +0100 | | common: state: Add variable type as enum | | The variable_type struct holds a name of its type. Checking the type of | a variable with this string needs much resources. | | This patch introduce a enum of the variable type for better type | checking. | | Signed-off-by: Daniel Schultz <d.schultz@phytec.de> | Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de> Signed-off-by: Ulrich Ölmann <u.oelmann@pengutronix.de> [rhi: fixed function parameter alignment in common/state/state_variables.c, and submitted those changes as patches for barebox: https://www.mail-archive.com/barebox@lists.infradead.org/msg29427.html https://www.mail-archive.com/barebox@lists.infradead.org/msg29462.html ] Signed-off-by: Roland Hieber <rhi@pengutronix.de>
-rw-r--r--src/barebox-state.c20
-rw-r--r--src/barebox-state/state.c3
-rw-r--r--src/barebox-state/state.h28
-rw-r--r--src/barebox-state/state_variables.c43
4 files changed, 58 insertions, 36 deletions
diff --git a/src/barebox-state.c b/src/barebox-state.c
index ff318b2..f10b7c6 100644
--- a/src/barebox-state.c
+++ b/src/barebox-state.c
@@ -63,28 +63,28 @@ struct variable_str_type {
static struct variable_str_type types[] = {
{
- .type = STATE_TYPE_U8,
+ .type = STATE_VARIABLE_TYPE_UINT8,
.type_name = "uint8",
.set = __state_uint8_set,
.get = __state_uint32_get,
}, {
- .type = STATE_TYPE_U32,
+ .type = STATE_VARIABLE_TYPE_UINT32,
.type_name = "uint32",
.set = __state_uint32_set,
.get = __state_uint32_get,
}, {
- .type = STATE_TYPE_ENUM,
+ .type = STATE_VARIABLE_TYPE_ENUM32,
.type_name = "enum32",
.set = __state_enum32_set,
.get = __state_enum32_get,
.info = __state_enum32_info,
}, {
- .type = STATE_TYPE_MAC,
+ .type = STATE_VARIABLE_TYPE_MAC,
.type_name = "mac",
.set = __state_mac_set,
.get = __state_mac_get,
}, {
- .type = STATE_TYPE_STRING,
+ .type = STATE_VARIABLE_TYPE_STRING,
.type_name = "string",
.set = __state_string_set,
.get = __state_string_get,
@@ -274,7 +274,7 @@ char *state_get_var(struct state *state, const char *var)
if (IS_ERR(sv))
return NULL;
- vtype = state_find_type(sv->type);
+ vtype = state_find_type(sv->type->type);
if (!vtype)
return NULL;
@@ -291,7 +291,7 @@ static int state_set_var(struct state *state, const char *var, const char *val)
if (IS_ERR(sv))
return PTR_ERR(sv);
- vtype = state_find_type(sv->type);
+ vtype = state_find_type(sv->type->type);
if (!vtype)
return -ENODEV;
@@ -543,10 +543,10 @@ int main(int argc, char *argv[])
list_for_each_entry(state, &state_list.list, list) {
state_for_each_var(state->state, v) {
struct variable_str_type *vtype;
- vtype = state_find_type(v->type);
+ vtype = state_find_type(v->type->type);
if (!vtype) {
- pr_err("no such type: %d\n", v->type);
+ pr_err("no such type: %d\n", v->type->type);
ret = 1;
goto out_unlock;
}
@@ -579,7 +579,7 @@ int main(int argc, char *argv[])
while ((ptr = strchr(ptr, '.')))
*ptr++ = '_';
- vtype = state_find_type(v->type);
+ vtype = state_find_type(v->type->type);
printf("%s_%s=\"%s\"\n", state->name, name, vtype->get(v));
}
}
diff --git a/src/barebox-state/state.c b/src/barebox-state/state.c
index d557733..2c613a9 100644
--- a/src/barebox-state/state.c
+++ b/src/barebox-state/state.c
@@ -245,7 +245,7 @@ static int state_convert_node_variable(struct state *state,
}
if (conv == STATE_CONVERT_FROM_NODE_CREATE) {
- sv = vtype->create(state, name, node);
+ sv = vtype->create(state, name, node, vtype);
if (IS_ERR(sv)) {
ret = PTR_ERR(sv);
dev_err(&state->dev, "failed to create %s: %s\n", name,
@@ -271,7 +271,6 @@ static int state_convert_node_variable(struct state *state,
sv->name = name;
sv->start = start_size[0];
- sv->type = vtype->type;
state_add_var(state, sv);
} else {
sv = state_find_var(state, name);
diff --git a/src/barebox-state/state.h b/src/barebox-state/state.h
index ead8cc8..fcc6b9f 100644
--- a/src/barebox-state/state.h
+++ b/src/barebox-state/state.h
@@ -9,6 +9,14 @@ enum state_flags {
STATE_FLAG_NO_AUTHENTIFICATION = (1 << 0),
};
+enum state_variable_type {
+ STATE_VARIABLE_TYPE_UINT8,
+ STATE_VARIABLE_TYPE_UINT32,
+ STATE_VARIABLE_TYPE_ENUM32,
+ STATE_VARIABLE_TYPE_MAC,
+ STATE_VARIABLE_TYPE_STRING
+};
+
/**
* state_backend_storage_bucket - This class describes a single backend storage
* object copy
@@ -114,35 +122,27 @@ enum state_convert {
STATE_CONVERT_FIXUP,
};
-enum state_variable_type {
- STATE_TYPE_INVALID = 0,
- STATE_TYPE_ENUM,
- STATE_TYPE_U8,
- STATE_TYPE_U32,
- STATE_TYPE_MAC,
- STATE_TYPE_STRING,
-};
-
struct state_variable;
/* A variable type (uint32, enum32) */
struct variable_type {
- enum state_variable_type type;
const char *type_name;
+ enum state_variable_type type;
struct list_head list;
int (*export) (struct state_variable *, struct device_node *,
enum state_convert);
int (*import) (struct state_variable *, struct device_node *);
- struct state_variable *(*create) (struct state * state,
- const char *name,
- struct device_node *);
+ struct state_variable *(*create) (struct state *,
+ const char *,
+ struct device_node *,
+ const struct variable_type *);
};
/* instance of a single variable */
struct state_variable {
struct state *state;
- enum state_variable_type type;
struct list_head list;
+ const struct variable_type *type;
const char *name;
unsigned int start;
unsigned int size;
diff --git a/src/barebox-state/state_variables.c b/src/barebox-state/state_variables.c
index 0a4d23d..16f630f 100644
--- a/src/barebox-state/state_variables.c
+++ b/src/barebox-state/state_variables.c
@@ -101,7 +101,8 @@ static int state_uint8_set(struct param_d *p, void *priv)
static struct state_variable *state_uint8_create(struct state *state,
const char *name,
- struct device_node *node)
+ struct device_node *node,
+ const struct variable_type *vtype)
{
struct state_uint32 *su32;
struct param_d *param;
@@ -116,6 +117,7 @@ static struct state_variable *state_uint8_create(struct state *state,
}
su32->param = param;
+ su32->var.type = vtype;
su32->var.size = sizeof(uint8_t);
#ifdef __LITTLE_ENDIAN
su32->var.raw = &su32->value;
@@ -129,7 +131,8 @@ static struct state_variable *state_uint8_create(struct state *state,
static struct state_variable *state_uint32_create(struct state *state,
const char *name,
- struct device_node *node)
+ struct device_node *node,
+ const struct variable_type *vtype)
{
struct state_uint32 *su32;
struct param_d *param;
@@ -144,6 +147,7 @@ static struct state_variable *state_uint32_create(struct state *state,
}
su32->param = param;
+ su32->var.type = vtype;
su32->var.size = sizeof(uint32_t);
su32->var.raw = &su32->value;
su32->var.state = state;
@@ -218,7 +222,8 @@ static int state_enum32_import(struct state_variable *sv,
static struct state_variable *state_enum32_create(struct state *state,
const char *name,
- struct device_node *node)
+ struct device_node *node,
+ const struct variable_type *vtype)
{
struct state_enum32 *enum32;
int ret, i, num_names;
@@ -234,6 +239,7 @@ static struct state_variable *state_enum32_create(struct state *state,
enum32->names = xzalloc(sizeof(char *) * num_names);
enum32->num_names = num_names;
+ enum32->var.type = vtype;
enum32->var.size = sizeof(uint32_t);
enum32->var.raw = &enum32->value;
enum32->var.state = state;
@@ -300,13 +306,15 @@ static int state_mac_import(struct state_variable *sv, struct device_node *node)
static struct state_variable *state_mac_create(struct state *state,
const char *name,
- struct device_node *node)
+ struct device_node *node,
+ const struct variable_type *vtype)
{
struct state_mac *mac;
int ret;
mac = xzalloc(sizeof(*mac));
+ mac->var.type = vtype;
mac->var.size = ARRAY_SIZE(mac->value);
mac->var.raw = mac->value;
mac->var.state = state;
@@ -400,7 +408,8 @@ static int state_string_get(struct param_d *p, void *priv)
static struct state_variable *state_string_create(struct state *state,
const char *name,
- struct device_node *node)
+ struct device_node *node,
+ const struct variable_type *vtype)
{
struct state_string *string;
uint32_t start_size[2];
@@ -418,6 +427,7 @@ static struct state_variable *state_string_create(struct state *state,
return ERR_PTR(-EILSEQ);
string = xzalloc(sizeof(*string) + start_size[1]);
+ string->var.type = vtype;
string->var.size = start_size[1];
string->var.raw = &string->raw;
string->var.state = state;
@@ -437,32 +447,32 @@ static struct state_variable *state_string_create(struct state *state,
static struct variable_type types[] = {
{
- .type = STATE_TYPE_U8,
.type_name = "uint8",
+ .type = STATE_VARIABLE_TYPE_UINT8,
.export = state_uint32_export,
.import = state_uint32_import,
.create = state_uint8_create,
}, {
- .type = STATE_TYPE_U32,
.type_name = "uint32",
+ .type = STATE_VARIABLE_TYPE_UINT32,
.export = state_uint32_export,
.import = state_uint32_import,
.create = state_uint32_create,
}, {
- .type = STATE_TYPE_ENUM,
.type_name = "enum32",
+ .type = STATE_VARIABLE_TYPE_ENUM32,
.export = state_enum32_export,
.import = state_enum32_import,
.create = state_enum32_create,
}, {
- .type = STATE_TYPE_MAC,
.type_name = "mac",
+ .type = STATE_VARIABLE_TYPE_MAC,
.export = state_mac_export,
.import = state_mac_import,
.create = state_mac_create,
}, {
- .type = STATE_TYPE_STRING,
.type_name = "string",
+ .type = STATE_VARIABLE_TYPE_STRING,
.export = state_string_export,
.import = state_string_import,
.create = state_string_create,
@@ -482,6 +492,19 @@ struct variable_type *state_find_type_by_name(const char *name)
return NULL;
}
+struct variable_type *state_find_type(const enum state_variable_type type)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(types); i++) {
+ if (type == types[i].type) {
+ return &types[i];
+ }
+ }
+
+ return NULL;
+}
+
struct state_variable *state_find_var(struct state *state, const char *name)
{
struct state_variable *sv;