2 * Componentized device handling.
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.
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
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>
23 struct component_match {
28 int (*fn)(struct device *, void *);
29 struct component *component;
35 struct list_head node;
38 const struct component_master_ops *ops;
40 struct component_match *match;
44 struct list_head node;
45 struct master *master;
48 const struct component_ops *ops;
52 static DEFINE_MUTEX(component_mutex);
53 static LIST_HEAD(component_list);
54 static LIST_HEAD(masters);
56 static struct master *__master_find(struct device *dev,
57 const struct component_master_ops *ops)
61 list_for_each_entry(m, &masters, node)
62 if (m->dev == dev && (!ops || m->ops == ops))
68 static struct component *find_component(struct master *master,
69 int (*compare)(struct device *, void *), void *compare_data)
73 list_for_each_entry(c, &component_list, node) {
74 if (c->master && c->master != master)
77 if (compare(c->dev, compare_data))
84 static int find_components(struct master *master)
86 struct component_match *match = master->match;
91 * Scan the array of match functions and attach
92 * any components which are found to this master.
94 for (i = 0; i < match->num; i++) {
97 dev_dbg(master->dev, "Looking for component %zu\n", i);
99 if (match->compare[i].component)
102 c = find_component(master, match->compare[i].fn,
103 match->compare[i].data);
109 dev_dbg(master->dev, "found component %s, duplicate %u\n", dev_name(c->dev), !!c->master);
111 /* Attach this component to the master */
112 match->compare[i].duplicate = !!c->master;
113 match->compare[i].component = c;
119 /* Detach component from associated master */
120 static void remove_component(struct master *master, struct component *c)
124 /* Detach the component from this master. */
125 for (i = 0; i < master->match->num; i++)
126 if (master->match->compare[i].component == c)
127 master->match->compare[i].component = NULL;
131 * Try to bring up a master. If component is NULL, we're interested in
132 * this master, otherwise it's a component which must be present to try
133 * and bring up the master.
135 * Returns 1 for successful bringup, 0 if not ready, or -ve errno.
137 static int try_to_bring_up_master(struct master *master,
138 struct component *component)
142 dev_dbg(master->dev, "trying to bring up master\n");
144 if (find_components(master)) {
145 dev_dbg(master->dev, "master has incomplete components\n");
149 if (component && component->master != master) {
150 dev_dbg(master->dev, "master is not for this component (%s)\n",
151 dev_name(component->dev));
155 if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
158 /* Found all components */
159 ret = master->ops->bind(master->dev);
161 devres_release_group(master->dev, NULL);
162 dev_info(master->dev, "master bind failed: %d\n", ret);
166 master->bound = true;
170 static int try_to_bring_up_masters(struct component *component)
175 list_for_each_entry(m, &masters, node) {
177 ret = try_to_bring_up_master(m, component);
186 static void take_down_master(struct master *master)
189 master->ops->unbind(master->dev);
190 devres_release_group(master->dev, NULL);
191 master->bound = false;
195 static size_t component_match_size(size_t num)
197 return offsetof(struct component_match, compare[num]);
200 static struct component_match *component_match_realloc(struct device *dev,
201 struct component_match *match, size_t num)
203 struct component_match *new;
205 if (match && match->alloc == num)
208 new = devm_kmalloc(dev, component_match_size(num), GFP_KERNEL);
210 return ERR_PTR(-ENOMEM);
213 memcpy(new, match, component_match_size(min(match->num, num)));
214 devm_kfree(dev, match);
225 * Add a component to be matched.
227 * The match array is first created or extended if necessary.
229 void component_match_add(struct device *dev, struct component_match **matchptr,
230 int (*compare)(struct device *, void *), void *compare_data)
232 struct component_match *match = *matchptr;
237 if (!match || match->num == match->alloc) {
238 size_t new_size = match ? match->alloc + 16 : 15;
240 match = component_match_realloc(dev, match, new_size);
248 match->compare[match->num].fn = compare;
249 match->compare[match->num].data = compare_data;
250 match->compare[match->num].component = NULL;
253 EXPORT_SYMBOL(component_match_add);
255 int component_master_add_with_match(struct device *dev,
256 const struct component_master_ops *ops,
257 struct component_match *match)
259 struct master *master;
262 /* Reallocate the match array for its true size */
263 match = component_match_realloc(dev, match, match->num);
265 return PTR_ERR(match);
267 master = kzalloc(sizeof(*master), GFP_KERNEL);
273 master->match = match;
275 /* Add to the list of available masters. */
276 mutex_lock(&component_mutex);
277 list_add(&master->node, &masters);
279 ret = try_to_bring_up_master(master, NULL);
282 /* Delete off the list if we weren't successful */
283 list_del(&master->node);
286 mutex_unlock(&component_mutex);
288 return ret < 0 ? ret : 0;
290 EXPORT_SYMBOL_GPL(component_master_add_with_match);
292 void component_master_del(struct device *dev,
293 const struct component_master_ops *ops)
295 struct master *master;
298 mutex_lock(&component_mutex);
299 master = __master_find(dev, ops);
301 struct component_match *match = master->match;
303 take_down_master(master);
305 list_del(&master->node);
308 for (i = 0; i < match->num; i++) {
309 struct component *c = match->compare[i].component;
316 mutex_unlock(&component_mutex);
318 EXPORT_SYMBOL_GPL(component_master_del);
320 static void component_unbind(struct component *component,
321 struct master *master, void *data)
323 WARN_ON(!component->bound);
325 component->ops->unbind(component->dev, master->dev, data);
326 component->bound = false;
328 /* Release all resources claimed in the binding of this component */
329 devres_release_group(component->dev, component);
332 void component_unbind_all(struct device *master_dev, void *data)
334 struct master *master;
338 WARN_ON(!mutex_is_locked(&component_mutex));
340 master = __master_find(master_dev, NULL);
344 /* Unbind components in reverse order */
345 for (i = master->match->num; i--; )
346 if (!master->match->compare[i].duplicate) {
347 c = master->match->compare[i].component;
348 component_unbind(c, master, data);
351 EXPORT_SYMBOL_GPL(component_unbind_all);
353 static int component_bind(struct component *component, struct master *master,
359 * Each component initialises inside its own devres group.
360 * This allows us to roll-back a failed component without
361 * affecting anything else.
363 if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
367 * Also open a group for the device itself: this allows us
368 * to release the resources claimed against the sub-device
369 * at the appropriate moment.
371 if (!devres_open_group(component->dev, component, GFP_KERNEL)) {
372 devres_release_group(master->dev, NULL);
376 dev_dbg(master->dev, "binding %s (ops %ps)\n",
377 dev_name(component->dev), component->ops);
379 ret = component->ops->bind(component->dev, master->dev, data);
381 component->bound = true;
384 * Close the component device's group so that resources
385 * allocated in the binding are encapsulated for removal
386 * at unbind. Remove the group on the DRM device as we
387 * can clean those resources up independently.
389 devres_close_group(component->dev, NULL);
390 devres_remove_group(master->dev, NULL);
392 dev_info(master->dev, "bound %s (ops %ps)\n",
393 dev_name(component->dev), component->ops);
395 devres_release_group(component->dev, NULL);
396 devres_release_group(master->dev, NULL);
398 dev_err(master->dev, "failed to bind %s (ops %ps): %d\n",
399 dev_name(component->dev), component->ops, ret);
405 int component_bind_all(struct device *master_dev, void *data)
407 struct master *master;
412 WARN_ON(!mutex_is_locked(&component_mutex));
414 master = __master_find(master_dev, NULL);
418 /* Bind components in match order */
419 for (i = 0; i < master->match->num; i++)
420 if (!master->match->compare[i].duplicate) {
421 c = master->match->compare[i].component;
422 ret = component_bind(c, master, data);
429 if (!master->match->compare[i].duplicate) {
430 c = master->match->compare[i].component;
431 component_unbind(c, master, data);
437 EXPORT_SYMBOL_GPL(component_bind_all);
439 int component_add(struct device *dev, const struct component_ops *ops)
441 struct component *component;
444 component = kzalloc(sizeof(*component), GFP_KERNEL);
448 component->ops = ops;
449 component->dev = dev;
451 dev_dbg(dev, "adding component (ops %ps)\n", ops);
453 mutex_lock(&component_mutex);
454 list_add_tail(&component->node, &component_list);
456 ret = try_to_bring_up_masters(component);
458 list_del(&component->node);
462 mutex_unlock(&component_mutex);
464 return ret < 0 ? ret : 0;
466 EXPORT_SYMBOL_GPL(component_add);
468 void component_del(struct device *dev, const struct component_ops *ops)
470 struct component *c, *component = NULL;
472 mutex_lock(&component_mutex);
473 list_for_each_entry(c, &component_list, node)
474 if (c->dev == dev && c->ops == ops) {
480 if (component && component->master) {
481 take_down_master(component->master);
482 remove_component(component->master, component);
485 mutex_unlock(&component_mutex);
490 EXPORT_SYMBOL_GPL(component_del);
492 MODULE_LICENSE("GPL v2");