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>
21 struct component_match
{
26 int (*fn
)(struct device
*, void *);
31 struct list_head node
;
32 struct list_head components
;
35 const struct component_master_ops
*ops
;
37 struct component_match
*match
;
41 struct list_head node
;
42 struct list_head master_node
;
43 struct master
*master
;
46 const struct component_ops
*ops
;
50 static DEFINE_MUTEX(component_mutex
);
51 static LIST_HEAD(component_list
);
52 static LIST_HEAD(masters
);
54 static struct master
*__master_find(struct device
*dev
,
55 const struct component_master_ops
*ops
)
59 list_for_each_entry(m
, &masters
, node
)
60 if (m
->dev
== dev
&& (!ops
|| m
->ops
== ops
))
66 /* Attach an unattached component to a master. */
67 static void component_attach_master(struct master
*master
, struct component
*c
)
71 list_add_tail(&c
->master_node
, &master
->components
);
74 /* Detach a component from a master. */
75 static void component_detach_master(struct master
*master
, struct component
*c
)
77 list_del(&c
->master_node
);
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.
87 static int component_master_add_child(struct master
*master
,
88 int (*compare
)(struct device
*, void *), void *compare_data
)
93 list_for_each_entry(c
, &component_list
, node
) {
94 if (c
->master
&& c
->master
!= master
)
97 if (compare(c
->dev
, compare_data
)) {
99 component_attach_master(master
, c
);
108 static int find_components(struct master
*master
)
110 struct component_match
*match
= master
->match
;
115 * Scan the array of match functions and attach
116 * any components which are found to this master.
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
);
128 /* Detach all attached components from this master */
129 static void master_remove_components(struct master
*master
)
131 while (!list_empty(&master
->components
)) {
132 struct component
*c
= list_first_entry(&master
->components
,
133 struct component
, master_node
);
135 WARN_ON(c
->master
!= master
);
137 component_detach_master(master
, c
);
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.
146 * Returns 1 for successful bringup, 0 if not ready, or -ve errno.
148 static int try_to_bring_up_master(struct master
*master
,
149 struct component
*component
)
157 * Search the list of components, looking for components that
158 * belong to this master, and attach them to the master.
160 if (find_components(master
)) {
161 /* Failed to find all components */
166 if (component
&& component
->master
!= master
) {
171 if (!devres_open_group(master
->dev
, NULL
, GFP_KERNEL
)) {
176 /* Found all components */
177 ret
= master
->ops
->bind(master
->dev
);
179 devres_release_group(master
->dev
, NULL
);
180 dev_info(master
->dev
, "master bind failed: %d\n", ret
);
184 master
->bound
= true;
188 master_remove_components(master
);
193 static int try_to_bring_up_masters(struct component
*component
)
198 list_for_each_entry(m
, &masters
, node
) {
199 ret
= try_to_bring_up_master(m
, component
);
207 static void take_down_master(struct master
*master
)
210 master
->ops
->unbind(master
->dev
);
211 devres_release_group(master
->dev
, NULL
);
212 master
->bound
= false;
215 master_remove_components(master
);
218 static size_t component_match_size(size_t num
)
220 return offsetof(struct component_match
, compare
[num
]);
223 static struct component_match
*component_match_realloc(struct device
*dev
,
224 struct component_match
*match
, size_t num
)
226 struct component_match
*new;
228 if (match
&& match
->alloc
== num
)
231 new = devm_kmalloc(dev
, component_match_size(num
), GFP_KERNEL
);
233 return ERR_PTR(-ENOMEM
);
236 memcpy(new, match
, component_match_size(min(match
->num
, num
)));
237 devm_kfree(dev
, match
);
248 * Add a component to be matched.
250 * The match array is first created or extended if necessary.
252 void component_match_add(struct device
*dev
, struct component_match
**matchptr
,
253 int (*compare
)(struct device
*, void *), void *compare_data
)
255 struct component_match
*match
= *matchptr
;
260 if (!match
|| match
->num
== match
->alloc
) {
261 size_t new_size
= match
? match
->alloc
+ 16 : 15;
263 match
= component_match_realloc(dev
, match
, new_size
);
271 match
->compare
[match
->num
].fn
= compare
;
272 match
->compare
[match
->num
].data
= compare_data
;
275 EXPORT_SYMBOL(component_match_add
);
277 int component_master_add_with_match(struct device
*dev
,
278 const struct component_master_ops
*ops
,
279 struct component_match
*match
)
281 struct master
*master
;
284 /* Reallocate the match array for its true size */
285 match
= component_match_realloc(dev
, match
, match
->num
);
287 return PTR_ERR(match
);
289 master
= kzalloc(sizeof(*master
), GFP_KERNEL
);
295 master
->match
= match
;
296 INIT_LIST_HEAD(&master
->components
);
298 /* Add to the list of available masters. */
299 mutex_lock(&component_mutex
);
300 list_add(&master
->node
, &masters
);
302 ret
= try_to_bring_up_master(master
, NULL
);
305 /* Delete off the list if we weren't successful */
306 list_del(&master
->node
);
309 mutex_unlock(&component_mutex
);
311 return ret
< 0 ? ret
: 0;
313 EXPORT_SYMBOL_GPL(component_master_add_with_match
);
315 void component_master_del(struct device
*dev
,
316 const struct component_master_ops
*ops
)
318 struct master
*master
;
320 mutex_lock(&component_mutex
);
321 master
= __master_find(dev
, ops
);
323 take_down_master(master
);
325 list_del(&master
->node
);
328 mutex_unlock(&component_mutex
);
330 EXPORT_SYMBOL_GPL(component_master_del
);
332 static void component_unbind(struct component
*component
,
333 struct master
*master
, void *data
)
335 WARN_ON(!component
->bound
);
337 component
->ops
->unbind(component
->dev
, master
->dev
, data
);
338 component
->bound
= false;
340 /* Release all resources claimed in the binding of this component */
341 devres_release_group(component
->dev
, component
);
344 void component_unbind_all(struct device
*master_dev
, void *data
)
346 struct master
*master
;
349 WARN_ON(!mutex_is_locked(&component_mutex
));
351 master
= __master_find(master_dev
, NULL
);
355 list_for_each_entry_reverse(c
, &master
->components
, master_node
)
356 component_unbind(c
, master
, data
);
358 EXPORT_SYMBOL_GPL(component_unbind_all
);
360 static int component_bind(struct component
*component
, struct master
*master
,
366 * Each component initialises inside its own devres group.
367 * This allows us to roll-back a failed component without
368 * affecting anything else.
370 if (!devres_open_group(master
->dev
, NULL
, GFP_KERNEL
))
374 * Also open a group for the device itself: this allows us
375 * to release the resources claimed against the sub-device
376 * at the appropriate moment.
378 if (!devres_open_group(component
->dev
, component
, GFP_KERNEL
)) {
379 devres_release_group(master
->dev
, NULL
);
383 dev_dbg(master
->dev
, "binding %s (ops %ps)\n",
384 dev_name(component
->dev
), component
->ops
);
386 ret
= component
->ops
->bind(component
->dev
, master
->dev
, data
);
388 component
->bound
= true;
391 * Close the component device's group so that resources
392 * allocated in the binding are encapsulated for removal
393 * at unbind. Remove the group on the DRM device as we
394 * can clean those resources up independently.
396 devres_close_group(component
->dev
, NULL
);
397 devres_remove_group(master
->dev
, NULL
);
399 dev_info(master
->dev
, "bound %s (ops %ps)\n",
400 dev_name(component
->dev
), component
->ops
);
402 devres_release_group(component
->dev
, NULL
);
403 devres_release_group(master
->dev
, NULL
);
405 dev_err(master
->dev
, "failed to bind %s (ops %ps): %d\n",
406 dev_name(component
->dev
), component
->ops
, ret
);
412 int component_bind_all(struct device
*master_dev
, void *data
)
414 struct master
*master
;
418 WARN_ON(!mutex_is_locked(&component_mutex
));
420 master
= __master_find(master_dev
, NULL
);
424 list_for_each_entry(c
, &master
->components
, master_node
) {
425 ret
= component_bind(c
, master
, data
);
431 list_for_each_entry_continue_reverse(c
, &master
->components
,
433 component_unbind(c
, master
, data
);
438 EXPORT_SYMBOL_GPL(component_bind_all
);
440 int component_add(struct device
*dev
, const struct component_ops
*ops
)
442 struct component
*component
;
445 component
= kzalloc(sizeof(*component
), GFP_KERNEL
);
449 component
->ops
= ops
;
450 component
->dev
= dev
;
452 dev_dbg(dev
, "adding component (ops %ps)\n", ops
);
454 mutex_lock(&component_mutex
);
455 list_add_tail(&component
->node
, &component_list
);
457 ret
= try_to_bring_up_masters(component
);
459 list_del(&component
->node
);
463 mutex_unlock(&component_mutex
);
465 return ret
< 0 ? ret
: 0;
467 EXPORT_SYMBOL_GPL(component_add
);
469 void component_del(struct device
*dev
, const struct component_ops
*ops
)
471 struct component
*c
, *component
= NULL
;
473 mutex_lock(&component_mutex
);
474 list_for_each_entry(c
, &component_list
, node
)
475 if (c
->dev
== dev
&& c
->ops
== ops
) {
481 if (component
&& component
->master
)
482 take_down_master(component
->master
);
484 mutex_unlock(&component_mutex
);
489 EXPORT_SYMBOL_GPL(component_del
);
491 MODULE_LICENSE("GPL v2");