component: add support for component match array
[linux-2.6/btrfs-unstable.git] / drivers / base / component.c
blobb4236daed4fa0401cf90827f422ab281936ba9c1
1 /*
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
11 * the case.
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 {
22 size_t alloc;
23 size_t num;
24 struct {
25 void *data;
26 int (*fn)(struct device *, void *);
27 } compare[0];
30 struct master {
31 struct list_head node;
32 struct list_head components;
33 bool bound;
35 const struct component_master_ops *ops;
36 struct device *dev;
37 struct component_match *match;
40 struct component {
41 struct list_head node;
42 struct list_head master_node;
43 struct master *master;
44 bool bound;
46 const struct component_ops *ops;
47 struct device *dev;
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)
57 struct master *m;
59 list_for_each_entry(m, &masters, node)
60 if (m->dev == dev && (!ops || m->ops == ops))
61 return m;
63 return NULL;
66 /* Attach an unattached component to a master. */
67 static void component_attach_master(struct master *master, struct component *c)
69 c->master = master;
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);
79 c->master = NULL;
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)
90 struct component *c;
91 int ret = -ENXIO;
93 list_for_each_entry(c, &component_list, node) {
94 if (c->master && c->master != master)
95 continue;
97 if (compare(c->dev, compare_data)) {
98 if (!c->master)
99 component_attach_master(master, c);
100 ret = 0;
101 break;
105 return ret;
107 EXPORT_SYMBOL_GPL(component_master_add_child);
109 static int find_components(struct master *master)
111 struct component_match *match = master->match;
112 size_t i;
113 int ret = 0;
115 if (!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);
131 if (ret)
132 break;
134 return ret;
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)
160 int ret;
162 if (master->bound)
163 return 0;
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 */
171 ret = 0;
172 goto out;
175 if (component && component->master != master) {
176 ret = 0;
177 goto out;
180 if (!devres_open_group(master->dev, NULL, GFP_KERNEL)) {
181 ret = -ENOMEM;
182 goto out;
185 /* Found all components */
186 ret = master->ops->bind(master->dev);
187 if (ret < 0) {
188 devres_release_group(master->dev, NULL);
189 dev_info(master->dev, "master bind failed: %d\n", ret);
190 goto out;
193 master->bound = true;
194 return 1;
196 out:
197 master_remove_components(master);
199 return ret;
202 static int try_to_bring_up_masters(struct component *component)
204 struct master *m;
205 int ret = 0;
207 list_for_each_entry(m, &masters, node) {
208 ret = try_to_bring_up_master(m, component);
209 if (ret != 0)
210 break;
213 return ret;
216 static void take_down_master(struct master *master)
218 if (master->bound) {
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)
238 return match;
240 new = devm_kmalloc(dev, component_match_size(num), GFP_KERNEL);
241 if (!new)
242 return ERR_PTR(-ENOMEM);
244 if (match) {
245 memcpy(new, match, component_match_size(min(match->num, num)));
246 devm_kfree(dev, match);
247 } else {
248 new->num = 0;
251 new->alloc = num;
253 return new;
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;
266 if (IS_ERR(match))
267 return;
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);
274 *matchptr = match;
276 if (IS_ERR(match))
277 return;
280 match->compare[match->num].fn = compare;
281 match->compare[match->num].data = compare_data;
282 match->num++;
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;
291 int ret;
293 if (ops->add_components && match)
294 return -EINVAL;
296 /* Reallocate the match array for its true size */
297 match = component_match_realloc(dev, match, match->num);
298 if (IS_ERR(match))
299 return PTR_ERR(match);
301 master = kzalloc(sizeof(*master), GFP_KERNEL);
302 if (!master)
303 return -ENOMEM;
305 master->dev = dev;
306 master->ops = ops;
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);
316 if (ret < 0) {
317 /* Delete off the list if we weren't successful */
318 list_del(&master->node);
319 kfree(master);
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);
341 if (master) {
342 take_down_master(master);
344 list_del(&master->node);
345 kfree(master);
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;
366 struct component *c;
368 WARN_ON(!mutex_is_locked(&component_mutex));
370 master = __master_find(master_dev, NULL);
371 if (!master)
372 return;
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,
380 void *data)
382 int ret;
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))
390 return -ENOMEM;
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);
399 return -ENOMEM;
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);
406 if (!ret) {
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);
420 } else {
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);
428 return ret;
431 int component_bind_all(struct device *master_dev, void *data)
433 struct master *master;
434 struct component *c;
435 int ret = 0;
437 WARN_ON(!mutex_is_locked(&component_mutex));
439 master = __master_find(master_dev, NULL);
440 if (!master)
441 return -EINVAL;
443 list_for_each_entry(c, &master->components, master_node) {
444 ret = component_bind(c, master, data);
445 if (ret)
446 break;
449 if (ret != 0) {
450 list_for_each_entry_continue_reverse(c, &master->components,
451 master_node)
452 component_unbind(c, master, data);
455 return ret;
457 EXPORT_SYMBOL_GPL(component_bind_all);
459 int component_add(struct device *dev, const struct component_ops *ops)
461 struct component *component;
462 int ret;
464 component = kzalloc(sizeof(*component), GFP_KERNEL);
465 if (!component)
466 return -ENOMEM;
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);
477 if (ret < 0) {
478 list_del(&component->node);
480 kfree(component);
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) {
495 list_del(&c->node);
496 component = c;
497 break;
500 if (component && component->master)
501 take_down_master(component->master);
503 mutex_unlock(&component_mutex);
505 WARN_ON(!component);
506 kfree(component);
508 EXPORT_SYMBOL_GPL(component_del);
510 MODULE_LICENSE("GPL v2");