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 if (ret != -EPROBE_DEFER)
166 dev_info(master->dev, "master bind failed: %d\n", ret);
170 master->bound = true;
174 static int try_to_bring_up_masters(struct component *component)
179 list_for_each_entry(m, &masters, node) {
181 ret = try_to_bring_up_master(m, component);
190 static void take_down_master(struct master *master)
193 master->ops->unbind(master->dev);
194 devres_release_group(master->dev, NULL);
195 master->bound = false;
199 static void component_match_release(struct device *master,
200 struct component_match *match)
204 for (i = 0; i < match->num; i++) {
205 struct component_match_array *mc = &match->compare[i];
208 mc->release(master, mc->data);
211 kfree(match->compare);
214 static void devm_component_match_release(struct device *dev, void *res)
216 component_match_release(dev, res);
219 static int component_match_realloc(struct device *dev,
220 struct component_match *match, size_t num)
222 struct component_match_array *new;
224 if (match->alloc == num)
227 new = kmalloc_array(num, sizeof(*new), GFP_KERNEL);
231 if (match->compare) {
232 memcpy(new, match->compare, sizeof(*new) *
233 min(match->num, num));
234 kfree(match->compare);
236 match->compare = new;
243 * Add a component to be matched, with a release function.
245 * The match array is first created or extended if necessary.
247 void component_match_add_release(struct device *master,
248 struct component_match **matchptr,
249 void (*release)(struct device *, void *),
250 int (*compare)(struct device *, void *), void *compare_data)
252 struct component_match *match = *matchptr;
258 match = devres_alloc(devm_component_match_release,
259 sizeof(*match), GFP_KERNEL);
261 *matchptr = ERR_PTR(-ENOMEM);
265 devres_add(master, match);
270 if (match->num == match->alloc) {
271 size_t new_size = match->alloc + 16;
274 ret = component_match_realloc(master, match, new_size);
276 *matchptr = ERR_PTR(ret);
281 match->compare[match->num].compare = compare;
282 match->compare[match->num].release = release;
283 match->compare[match->num].data = compare_data;
284 match->compare[match->num].component = NULL;
287 EXPORT_SYMBOL(component_match_add_release);
289 static void free_master(struct master *master)
291 struct component_match *match = master->match;
294 list_del(&master->node);
297 for (i = 0; i < match->num; i++) {
298 struct component *c = match->compare[i].component;
307 int component_master_add_with_match(struct device *dev,
308 const struct component_master_ops *ops,
309 struct component_match *match)
311 struct master *master;
314 /* Reallocate the match array for its true size */
315 ret = component_match_realloc(dev, match, match->num);
319 master = kzalloc(sizeof(*master), GFP_KERNEL);
325 master->match = match;
327 /* Add to the list of available masters. */
328 mutex_lock(&component_mutex);
329 list_add(&master->node, &masters);
331 ret = try_to_bring_up_master(master, NULL);
336 mutex_unlock(&component_mutex);
338 return ret < 0 ? ret : 0;
340 EXPORT_SYMBOL_GPL(component_master_add_with_match);
342 void component_master_del(struct device *dev,
343 const struct component_master_ops *ops)
345 struct master *master;
347 mutex_lock(&component_mutex);
348 master = __master_find(dev, ops);
350 take_down_master(master);
353 mutex_unlock(&component_mutex);
355 EXPORT_SYMBOL_GPL(component_master_del);
357 static void component_unbind(struct component *component,
358 struct master *master, void *data)
360 WARN_ON(!component->bound);
362 component->ops->unbind(component->dev, master->dev, data);
363 component->bound = false;
365 /* Release all resources claimed in the binding of this component */
366 devres_release_group(component->dev, component);
369 void component_unbind_all(struct device *master_dev, void *data)
371 struct master *master;
375 WARN_ON(!mutex_is_locked(&component_mutex));
377 master = __master_find(master_dev, NULL);
381 /* Unbind components in reverse order */
382 for (i = master->match->num; i--; )
383 if (!master->match->compare[i].duplicate) {
384 c = master->match->compare[i].component;
385 component_unbind(c, master, data);
388 EXPORT_SYMBOL_GPL(component_unbind_all);
390 static int component_bind(struct component *component, struct master *master,
396 * Each component initialises inside its own devres group.
397 * This allows us to roll-back a failed component without
398 * affecting anything else.
400 if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
404 * Also open a group for the device itself: this allows us
405 * to release the resources claimed against the sub-device
406 * at the appropriate moment.
408 if (!devres_open_group(component->dev, component, GFP_KERNEL)) {
409 devres_release_group(master->dev, NULL);
413 dev_dbg(master->dev, "binding %s (ops %ps)\n",
414 dev_name(component->dev), component->ops);
416 ret = component->ops->bind(component->dev, master->dev, data);
418 component->bound = true;
421 * Close the component device's group so that resources
422 * allocated in the binding are encapsulated for removal
423 * at unbind. Remove the group on the DRM device as we
424 * can clean those resources up independently.
426 devres_close_group(component->dev, NULL);
427 devres_remove_group(master->dev, NULL);
429 dev_info(master->dev, "bound %s (ops %ps)\n",
430 dev_name(component->dev), component->ops);
432 devres_release_group(component->dev, NULL);
433 devres_release_group(master->dev, NULL);
435 if (ret != -EPROBE_DEFER)
436 dev_err(master->dev, "failed to bind %s (ops %ps): %d\n",
437 dev_name(component->dev), component->ops, ret);
443 int component_bind_all(struct device *master_dev, void *data)
445 struct master *master;
450 WARN_ON(!mutex_is_locked(&component_mutex));
452 master = __master_find(master_dev, NULL);
456 /* Bind components in match order */
457 for (i = 0; i < master->match->num; i++)
458 if (!master->match->compare[i].duplicate) {
459 c = master->match->compare[i].component;
460 ret = component_bind(c, master, data);
467 if (!master->match->compare[i - 1].duplicate) {
468 c = master->match->compare[i - 1].component;
469 component_unbind(c, master, data);
475 EXPORT_SYMBOL_GPL(component_bind_all);
477 int component_add(struct device *dev, const struct component_ops *ops)
479 struct component *component;
482 component = kzalloc(sizeof(*component), GFP_KERNEL);
486 component->ops = ops;
487 component->dev = dev;
489 dev_dbg(dev, "adding component (ops %ps)\n", ops);
491 mutex_lock(&component_mutex);
492 list_add_tail(&component->node, &component_list);
494 ret = try_to_bring_up_masters(component);
496 if (component->master)
497 remove_component(component->master, component);
498 list_del(&component->node);
502 mutex_unlock(&component_mutex);
504 return ret < 0 ? ret : 0;
506 EXPORT_SYMBOL_GPL(component_add);
508 void component_del(struct device *dev, const struct component_ops *ops)
510 struct component *c, *component = NULL;
512 mutex_lock(&component_mutex);
513 list_for_each_entry(c, &component_list, node)
514 if (c->dev == dev && c->ops == ops) {
520 if (component && component->master) {
521 take_down_master(component->master);
522 remove_component(component->master, component);
525 mutex_unlock(&component_mutex);
530 EXPORT_SYMBOL_GPL(component_del);
532 MODULE_LICENSE("GPL v2");