summaryrefslogtreecommitdiffstats
path: root/common/state/state_variables.c
blob: 56bcd9590a9b166c831aff35464d1404080a0d44 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
 * Copyright (C) 2012-2014 Pengutronix, Jan Luebbe <j.luebbe@pengutronix.de>
 * Copyright (C) 2013-2014 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
 * Copyright (C) 2015 Pengutronix, Marc Kleine-Budde <mkl@pengutronix.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/string.h>
#include <linux/types.h>
#include <malloc.h>
#include <net.h>
#include <printk.h>
#include <of.h>
#include <stdio.h>

#include "state.h"

/**
 * state_set_dirty - Helper function to set the state to dirty. Only used for
 * state variables callbacks
 * @param p
 * @param priv
 * @return
 */
static int state_set_dirty(struct param_d *p, void *priv)
{
	struct state_variable *sv = priv;

	sv->state->dirty = 1;

	return 0;
}

static int state_var_compare(struct list_head *a, struct list_head *b)
{
	struct state_variable *va = list_entry(a, struct state_variable, list);
	struct state_variable *vb = list_entry(b, struct state_variable, list);

	return va->start < vb->start ? -1 : 1;
}

void state_add_var(struct state *state, struct state_variable *var)
{
	list_add_sort(&var->list, &state->variables, state_var_compare);
}

static int state_uint32_export(struct state_variable *var,
			       struct device_node *node,
			       enum state_convert conv)
{
	struct state_uint32 *su32 = to_state_uint32(var);
	int ret;

	if (su32->value_default) {
		ret = of_property_write_u32(node, "default",
					    su32->value_default);
		if (ret)
			return ret;
	}

	if (conv == STATE_CONVERT_FIXUP)
		return 0;

	return of_property_write_u32(node, "value", su32->value);
}

static int state_uint32_import(struct state_variable *sv,
			       struct device_node *node)
{
	struct state_uint32 *su32 = to_state_uint32(sv);

	of_property_read_u32(node, "default", &su32->value_default);
	if (of_property_read_u32(node, "value", &su32->value))
		su32->value = su32->value_default;

	return 0;
}

static int state_uint8_set(struct param_d *p, void *priv)
{
	struct state_variable *sv = priv;
	struct state_uint32 *su32 = to_state_uint32(sv);

	if (su32->value > 255)
		return -ERANGE;

	return state_set_dirty(p, sv);
}

static struct state_variable *state_uint8_create(struct state *state,
						 const char *name,
						 struct device_node *node)
{
	struct state_uint32 *su32;
	struct param_d *param;

	su32 = xzalloc(sizeof(*su32));

	param = dev_add_param_uint32(&state->dev, name, state_uint8_set,
				  NULL, &su32->value, "%u", &su32->var);
	if (IS_ERR(param)) {
		free(su32);
		return ERR_CAST(param);
	}

	su32->param = param;
	su32->var.size = sizeof(uint8_t);
#ifdef __LITTLE_ENDIAN
	su32->var.raw = &su32->value;
#else
	su32->var.raw = &su32->value + 3;
#endif
	su32->var.state = state;

	return &su32->var;
}

static struct state_variable *state_uint32_create(struct state *state,
						  const char *name,
						  struct device_node *node)
{
	struct state_uint32 *su32;
	struct param_d *param;

	su32 = xzalloc(sizeof(*su32));

	param = dev_add_param_uint32(&state->dev, name, state_set_dirty,
				  NULL, &su32->value, "%u", &su32->var);
	if (IS_ERR(param)) {
		free(su32);
		return ERR_CAST(param);
	}

	su32->param = param;
	su32->var.size = sizeof(uint32_t);
	su32->var.raw = &su32->value;
	su32->var.state = state;

	return &su32->var;
}

static int state_enum32_export(struct state_variable *var,
			       struct device_node *node,
			       enum state_convert conv)
{
	struct state_enum32 *enum32 = to_state_enum32(var);
	int ret, i, len;
	char *prop, *str;

	if (enum32->value_default) {
		ret = of_property_write_u32(node, "default",
					    enum32->value_default);
		if (ret)
			return ret;
	}

