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 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
);
107 EXPORT_SYMBOL_GPL(component_master_add_child
);
109 static int find_components(struct master
*master
)
111 struct component_match
*match
= master
->match
;
117 * Search the list of components, looking for components that
118 * belong to this master, and attach them to the master.
120 return master
->ops
->add_components(master
->dev
, master
);
124 * Scan the array of match functions and attach
125 * any components which are found to this master.
127 for (i
= 0; i
< match
->num
; i
++) {
128 ret
= component_master_add_child(master
,
129 match
->compare
[i
].fn
,
130 match
->compare
[i
].data
);
137 /* Detach all attached components from this master */
138 static void master_remove_components(struct master
*master
)
140 while (!list_empty(&master
->components
)) {
141 struct component
*c
= list_first_entry(&master
->components
,
142 struct component
, master_node
);
144 WARN_ON(c
->master
!= master
);
146 component_detach_master(master
, c
);
151 * Try to bring up a master. If component is NULL, we're interested in
152 * this master, otherwise it's a component which must be present to try
153 * and bring up the master.
155 * Returns 1 for successful bringup, 0 if not ready, or -ve errno.
157 static int try_to_bring_up_master(struct master
*master
,
158 struct component
*component
)
166 * Search the list of components, looking for components that
167 * belong to this master, and attach them to the master.
169 if (find_components(master
)) {
170 /* Failed to find all components */
175 if (component
&& component
->master
!= master
) {
180 if (!devres_open_group(master
->dev
, NULL
, GFP_KERNEL
)) {
185 /* Found all components */
186 ret
= master
->ops
->bind(master
->dev
);
188 devres_release_group(master
->dev
, NULL
);
189 dev_info(master
->dev
, "master bind failed: %d\n", ret
);
193 master
->bound
= true;
197 master_remove_components(master
);
202 static int try_to_bring_up_masters(struct component
*component
)
207 list_for_each_entry(m
, &masters
, node
) {
208 ret
= try_to_bring_up_master(m
, component
);
216 static void take_down_master(struct master
*master
)
219 master
->ops
->unbind(master
->dev
);
220 devres_release_group(master
->dev
, NULL
);
221 master
->bound
= false;
224 master_remove_components(master
);
227 static size_t component_match_size(size_t num
)
229 return offsetof(struct component_match
, compare
[num
]);
232 static struct component_match
*component_match_realloc(struct device
*dev
,
233 struct component_match
*match
, size_t num
)
235 struct component_match
*new;
237 if (match
&& match
->alloc
== num
)
240 new = devm_kmalloc(dev
, component_match_size(num
), GFP_KERNEL
);
242 return ERR_PTR(-ENOMEM
);
245 memcpy(new, match
, component_match_size(min(match
->num
, num
)));
246 devm_kfree(dev
, match
);
257 * Add a component to be matched.
259 * The match array is first created or extended if necessary.
261 void component_match_add(struct device
*dev
, struct component_match
**matchptr
,
262 int (*compare
)(struct device
*, void *), void *compare_data
)
264 struct component_match
*match
= *matchptr
;
269 if (!match
|| match
->num
== match
->alloc
) {
270 size_t new_size
= match
? match
->alloc
+ 16 : 15;
272 match
= component_match_realloc(dev
, match
, new_size
);
280 match
->compare
[match
->num
].fn
= compare
;
281 match
->compare
[match
->num
].data
= compare_data
;
284 EXPORT_SYMBOL(component_match_add
);
286 int component_master_add_with_match(struct device
*dev
,
287 const struct component_master_ops
*ops
,
288 struct component_match
*match
)
290 struct master
*master
;
293 if (ops
->add_components
&& match
)
297 /* Reallocate the match array for its true size */
298 match
= component_match_realloc(dev
, match
, match
->num
);
300 return PTR_ERR(match
);
303 master
= kzalloc(sizeof(*master
), GFP_KERNEL
);
309 master
->match
= match
;
310 INIT_LIST_HEAD(&master
->components
);
312 /* Add to the list of available masters. */
313 mutex_lock(&component_mutex
);
314 list_add(&master
->node
, &masters
);
316 ret
= try_to_bring_up_master(master
, NULL
);
319 /* Delete off the list if we weren't successful */
320 list_del(&master
->node
);
323 mutex_unlock(&component_mutex
);
325 return ret
< 0 ? ret
: 0;
327 EXPORT_SYMBOL_GPL(component_master_add_with_match
);
329 int component_master_add(struct device
*dev
,
330 const struct component_master_ops
*ops
)
332 return component_master_add_with_match(dev
, ops
, NULL
);
334 EXPORT_SYMBOL_GPL(component_master_add
);
336 void component_master_del(struct device
*dev
,
337 const struct component_master_ops
*ops
)
339 struct master
*master
;
341 mutex_lock(&component_mutex
);
342 master
= __master_find(dev
, ops
);
344 take_down_master(master
);
346 list_del(&master
->node
);
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
;
370 WARN_ON(!mutex_is_locked(&component_mutex
));
372 master
= __master_find(master_dev
, NULL
);
376 list_for_each_entry_reverse(c
, &master
->components
, master_node
)
377 component_unbind(c
, master
, data
);
379 EXPORT_SYMBOL_GPL(component_unbind_all
);
381 static int component_bind(struct component
*component
, struct master
*master
,
387 * Each component initialises inside its own devres group.
388 * This allows us to roll-back a failed component without
389 * affecting anything else.
391 if (!devres_open_group(master
->dev
, NULL
, GFP_KERNEL
))
395 * Also open a group for the device itself: this allows us
396 * to release the resources claimed against the sub-device
397 * at the appropriate moment.
399 if (!devres_open_group(component
->dev
, component
, GFP_KERNEL
)) {
400 devres_release_group(master
->dev
, NULL
);
404 dev_dbg(master
->dev
, "binding %s (ops %ps)\n",
405 dev_name(component
->dev
), component
->ops
);
407 ret
= component
->ops
->bind(component
->dev
, master
->dev
, data
);
409 component
->bound
= true;
412 * Close the component device's group so that resources
413 * allocated in the binding are encapsulated for removal
414 * at unbind. Remove the group on the DRM device as we
415 * can clean those resources up independently.
417 devres_close_group(component
->dev
, NULL
);
418 devres_remove_group(master
->dev
, NULL
);
420 dev_info(master
->dev
, "bound %s (ops %ps)\n",
421 dev_name(component
->dev
), component
->ops
);
423 devres_release_group(component
->dev
, NULL
);
424 devres_release_group(master
->dev
, NULL
);
426 dev_err(master
->dev
, "failed to bind %s (ops %ps): %d\n",
427 dev_name(component
->dev
), component
->ops
, ret
);
433 int component_bind_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 list_for_each_entry(c
, &master
->components
, master_node
) {
446 ret
= component_bind(c
, master
, data
);
452 list_for_each_entry_continue_reverse(c
, &master
->components
,
454 component_unbind(c
, master
, data
);
459 EXPORT_SYMBOL_GPL(component_bind_all
);
461 int component_add(struct device
*dev
, const struct component_ops
*ops
)
463 struct component
*component
;
466 component
= kzalloc(sizeof(*component
), GFP_KERNEL
);
470 component
->ops
= ops
;
471 component
->dev
= dev
;
473 dev_dbg(dev
, "adding component (ops %ps)\n", ops
);
475 mutex_lock(&component_mutex
);
476 list_add_tail(&component
->node
, &component_list
);
478 ret
= try_to_bring_up_masters(component
);
480 list_del(&component
->node
);
484 mutex_unlock(&component_mutex
);
486 return ret
< 0 ? ret
: 0;
488 EXPORT_SYMBOL_GPL(component_add
);
490 void component_del(struct device
*dev
, const struct component_ops
*ops
)
492 struct component
*c
, *component
= NULL
;
494 mutex_lock(&component_mutex
);
495 list_for_each_entry(c
, &component_list
, node
)
496 if (c
->dev
== dev
&& c
->ops
== ops
) {
502 if (component
&& component
->master
)
503 take_down_master(component
->master
);
505 mutex_unlock(&component_mutex
);
510 EXPORT_SYMBOL_GPL(component_del
);
512 MODULE_LICENSE("GPL v2");