component: move check for unbound master into try_to_bring_up_masters()
[deliverable/linux.git] / drivers / base / component.c
1 /*
2 * Componentized device handling.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This is work in progress. We gather up the component devices into a list,
9 * and bind them when instructed. At the moment, we're specific to the DRM
10 * subsystem, and only handles one master device, but this doesn't have to be
11 * the case.
12 */
13 #include <linux/component.h>
14 #include <linux/device.h>
15 #include <linux/kref.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/slab.h>
20
21 struct component_match {
22 size_t alloc;
23 size_t num;
24 struct {
25 void *data;
26 int (*fn)(struct device *, void *);
27 } compare[0];
28 };
29
30 struct master {
31 struct list_head node;
32 struct list_head components;
33 bool bound;
34
35 const struct component_master_ops *ops;
36 struct device *dev;
37 struct component_match *match;
38 };
39
40 struct component {
41 struct list_head node;
42 struct list_head master_node;
43 struct master *master;
44 bool bound;
45
46 const struct component_ops *ops;
47 struct device *dev;
48 };
49
50 static DEFINE_MUTEX(component_mutex);
51 static LIST_HEAD(component_list);
52 static LIST_HEAD(masters);
53
54 static struct master *__master_find(struct device *dev,
55 const struct component_master_ops *ops)
56 {
57 struct master *m;
58
59 list_for_each_entry(m, &masters, node)
60 if (m->dev == dev && (!ops || m->ops == ops))
61 return m;
62
63 return NULL;
64 }
65
66 /* Attach an unattached component to a master. */
67 static void component_attach_master(struct master *master, struct component *c)
68 {
69 c->master = master;
70
71 list_add_tail(&c->master_node, &master->components);
72 }
73
74 /* Detach a component from a master. */
75 static void component_detach_master(struct master *master, struct component *c)
76 {
77 list_del(&c->master_node);
78
79 c->master = NULL;
80 }
81
82 /*
83 * Add a component to a master, finding the component via the compare
84 * function and compare data. This is safe to call for duplicate matches
85 * and will not result in the same component being added multiple times.
86 */
87 static int component_master_add_child(struct master *master,
88 int (*compare)(struct device *, void *), void *compare_data)
89 {
90 struct component *c;
91 int ret = -ENXIO;
92
93 list_for_each_entry(c, &component_list, node) {
94 if (c->master && c->master != master)
95 continue;
96
97 if (compare(c->dev, compare_data)) {
98 if (!c->master)
99 component_attach_master(master, c);
100 ret = 0;
101 break;
102 }
103 }
104
105 return ret;
106 }
107
108 static int find_components(struct master *master)
109 {
110 struct component_match *match = master->match;
111 size_t i;
112 int ret = 0;
113
114 /*
115 * Scan the array of match functions and attach
116 * any components which are found to this master.
117 */
118 for (i = 0; i < match->num; i++) {
119 ret = component_master_add_child(master,
120 match->compare[i].fn,
121 match->compare[i].data);
122 if (ret)
123 break;
124 }
125 return ret;
126 }
127
128 /* Detach all attached components from this master */
129 static void master_remove_components(struct master *master)
130 {
131 while (!list_empty(&master->components)) {
132 struct component *c = list_first_entry(&master->components,
133 struct component, master_node);
134
135 WARN_ON(c->master != master);
136
137 component_detach_master(master, c);
138 }
139 }
140
141 /*
142 * Try to bring up a master. If component is NULL, we're interested in
143 * this master, otherwise it's a component which must be present to try
144 * and bring up the master.
145 *
146 * Returns 1 for successful bringup, 0 if not ready, or -ve errno.
147 */
148 static int try_to_bring_up_master(struct master *master,
149 struct component *component)
150 {
151 int ret;
152
153 if (find_components(master)) {
154 /* Failed to find all components */
155 ret = 0;
156 goto out;
157 }
158
159 if (component && component->master != master) {
160 ret = 0;
161 goto out;
162 }
163
164 if (!devres_open_group(master->dev, NULL, GFP_KERNEL)) {
165 ret = -ENOMEM;
166 goto out;
167 }
168
169 /* Found all components */
170 ret = master->ops->bind(master->dev);
171 if (ret < 0) {
172 devres_release_group(master->dev, NULL);
173 dev_info(master->dev, "master bind failed: %d\n", ret);
174 goto out;
175 }
176
177 master->bound = true;
178 return 1;
179
180 out:
181 master_remove_components(master);
182
183 return ret;
184 }
185
186 static int try_to_bring_up_masters(struct component *component)
187 {
188 struct master *m;
189 int ret = 0;
190
191 list_for_each_entry(m, &masters, node) {
192 if (!m->bound) {
193 ret = try_to_bring_up_master(m, component);
194 if (ret != 0)
195 break;
196 }
197 }
198
199 return ret;
200 }
201
202 static void take_down_master(struct master *master)
203 {
204 if (master->bound) {
205 master->ops->unbind(master->dev);
206 devres_release_group(master->dev, NULL);
207 master->bound = false;
208 }
209
210 master_remove_components(master);
211 }
212
213 static size_t component_match_size(size_t num)
214 {
215 return offsetof(struct component_match, compare[num]);
216 }
217
218 static struct component_match *component_match_realloc(struct device *dev,
219 struct component_match *match, size_t num)
220 {
221 struct component_match *new;
222
223 if (match && match->alloc == num)
224 return match;
225
226 new = devm_kmalloc(dev, component_match_size(num), GFP_KERNEL);
227 if (!new)
228 return ERR_PTR(-ENOMEM);
229
230 if (match) {
231 memcpy(new, match, component_match_size(min(match->num, num)));
232 devm_kfree(dev, match);
233 } else {
234 new->num = 0;
235 }
236
237 new->alloc = num;
238
239 return new;
240 }
241
242 /*
243 * Add a component to be matched.
244 *
245 * The match array is first created or extended if necessary.
246 */
247 void component_match_add(struct device *dev, struct component_match **matchptr,
248 int (*compare)(struct device *, void *), void *compare_data)
249 {
250 struct component_match *match = *matchptr;
251
252 if (IS_ERR(match))
253 return;
254
255 if (!match || match->num == match->alloc) {
256 size_t new_size = match ? match->alloc + 16 : 15;
257
258 match = component_match_realloc(dev, match, new_size);
259
260 *matchptr = match;
261
262 if (IS_ERR(match))
263 return;
264 }
265
266 match->compare[match->num].fn = compare;
267 match->compare[match->num].data = compare_data;
268 match->num++;
269 }
270 EXPORT_SYMBOL(component_match_add);
271
272 int component_master_add_with_match(struct device *dev,
273 const struct component_master_ops *ops,
274 struct component_match *match)
275 {
276 struct master *master;
277 int ret;
278
279 /* Reallocate the match array for its true size */
280 match = component_match_realloc(dev, match, match->num);
281 if (IS_ERR(match))
282 return PTR_ERR(match);
283
284 master = kzalloc(sizeof(*master), GFP_KERNEL);
285 if (!master)
286 return -ENOMEM;
287
288 master->dev = dev;
289 master->ops = ops;
290 master->match = match;
291 INIT_LIST_HEAD(&master->components);
292
293 /* Add to the list of available masters. */
294 mutex_lock(&component_mutex);
295 list_add(&master->node, &masters);
296
297 ret = try_to_bring_up_master(master, NULL);
298
299 if (ret < 0) {
300 /* Delete off the list if we weren't successful */
301 list_del(&master->node);
302 kfree(master);
303 }
304 mutex_unlock(&component_mutex);
305
306 return ret < 0 ? ret : 0;
307 }
308 EXPORT_SYMBOL_GPL(component_master_add_with_match);
309
310 void component_master_del(struct device *dev,
311 const struct component_master_ops *ops)
312 {
313 struct master *master;
314
315 mutex_lock(&component_mutex);
316 master = __master_find(dev, ops);
317 if (master) {
318 take_down_master(master);
319
320 list_del(&master->node);
321 kfree(master);
322 }
323 mutex_unlock(&component_mutex);
324 }
325 EXPORT_SYMBOL_GPL(component_master_del);
326
327 static void component_unbind(struct component *component,
328 struct master *master, void *data)
329 {
330 WARN_ON(!component->bound);
331
332 component->ops->unbind(component->dev, master->dev, data);
333 component->bound = false;
334
335 /* Release all resources claimed in the binding of this component */
336 devres_release_group(component->dev, component);
337 }
338
339 void component_unbind_all(struct device *master_dev, void *data)
340 {
341 struct master *master;
342 struct component *c;
343
344 WARN_ON(!mutex_is_locked(&component_mutex));
345
346 master = __master_find(master_dev, NULL);
347 if (!master)
348 return;
349
350 list_for_each_entry_reverse(c, &master->components, master_node)
351 component_unbind(c, master, data);
352 }
353 EXPORT_SYMBOL_GPL(component_unbind_all);
354
355 static int component_bind(struct component *component, struct master *master,
356 void *data)
357 {
358 int ret;
359
360 /*
361 * Each component initialises inside its own devres group.
362 * This allows us to roll-back a failed component without
363 * affecting anything else.
364 */
365 if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
366 return -ENOMEM;
367
368 /*
369 * Also open a group for the device itself: this allows us
370 * to release the resources claimed against the sub-device
371 * at the appropriate moment.
372 */
373 if (!devres_open_group(component->dev, component, GFP_KERNEL)) {
374 devres_release_group(master->dev, NULL);
375 return -ENOMEM;
376 }
377
378 dev_dbg(master->dev, "binding %s (ops %ps)\n",
379 dev_name(component->dev), component->ops);
380
381 ret = component->ops->bind(component->dev, master->dev, data);
382 if (!ret) {
383 component->bound = true;
384
385 /*
386 * Close the component device's group so that resources
387 * allocated in the binding are encapsulated for removal
388 * at unbind. Remove the group on the DRM device as we
389 * can clean those resources up independently.
390 */
391 devres_close_group(component->dev, NULL);
392 devres_remove_group(master->dev, NULL);
393
394 dev_info(master->dev, "bound %s (ops %ps)\n",
395 dev_name(component->dev), component->ops);
396 } else {
397 devres_release_group(component->dev, NULL);
398 devres_release_group(master->dev, NULL);
399
400 dev_err(master->dev, "failed to bind %s (ops %ps): %d\n",
401 dev_name(component->dev), component->ops, ret);
402 }
403
404 return ret;
405 }
406
407 int component_bind_all(struct device *master_dev, void *data)
408 {
409 struct master *master;
410 struct component *c;
411 int ret = 0;
412
413 WARN_ON(!mutex_is_locked(&component_mutex));
414
415 master = __master_find(master_dev, NULL);
416 if (!master)
417 return -EINVAL;
418
419 list_for_each_entry(c, &master->components, master_node) {
420 ret = component_bind(c, master, data);
421 if (ret)
422 break;
423 }
424
425 if (ret != 0) {
426 list_for_each_entry_continue_reverse(c, &master->components,
427 master_node)
428 component_unbind(c, master, data);
429 }
430
431 return ret;
432 }
433 EXPORT_SYMBOL_GPL(component_bind_all);
434
435 int component_add(struct device *dev, const struct component_ops *ops)
436 {
437 struct component *component;
438 int ret;
439
440 component = kzalloc(sizeof(*component), GFP_KERNEL);
441 if (!component)
442 return -ENOMEM;
443
444 component->ops = ops;
445 component->dev = dev;
446
447 dev_dbg(dev, "adding component (ops %ps)\n", ops);
448
449 mutex_lock(&component_mutex);
450 list_add_tail(&component->node, &component_list);
451
452 ret = try_to_bring_up_masters(component);
453 if (ret < 0) {
454 list_del(&component->node);
455
456 kfree(component);
457 }
458 mutex_unlock(&component_mutex);
459
460 return ret < 0 ? ret : 0;
461 }
462 EXPORT_SYMBOL_GPL(component_add);
463
464 void component_del(struct device *dev, const struct component_ops *ops)
465 {
466 struct component *c, *component = NULL;
467
468 mutex_lock(&component_mutex);
469 list_for_each_entry(c, &component_list, node)
470 if (c->dev == dev && c->ops == ops) {
471 list_del(&c->node);
472 component = c;
473 break;
474 }
475
476 if (component && component->master)
477 take_down_master(component->master);
478
479 mutex_unlock(&component_mutex);
480
481 WARN_ON(!component);
482 kfree(component);
483 }
484 EXPORT_SYMBOL_GPL(component_del);
485
486 MODULE_LICENSE("GPL v2");
This page took 0.040844 seconds and 5 git commands to generate.