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>
22 struct list_head node
;
23 struct list_head components
;
26 const struct component_master_ops
*ops
;
31 struct list_head node
;
32 struct list_head master_node
;
33 struct master
*master
;
36 const struct component_ops
*ops
;
40 static DEFINE_MUTEX(component_mutex
);
41 static LIST_HEAD(component_list
);
42 static LIST_HEAD(masters
);
44 static struct master
*__master_find(struct device
*dev
,
45 const struct component_master_ops
*ops
)
49 list_for_each_entry(m
, &masters
, node
)
50 if (m
->dev
== dev
&& (!ops
|| m
->ops
== ops
))
56 /* Attach an unattached component to a master. */
57 static void component_attach_master(struct master
*master
, struct component
*c
)
61 list_add_tail(&c
->master_node
, &master
->components
);
64 /* Detach a component from a master. */
65 static void component_detach_master(struct master
*master
, struct component
*c
)
67 list_del(&c
->master_node
);
72 int component_master_add_child(struct master
*master
,
73 int (*compare
)(struct device
*, void *), void *compare_data
)
78 list_for_each_entry(c
, &component_list
, node
) {
82 if (compare(c
->dev
, compare_data
)) {
83 component_attach_master(master
, c
);
91 EXPORT_SYMBOL_GPL(component_master_add_child
);
93 /* Detach all attached components from this master */
94 static void master_remove_components(struct master
*master
)
96 while (!list_empty(&master
->components
)) {
97 struct component
*c
= list_first_entry(&master
->components
,
98 struct component
, master_node
);
100 WARN_ON(c
->master
!= master
);
102 component_detach_master(master
, c
);
107 * Try to bring up a master. If component is NULL, we're interested in
108 * this master, otherwise it's a component which must be present to try
109 * and bring up the master.
111 * Returns 1 for successful bringup, 0 if not ready, or -ve errno.
113 static int try_to_bring_up_master(struct master
*master
,
114 struct component
*component
)
118 if (!master
->bound
) {
120 * Search the list of components, looking for components that
121 * belong to this master, and attach them to the master.
123 if (master
->ops
->add_components(master
->dev
, master
)) {
124 /* Failed to find all components */
125 master_remove_components(master
);
130 if (component
&& component
->master
!= master
) {
131 master_remove_components(master
);
136 /* Found all components */
137 ret
= master
->ops
->bind(master
->dev
);
139 master_remove_components(master
);
143 master
->bound
= true;
151 static int try_to_bring_up_masters(struct component
*component
)
156 list_for_each_entry(m
, &masters
, node
) {
157 ret
= try_to_bring_up_master(m
, component
);
165 static void take_down_master(struct master
*master
)
168 master
->ops
->unbind(master
->dev
);
169 master
->bound
= false;
172 master_remove_components(master
);
175 int component_master_add(struct device
*dev
,
176 const struct component_master_ops
*ops
)
178 struct master
*master
;
181 master
= kzalloc(sizeof(*master
), GFP_KERNEL
);
187 INIT_LIST_HEAD(&master
->components
);
189 /* Add to the list of available masters. */
190 mutex_lock(&component_mutex
);
191 list_add(&master
->node
, &masters
);
193 ret
= try_to_bring_up_master(master
, NULL
);
196 /* Delete off the list if we weren't successful */
197 list_del(&master
->node
);
200 mutex_unlock(&component_mutex
);
202 return ret
< 0 ? ret
: 0;
204 EXPORT_SYMBOL_GPL(component_master_add
);
206 void component_master_del(struct device
*dev
,
207 const struct component_master_ops
*ops
)
209 struct master
*master
;
211 mutex_lock(&component_mutex
);
212 master
= __master_find(dev
, ops
);
214 take_down_master(master
);
216 list_del(&master
->node
);
219 mutex_unlock(&component_mutex
);
221 EXPORT_SYMBOL_GPL(component_master_del
);
223 static void component_unbind(struct component
*component
,
224 struct master
*master
, void *data
)
226 WARN_ON(!component
->bound
);
228 component
->ops
->unbind(component
->dev
, master
->dev
, data
);
229 component
->bound
= false;
231 /* Release all resources claimed in the binding of this component */
232 devres_release_group(component
->dev
, component
);
235 void component_unbind_all(struct device
*master_dev
, void *data
)
237 struct master
*master
;
240 WARN_ON(!mutex_is_locked(&component_mutex
));
242 master
= __master_find(master_dev
, NULL
);
246 list_for_each_entry_reverse(c
, &master
->components
, master_node
)
247 component_unbind(c
, master
, data
);
249 EXPORT_SYMBOL_GPL(component_unbind_all
);
251 static int component_bind(struct component
*component
, struct master
*master
,
257 * Each component initialises inside its own devres group.
258 * This allows us to roll-back a failed component without
259 * affecting anything else.
261 if (!devres_open_group(master
->dev
, NULL
, GFP_KERNEL
))
265 * Also open a group for the device itself: this allows us
266 * to release the resources claimed against the sub-device
267 * at the appropriate moment.
269 if (!devres_open_group(component
->dev
, component
, GFP_KERNEL
)) {
270 devres_release_group(master
->dev
, NULL
);
274 dev_dbg(master
->dev
, "binding %s (ops %ps)\n",
275 dev_name(component
->dev
), component
->ops
);
277 ret
= component
->ops
->bind(component
->dev
, master
->dev
, data
);
279 component
->bound
= true;
282 * Close the component device's group so that resources
283 * allocated in the binding are encapsulated for removal
284 * at unbind. Remove the group on the DRM device as we
285 * can clean those resources up independently.
287 devres_close_group(component
->dev
, NULL
);
288 devres_remove_group(master
->dev
, NULL
);
290 dev_info(master
->dev
, "bound %s (ops %ps)\n",
291 dev_name(component
->dev
), component
->ops
);
293 devres_release_group(component
->dev
, NULL
);
294 devres_release_group(master
->dev
, NULL
);
296 dev_err(master
->dev
, "failed to bind %s (ops %ps): %d\n",
297 dev_name(component
->dev
), component
->ops
, ret
);
303 int component_bind_all(struct device
*master_dev
, void *data
)
305 struct master
*master
;
309 WARN_ON(!mutex_is_locked(&component_mutex
));
311 master
= __master_find(master_dev
, NULL
);
315 list_for_each_entry(c
, &master
->components
, master_node
) {
316 ret
= component_bind(c
, master
, data
);
322 list_for_each_entry_continue_reverse(c
, &master
->components
,
324 component_unbind(c
, master
, data
);
329 EXPORT_SYMBOL_GPL(component_bind_all
);
331 int component_add(struct device
*dev
, const struct component_ops
*ops
)
333 struct component
*component
;
336 component
= kzalloc(sizeof(*component
), GFP_KERNEL
);
340 component
->ops
= ops
;
341 component
->dev
= dev
;
343 dev_dbg(dev
, "adding component (ops %ps)\n", ops
);
345 mutex_lock(&component_mutex
);
346 list_add_tail(&component
->node
, &component_list
);
348 ret
= try_to_bring_up_masters(component
);
350 list_del(&component
->node
);
354 mutex_unlock(&component_mutex
);
356 return ret
< 0 ? ret
: 0;
358 EXPORT_SYMBOL_GPL(component_add
);
360 void component_del(struct device
*dev
, const struct component_ops
*ops
)
362 struct component
*c
, *component
= NULL
;
364 mutex_lock(&component_mutex
);
365 list_for_each_entry(c
, &component_list
, node
)
366 if (c
->dev
== dev
&& c
->ops
== ops
) {
372 if (component
&& component
->master
)
373 take_down_master(component
->master
);
375 mutex_unlock(&component_mutex
);
380 EXPORT_SYMBOL_GPL(component_del
);
382 MODULE_LICENSE("GPL v2");