fc826f3f7e4e87ee6bb01d45ab1b7bf22c60adf6
[openwrt/staging/xback.git] /
1 From 7c8979b42b1a9c5604f431ba804928e55919263c Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Thu, 21 Dec 2023 18:34:18 +0100
4 Subject: [PATCH] nvmem: u-boot-env: use nvmem_add_one_cell() nvmem subsystem
5 helper
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
10 Simplify adding NVMEM cells.
11
12 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
13 Reviewed-by: Miquel Raynal <miquel.raynal@bootlin.com>
14 Link: https://lore.kernel.org/r/20231221173421.13737-3-zajec5@gmail.com
15 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
16 ---
17 drivers/nvmem/u-boot-env.c | 55 +++++++++++++++-----------------------
18 1 file changed, 21 insertions(+), 34 deletions(-)
19
20 --- a/drivers/nvmem/u-boot-env.c
21 +++ b/drivers/nvmem/u-boot-env.c
22 @@ -23,13 +23,10 @@ enum u_boot_env_format {
23
24 struct u_boot_env {
25 struct device *dev;
26 + struct nvmem_device *nvmem;
27 enum u_boot_env_format format;
28
29 struct mtd_info *mtd;
30 -
31 - /* Cells */
32 - struct nvmem_cell_info *cells;
33 - int ncells;
34 };
35
36 struct u_boot_env_image_single {
37 @@ -94,43 +91,36 @@ static int u_boot_env_read_post_process_
38 static int u_boot_env_add_cells(struct u_boot_env *priv, uint8_t *buf,
39 size_t data_offset, size_t data_len)
40 {
41 + struct nvmem_device *nvmem = priv->nvmem;
42 struct device *dev = priv->dev;
43 char *data = buf + data_offset;
44 char *var, *value, *eq;
45 - int idx;
46 -
47 - priv->ncells = 0;
48 - for (var = data; var < data + data_len && *var; var += strlen(var) + 1)
49 - priv->ncells++;
50 -
51 - priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
52 - if (!priv->cells)
53 - return -ENOMEM;
54
55 - for (var = data, idx = 0;
56 + for (var = data;
57 var < data + data_len && *var;
58 - var = value + strlen(value) + 1, idx++) {
59 + var = value + strlen(value) + 1) {
60 + struct nvmem_cell_info info = {};
61 +
62 eq = strchr(var, '=');
63 if (!eq)
64 break;
65 *eq = '\0';
66 value = eq + 1;
67
68 - priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL);
69 - if (!priv->cells[idx].name)
70 + info.name = devm_kstrdup(dev, var, GFP_KERNEL);
71 + if (!info.name)
72 return -ENOMEM;
73 - priv->cells[idx].offset = data_offset + value - data;
74 - priv->cells[idx].bytes = strlen(value);
75 - priv->cells[idx].np = of_get_child_by_name(dev->of_node, priv->cells[idx].name);
76 + info.offset = data_offset + value - data;
77 + info.bytes = strlen(value);
78 + info.np = of_get_child_by_name(dev->of_node, info.name);
79 if (!strcmp(var, "ethaddr")) {
80 - priv->cells[idx].raw_len = strlen(value);
81 - priv->cells[idx].bytes = ETH_ALEN;
82 - priv->cells[idx].read_post_process = u_boot_env_read_post_process_ethaddr;
83 + info.raw_len = strlen(value);
84 + info.bytes = ETH_ALEN;
85 + info.read_post_process = u_boot_env_read_post_process_ethaddr;
86 }
87 - }
88
89 - if (WARN_ON(idx != priv->ncells))
90 - priv->ncells = idx;
91 + nvmem_add_one_cell(nvmem, &info);
92 + }
93
94 return 0;
95 }
96 @@ -209,7 +199,6 @@ static int u_boot_env_probe(struct platf
97 struct device *dev = &pdev->dev;
98 struct device_node *np = dev->of_node;
99 struct u_boot_env *priv;
100 - int err;
101
102 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
103 if (!priv)
104 @@ -224,17 +213,15 @@ static int u_boot_env_probe(struct platf
105 return PTR_ERR(priv->mtd);
106 }
107
108 - err = u_boot_env_parse(priv);
109 - if (err)
110 - return err;
111 -
112 config.dev = dev;
113 - config.cells = priv->cells;
114 - config.ncells = priv->ncells;
115 config.priv = priv;
116 config.size = priv->mtd->size;
117
118 - return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
119 + priv->nvmem = devm_nvmem_register(dev, &config);
120 + if (IS_ERR(priv->nvmem))
121 + return PTR_ERR(priv->nvmem);
122 +
123 + return u_boot_env_parse(priv);
124 }
125
126 static const struct of_device_id u_boot_env_of_match_table[] = {