/
0102-OF-overlay-Fix-overlay-revert-failure.patch
341 lines (305 loc) · 10.2 KB
/
0102-OF-overlay-Fix-overlay-revert-failure.patch
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
From 84b872382605674588b1c57495f78e5628d30ab1 Mon Sep 17 00:00:00 2001
From: Pantelis Antoniou <panto@antoniou-consulting.com>
Date: Wed, 27 Feb 2013 11:28:19 +0200
Subject: [PATCH 102/184] OF-overlay: Fix overlay revert failure
Somewhere along the line of changes we dropped the ability to cleanly revert
overlays. This patchset fixes the bug, and makes it possible to add/remove
overlays at will.
Signed-off-by: Pantelis Antoniou <panto@antoniou-consulting.com>
---
drivers/of/overlay.c | 136 ++++++++++++++++++++++++++++++++-------------------
include/linux/of.h | 5 ++
2 files changed, 91 insertions(+), 50 deletions(-)
diff --git a/drivers/of/overlay.c b/drivers/of/overlay.c
index 0577067..41b883f 100644
--- a/drivers/of/overlay.c
+++ b/drivers/of/overlay.c
@@ -8,12 +8,13 @@
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*/
-
+#undef DEBUG
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_i2c.h>
+#include <linux/i2c.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/errno.h>
@@ -169,12 +170,12 @@ static int of_overlay_apply_one(struct device_node *target,
struct of_overlay_device_entry *of_overlay_device_entry_lookup(
struct of_overlay_info *ovinfo, struct device_node *node)
{
- struct of_overlay_device_entry *re;
+ struct of_overlay_device_entry *de;
- /* no need for locks, we're under the ovinfo->lock */
- list_for_each_entry(re, &ovinfo->de_list, node) {
- if (re->np == node)
- return re;
+ /* no need for locks, we'de under the ovinfo->lock */
+ list_for_each_entry(de, &ovinfo->de_list, node) {
+ if (de->np == node)
+ return de;
}
return NULL;
}
@@ -215,10 +216,11 @@ static int of_overlay_log_entry_entry_add(struct of_overlay_info *ovinfo,
* Add an overlay device entry
*/
static void of_overlay_device_entry_entry_add(struct of_overlay_info *ovinfo,
- struct device_node *node, struct platform_device *pdev,
- int state)
+ struct device_node *node,
+ struct platform_device *pdev, struct i2c_client *client,
+ int prevstate, int state)
{
- struct of_overlay_device_entry *re;
+ struct of_overlay_device_entry *de;
int fresh;
/* check */
@@ -226,24 +228,29 @@ static void of_overlay_device_entry_entry_add(struct of_overlay_info *ovinfo,
return;
fresh = 0;
- re = of_overlay_device_entry_lookup(ovinfo, node);
- if (re == NULL) {
- re = kzalloc(sizeof(*re), GFP_KERNEL);
- if (re == NULL) {
+ de = of_overlay_device_entry_lookup(ovinfo, node);
+ if (de == NULL) {
+ de = kzalloc(sizeof(*de), GFP_KERNEL);
+ if (de == NULL) {
pr_err("%s: Failed to allocate\n", __func__);
return;
}
fresh = 1;
+ de->prevstate = -1;
}
- if (re->np == NULL)
- re->np = of_node_get(node);
- if (re->pdev == NULL)
- re->pdev = of_dev_get(pdev);
- re->state = state;
+ if (de->np == NULL)
+ de->np = of_node_get(node);
+ if (de->pdev == NULL && pdev)
+ de->pdev = of_dev_get(pdev);
+ if (de->client == NULL && client)
+ de->client = i2c_use_client(client);
+ if (fresh)
+ de->prevstate = prevstate;
+ de->state = state;
if (fresh)
- list_add_tail(&re->node, &ovinfo->de_list);
+ list_add_tail(&de->node, &ovinfo->de_list);
}
/**
@@ -263,7 +270,8 @@ static int of_overlay_notify(struct notifier_block *nb,
struct property *prop, *sprop, *cprop;
struct of_prop_reconfig *pr;
struct platform_device *pdev;
- int state;
+ struct i2c_client *client;
+ int prevstate, state;
int err = 0;
ovinfo = container_of(nb, struct of_overlay_info, notifier);
@@ -301,7 +309,9 @@ static int of_overlay_notify(struct notifier_block *nb,
/* come up with the device entry (if any) */
pdev = NULL;
+ client = NULL;
state = 0;
+ prevstate = 0;
/* determine the state the node will end up */
switch (action) {
@@ -311,8 +321,11 @@ static int of_overlay_notify(struct notifier_block *nb,
of_device_is_available(node);
break;
case OF_RECONFIG_DETACH_NODE:
+ prevstate = of_find_property(node, "compatible", NULL) &&
+ of_device_is_available(node);
state = 0;
pdev = of_find_device_by_node(node);
+ client = of_find_i2c_device_by_node(node);
break;
case OF_RECONFIG_ADD_PROPERTY:
case OF_RECONFIG_REMOVE_PROPERTY:
@@ -334,6 +347,8 @@ static int of_overlay_notify(struct notifier_block *nb,
prop : NULL;
}
+ prevstate = of_find_property(node, "compatible", NULL) &&
+ of_device_is_available(node);
state = cprop && cprop->length > 0 &&
(!sprop || (sprop->length > 0 &&
(strcmp(sprop->value, "okay") == 0 ||
@@ -344,10 +359,13 @@ static int of_overlay_notify(struct notifier_block *nb,
return notifier_from_errno(0);
}
- if (state == 0)
+ if (state == 0) {
pdev = of_find_device_by_node(node);
+ client = of_find_i2c_device_by_node(node);
+ }
- of_overlay_device_entry_entry_add(ovinfo, node, pdev, state);
+ of_overlay_device_entry_entry_add(ovinfo, node, pdev, client,
+ prevstate, state);
return notifier_from_errno(err);
}
@@ -370,21 +388,21 @@ static int of_overlay_prep_one(struct of_overlay_info *ovinfo)
}
static int of_overlay_device_entry_change(struct of_overlay_info *ovinfo,
- struct of_overlay_device_entry *re, int revert)
+ struct of_overlay_device_entry *de, int revert)
{
struct i2c_adapter *adap = NULL;
struct i2c_client *client;
struct platform_device *pdev, *parent_pdev = NULL;
int state;
- state = !!re->state ^ !!revert;
+ state = !!de->state ^ !!revert;
- if (re->np && re->np->parent) {
+ if (de->np && de->np->parent) {
pr_debug("%s: parent is %s\n",
- __func__, re->np->parent->full_name);
- adap = of_find_i2c_adapter_by_node(re->np->parent);
+ __func__, de->np->parent->full_name);
+ adap = of_find_i2c_adapter_by_node(de->np->parent);
if (adap == NULL)
- parent_pdev = of_find_device_by_node(re->np->parent);
+ parent_pdev = of_find_device_by_node(de->np->parent);
}
if (state) {
@@ -394,35 +412,46 @@ static int of_overlay_device_entry_change(struct of_overlay_info *ovinfo,
pr_debug("%s: creating new platform device "
"new_node='%s' %p\n",
- __func__, re->np->full_name, re->np);
+ __func__, de->np->full_name, de->np);
- pdev = of_platform_device_create(re->np, NULL,
+ pdev = of_platform_device_create(de->np, NULL,
parent_pdev ? &parent_pdev->dev : NULL);
if (pdev == NULL) {
pr_warn("%s: Failed to create platform device "
"for '%s'\n",
- __func__, re->np->full_name);
- }
+ __func__, de->np->full_name);
+ } else
+ de->pdev = pdev;
+
} else {
pr_debug("%s: creating new i2c_client device "
"new_node='%s' %p\n",
- __func__, re->np->full_name, re->np);
+ __func__, de->np->full_name, de->np);
- client = of_i2c_register_device(adap, re->np);
+ client = of_i2c_register_device(adap, de->np);
if (client == NULL) {
pr_warn("%s: Failed to create i2c client device "
"for '%s'\n",
- __func__, re->np->full_name);
- }
+ __func__, de->np->full_name);
+ } else
+ de->client = client;
}
} else {
- if (re->pdev) {
+ if (de->pdev) {
pr_debug("%s: removing pdev %s\n", __func__,
- dev_name(&re->pdev->dev));
- platform_device_unregister(re->pdev);
+ dev_name(&de->pdev->dev));
+ platform_device_unregister(de->pdev);
+ de->pdev = NULL;
+ }
+
+ if (de->client) {
+ pr_debug("%s: removing i2c client %s\n", __func__,
+ dev_name(&de->client->dev));
+ i2c_unregister_device(de->client);
+ de->client = NULL;
}
}
@@ -444,7 +473,7 @@ static int of_overlay_device_entry_change(struct of_overlay_info *ovinfo,
*/
static void of_overlay_revert_one(struct of_overlay_info *ovinfo)
{
- struct of_overlay_device_entry *re, *ren;
+ struct of_overlay_device_entry *de, *den;
struct of_overlay_log_entry *le, *len;
struct property *prop, **propp;
int ret;
@@ -457,13 +486,11 @@ static void of_overlay_revert_one(struct of_overlay_info *ovinfo)
ovinfo->target->full_name);
/* overlay applied correctly, now create/destroy pdevs */
- list_for_each_entry_safe_reverse(re, ren, &ovinfo->de_list, node) {
-
- of_overlay_device_entry_change(ovinfo, re, 1);
-
- of_node_put(re->np);
- list_del(&re->node);
- kfree(re);
+ list_for_each_entry_safe_reverse(de, den, &ovinfo->de_list, node) {
+ of_overlay_device_entry_change(ovinfo, de, 1);
+ of_node_put(de->np);
+ list_del(&de->node);
+ kfree(de);
}
list_for_each_entry_safe_reverse(le, len, &ovinfo->le_list, node) {
@@ -554,7 +581,7 @@ static void of_overlay_revert_one(struct of_overlay_info *ovinfo)
*/
static int of_overlay_post_one(struct of_overlay_info *ovinfo, int err)
{
- struct of_overlay_device_entry *re;
+ struct of_overlay_device_entry *de, *den;
of_reconfig_notifier_unregister(&ovinfo->notifier);
@@ -565,8 +592,17 @@ static int of_overlay_post_one(struct of_overlay_info *ovinfo, int err)
}
/* overlay applied correctly, now create/destroy pdevs */
- list_for_each_entry(re, &ovinfo->de_list, node)
- of_overlay_device_entry_change(ovinfo, re, 0);
+ list_for_each_entry_safe(de, den, &ovinfo->de_list, node) {
+
+ /* no state change? just remove this entry */
+ if (de->prevstate == de->state) {
+ of_node_put(de->np);
+ list_del(&de->node);
+ kfree(de);
+ } else {
+ of_overlay_device_entry_change(ovinfo, de, 0);
+ }
+ }
return 0;
}
diff --git a/include/linux/of.h b/include/linux/of.h
index 8a908f0..73cfde5 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -23,6 +23,7 @@
#include <linux/spinlock.h>
#include <linux/topology.h>
#include <linux/notifier.h>
+#include <linux/i2c.h>
#include <asm/byteorder.h>
#include <asm/errno.h>
@@ -697,7 +698,9 @@ struct of_overlay_log_entry {
* @node: list_head for the device list
* @np: device node pointer to the device node affected
* @pdev: pointer to the platform device affected
+ * @client: pointer to the I2C client device affected
* @state: new device state
+ * @prevstate: previous device state
*
* When the overlay results in a device node's state to change this
* fact is recorded in a list of device entries. After the overlay
@@ -708,6 +711,8 @@ struct of_overlay_device_entry {
struct list_head node;
struct device_node *np;
struct platform_device *pdev;
+ struct i2c_client *client;
+ int prevstate;
int state;
};
--
1.8.2.1