]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/base/component.c
component: remove old add_components method
[mirror_ubuntu-artful-kernel.git] / drivers / base / component.c
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
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];
28 };
29
30 struct master {
31 struct list_head node;
32 struct list_head components;
33 bool bound;
34
35 const struct component_master_ops *ops;
36 struct device *dev;
37 struct component_match *match;
38 };
39
40 struct component {
41 struct list_head node;
42 struct list_head master_node;
43 struct master *master;
44 bool bound;
45
46 const struct component_ops *ops;
47 struct device *dev;
48 };
49
50 static DEFINE_MUTEX(component_mutex);
51 static LIST_HEAD(component_list);
52 static LIST_HEAD(masters);
53
54 static struct master *__master_find(struct device *dev,
55 const struct component_master_ops *ops)
56 {
57 struct master *m;
58
59 list_for_each_entry(m, &masters, node)
60 if (m->dev == dev && (!ops || m->ops == ops))
61 return m;
62
63 return NULL;
64 }
65
66 /* Attach an unattached component to a master. */
67 static void component_attach_master(struct master *master, struct component *c)
68 {
69 c->master = master;
70
71 list_add_tail(&c->master_node, &master->components);
72 }
73
74 /* Detach a component from a master. */
75 static void component_detach_master(struct master *master, struct component *c)
76 {
77 list_del(&c->master_node);
78
79 c->master = NULL;
80 }
81
82 /*
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.
86 */
87 static int component_master_add_child(struct master *master,
88 int (*compare)(struct device *, void *), void *compare_data)
89 {
90 struct component *c;
91 int ret = -ENXIO;
92
93 list_for_each_entry(c, &component_list, node) {
94 if (c->master && c->master != master)
95 continue;
96
97 if (compare(c->dev, compare_data)) {
98 if (!c->master)
99 component_attach_master(master, c);
100 ret = 0;
101 break;
102 }
103 }
104
105 return ret;
106 }
107
108 static int find_components(struct master *master)
109 {
110 struct component_match *match = master->match;
111 size_t i;
112 int ret = 0;
113
114 /*
115 * Scan the array of match functions and attach
116 * any components which are found to this master.
117 */
118 for (i = 0; i < match->num; i++) {
119 ret = component_master_add_child(master,
120 match->compare[i].fn,
121 match->compare[i].data);
122 if (ret)
123 break;
124 }
125 return ret;
126 }
127
128 /* Detach all attached components from this master */
129 static void master_remove_components(struct master *master)
130 {
131 while (!list_empty(&master->components)) {
132 struct component *c = list_first_entry(&master->components,
133 struct component, master_node);
134
135 WARN_ON(c->master != master);
136
137 component_detach_master(master, c);
138 }
139 }
140
141 /*
142 * Try to bring up a master. If component is NULL, we're interested in
143 * this master, otherwise it's a component which must be present to try
144 * and bring up the master.
145 *
146 * Returns 1 for successful bringup, 0 if not ready, or -ve errno.
147 */
148 static int try_to_bring_up_master(struct master *master,
149 struct component *component)
150 {
151 int ret;
152
153 if (master->bound)
154 return 0;
155
156 /*
157 * Search the list of components, looking for components that
158 * belong to this master, and attach them to the master.
159 */
160 if (find_components(master)) {
161 /* Failed to find all components */
162 ret = 0;
163 goto out;
164 }
165
166 if (component && component->master != master) {
167 ret = 0;
168 goto out;
169 }
170
171 if (!devres_open_group(master->dev, NULL, GFP_KERNEL)) {
172 ret = -ENOMEM;
173 goto out;
174 }
175
176 /* Found all components */
177 ret = master->ops->bind(master->dev);
178 if (ret < 0) {
179 devres_release_group(master->dev, NULL);
180 dev_info(master->dev, "master bind failed: %d\n", ret);
181 goto out;
182 }
183
184 master->bound = true;
185 return 1;
186
187 out:
188 master_remove_components(master);
189
190 return ret;
191 }
192
193 static int try_to_bring_up_masters(struct component *component)
194 {
195 struct master *m;
196 int ret = 0;
197
198 list_for_each_entry(m, &masters, node) {
199 ret = try_to_bring_up_master(m, component);
200 if (ret != 0)
201 break;
202 }
203
204 return ret;
205 }
206
207 static void take_down_master(struct master *master)
208 {
209 if (master->bound) {
210 master->ops->unbind(master->dev);
211 devres_release_group(master->dev, NULL);
212 master->bound = false;
213 }
214
215 master_remove_components(master);
216 }
217
218 static size_t component_match_size(size_t num)
219 {
220 return offsetof(struct component_match, compare[num]);
221 }
222
223 static struct component_match *component_match_realloc(struct device *dev,
224 struct component_match *match, size_t num)
225 {
226 struct component_match *new;
227
228 if (match && match->alloc == num)
229 return match;
230
231 new = devm_kmalloc(dev, component_match_size(num), GFP_KERNEL);
232 if (!new)
233 return ERR_PTR(-ENOMEM);
234
235 if (match) {
236 memcpy(new, match, component_match_size(min(match->num, num)));
237 devm_kfree(dev, match);
238 } else {
239 new->num = 0;
240 }
241
242 new->alloc = num;
243
244 return new;
245 }
246
247 /*
248 * Add a component to be matched.
249 *
250 * The match array is first created or extended if necessary.
251 */
252 void component_match_add(struct device *dev, struct component_match **matchptr,
253 int (*compare)(struct device *, void *), void *compare_data)
254 {
255 struct component_match *match = *matchptr;
256
257 if (IS_ERR(match))
258 return;
259
260 if (!match || match->num == match->alloc) {
261 size_t new_size = match ? match->alloc + 16 : 15;
262
263 match = component_match_realloc(dev, match, new_size);
264
265 *matchptr = match;
266
267 if (IS_ERR(match))
268 return;
269 }
270
271 match->compare[match->num].fn = compare;
272 match->compare[match->num].data = compare_data;
273 match->num++;
274 }
275 EXPORT_SYMBOL(component_match_add);
276
277 int component_master_add_with_match(struct device *dev,
278 const struct component_master_ops *ops,
279 struct component_match *match)
280 {
281 struct master *master;
282 int ret;
283
284 /* Reallocate the match array for its true size */
285 match = component_match_realloc(dev, match, match->num);
286 if (IS_ERR(match))
287 return PTR_ERR(match);
288
289 master = kzalloc(sizeof(*master), GFP_KERNEL);
290 if (!master)
291 return -ENOMEM;
292
293 master->dev = dev;
294 master->ops = ops;
295 master->match = match;
296 INIT_LIST_HEAD(&master->components);
297
298 /* Add to the list of available masters. */
299 mutex_lock(&component_mutex);
300 list_add(&master->node, &masters);
301
302 ret = try_to_bring_up_master(master, NULL);
303
304 if (ret < 0) {
305 /* Delete off the list if we weren't successful */
306 list_del(&master->node);
307 kfree(master);
308 }
309 mutex_unlock(&component_mutex);
310
311 return ret < 0 ? ret : 0;
312 }
313 EXPORT_SYMBOL_GPL(component_master_add_with_match);
314
315 void component_master_del(struct device *dev,
316 const struct component_master_ops *ops)
317 {
318 struct master *master;
319
320 mutex_lock(&component_mutex);
321 master = __master_find(dev, ops);
322 if (master) {
323 take_down_master(master);
324
325 list_del(&master->node);
326 kfree(master);
327 }
328 mutex_unlock(&component_mutex);
329 }
330 EXPORT_SYMBOL_GPL(component_master_del);
331
332 static void component_unbind(struct component *component,
333 struct master *master, void *data)
334 {
335 WARN_ON(!component->bound);
336
337 component->ops->unbind(component->dev, master->dev, data);
338 component->bound = false;
339
340 /* Release all resources claimed in the binding of this component */
341 devres_release_group(component->dev, component);
342 }
343
344 void component_unbind_all(struct device *master_dev, void *data)
345 {
346 struct master *master;
347 struct component *c;
348
349 WARN_ON(!mutex_is_locked(&component_mutex));
350
351 master = __master_find(master_dev, NULL);
352 if (!master)
353 return;
354
355 list_for_each_entry_reverse(c, &master->components, master_node)
356 component_unbind(c, master, data);
357 }
358 EXPORT_SYMBOL_GPL(component_unbind_all);
359
360 static int component_bind(struct component *component, struct master *master,
361 void *data)
362 {
363 int ret;
364
365 /*
366 * Each component initialises inside its own devres group.
367 * This allows us to roll-back a failed component without
368 * affecting anything else.
369 */
370 if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
371 return -ENOMEM;
372
373 /*
374 * Also open a group for the device itself: this allows us
375 * to release the resources claimed against the sub-device
376 * at the appropriate moment.
377 */
378 if (!devres_open_group(component->dev, component, GFP_KERNEL)) {
379 devres_release_group(master->dev, NULL);
380 return -ENOMEM;
381 }
382
383 dev_dbg(master->dev, "binding %s (ops %ps)\n",
384 dev_name(component->dev), component->ops);
385
386 ret = component->ops->bind(component->dev, master->dev, data);
387 if (!ret) {
388 component->bound = true;
389
390 /*
391 * Close the component device's group so that resources
392 * allocated in the binding are encapsulated for removal
393 * at unbind. Remove the group on the DRM device as we
394 * can clean those resources up independently.
395 */
396 devres_close_group(component->dev, NULL);
397 devres_remove_group(master->dev, NULL);
398
399 dev_info(master->dev, "bound %s (ops %ps)\n",
400 dev_name(component->dev), component->ops);
401 } else {
402 devres_release_group(component->dev, NULL);
403 devres_release_group(master->dev, NULL);
404
405 dev_err(master->dev, "failed to bind %s (ops %ps): %d\n",
406 dev_name(component->dev), component->ops, ret);
407 }
408
409 return ret;
410 }
411
412 int component_bind_all(struct device *master_dev, void *data)
413 {
414 struct master *master;
415 struct component *c;
416 int ret = 0;
417
418 WARN_ON(!mutex_is_locked(&component_mutex));
419
420 master = __master_find(master_dev, NULL);
421 if (!master)
422 return -EINVAL;
423
424 list_for_each_entry(c, &master->components, master_node) {
425 ret = component_bind(c, master, data);
426 if (ret)
427 break;
428 }
429
430 if (ret != 0) {
431 list_for_each_entry_continue_reverse(c, &master->components,
432 master_node)
433 component_unbind(c, master, data);
434 }
435
436 return ret;
437 }
438 EXPORT_SYMBOL_GPL(component_bind_all);
439
440 int component_add(struct device *dev, const struct component_ops *ops)
441 {
442 struct component *component;
443 int ret;
444
445 component = kzalloc(sizeof(*component), GFP_KERNEL);
446 if (!component)
447 return -ENOMEM;
448
449 component->ops = ops;
450 component->dev = dev;
451
452 dev_dbg(dev, "adding component (ops %ps)\n", ops);
453
454 mutex_lock(&component_mutex);
455 list_add_tail(&component->node, &component_list);
456
457 ret = try_to_bring_up_masters(component);
458 if (ret < 0) {
459 list_del(&component->node);
460
461 kfree(component);
462 }
463 mutex_unlock(&component_mutex);
464
465 return ret < 0 ? ret : 0;
466 }
467 EXPORT_SYMBOL_GPL(component_add);
468
469 void component_del(struct device *dev, const struct component_ops *ops)
470 {
471 struct component *c, *component = NULL;
472
473 mutex_lock(&component_mutex);
474 list_for_each_entry(c, &component_list, node)
475 if (c->dev == dev && c->ops == ops) {
476 list_del(&c->node);
477 component = c;
478 break;
479 }
480
481 if (component && component->master)
482 take_down_master(component->master);
483
484 mutex_unlock(&component_mutex);
485
486 WARN_ON(!component);
487 kfree(component);
488 }
489 EXPORT_SYMBOL_GPL(component_del);
490
491 MODULE_LICENSE("GPL v2");