component: ignore multiple additions of the same component
[deliverable/linux.git] / drivers / base / component.c
CommitLineData
2a41e607
RK
1/*
2 * Componentized device handling.
3 *
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.
7 *
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
11 * the case.
12 */
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>
20
21struct master {
22 struct list_head node;
23 struct list_head components;
24 bool bound;
25
26 const struct component_master_ops *ops;
27 struct device *dev;
28};
29
30struct component {
31 struct list_head node;
32 struct list_head master_node;
33 struct master *master;
34 bool bound;
35
36 const struct component_ops *ops;
37 struct device *dev;
38};
39
40static DEFINE_MUTEX(component_mutex);
41static LIST_HEAD(component_list);
42static LIST_HEAD(masters);
43
44static struct master *__master_find(struct device *dev,
45 const struct component_master_ops *ops)
46{
47 struct master *m;
48
49 list_for_each_entry(m, &masters, node)
50 if (m->dev == dev && (!ops || m->ops == ops))
51 return m;
52
53 return NULL;
54}
55
56/* Attach an unattached component to a master. */
57static void component_attach_master(struct master *master, struct component *c)
58{
59 c->master = master;
60
61 list_add_tail(&c->master_node, &master->components);
62}
63
64/* Detach a component from a master. */
65static void component_detach_master(struct master *master, struct component *c)
66{
67 list_del(&c->master_node);
68
69 c->master = NULL;
70}
71
fcbcebce
RK
72/*
73 * Add a component to a master, finding the component via the compare
74 * function and compare data. This is safe to call for duplicate matches
75 * and will not result in the same component being added multiple times.
76 */
2a41e607
RK
77int component_master_add_child(struct master *master,
78 int (*compare)(struct device *, void *), void *compare_data)
79{
80 struct component *c;
81 int ret = -ENXIO;
82
83 list_for_each_entry(c, &component_list, node) {
fcbcebce 84 if (c->master && c->master != master)
2a41e607
RK
85 continue;
86
87 if (compare(c->dev, compare_data)) {
fcbcebce
RK
88 if (!c->master)
89 component_attach_master(master, c);
2a41e607
RK
90 ret = 0;
91 break;
92 }
93 }
94
95 return ret;
96}
97EXPORT_SYMBOL_GPL(component_master_add_child);
98
99/* Detach all attached components from this master */
100static void master_remove_components(struct master *master)
101{
102 while (!list_empty(&master->components)) {
103 struct component *c = list_first_entry(&master->components,
104 struct component, master_node);
105
106 WARN_ON(c->master != master);
107
108 component_detach_master(master, c);
109 }
110}
111
112/*
113 * Try to bring up a master. If component is NULL, we're interested in
114 * this master, otherwise it's a component which must be present to try
115 * and bring up the master.
116 *
117 * Returns 1 for successful bringup, 0 if not ready, or -ve errno.
118 */
119static int try_to_bring_up_master(struct master *master,
120 struct component *component)
121{
c334940e
RK
122 int ret;
123
124 if (master->bound)
125 return 0;
126
127 /*
128 * Search the list of components, looking for components that
129 * belong to this master, and attach them to the master.
130 */
131 if (master->ops->add_components(master->dev, master)) {
132 /* Failed to find all components */
133 ret = 0;
134 goto out;
135 }
2a41e607 136
c334940e
RK
137 if (component && component->master != master) {
138 ret = 0;
139 goto out;
140 }
2a41e607 141
c334940e
RK
142 if (!devres_open_group(master->dev, NULL, GFP_KERNEL)) {
143 ret = -ENOMEM;
144 goto out;
145 }
2a41e607 146
c334940e
RK
147 /* Found all components */
148 ret = master->ops->bind(master->dev);
149 if (ret < 0) {
150 devres_release_group(master->dev, NULL);
151 dev_info(master->dev, "master bind failed: %d\n", ret);
152 goto out;
153 }
9e1ccb4a 154
c334940e
RK
155 master->bound = true;
156 return 1;
2a41e607 157
2a41e607 158out:
c334940e 159 master_remove_components(master);
2a41e607
RK
160
161 return ret;
162}
163
164static int try_to_bring_up_masters(struct component *component)
165{
166 struct master *m;
167 int ret = 0;
168
169 list_for_each_entry(m, &masters, node) {
170 ret = try_to_bring_up_master(m, component);
171 if (ret != 0)
172 break;
173 }
174
175 return ret;
176}
177
178static void take_down_master(struct master *master)
179{
180 if (master->bound) {
181 master->ops->unbind(master->dev);
9e1ccb4a 182 devres_release_group(master->dev, NULL);
2a41e607
RK
183 master->bound = false;
184 }
185
186 master_remove_components(master);
187}
188
189int component_master_add(struct device *dev,
190 const struct component_master_ops *ops)
191{
192 struct master *master;
193 int ret;
194
195 master = kzalloc(sizeof(*master), GFP_KERNEL);
196 if (!master)
197 return -ENOMEM;
198
199 master->dev = dev;
200 master->ops = ops;
201 INIT_LIST_HEAD(&master->components);
202
203 /* Add to the list of available masters. */
204 mutex_lock(&component_mutex);
205 list_add(&master->node, &masters);
206
207 ret = try_to_bring_up_master(master, NULL);
208
209 if (ret < 0) {
210 /* Delete off the list if we weren't successful */
211 list_del(&master->node);
212 kfree(master);
213 }
214 mutex_unlock(&component_mutex);
215
216 return ret < 0 ? ret : 0;
217}
218EXPORT_SYMBOL_GPL(component_master_add);
219
220void component_master_del(struct device *dev,
221 const struct component_master_ops *ops)
222{
223 struct master *master;
224
225 mutex_lock(&component_mutex);
226 master = __master_find(dev, ops);
227 if (master) {
228 take_down_master(master);
229
230 list_del(&master->node);
231 kfree(master);
232 }
233 mutex_unlock(&component_mutex);
234}
235EXPORT_SYMBOL_GPL(component_master_del);
236
237static void component_unbind(struct component *component,
238 struct master *master, void *data)
239{
240 WARN_ON(!component->bound);
241
242 component->ops->unbind(component->dev, master->dev, data);
243 component->bound = false;
244
245 /* Release all resources claimed in the binding of this component */
246 devres_release_group(component->dev, component);
247}
248
249void component_unbind_all(struct device *master_dev, void *data)
250{
251 struct master *master;
252 struct component *c;
253
254 WARN_ON(!mutex_is_locked(&component_mutex));
255
256 master = __master_find(master_dev, NULL);
257 if (!master)
258 return;
259
260 list_for_each_entry_reverse(c, &master->components, master_node)
261 component_unbind(c, master, data);
262}
263EXPORT_SYMBOL_GPL(component_unbind_all);
264
265static int component_bind(struct component *component, struct master *master,
266 void *data)
267{
268 int ret;
269
270 /*
271 * Each component initialises inside its own devres group.
272 * This allows us to roll-back a failed component without
273 * affecting anything else.
274 */
275 if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
276 return -ENOMEM;
277
278 /*
279 * Also open a group for the device itself: this allows us
280 * to release the resources claimed against the sub-device
281 * at the appropriate moment.
282 */
283 if (!devres_open_group(component->dev, component, GFP_KERNEL)) {
284 devres_release_group(master->dev, NULL);
285 return -ENOMEM;
286 }
287
288 dev_dbg(master->dev, "binding %s (ops %ps)\n",
289 dev_name(component->dev), component->ops);
290
291 ret = component->ops->bind(component->dev, master->dev, data);
292 if (!ret) {
293 component->bound = true;
294
295 /*
296 * Close the component device's group so that resources
297 * allocated in the binding are encapsulated for removal
298 * at unbind. Remove the group on the DRM device as we
299 * can clean those resources up independently.
300 */
301 devres_close_group(component->dev, NULL);
302 devres_remove_group(master->dev, NULL);
303
304 dev_info(master->dev, "bound %s (ops %ps)\n",
305 dev_name(component->dev), component->ops);
306 } else {
307 devres_release_group(component->dev, NULL);
308 devres_release_group(master->dev, NULL);
309
310 dev_err(master->dev, "failed to bind %s (ops %ps): %d\n",
311 dev_name(component->dev), component->ops, ret);
312 }
313
314 return ret;
315}
316
317int component_bind_all(struct device *master_dev, void *data)
318{
319 struct master *master;
320 struct component *c;
321 int ret = 0;
322
323 WARN_ON(!mutex_is_locked(&component_mutex));
324
325 master = __master_find(master_dev, NULL);
326 if (!master)
327 return -EINVAL;
328
329 list_for_each_entry(c, &master->components, master_node) {
330 ret = component_bind(c, master, data);
331 if (ret)
332 break;
333 }
334
335 if (ret != 0) {
336 list_for_each_entry_continue_reverse(c, &master->components,
337 master_node)
338 component_unbind(c, master, data);
339 }
340
341 return ret;
342}
343EXPORT_SYMBOL_GPL(component_bind_all);
344
345int component_add(struct device *dev, const struct component_ops *ops)
346{
347 struct component *component;
348 int ret;
349
350 component = kzalloc(sizeof(*component), GFP_KERNEL);
351 if (!component)
352 return -ENOMEM;
353
354 component->ops = ops;
355 component->dev = dev;
356
357 dev_dbg(dev, "adding component (ops %ps)\n", ops);
358
359 mutex_lock(&component_mutex);
360 list_add_tail(&component->node, &component_list);
361
362 ret = try_to_bring_up_masters(component);
363 if (ret < 0) {
364 list_del(&component->node);
365
366 kfree(component);
367 }
368 mutex_unlock(&component_mutex);
369
370 return ret < 0 ? ret : 0;
371}
372EXPORT_SYMBOL_GPL(component_add);
373
374void component_del(struct device *dev, const struct component_ops *ops)
375{
376 struct component *c, *component = NULL;
377
378 mutex_lock(&component_mutex);
379 list_for_each_entry(c, &component_list, node)
380 if (c->dev == dev && c->ops == ops) {
381 list_del(&c->node);
382 component = c;
383 break;
384 }
385
386 if (component && component->master)
387 take_down_master(component->master);
388
389 mutex_unlock(&component_mutex);
390
391 WARN_ON(!component);
392 kfree(component);
393}
394EXPORT_SYMBOL_GPL(component_del);
395
396MODULE_LICENSE("GPL v2");
This page took 0.064939 seconds and 5 git commands to generate.