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
)
296 /* Reallocate the match array for its true size */
297 match
= component_match_realloc(dev
, match
, match
->num
);
299 return PTR_ERR(match
);
301 master
= kzalloc(sizeof(*master
), GFP_KERNEL
);
307 master
->match
= match
;
308 INIT_LIST_HEAD(&master
->components
);
310 /* Add to the list of available masters. */
311 mutex_lock(&component_mutex
);
312 list_add(&master
->node
, &masters
);
314 ret
= try_to_bring_up_master(master
, NULL
);
317 /* Delete off the list if we weren't successful */
318 list_del(&master
->node
);
321 mutex_unlock(&component_mutex
);
323 return ret
< 0 ? ret
: 0;
325 EXPORT_SYMBOL_GPL(component_master_add_with_match
);
327 int component_master_add(struct device
*dev
,
328 const struct component_master_ops
*ops
)
330 return component_master_add_with_match(dev
, ops
, NULL
);
332 EXPORT_SYMBOL_GPL(component_master_add
);
334 void component_master_del(struct device
*dev
,
335 const struct component_master_ops
*ops
)
337 struct master
*master
;
339 mutex_lock(&component_mutex
);
340 master
= __master_find(dev
, ops
);
342 take_down_master(master
);
344 list_del(&master
->node
);
347 mutex_unlock(&component_mutex
);
349 EXPORT_SYMBOL_GPL(component_master_del
);
351 static void component_unbind(struct component
*component
,
352 struct master
*master
, void *data
)
354 WARN_ON(!component
->bound
);
356 component
->ops
->unbind(component
->dev
, master
->dev
, data
);
357 component
->bound
= false;
359 /* Release all resources claimed in the binding of this component */
360 devres_release_group(component
->dev
, component
);
363 void component_unbind_all(struct device
*master_dev
, void *data
)
365 struct master
*master
;
368 WARN_ON(!mutex_is_locked(&component_mutex
));
370 master
= __master_find(master_dev
, NULL
);
374 list_for_each_entry_reverse(c
, &master
->components
, master_node
)
375 component_unbind(c
, master
, data
);
377 EXPORT_SYMBOL_GPL(component_unbind_all
);
379 static int component_bind(struct component
*component
, struct master
*master
,
385 * Each component initialises inside its own devres group.
386 * This allows us to roll-back a failed component without
387 * affecting anything else.
389 if (!devres_open_group(master
->dev
, NULL
, GFP_KERNEL
))
393 * Also open a group for the device itself: this allows us
394 * to release the resources claimed against the sub-device
395 * at the appropriate moment.
397 if (!devres_open_group(component
->dev
, component
, GFP_KERNEL
)) {
398 devres_release_group(master
->dev
, NULL
);
402 dev_dbg(master
->dev
, "binding %s (ops %ps)\n",
403 dev_name(component
->dev
), component
->ops
);
405 ret
= component
->ops
->bind(component
->dev
, master
->dev
, data
);
407 component
->bound
= true;
410 * Close the component device's group so that resources
411 * allocated in the binding are encapsulated for removal
412 * at unbind. Remove the group on the DRM device as we
413 * can clean those resources up independently.
415 devres_close_group(component
->dev
, NULL
);
416 devres_remove_group(master
->dev
, NULL
);
418 dev_info(master
->dev
, "bound %s (ops %ps)\n",
419 dev_name(component
->dev
), component
->ops
);
421 devres_release_group(component
->dev
, NULL
);
422 devres_release_group(master
->dev
, NULL
);
424 dev_err(master
->dev
, "failed to bind %s (ops %ps): %d\n",
425 dev_name(component
->dev
), component
->ops
, ret
);
431 int component_bind_all(struct device
*master_dev
, void *data
)
433 struct master
*master
;
437 WARN_ON(!mutex_is_locked(&component_mutex
));
439 master
= __master_find(master_dev
, NULL
);
443 list_for_each_entry(c
, &master
->components
, master_node
) {
444 ret
= component_bind(c
, master
, data
);
450 list_for_each_entry_continue_reverse(c
, &master
->components
,
452 component_unbind(c
, master
, data
);
457 EXPORT_SYMBOL_GPL(component_bind_all
);
459 int component_add(struct device
*dev
, const struct component_ops
*ops
)
461 struct component
*component
;
464 component
= kzalloc(sizeof(*component
), GFP_KERNEL
);
468 component
->ops
= ops
;
469 component
->dev
= dev
;
471 dev_dbg(dev
, "adding component (ops %ps)\n", ops
);
473 mutex_lock(&component_mutex
);
474 list_add_tail(&component
->node
, &component_list
);
476 ret
= try_to_bring_up_masters(component
);
478 list_del(&component
->node
);
482 mutex_unlock(&component_mutex
);
484 return ret
< 0 ? ret
: 0;
486 EXPORT_SYMBOL_GPL(component_add
);
488 void component_del(struct device
*dev
, const struct component_ops
*ops
)
490 struct component
*c
, *component
= NULL
;
492 mutex_lock(&component_mutex
);
493 list_for_each_entry(c
, &component_list
, node
)
494 if (c
->dev
== dev
&& c
->ops
== ops
) {
500 if (component
&& component
->master
)
501 take_down_master(component
->master
);
503 mutex_unlock(&component_mutex
);
508 EXPORT_SYMBOL_GPL(component_del
);
510 MODULE_LICENSE("GPL v2");