staging: octeon-usb: remove useless prefix from pipe flags
[linux/fpc-iii.git] / drivers / base / component.c
blobf748430bb654154918ae67984405021835f44ec8
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 if (match) {
297 /* Reallocate the match array for its true size */
298 match = component_match_realloc(dev, match, match->num);
299 if (IS_ERR(match))
300 return PTR_ERR(match);
303 master = kzalloc(sizeof(*master), GFP_KERNEL);
304 if (!master)
305 return -ENOMEM;
307 master->dev = dev;
308 master->ops = ops;
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);
318 if (ret < 0) {
319 /* Delete off the list if we weren't successful */
320 list_del(&master->node);
321 kfree(master);
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);
343 if (master) {
344 take_down_master(master);
346 list_del(&master->node);
347 kfree(master);
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;
368 struct component *c;
370 WARN_ON(!mutex_is_locked(&component_mutex));
372 master = __master_find(master_dev, NULL);
373 if (!master)
374 return;
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,
382 void *data)
384 int ret;
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))
392 return -ENOMEM;
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);
401 return -ENOMEM;
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);
408 if (!ret) {
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);
422 } else {
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);
430 return ret;
433 int component_bind_all(struct device *master_dev, void *data)
435 struct master *master;
436 struct component *c;
437 int ret = 0;
439 WARN_ON(!mutex_is_locked(&component_mutex));
441 master = __master_find(master_dev, NULL);
442 if (!master)
443 return -EINVAL;
445 list_for_each_entry(c, &master->components, master_node) {
446 ret = component_bind(c, master, data);
447 if (ret)
448 break;
451 if (ret != 0) {
452 list_for_each_entry_continue_reverse(c, &master->components,
453 master_node)
454 component_unbind(c, master, data);
457 return ret;
459 EXPORT_SYMBOL_GPL(component_bind_all);
461 int component_add(struct device *dev, const struct component_ops *ops)
463 struct component *component;
464 int ret;
466 component = kzalloc(sizeof(*component), GFP_KERNEL);
467 if (!component)
468 return -ENOMEM;
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);
479 if (ret < 0) {
480 list_del(&component->node);
482 kfree(component);
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) {
497 list_del(&c->node);
498 component = c;
499 break;
502 if (component && component->master)
503 take_down_master(component->master);
505 mutex_unlock(&component_mutex);
507 WARN_ON(!component);
508 kfree(component);
510 EXPORT_SYMBOL_GPL(component_del);
512 MODULE_LICENSE("GPL v2");