	len = 0;

	for (i = 0; i < enum32->num_names; i++)
		len += strlen(enum32->names[i]) + 1;

	prop = xzalloc(len);
	str = prop;

	for (i = 0; i < enum32->num_names; i++)
		str += sprintf(str, "%s", enum32->names[i]) + 1;

	ret = of_set_property(node, "names", prop, len, 1);

	free(prop);

	if (conv == STATE_CONVERT_FIXUP)
		return 0;

	ret = of_property_write_u32(node, "value", enum32->value);
	if (ret)
		return ret;

	return ret;
}

static int state_enum32_import(struct state_variable *sv,
			       struct device_node *node)
{
	struct state_enum32 *enum32 = to_state_enum32(sv);
	int len;
	const __be32 *value, *value_default;

	value = of_get_property(node, "value", &len);
	if (value && len != sizeof(uint32_t))
		return -EINVAL;

	value_default = of_get_property(node, "default", &len);
	if (value_default && len != sizeof(uint32_t))
		return -EINVAL;

	if (value_default)
		enum32->value_default = be32_to_cpu(*value_default);
	if (value)
		enum32->value = be32_to_cpu(*value);
	else
		enum32->value = enum32->value_default;

	return 0;
}

static struct state_variable *state_enum32_create(struct state *state,
						  const char *name,
						  struct device_node *node)
{
	struct state_enum32 *enum32;
	int ret, i, num_names;

	enum32 = xzalloc(sizeof(*enum32));

	num_names = of_property_count_strings(node, "names");
	if (num_names < 0) {
		dev_err(&state->dev,
			"enum32 node without \"names\" property\n");
		return ERR_PTR(-EINVAL);
	}

	enum32->names = xzalloc(sizeof(char *) * num_names);
	enum32->num_names = num_names;
	enum32->var.size = sizeof(uint32_t);
	enum32->var.raw = &enum32->value;
	enum32->var.state = state;

	for (i = 0; i < num_names; i++) {
		const char *name;

		ret = of_property_read_string_index(node, "names", i, &name);
		if (ret)
			goto out;
		enum32->names[i] = xstrdup(name);
	}

	enum32->param = dev_add_param_enum(&state->dev, name, state_set_dirty,
					   NULL, &enum32->value, enum32->names,
					   num_names, &enum32->var);
	if (IS_ERR(enum32->param)) {
		ret = PTR_ERR(enum32->param);
		goto out;
	}

	return &enum32->var;
 out:	for (i--; i >= 0; i--)
		free((char *)enum32->names[i]);
	free(enum32->names);
	free(enum32);
	return ERR_PTR(ret);
}

static int state_mac_export(struct state_variable *var,
			    struct device_node *node, enum state_convert conv)
{
	struct state_mac *mac = to_state_mac(var);
	int ret;

	if (!is_zero_ether_addr(mac->value_default)) {
		ret = of_property_write_u8_array(node, "default",
						 mac->value_default,
						 ARRAY_SIZE(mac->
							    value_default));
		if (ret)
			return ret;
	}

	if (conv == STATE_CONVERT_FIXUP)
		return 0;

	return of_property_write_u8_array(node, "value", mac->value,
					  ARRAY_SIZE(mac->value));
}

static int state_mac_import(struct state_variable *sv, struct device_node *node)
{
	struct state_mac *mac = to_state_mac(sv);

	of_property_read_u8_array(node, "default", mac->value_default,
				  ARRAY_SIZE(mac->value_default));
	if (of_property_read_u8_array(node, "value", mac->value,
				      ARRAY_SIZE(mac->value)))
		memcpy(mac->value, mac->value_default, ARRAY_SIZE(mac->value));

	return 0;
}

static struct state_variable *state_mac_create(struct state *state,
					       const char *name,
					       struct device_node *node)
{
	struct state_mac *mac;
	int ret;

	mac = xzalloc(sizeof(*mac));

	mac->var.size = ARRAY_SIZE(mac->value);
	mac->var.raw = mac->value;
	mac->var.state = state;

	mac->param = dev_add_param_mac(&state->dev, name, state_set_dirty,
				       NULL, mac->value, &mac->var);
	if (IS_ERR(mac->param)) {
		ret = PTR_ERR(mac->param);
		goto out;
	}

