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
)
153 if (find_components(master
)) {
154 /* Failed to find all components */
159 if (component
&& component
->master
!= master
) {
164 if (!devres_open_group(master
->dev
, NULL
, GFP_KERNEL
)) {
169 /* Found all components */
170 ret
= master
->ops
->bind(master
->dev
);
172 devres_release_group(master
->dev
, NULL
);
173 dev_info(master
->dev
, "master bind failed: %d\n", ret
);
177 master
->bound
= true;
181 master_remove_components(master
);
186 static int try_to_bring_up_masters(struct component
*component
)
191 list_for_each_entry(m
, &masters
, node
) {
193 ret
= try_to_bring_up_master(m
, component
);
202 static void take_down_master(struct master
*master
)
205 master
->ops
->unbind(master
->dev
);
206 devres_release_group(master
->dev
, NULL
);
207 master
->bound
= false;
210 master_remove_components(master
);
213 static size_t component_match_size(size_t num
)
215 return offsetof(struct component_match
, compare
[num
]);
218 static struct component_match
*component_match_realloc(struct device
*dev
,
219 struct component_match
*match
, size_t num
)
221 struct component_match
*new;
223 if (match
&& match
->alloc
== num
)
226 new = devm_kmalloc(dev
, component_match_size(num
), GFP_KERNEL
);
228 return ERR_PTR(-ENOMEM
);
231 memcpy(new, match
, component_match_size(min(match
->num
, num
)));
232 devm_kfree(dev
, match
);
243 * Add a component to be matched.
245 * The match array is first created or extended if necessary.
247 void component_match_add(struct device
*dev
, struct component_match
**matchptr
,
248 int (*compare
)(struct device
*, void *), void *compare_data
)
250 struct component_match
*match
= *matchptr
;
255 if (!match
|| match
->num
== match
->alloc
) {
256 size_t new_size
= match
? match
->alloc
+ 16 : 15;
258 match
= component_match_realloc(dev
, match
, new_size
);
266 match
->compare
[match
->num
].fn
= compare
;
267 match
->compare
[match
->num
].data
= compare_data
;
270 EXPORT_SYMBOL(component_match_add
);
272 int component_master_add_with_match(struct device
*dev
,
273 const struct component_master_ops
*ops
,
274 struct component_match
*match
)
276 struct master
*master
;
279 /* Reallocate the match array for its true size */
280 match
= component_match_realloc(dev
, match
, match
->num
);
282 return PTR_ERR(match
);
284 master
= kzalloc(sizeof(*master
), GFP_KERNEL
);
290 master
->match
= match
;
291 INIT_LIST_HEAD(&master
->components
);
293 /* Add to the list of available masters. */
294 mutex_lock(&component_mutex
);
295 list_add(&master
->node
, &masters
);
297 ret
= try_to_bring_up_master(master
, NULL
);
300 /* Delete off the list if we weren't successful */
301 list_del(&master
->node
);
304 mutex_unlock(&component_mutex
);
306 return ret
< 0 ? ret
: 0;
308 EXPORT_SYMBOL_GPL(component_master_add_with_match
);
310 void component_master_del(struct device
*dev
,
311 const struct component_master_ops
*ops
)
313 struct master
*master
;
315 mutex_lock(&component_mutex
);
316 master
= __master_find(dev
, ops
);
318 take_down_master(master
);
320 list_del(&master
->node
);
323 mutex_unlock(&component_mutex
);
325 EXPORT_SYMBOL_GPL(component_master_del
);
327 static void component_unbind(struct component
*component
,
328 struct master
*master
, void *data
)
330 WARN_ON(!component
->bound
);
332 component
->ops
->unbind(component
->dev
, master
->dev
, data
);
333 component
->bound
= false;
335 /* Release all resources claimed in the binding of this component */
336 devres_release_group(component
->dev
, component
);
339 void component_unbind_all(struct device
*master_dev
, void *data
)
341 struct master
*master
;
344 WARN_ON(!mutex_is_locked(&component_mutex
));
346 master
= __master_find(master_dev
, NULL
);
350 list_for_each_entry_reverse(c
, &master
->components
, master_node
)
351 component_unbind(c
, master
, data
);
353 EXPORT_SYMBOL_GPL(component_unbind_all
);
355 static int component_bind(struct component
*component
, struct master
*master
,
361 * Each component initialises inside its own devres group.
362 * This allows us to roll-back a failed component without
363 * affecting anything else.
365 if (!devres_open_group(master
->dev
, NULL
, GFP_KERNEL
))
369 * Also open a group for the device itself: this allows us
370 * to release the resources claimed against the sub-device
371 * at the appropriate moment.
373 if (!devres_open_group(component
->dev
, component
, GFP_KERNEL
)) {
374 devres_release_group(master
->dev
, NULL
);
378 dev_dbg(master
->dev
, "binding %s (ops %ps)\n",
379 dev_name(component
->dev
), component
->ops
);
381 ret
= component
->ops
->bind(component
->dev
, master
->dev
, data
);
383 component
->bound
= true;
386 * Close the component device's group so that resources
387 * allocated in the binding are encapsulated for removal
388 * at unbind. Remove the group on the DRM device as we
389 * can clean those resources up independently.
391 devres_close_group(component
->dev
, NULL
);
392 devres_remove_group(master
->dev
, NULL
);
394 dev_info(master
->dev
, "bound %s (ops %ps)\n",
395 dev_name(component
->dev
), component
->ops
);
397 devres_release_group(component
->dev
, NULL
);
398 devres_release_group(master
->dev
, NULL
);
400 dev_err(master
->dev
, "failed to bind %s (ops %ps): %d\n",
401 dev_name(component
->dev
), component
->ops
, ret
);
407 int component_bind_all(struct device
*master_dev
, void *data
)
409 struct master
*master
;
413 WARN_ON(!mutex_is_locked(&component_mutex
));
415 master
= __master_find(master_dev
, NULL
);
419 list_for_each_entry(c
, &master
->components
, master_node
) {
420 ret
= component_bind(c
, master
, data
);
426 list_for_each_entry_continue_reverse(c
, &master
->components
,
428 component_unbind(c
, master
, data
);
433 EXPORT_SYMBOL_GPL(component_bind_all
);
435 int component_add(struct device
*dev
, const struct component_ops
*ops
)
437 struct component
*component
;
440 component
= kzalloc(sizeof(*component
), GFP_KERNEL
);
444 component
->ops
= ops
;
445 component
->dev
= dev
;
447 dev_dbg(dev
, "adding component (ops %ps)\n", ops
);
449 mutex_lock(&component_mutex
);
450 list_add_tail(&component
->node
, &component_list
);
452 ret
= try_to_bring_up_masters(component
);
454 list_del(&component
->node
);
458 mutex_unlock(&component_mutex
);
460 return ret
< 0 ? ret
: 0;
462 EXPORT_SYMBOL_GPL(component_add
);
464 void component_del(struct device
*dev
, const struct component_ops
*ops
)
466 struct component
*c
, *component
= NULL
;
468 mutex_lock(&component_mutex
);
469 list_for_each_entry(c
, &component_list
, node
)
470 if (c
->dev
== dev
&& c
->ops
== ops
) {
476 if (component
&& component
->master
)
477 take_down_master(component
->master
);
479 mutex_unlock(&component_mutex
);
484 EXPORT_SYMBOL_GPL(component_del
);
486 MODULE_LICENSE("GPL v2");