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