	return &mac->var;
 out:	free(mac);
	return ERR_PTR(ret);
}

static int state_string_export(struct state_variable *var,
			       struct device_node *node,
			       enum state_convert conv)
{
	struct state_string *string = to_state_string(var);
	int ret = 0;

	if (string->value_default) {
		ret = of_set_property(node, "default", string->value_default,
				      strlen(string->value_default) + 1, 1);

		if (ret)
			return ret;
	}

	if (conv == STATE_CONVERT_FIXUP)
		return 0;

	if (string->value)
		ret = of_set_property(node, "value", string->value,
				      strlen(string->value) + 1, 1);

	return ret;
}

static int state_string_import(struct state_variable *sv,
			       struct device_node *node)
{
	struct state_string *string = to_state_string(sv);
	const char *value = NULL;
	size_t len;
	int ret;

	of_property_read_string(node, "default", &string->value_default);
	if (string->value_default) {
		len = strlen(string->value_default);
		if (len > string->var.size)
			return -EILSEQ;
	}

	ret = of_property_read_string(node, "value", &value);
	if (ret)
		value = string->value_default;

	if (value)
		return state_string_copy_to_raw(string, value);

	return 0;
}

static int state_string_set(struct param_d *p, void *priv)
{
	struct state_variable *sv = priv;
	struct state_string *string = to_state_string(sv);
	int ret;

	ret = state_string_copy_to_raw(string, string->value);
	if (ret)
		return ret;

	return state_set_dirty(p, sv);
}

static int state_string_get(struct param_d *p, void *priv)
{
	struct state_variable *sv = priv;
	struct state_string *string = to_state_string(sv);

	free(string->value);
	if (string->raw[0])
		string->value = xstrndup(string->raw, string->var.size);
	else
		string->value = xstrdup("");

	return 0;
}

static struct state_variable *state_string_create(struct state *state,
						  const char *name,
						  struct device_node *node)
{
	struct state_string *string;
	uint32_t start_size[2];
	int ret;

	ret = of_property_read_u32_array(node, "reg", start_size,
					 ARRAY_SIZE(start_size));
	if (ret) {
		dev_err(&state->dev, "%s: reg property not found\n", name);
		return ERR_PTR(ret);
	}

	/* limit to arbitrary len of 4k */
	if (start_size[1] > 4096)
		return ERR_PTR(-EILSEQ);

	string = xzalloc(sizeof(*string) + start_size[1]);
	string->var.size = start_size[1];
	string->var.raw = &string->raw;
	string->var.state = state;

	string->param = dev_add_param_string(&state->dev, name,
					     state_string_set, state_string_get,
					     &string->value, &string->var);
	if (IS_ERR(string->param)) {
		ret = PTR_ERR(string->param);
		goto out;
	}

	return &string->var;
 out:	free(string);
	return ERR_PTR(ret);
}

static struct variable_type types[] = {
	{
		.type_name = "uint8",
		.export = state_uint32_export,
		.import = state_uint32_import,
		.create = state_uint8_create,
	}, {
		.type_name = "uint32",
		.export = state_uint32_export,
		.import = state_uint32_import,
		.create = state_uint32_create,
	}, {
		.type_name = "enum32",
		.export = state_enum32_export,
		.import = state_enum32_import,
		.create = state_enum32_create,
	}, {
		.type_name = "mac",
		.export = state_mac_export,
		.import = state_mac_import,
		.create = state_mac_create,
	}, {
		.type_name = "string",
		.export = state_string_export,
		.import = state_string_import,
		.create = state_string_create,
	}
};

struct variable_type *state_find_type_by_name(const char *name)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(types); i++) {
		if (!strcmp(name, types[i].type_name)) {
			return &types[i];
		}
	}

	return NULL;
}

struct state_variable *state_find_var(struct state *state, const char *name)
{
	struct state_variable *sv;

	list_for_each_entry(sv, &state->variables, list) {
		if (!strcmp(sv->name, name))
			return sv;
	}

	return ERR_PTR(-ENOENT);
}