blob: 89b032f2ffd222ed65ec771634fe1e694b916bbb [file] [log] [blame]
Russell King2a41e602014-01-10 23:23:37 +00001/*
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
Russell Kingffc30b72014-04-18 23:05:53 +010021struct component;
22
Russell Kingce657b12015-11-17 12:08:01 +000023struct 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
Russell King6955b582014-04-19 11:18:01 +010031struct component_match {
32 size_t alloc;
33 size_t num;
Russell Kingce657b12015-11-17 12:08:01 +000034 struct component_match_array *compare;
Russell King6955b582014-04-19 11:18:01 +010035};
36
Russell King2a41e602014-01-10 23:23:37 +000037struct master {
38 struct list_head node;
Russell King2a41e602014-01-10 23:23:37 +000039 bool bound;
40
41 const struct component_master_ops *ops;
42 struct device *dev;
Russell King6955b582014-04-19 11:18:01 +010043 struct component_match *match;
Russell King2a41e602014-01-10 23:23:37 +000044};
45
46struct component {
47 struct list_head node;
Russell King2a41e602014-01-10 23:23:37 +000048 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
Russell Kingffc30b72014-04-18 23:05:53 +010071static struct component *find_component(struct master *master,
Russell King2a41e602014-01-10 23:23:37 +000072 int (*compare)(struct device *, void *), void *compare_data)
73{
74 struct component *c;
Russell King2a41e602014-01-10 23:23:37 +000075
76 list_for_each_entry(c, &component_list, node) {
Russell Kingfcbcebc2014-04-18 20:16:22 +010077 if (c->master && c->master != master)
Russell King2a41e602014-01-10 23:23:37 +000078 continue;
79
Russell Kingffc30b72014-04-18 23:05:53 +010080 if (compare(c->dev, compare_data))
81 return c;
Russell King2a41e602014-01-10 23:23:37 +000082 }
83
Russell Kingffc30b72014-04-18 23:05:53 +010084 return NULL;
Russell King2a41e602014-01-10 23:23:37 +000085}
Russell King2a41e602014-01-10 23:23:37 +000086
Russell King6955b582014-04-19 11:18:01 +010087static int find_components(struct master *master)
88{
89 struct component_match *match = master->match;
90 size_t i;
91 int ret = 0;
92
Russell King6955b582014-04-19 11:18:01 +010093 /*
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++) {
Russell Kingce657b12015-11-17 12:08:01 +000098 struct component_match_array *mc = &match->compare[i];
Russell Kingffc30b72014-04-18 23:05:53 +010099 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
Russell Kingce657b12015-11-17 12:08:01 +0000106 c = find_component(master, mc->compare, mc->data);
Russell Kingffc30b72014-04-18 23:05:53 +0100107 if (!c) {
108 ret = -ENXIO;
Russell King6955b582014-04-19 11:18:01 +0100109 break;
Russell Kingffc30b72014-04-18 23:05:53 +0100110 }
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;
Russell King6955b582014-04-19 11:18:01 +0100118 }
119 return ret;
120}
121
Russell Kingffc30b72014-04-18 23:05:53 +0100122/* Detach component from associated master */
123static void remove_component(struct master *master, struct component *c)
Russell King2a41e602014-01-10 23:23:37 +0000124{
Russell Kingffc30b72014-04-18 23:05:53 +0100125 size_t i;
Russell King2a41e602014-01-10 23:23:37 +0000126
Russell Kingffc30b72014-04-18 23:05:53 +0100127 /* 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;
Russell King2a41e602014-01-10 23:23:37 +0000131}
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{
Russell Kingc3349402014-04-23 10:52:17 +0100143 int ret;
Russell King2a41e602014-01-10 23:23:37 +0000144
Russell Kingffc30b72014-04-18 23:05:53 +0100145 dev_dbg(master->dev, "trying to bring up master\n");
146
Russell King6955b582014-04-19 11:18:01 +0100147 if (find_components(master)) {
Russell Kingffc30b72014-04-18 23:05:53 +0100148 dev_dbg(master->dev, "master has incomplete components\n");
149 return 0;
Russell King2a41e602014-01-10 23:23:37 +0000150 }
Russell Kingc3349402014-04-23 10:52:17 +0100151
152 if (component && component->master != master) {
Russell Kingffc30b72014-04-18 23:05:53 +0100153 dev_dbg(master->dev, "master is not for this component (%s)\n",
154 dev_name(component->dev));
155 return 0;
Russell Kingc3349402014-04-23 10:52:17 +0100156 }
157
Russell Kingffc30b72014-04-18 23:05:53 +0100158 if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
159 return -ENOMEM;
Russell Kingc3349402014-04-23 10:52:17 +0100160
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);
Russell Kingffc30b72014-04-18 23:05:53 +0100166 return ret;
Russell Kingc3349402014-04-23 10:52:17 +0100167 }
168
169 master->bound = true;
170 return 1;
Russell King2a41e602014-01-10 23:23:37 +0000171}
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) {
Russell King29f1c7f2014-04-23 10:46:11 +0100179 if (!m->bound) {
180 ret = try_to_bring_up_master(m, component);
181 if (ret != 0)
182 break;
183 }
Russell King2a41e602014-01-10 23:23:37 +0000184 }
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);
Russell King9e1ccb42014-02-07 20:09:27 +0000193 devres_release_group(master->dev, NULL);
Russell King2a41e602014-01-10 23:23:37 +0000194 master->bound = false;
195 }
Russell King2a41e602014-01-10 23:23:37 +0000196}
197
Russell Kingce657b12015-11-17 12:08:01 +0000198static void component_match_release(struct device *master,
199 struct component_match *match)
Russell King6955b582014-04-19 11:18:01 +0100200{
Russell Kingce657b12015-11-17 12:08:01 +0000201 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 }
Russell King9a4e7842016-01-26 14:49:22 +0000209
210 kfree(match->compare);
Russell King6955b582014-04-19 11:18:01 +0100211}
212
Russell Kingce657b12015-11-17 12:08:01 +0000213static 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,
Russell King6955b582014-04-19 11:18:01 +0100219 struct component_match *match, size_t num)
220{
Russell Kingce657b12015-11-17 12:08:01 +0000221 struct component_match_array *new;
Russell King6955b582014-04-19 11:18:01 +0100222
Russell Kingce657b12015-11-17 12:08:01 +0000223 if (match->alloc == num)
224 return 0;
Russell King6955b582014-04-19 11:18:01 +0100225
Russell King9a4e7842016-01-26 14:49:22 +0000226 new = kmalloc_array(num, sizeof(*new), GFP_KERNEL);
Russell King6955b582014-04-19 11:18:01 +0100227 if (!new)
Russell Kingce657b12015-11-17 12:08:01 +0000228 return -ENOMEM;
Russell King6955b582014-04-19 11:18:01 +0100229
Russell Kingce657b12015-11-17 12:08:01 +0000230 if (match->compare) {
231 memcpy(new, match->compare, sizeof(*new) *
232 min(match->num, num));
Russell King9a4e7842016-01-26 14:49:22 +0000233 kfree(match->compare);
Russell King6955b582014-04-19 11:18:01 +0100234 }
Russell Kingce657b12015-11-17 12:08:01 +0000235 match->compare = new;
236 match->alloc = num;
Russell King6955b582014-04-19 11:18:01 +0100237
Russell Kingce657b12015-11-17 12:08:01 +0000238 return 0;
Russell King6955b582014-04-19 11:18:01 +0100239}
240
241/*
Russell Kingce657b12015-11-17 12:08:01 +0000242 * Add a component to be matched, with a release function.
Russell King6955b582014-04-19 11:18:01 +0100243 *
244 * The match array is first created or extended if necessary.
245 */
Russell Kingce657b12015-11-17 12:08:01 +0000246void component_match_add_release(struct device *master,
247 struct component_match **matchptr,
248 void (*release)(struct device *, void *),
Russell King6955b582014-04-19 11:18:01 +0100249 int (*compare)(struct device *, void *), void *compare_data)
250{
251 struct component_match *match = *matchptr;
252
253 if (IS_ERR(match))
254 return;
255
Russell Kingce657b12015-11-17 12:08:01 +0000256 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 }
Russell King6955b582014-04-19 11:18:01 +0100263
Russell Kingce657b12015-11-17 12:08:01 +0000264 devres_add(master, match);
Russell King6955b582014-04-19 11:18:01 +0100265
266 *matchptr = match;
Russell King6955b582014-04-19 11:18:01 +0100267 }
268
Russell Kingce657b12015-11-17 12:08:01 +0000269 if (match->num == match->alloc) {
Sudip Mukherjee4877bb92016-02-02 12:57:53 +0530270 size_t new_size = match->alloc + 16;
Russell Kingce657b12015-11-17 12:08:01 +0000271 int ret;
272
273 ret = component_match_realloc(master, match, new_size);
274 if (ret) {
275 *matchptr = ERR_PTR(ret);
276 return;
277 }
278 }
279
280 match->compare[match->num].compare = compare;
281 match->compare[match->num].release = release;
Russell King6955b582014-04-19 11:18:01 +0100282 match->compare[match->num].data = compare_data;
Russell Kingffc30b72014-04-18 23:05:53 +0100283 match->compare[match->num].component = NULL;
Russell King6955b582014-04-19 11:18:01 +0100284 match->num++;
285}
Russell Kingce657b12015-11-17 12:08:01 +0000286EXPORT_SYMBOL(component_match_add_release);
Russell King6955b582014-04-19 11:18:01 +0100287
Jon Medhurst (Tixy)57480482016-01-26 17:59:13 +0000288static void free_master(struct master *master)
289{
290 struct component_match *match = master->match;
291 int i;
292
293 list_del(&master->node);
294
295 if (match) {
296 for (i = 0; i < match->num; i++) {
297 struct component *c = match->compare[i].component;
298 if (c)
299 c->master = NULL;
300 }
301 }
302
303 kfree(master);
304}
305
Russell King6955b582014-04-19 11:18:01 +0100306int component_master_add_with_match(struct device *dev,
307 const struct component_master_ops *ops,
308 struct component_match *match)
Russell King2a41e602014-01-10 23:23:37 +0000309{
310 struct master *master;
311 int ret;
312
Russell Kingfae9e2e2014-04-18 22:10:32 +0100313 /* Reallocate the match array for its true size */
Russell Kingce657b12015-11-17 12:08:01 +0000314 ret = component_match_realloc(dev, match, match->num);
315 if (ret)
316 return ret;
Russell King6955b582014-04-19 11:18:01 +0100317
Russell King2a41e602014-01-10 23:23:37 +0000318 master = kzalloc(sizeof(*master), GFP_KERNEL);
319 if (!master)
320 return -ENOMEM;
321
322 master->dev = dev;
323 master->ops = ops;
Russell King6955b582014-04-19 11:18:01 +0100324 master->match = match;
Russell King2a41e602014-01-10 23:23:37 +0000325
326 /* Add to the list of available masters. */
327 mutex_lock(&component_mutex);
328 list_add(&master->node, &masters);
329
330 ret = try_to_bring_up_master(master, NULL);
331
Jon Medhurst (Tixy)57480482016-01-26 17:59:13 +0000332 if (ret < 0)
333 free_master(master);
334
Russell King2a41e602014-01-10 23:23:37 +0000335 mutex_unlock(&component_mutex);
336
337 return ret < 0 ? ret : 0;
338}
Russell King6955b582014-04-19 11:18:01 +0100339EXPORT_SYMBOL_GPL(component_master_add_with_match);
340
Russell King2a41e602014-01-10 23:23:37 +0000341void component_master_del(struct device *dev,
342 const struct component_master_ops *ops)
343{
344 struct master *master;
345
346 mutex_lock(&component_mutex);
347 master = __master_find(dev, ops);
348 if (master) {
349 take_down_master(master);
Jon Medhurst (Tixy)57480482016-01-26 17:59:13 +0000350 free_master(master);
Russell King2a41e602014-01-10 23:23:37 +0000351 }
352 mutex_unlock(&component_mutex);
353}
354EXPORT_SYMBOL_GPL(component_master_del);
355
356static void component_unbind(struct component *component,
357 struct master *master, void *data)
358{
359 WARN_ON(!component->bound);
360
361 component->ops->unbind(component->dev, master->dev, data);
362 component->bound = false;
363
364 /* Release all resources claimed in the binding of this component */
365 devres_release_group(component->dev, component);
366}
367
368void component_unbind_all(struct device *master_dev, void *data)
369{
370 struct master *master;
371 struct component *c;
Russell Kingffc30b72014-04-18 23:05:53 +0100372 size_t i;
Russell King2a41e602014-01-10 23:23:37 +0000373
374 WARN_ON(!mutex_is_locked(&component_mutex));
375
376 master = __master_find(master_dev, NULL);
377 if (!master)
378 return;
379
Russell Kingffc30b72014-04-18 23:05:53 +0100380 /* Unbind components in reverse order */
381 for (i = master->match->num; i--; )
382 if (!master->match->compare[i].duplicate) {
383 c = master->match->compare[i].component;
384 component_unbind(c, master, data);
385 }
Russell King2a41e602014-01-10 23:23:37 +0000386}
387EXPORT_SYMBOL_GPL(component_unbind_all);
388
389static int component_bind(struct component *component, struct master *master,
390 void *data)
391{
392 int ret;
393
394 /*
395 * Each component initialises inside its own devres group.
396 * This allows us to roll-back a failed component without
397 * affecting anything else.
398 */
399 if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
400 return -ENOMEM;
401
402 /*
403 * Also open a group for the device itself: this allows us
404 * to release the resources claimed against the sub-device
405 * at the appropriate moment.
406 */
407 if (!devres_open_group(component->dev, component, GFP_KERNEL)) {
408 devres_release_group(master->dev, NULL);
409 return -ENOMEM;
410 }
411
412 dev_dbg(master->dev, "binding %s (ops %ps)\n",
413 dev_name(component->dev), component->ops);
414
415 ret = component->ops->bind(component->dev, master->dev, data);
416 if (!ret) {
417 component->bound = true;
418
419 /*
420 * Close the component device's group so that resources
421 * allocated in the binding are encapsulated for removal
422 * at unbind. Remove the group on the DRM device as we
423 * can clean those resources up independently.
424 */
425 devres_close_group(component->dev, NULL);
426 devres_remove_group(master->dev, NULL);
427
428 dev_info(master->dev, "bound %s (ops %ps)\n",
429 dev_name(component->dev), component->ops);
430 } else {
431 devres_release_group(component->dev, NULL);
432 devres_release_group(master->dev, NULL);
433
434 dev_err(master->dev, "failed to bind %s (ops %ps): %d\n",
435 dev_name(component->dev), component->ops, ret);
436 }
437
438 return ret;
439}
440
441int component_bind_all(struct device *master_dev, void *data)
442{
443 struct master *master;
444 struct component *c;
Russell Kingffc30b72014-04-18 23:05:53 +0100445 size_t i;
Russell King2a41e602014-01-10 23:23:37 +0000446 int ret = 0;
447
448 WARN_ON(!mutex_is_locked(&component_mutex));
449
450 master = __master_find(master_dev, NULL);
451 if (!master)
452 return -EINVAL;
453
Russell Kingffc30b72014-04-18 23:05:53 +0100454 /* Bind components in match order */
455 for (i = 0; i < master->match->num; i++)
456 if (!master->match->compare[i].duplicate) {
457 c = master->match->compare[i].component;
458 ret = component_bind(c, master, data);
459 if (ret)
460 break;
461 }
Russell King2a41e602014-01-10 23:23:37 +0000462
463 if (ret != 0) {
Russell Kingffc30b72014-04-18 23:05:53 +0100464 for (; i--; )
465 if (!master->match->compare[i].duplicate) {
466 c = master->match->compare[i].component;
467 component_unbind(c, master, data);
468 }
Russell King2a41e602014-01-10 23:23:37 +0000469 }
470
471 return ret;
472}
473EXPORT_SYMBOL_GPL(component_bind_all);
474
475int component_add(struct device *dev, const struct component_ops *ops)
476{
477 struct component *component;
478 int ret;
479
480 component = kzalloc(sizeof(*component), GFP_KERNEL);
481 if (!component)
482 return -ENOMEM;
483
484 component->ops = ops;
485 component->dev = dev;
486
487 dev_dbg(dev, "adding component (ops %ps)\n", ops);
488
489 mutex_lock(&component_mutex);
490 list_add_tail(&component->node, &component_list);
491
492 ret = try_to_bring_up_masters(component);
493 if (ret < 0) {
Daniel Stone8e7199c2016-02-08 21:12:58 +0000494 if (component->master)
495 remove_component(component->master, component);
Russell King2a41e602014-01-10 23:23:37 +0000496 list_del(&component->node);
497
498 kfree(component);
499 }
500 mutex_unlock(&component_mutex);
501
502 return ret < 0 ? ret : 0;
503}
504EXPORT_SYMBOL_GPL(component_add);
505
506void component_del(struct device *dev, const struct component_ops *ops)
507{
508 struct component *c, *component = NULL;
509
510 mutex_lock(&component_mutex);
511 list_for_each_entry(c, &component_list, node)
512 if (c->dev == dev && c->ops == ops) {
513 list_del(&c->node);
514 component = c;
515 break;
516 }
517
Russell Kingffc30b72014-04-18 23:05:53 +0100518 if (component && component->master) {
Russell King2a41e602014-01-10 23:23:37 +0000519 take_down_master(component->master);
Russell Kingffc30b72014-04-18 23:05:53 +0100520 remove_component(component->master, component);
521 }
Russell King2a41e602014-01-10 23:23:37 +0000522
523 mutex_unlock(&component_mutex);
524
525 WARN_ON(!component);
526 kfree(component);
527}
528EXPORT_SYMBOL_GPL(component_del);
529
530MODULE_LICENSE("GPL v2");