1 // SPDX-License-Identifier: GPL-2.0
3 * Componentized device handling.
5 * This is work in progress. We gather up the component devices into a list,
6 * and bind them when instructed. At the moment, we're specific to the DRM
7 * subsystem, and only handles one master device, but this doesn't have to be
10 #include <linux/component.h>
11 #include <linux/device.h>
12 #include <linux/kref.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/slab.h>
17 #include <linux/debugfs.h>
21 struct component_match_array {
23 int (*compare)(struct device *, void *);
24 void (*release)(struct device *, void *);
25 struct component *component;
29 struct component_match {
32 struct component_match_array *compare;
36 struct list_head node;
39 const struct component_master_ops *ops;
41 struct component_match *match;
42 struct dentry *dentry;
46 struct list_head node;
47 struct master *master;
50 const struct component_ops *ops;
54 static DEFINE_MUTEX(component_mutex);
55 static LIST_HEAD(component_list);
56 static LIST_HEAD(masters);
58 #ifdef CONFIG_DEBUG_FS
60 static struct dentry *component_debugfs_dir;
62 static int component_devices_show(struct seq_file *s, void *data)
64 struct master *m = s->private;
65 struct component_match *match = m->match;
68 mutex_lock(&component_mutex);
69 seq_printf(s, "%-40s %20s\n", "master name", "status");
70 seq_puts(s, "-------------------------------------------------------------\n");
71 seq_printf(s, "%-40s %20s\n\n",
72 dev_name(m->dev), m->bound ? "bound" : "not bound");
74 seq_printf(s, "%-40s %20s\n", "device name", "status");
75 seq_puts(s, "-------------------------------------------------------------\n");
76 for (i = 0; i < match->num; i++) {
77 struct device *d = (struct device *)match->compare[i].data;
79 seq_printf(s, "%-40s %20s\n", dev_name(d),
80 match->compare[i].component ?
81 "registered" : "not registered");
83 mutex_unlock(&component_mutex);
88 DEFINE_SHOW_ATTRIBUTE(component_devices);
90 static int __init component_debug_init(void)
92 component_debugfs_dir = debugfs_create_dir("device_component", NULL);
97 core_initcall(component_debug_init);
99 static void component_master_debugfs_add(struct master *m)
101 m->dentry = debugfs_create_file(dev_name(m->dev), 0444,
102 component_debugfs_dir,
103 m, &component_devices_fops);
106 static void component_master_debugfs_del(struct master *m)
108 debugfs_remove(m->dentry);
114 static void component_master_debugfs_add(struct master *m)
117 static void component_master_debugfs_del(struct master *m)
122 static struct master *__master_find(struct device *dev,
123 const struct component_master_ops *ops)
127 list_for_each_entry(m, &masters, node)
128 if (m->dev == dev && (!ops || m->ops == ops))
134 static struct component *find_component(struct master *master,
135 int (*compare)(struct device *, void *), void *compare_data)
139 list_for_each_entry(c, &component_list, node) {
140 if (c->master && c->master != master)
143 if (compare(c->dev, compare_data))
150 static int find_components(struct master *master)
152 struct component_match *match = master->match;
157 * Scan the array of match functions and attach
158 * any components which are found to this master.
160 for (i = 0; i < match->num; i++) {
161 struct component_match_array *mc = &match->compare[i];
164 dev_dbg(master->dev, "Looking for component %zu\n", i);
166 if (match->compare[i].component)
169 c = find_component(master, mc->compare, mc->data);
175 dev_dbg(master->dev, "found component %s, duplicate %u\n", dev_name(c->dev), !!c->master);
177 /* Attach this component to the master */
178 match->compare[i].duplicate = !!c->master;
179 match->compare[i].component = c;
185 /* Detach component from associated master */
186 static void remove_component(struct master *master, struct component *c)
190 /* Detach the component from this master. */
191 for (i = 0; i < master->match->num; i++)
192 if (master->match->compare[i].component == c)
193 master->match->compare[i].component = NULL;
197 * Try to bring up a master. If component is NULL, we're interested in
198 * this master, otherwise it's a component which must be present to try
199 * and bring up the master.
201 * Returns 1 for successful bringup, 0 if not ready, or -ve errno.
203 static int try_to_bring_up_master(struct master *master,
204 struct component *component)
208 dev_dbg(master->dev, "trying to bring up master\n");
210 if (find_components(master)) {
211 dev_dbg(master->dev, "master has incomplete components\n");
215 if (component && component->master != master) {
216 dev_dbg(master->dev, "master is not for this component (%s)\n",
217 dev_name(component->dev));
221 if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
224 /* Found all components */
225 ret = master->ops->bind(master->dev);
227 devres_release_group(master->dev, NULL);
228 dev_info(master->dev, "master bind failed: %d\n", ret);
232 master->bound = true;
236 static int try_to_bring_up_masters(struct component *component)
241 list_for_each_entry(m, &masters, node) {
243 ret = try_to_bring_up_master(m, component);
252 static void take_down_master(struct master *master)
255 master->ops->unbind(master->dev);
256 devres_release_group(master->dev, NULL);
257 master->bound = false;
261 static void component_match_release(struct device *master,
262 struct component_match *match)
266 for (i = 0; i < match->num; i++) {
267 struct component_match_array *mc = &match->compare[i];
270 mc->release(master, mc->data);
273 kfree(match->compare);
276 static void devm_component_match_release(struct device *dev, void *res)
278 component_match_release(dev, res);
281 static int component_match_realloc(struct device *dev,
282 struct component_match *match, size_t num)
284 struct component_match_array *new;
286 if (match->alloc == num)
289 new = kmalloc_array(num, sizeof(*new), GFP_KERNEL);
293 if (match->compare) {
294 memcpy(new, match->compare, sizeof(*new) *
295 min(match->num, num));
296 kfree(match->compare);
298 match->compare = new;
305 * Add a component to be matched, with a release function.
307 * The match array is first created or extended if necessary.
309 void component_match_add_release(struct device *master,
310 struct component_match **matchptr,
311 void (*release)(struct device *, void *),
312 int (*compare)(struct device *, void *), void *compare_data)
314 struct component_match *match = *matchptr;
320 match = devres_alloc(devm_component_match_release,
321 sizeof(*match), GFP_KERNEL);
323 *matchptr = ERR_PTR(-ENOMEM);
327 devres_add(master, match);
332 if (match->num == match->alloc) {
333 size_t new_size = match->alloc + 16;
336 ret = component_match_realloc(master, match, new_size);
338 *matchptr = ERR_PTR(ret);
343 match->compare[match->num].compare = compare;
344 match->compare[match->num].release = release;
345 match->compare[match->num].data = compare_data;
346 match->compare[match->num].component = NULL;
349 EXPORT_SYMBOL(component_match_add_release);
351 static void free_master(struct master *master)
353 struct component_match *match = master->match;
356 component_master_debugfs_del(master);
357 list_del(&master->node);
360 for (i = 0; i < match->num; i++) {
361 struct component *c = match->compare[i].component;
370 int component_master_add_with_match(struct device *dev,
371 const struct component_master_ops *ops,
372 struct component_match *match)
374 struct master *master;
377 /* Reallocate the match array for its true size */
378 ret = component_match_realloc(dev, match, match->num);
382 master = kzalloc(sizeof(*master), GFP_KERNEL);
388 master->match = match;
390 component_master_debugfs_add(master);
391 /* Add to the list of available masters. */
392 mutex_lock(&component_mutex);
393 list_add(&master->node, &masters);
395 ret = try_to_bring_up_master(master, NULL);
400 mutex_unlock(&component_mutex);
402 return ret < 0 ? ret : 0;
404 EXPORT_SYMBOL_GPL(component_master_add_with_match);
406 void component_master_del(struct device *dev,
407 const struct component_master_ops *ops)
409 struct master *master;
411 mutex_lock(&component_mutex);
412 master = __master_find(dev, ops);
414 take_down_master(master);
417 mutex_unlock(&component_mutex);
419 EXPORT_SYMBOL_GPL(component_master_del);
421 static void component_unbind(struct component *component,
422 struct master *master, void *data)
424 WARN_ON(!component->bound);
426 component->ops->unbind(component->dev, master->dev, data);
427 component->bound = false;
429 /* Release all resources claimed in the binding of this component */
430 devres_release_group(component->dev, component);
433 void component_unbind_all(struct device *master_dev, void *data)
435 struct master *master;
439 WARN_ON(!mutex_is_locked(&component_mutex));
441 master = __master_find(master_dev, NULL);
445 /* Unbind components in reverse order */
446 for (i = master->match->num; i--; )
447 if (!master->match->compare[i].duplicate) {
448 c = master->match->compare[i].component;
449 component_unbind(c, master, data);
452 EXPORT_SYMBOL_GPL(component_unbind_all);
454 static int component_bind(struct component *component, struct master *master,
460 * Each component initialises inside its own devres group.
461 * This allows us to roll-back a failed component without
462 * affecting anything else.
464 if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
468 * Also open a group for the device itself: this allows us
469 * to release the resources claimed against the sub-device
470 * at the appropriate moment.
472 if (!devres_open_group(component->dev, component, GFP_KERNEL)) {
473 devres_release_group(master->dev, NULL);
477 dev_dbg(master->dev, "binding %s (ops %ps)\n",
478 dev_name(component->dev), component->ops);
480 ret = component->ops->bind(component->dev, master->dev, data);
482 component->bound = true;
485 * Close the component device's group so that resources
486 * allocated in the binding are encapsulated for removal
487 * at unbind. Remove the group on the DRM device as we
488 * can clean those resources up independently.
490 devres_close_group(component->dev, NULL);
491 devres_remove_group(master->dev, NULL);
493 dev_info(master->dev, "bound %s (ops %ps)\n",
494 dev_name(component->dev), component->ops);
496 devres_release_group(component->dev, NULL);
497 devres_release_group(master->dev, NULL);
499 dev_err(master->dev, "failed to bind %s (ops %ps): %d\n",
500 dev_name(component->dev), component->ops, ret);
506 int component_bind_all(struct device *master_dev, void *data)
508 struct master *master;
513 WARN_ON(!mutex_is_locked(&component_mutex));
515 master = __master_find(master_dev, NULL);
519 /* Bind components in match order */
520 for (i = 0; i < master->match->num; i++)
521 if (!master->match->compare[i].duplicate) {
522 c = master->match->compare[i].component;
523 ret = component_bind(c, master, data);
530 if (!master->match->compare[i - 1].duplicate) {
531 c = master->match->compare[i - 1].component;
532 component_unbind(c, master, data);
538 EXPORT_SYMBOL_GPL(component_bind_all);
540 int component_add(struct device *dev, const struct component_ops *ops)
542 struct component *component;
545 component = kzalloc(sizeof(*component), GFP_KERNEL);
549 component->ops = ops;
550 component->dev = dev;
552 dev_dbg(dev, "adding component (ops %ps)\n", ops);
554 mutex_lock(&component_mutex);
555 list_add_tail(&component->node, &component_list);
557 ret = try_to_bring_up_masters(component);
559 if (component->master)
560 remove_component(component->master, component);
561 list_del(&component->node);
565 mutex_unlock(&component_mutex);
567 return ret < 0 ? ret : 0;
569 EXPORT_SYMBOL_GPL(component_add);
571 void component_del(struct device *dev, const struct component_ops *ops)
573 struct component *c, *component = NULL;
575 mutex_lock(&component_mutex);
576 list_for_each_entry(c, &component_list, node)
577 if (c->dev == dev && c->ops == ops) {
583 if (component && component->master) {
584 take_down_master(component->master);
585 remove_component(component->master, component);
588 mutex_unlock(&component_mutex);
593 EXPORT_SYMBOL_GPL(component_del);
595 MODULE_LICENSE("GPL v2");