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_array {
25 int (*compare)(struct device *, void *);
26 void (*release)(struct device *, void *);
27 struct component *component;
31 struct component_match {
34 struct component_match_array *compare;
38 struct list_head node;
41 const struct component_master_ops *ops;
43 struct component_match *match;
47 struct list_head node;
48 struct master *master;
51 const struct component_ops *ops;
55 static DEFINE_MUTEX(component_mutex);
56 static LIST_HEAD(component_list);
57 static LIST_HEAD(masters);
59 static struct master *__master_find(struct device *dev,
60 const struct component_master_ops *ops)
64 list_for_each_entry(m, &masters, node)
65 if (m->dev == dev && (!ops || m->ops == ops))
71 static struct component *find_component(struct master *master,
72 int (*compare)(struct device *, void *), void *compare_data)
76 list_for_each_entry(c, &component_list, node) {
77 if (c->master && c->master != master)
80 if (compare(c->dev, compare_data))
87 static int find_components(struct master *master)
89 struct component_match *match = master->match;
94 * Scan the array of match functions and attach
95 * any components which are found to this master.
97 for (i = 0; i < match->num; i++) {
98 struct component_match_array *mc = &match->compare[i];
101 dev_dbg(master->dev, "Looking for component %zu\n", i);
103 if (match->compare[i].component)
106 c = find_component(master, mc->compare, mc->data);
112 dev_dbg(master->dev, "found component %s, duplicate %u\n", dev_name(c->dev), !!c->master);
114 /* Attach this component to the master */
115 match->compare[i].duplicate = !!c->master;
116 match->compare[i].component = c;
122 /* Detach component from associated master */
123 static void remove_component(struct master *master, struct component *c)
127 /* Detach the component from this master. */
128 for (i = 0; i < master->match->num; i++)
129 if (master->match->compare[i].component == c)
130 master->match->compare[i].component = NULL;
134 * Try to bring up a master. If component is NULL, we're interested in
135 * this master, otherwise it's a component which must be present to try
136 * and bring up the master.
138 * Returns 1 for successful bringup, 0 if not ready, or -ve errno.
140 static int try_to_bring_up_master(struct master *master,
141 struct component *component)
145 dev_dbg(master->dev, "trying to bring up master\n");
147 if (find_components(master)) {
148 dev_dbg(master->dev, "master has incomplete components\n");
152 if (component && component->master != master) {
153 dev_dbg(master->dev, "master is not for this component (%s)\n",
154 dev_name(component->dev));
158 if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
161 /* Found all components */
162 ret = master->ops->bind(master->dev);
164 devres_release_group(master->dev, NULL);
165 dev_info(master->dev, "master bind failed: %d\n", ret);
169 master->bound = true;
173 static int try_to_bring_up_masters(struct component *component)
178 list_for_each_entry(m, &masters, node) {
180 ret = try_to_bring_up_master(m, component);
189 static void take_down_master(struct master *master)
192 master->ops->unbind(master->dev);
193 devres_release_group(master->dev, NULL);
194 master->bound = false;
198 static void component_match_release(struct device *master,
199 struct component_match *match)
203 for (i = 0; i < match->num; i++) {
204 struct component_match_array *mc = &match->compare[i];
207 mc->release(master, mc->data);
210 kfree(match->compare);
213 static void devm_component_match_release(struct device *dev, void *res)
215 component_match_release(dev, res);
218 static int component_match_realloc(struct device *dev,
219 struct component_match *match, size_t num)
221 struct component_match_array *new;
223 if (match->alloc == num)
226 new = kmalloc_array(num, sizeof(*new), GFP_KERNEL);
230 if (match->compare) {
231 memcpy(new, match->compare, sizeof(*new) *
232 min(match->num, num));
233 kfree(match->compare);
235 match->compare = new;
242 * Add a component to be matched, with a release function.
244 * The match array is first created or extended if necessary.
246 void component_match_add_release(struct device *master,
247 struct component_match **matchptr,
248 void (*release)(struct device *, void *),
249 int (*compare)(struct device *, void *), void *compare_data)
251 struct component_match *match = *matchptr;
257 match = devres_alloc(devm_component_match_release,
258 sizeof(*match), GFP_KERNEL);
260 *matchptr = ERR_PTR(-ENOMEM);
264 devres_add(master, match);
269 if (match->num == match->alloc) {
270 size_t new_size = match ? match->alloc + 16 : 15;
273 ret = component_match_realloc(master, match, new_size);
275 *matchptr = ERR_PTR(ret);
280 match->compare[match->num].compare = compare;
281 match->compare[match->num].release = release;
282 match->compare[match->num].data = compare_data;
283 match->compare[match->num].component = NULL;
286 EXPORT_SYMBOL(component_match_add_release);
288 int component_master_add_with_match(struct device *dev,
289 const struct component_master_ops *ops,
290 struct component_match *match)
292 struct master *master;
295 /* Reallocate the match array for its true size */
296 ret = component_match_realloc(dev, match, match->num);
300 master = kzalloc(sizeof(*master), GFP_KERNEL);
306 master->match = match;
308 /* Add to the list of available masters. */
309 mutex_lock(&component_mutex);
310 list_add(&master->node, &masters);
312 ret = try_to_bring_up_master(master, NULL);
315 /* Delete off the list if we weren't successful */
316 list_del(&master->node);
319 mutex_unlock(&component_mutex);
321 return ret < 0 ? ret : 0;
323 EXPORT_SYMBOL_GPL(component_master_add_with_match);
325 void component_master_del(struct device *dev,
326 const struct component_master_ops *ops)
328 struct master *master;
331 mutex_lock(&component_mutex);
332 master = __master_find(dev, ops);
334 struct component_match *match = master->match;
336 take_down_master(master);
338 list_del(&master->node);
341 for (i = 0; i < match->num; i++) {
342 struct component *c = match->compare[i].component;
349 mutex_unlock(&component_mutex);
351 EXPORT_SYMBOL_GPL(component_master_del);
353 static void component_unbind(struct component *component,
354 struct master *master, void *data)
356 WARN_ON(!component->bound);
358 component->ops->unbind(component->dev, master->dev, data);
359 component->bound = false;
361 /* Release all resources claimed in the binding of this component */
362 devres_release_group(component->dev, component);
365 void component_unbind_all(struct device *master_dev, void *data)
367 struct master *master;
371 WARN_ON(!mutex_is_locked(&component_mutex));
373 master = __master_find(master_dev, NULL);
377 /* Unbind components in reverse order */
378 for (i = master->match->num; i--; )
379 if (!master->match->compare[i].duplicate) {
380 c = master->match->compare[i].component;
381 component_unbind(c, master, data);
384 EXPORT_SYMBOL_GPL(component_unbind_all);
386 static int component_bind(struct component *component, struct master *master,
392 * Each component initialises inside its own devres group.
393 * This allows us to roll-back a failed component without
394 * affecting anything else.
396 if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
400 * Also open a group for the device itself: this allows us
401 * to release the resources claimed against the sub-device
402 * at the appropriate moment.
404 if (!devres_open_group(component->dev, component, GFP_KERNEL)) {
405 devres_release_group(master->dev, NULL);
409 dev_dbg(master->dev, "binding %s (ops %ps)\n",
410 dev_name(component->dev), component->ops);
412 ret = component->ops->bind(component->dev, master->dev, data);
414 component->bound = true;
417 * Close the component device's group so that resources
418 * allocated in the binding are encapsulated for removal
419 * at unbind. Remove the group on the DRM device as we
420 * can clean those resources up independently.
422 devres_close_group(component->dev, NULL);
423 devres_remove_group(master->dev, NULL);
425 dev_info(master->dev, "bound %s (ops %ps)\n",
426 dev_name(component->dev), component->ops);
428 devres_release_group(component->dev, NULL);
429 devres_release_group(master->dev, NULL);
431 dev_err(master->dev, "failed to bind %s (ops %ps): %d\n",
432 dev_name(component->dev), component->ops, ret);
438 int component_bind_all(struct device *master_dev, void *data)
440 struct master *master;
445 WARN_ON(!mutex_is_locked(&component_mutex));
447 master = __master_find(master_dev, NULL);
451 /* Bind components in match order */
452 for (i = 0; i < master->match->num; i++)
453 if (!master->match->compare[i].duplicate) {
454 c = master->match->compare[i].component;
455 ret = component_bind(c, master, data);
462 if (!master->match->compare[i].duplicate) {
463 c = master->match->compare[i].component;
464 component_unbind(c, master, data);
470 EXPORT_SYMBOL_GPL(component_bind_all);
472 int component_add(struct device *dev, const struct component_ops *ops)
474 struct component *component;
477 component = kzalloc(sizeof(*component), GFP_KERNEL);
481 component->ops = ops;
482 component->dev = dev;
484 dev_dbg(dev, "adding component (ops %ps)\n", ops);
486 mutex_lock(&component_mutex);
487 list_add_tail(&component->node, &component_list);
489 ret = try_to_bring_up_masters(component);
491 list_del(&component->node);
495 mutex_unlock(&component_mutex);
497 return ret < 0 ? ret : 0;
499 EXPORT_SYMBOL_GPL(component_add);
501 void component_del(struct device *dev, const struct component_ops *ops)
503 struct component *c, *component = NULL;
505 mutex_lock(&component_mutex);
506 list_for_each_entry(c, &component_list, node)
507 if (c->dev == dev && c->ops == ops) {
513 if (component && component->master) {
514 take_down_master(component->master);
515 remove_component(component->master, component);
518 mutex_unlock(&component_mutex);
523 EXPORT_SYMBOL_GPL(component_del);
525 MODULE_LICENSE("GPL v2");