blob: 891f0e38647ccb9c6eec0a934240411dc5f4ea28 [file] [log] [blame]
Greg Kroah-Hartman989d42e2017-11-07 17:30:07 +01001// SPDX-License-Identifier: GPL-2.0
Russell King2a41e602014-01-10 23:23:37 +00002/*
3 * Componentized device handling.
4 *
Russell King2a41e602014-01-10 23:23:37 +00005 * This is work in progress. We gather up the component devices into a list,
6 * and bind them when instructed. At the moment, we're specific to the DRM
7 * subsystem, and only handles one master device, but this doesn't have to be
8 * the case.
9 */
10#include <linux/component.h>
11#include <linux/device.h>
12#include <linux/kref.h>
13#include <linux/list.h>
14#include <linux/module.h>
15#include <linux/mutex.h>
16#include <linux/slab.h>
17
Russell Kingffc30b72014-04-18 23:05:53 +010018struct component;
19
Russell Kingce657b12015-11-17 12:08:01 +000020struct component_match_array {
21 void *data;
22 int (*compare)(struct device *, void *);
23 void (*release)(struct device *, void *);
24 struct component *component;
25 bool duplicate;
26};
27
Russell King6955b582014-04-19 11:18:01 +010028struct component_match {
29 size_t alloc;
30 size_t num;
Russell Kingce657b12015-11-17 12:08:01 +000031 struct component_match_array *compare;
Russell King6955b582014-04-19 11:18:01 +010032};
33
Russell King2a41e602014-01-10 23:23:37 +000034struct master {
35 struct list_head node;
Russell King2a41e602014-01-10 23:23:37 +000036 bool bound;
37
38 const struct component_master_ops *ops;
39 struct device *dev;
Russell King6955b582014-04-19 11:18:01 +010040 struct component_match *match;
Russell King2a41e602014-01-10 23:23:37 +000041};
42
43struct component {
44 struct list_head node;
Russell King2a41e602014-01-10 23:23:37 +000045 struct master *master;
46 bool bound;
47
48 const struct component_ops *ops;
49 struct device *dev;
50};
51
52static DEFINE_MUTEX(component_mutex);
53static LIST_HEAD(component_list);
54static LIST_HEAD(masters);
55
56static struct master *__master_find(struct device *dev,
57 const struct component_master_ops *ops)
58{
59 struct master *m;
60
61 list_for_each_entry(m, &masters, node)
62 if (m->dev == dev && (!ops || m->ops == ops))
63 return m;
64
65 return NULL;
66}
67
Russell Kingffc30b72014-04-18 23:05:53 +010068static struct component *find_component(struct master *master,
Russell King2a41e602014-01-10 23:23:37 +000069 int (*compare)(struct device *, void *), void *compare_data)
70{
71 struct component *c;
Russell King2a41e602014-01-10 23:23:37 +000072
73 list_for_each_entry(c, &component_list, node) {
Russell Kingfcbcebc2014-04-18 20:16:22 +010074 if (c->master && c->master != master)
Russell King2a41e602014-01-10 23:23:37 +000075 continue;
76
Russell Kingffc30b72014-04-18 23:05:53 +010077 if (compare(c->dev, compare_data))
78 return c;
Russell King2a41e602014-01-10 23:23:37 +000079 }
80
Russell Kingffc30b72014-04-18 23:05:53 +010081 return NULL;
Russell King2a41e602014-01-10 23:23:37 +000082}
Russell King2a41e602014-01-10 23:23:37 +000083
Russell King6955b582014-04-19 11:18:01 +010084static int find_components(struct master *master)
85{
86 struct component_match *match = master->match;
87 size_t i;
88 int ret = 0;
89
Russell King6955b582014-04-19 11:18:01 +010090 /*
91 * Scan the array of match functions and attach
92 * any components which are found to this master.
93 */
94 for (i = 0; i < match->num; i++) {
Russell Kingce657b12015-11-17 12:08:01 +000095 struct component_match_array *mc = &match->compare[i];
Russell Kingffc30b72014-04-18 23:05:53 +010096 struct component *c;
97
98 dev_dbg(master->dev, "Looking for component %zu\n", i);
99
100 if (match->compare[i].component)
101 continue;
102
Russell Kingce657b12015-11-17 12:08:01 +0000103 c = find_component(master, mc->compare, mc->data);
Russell Kingffc30b72014-04-18 23:05:53 +0100104 if (!c) {
105 ret = -ENXIO;
Russell King6955b582014-04-19 11:18:01 +0100106 break;
Russell Kingffc30b72014-04-18 23:05:53 +0100107 }
108
109 dev_dbg(master->dev, "found component %s, duplicate %u\n", dev_name(c->dev), !!c->master);
110
111 /* Attach this component to the master */
112 match->compare[i].duplicate = !!c->master;
113 match->compare[i].component = c;
114 c->master = master;
Russell King6955b582014-04-19 11:18:01 +0100115 }
116 return ret;
117}
118
Russell Kingffc30b72014-04-18 23:05:53 +0100119/* Detach component from associated master */
120static void remove_component(struct master *master, struct component *c)
Russell King2a41e602014-01-10 23:23:37 +0000121{
Russell Kingffc30b72014-04-18 23:05:53 +0100122 size_t i;
Russell King2a41e602014-01-10 23:23:37 +0000123
Russell Kingffc30b72014-04-18 23:05:53 +0100124 /* Detach the component from this master. */
125 for (i = 0; i < master->match->num; i++)
126 if (master->match->compare[i].component == c)
127 master->match->compare[i].component = NULL;
Russell King2a41e602014-01-10 23:23:37 +0000128}
129
130/*
131 * Try to bring up a master. If component is NULL, we're interested in
132 * this master, otherwise it's a component which must be present to try
133 * and bring up the master.
134 *
135 * Returns 1 for successful bringup, 0 if not ready, or -ve errno.
136 */
137static int try_to_bring_up_master(struct master *master,
138 struct component *component)
139{
Russell Kingc3349402014-04-23 10:52:17 +0100140 int ret;
Russell King2a41e602014-01-10 23:23:37 +0000141
Russell Kingffc30b72014-04-18 23:05:53 +0100142 dev_dbg(master->dev, "trying to bring up master\n");
143
Russell King6955b582014-04-19 11:18:01 +0100144 if (find_components(master)) {
Russell Kingffc30b72014-04-18 23:05:53 +0100145 dev_dbg(master->dev, "master has incomplete components\n");
146 return 0;
Russell King2a41e602014-01-10 23:23:37 +0000147 }
Russell Kingc3349402014-04-23 10:52:17 +0100148
149 if (component && component->master != master) {
Russell Kingffc30b72014-04-18 23:05:53 +0100150 dev_dbg(master->dev, "master is not for this component (%s)\n",
151 dev_name(component->dev));
152 return 0;
Russell Kingc3349402014-04-23 10:52:17 +0100153 }
154
Russell Kingffc30b72014-04-18 23:05:53 +0100155 if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
156 return -ENOMEM;
Russell Kingc3349402014-04-23 10:52:17 +0100157
158 /* Found all components */
159 ret = master->ops->bind(master->dev);
160 if (ret < 0) {
161 devres_release_group(master->dev, NULL);
162 dev_info(master->dev, "master bind failed: %d\n", ret);
Russell Kingffc30b72014-04-18 23:05:53 +0100163 return ret;
Russell Kingc3349402014-04-23 10:52:17 +0100164 }
165
166 master->bound = true;
167 return 1;
Russell King2a41e602014-01-10 23:23:37 +0000168}
169
170static int try_to_bring_up_masters(struct component *component)
171{
172 struct master *m;
173 int ret = 0;
174
175 list_for_each_entry(m, &masters, node) {
Russell King29f1c7f2014-04-23 10:46:11 +0100176 if (!m->bound) {
177 ret = try_to_bring_up_master(m, component);
178 if (ret != 0)
179 break;
180 }
Russell King2a41e602014-01-10 23:23:37 +0000181 }
182
183 return ret;
184}
185
186static void take_down_master(struct master *master)
187{
188 if (master->bound) {
189 master->ops->unbind(master->dev);
Russell King9e1ccb42014-02-07 20:09:27 +0000190 devres_release_group(master->dev, NULL);
Russell King2a41e602014-01-10 23:23:37 +0000191 master->bound = false;
192 }
Russell King2a41e602014-01-10 23:23:37 +0000193}
194
Russell Kingce657b12015-11-17 12:08:01 +0000195static void component_match_release(struct device *master,
196 struct component_match *match)
Russell King6955b582014-04-19 11:18:01 +0100197{
Russell Kingce657b12015-11-17 12:08:01 +0000198 unsigned int i;
199
200 for (i = 0; i < match->num; i++) {
201 struct component_match_array *mc = &match->compare[i];
202
203 if (mc->release)
204 mc->release(master, mc->data);
205 }
Russell King9a4e7842016-01-26 14:49:22 +0000206
207 kfree(match->compare);
Russell King6955b582014-04-19 11:18:01 +0100208}
209
Russell Kingce657b12015-11-17 12:08:01 +0000210static void devm_component_match_release(struct device *dev, void *res)
211{
212 component_match_release(dev, res);
213}
214
215static int component_match_realloc(struct device *dev,
Russell King6955b582014-04-19 11:18:01 +0100216 struct component_match *match, size_t num)
217{
Russell Kingce657b12015-11-17 12:08:01 +0000218 struct component_match_array *new;
Russell King6955b582014-04-19 11:18:01 +0100219
Russell Kingce657b12015-11-17 12:08:01 +0000220 if (match->alloc == num)
221 return 0;
Russell King6955b582014-04-19 11:18:01 +0100222
Russell King9a4e7842016-01-26 14:49:22 +0000223 new = kmalloc_array(num, sizeof(*new), GFP_KERNEL);
Russell King6955b582014-04-19 11:18:01 +0100224 if (!new)
Russell Kingce657b12015-11-17 12:08:01 +0000225 return -ENOMEM;
Russell King6955b582014-04-19 11:18:01 +0100226
Russell Kingce657b12015-11-17 12:08:01 +0000227 if (match->compare) {
228 memcpy(new, match->compare, sizeof(*new) *
229 min(match->num, num));
Russell King9a4e7842016-01-26 14:49:22 +0000230 kfree(match->compare);
Russell King6955b582014-04-19 11:18:01 +0100231 }
Russell Kingce657b12015-11-17 12:08:01 +0000232 match->compare = new;
233 match->alloc = num;
Russell King6955b582014-04-19 11:18:01 +0100234
Russell Kingce657b12015-11-17 12:08:01 +0000235 return 0;
Russell King6955b582014-04-19 11:18:01 +0100236}
237
238/*
Russell Kingce657b12015-11-17 12:08:01 +0000239 * Add a component to be matched, with a release function.
Russell King6955b582014-04-19 11:18:01 +0100240 *
241 * The match array is first created or extended if necessary.
242 */
Russell Kingce657b12015-11-17 12:08:01 +0000243void component_match_add_release(struct device *master,
244 struct component_match **matchptr,
245 void (*release)(struct device *, void *),
Russell King6955b582014-04-19 11:18:01 +0100246 int (*compare)(struct device *, void *), void *compare_data)
247{
248 struct component_match *match = *matchptr;
249
250 if (IS_ERR(match))
251 return;
252
Russell Kingce657b12015-11-17 12:08:01 +0000253 if (!match) {
254 match = devres_alloc(devm_component_match_release,
255 sizeof(*match), GFP_KERNEL);
256 if (!match) {
257 *matchptr = ERR_PTR(-ENOMEM);
258 return;
259 }
Russell King6955b582014-04-19 11:18:01 +0100260
Russell Kingce657b12015-11-17 12:08:01 +0000261 devres_add(master, match);
Russell King6955b582014-04-19 11:18:01 +0100262
263 *matchptr = match;
Russell King6955b582014-04-19 11:18:01 +0100264 }
265
Russell Kingce657b12015-11-17 12:08:01 +0000266 if (match->num == match->alloc) {
Sudip Mukherjee4877bb92016-02-02 12:57:53 +0530267 size_t new_size = match->alloc + 16;
Russell Kingce657b12015-11-17 12:08:01 +0000268 int ret;
269
270 ret = component_match_realloc(master, match, new_size);
271 if (ret) {
272 *matchptr = ERR_PTR(ret);
273 return;
274 }
275 }
276
277 match->compare[match->num].compare = compare;
278 match->compare[match->num].release = release;
Russell King6955b582014-04-19 11:18:01 +0100279 match->compare[match->num].data = compare_data;
Russell Kingffc30b72014-04-18 23:05:53 +0100280 match->compare[match->num].component = NULL;
Russell King6955b582014-04-19 11:18:01 +0100281 match->num++;
282}
Russell Kingce657b12015-11-17 12:08:01 +0000283EXPORT_SYMBOL(component_match_add_release);
Russell King6955b582014-04-19 11:18:01 +0100284
Jon Medhurst (Tixy)57480482016-01-26 17:59:13 +0000285static void free_master(struct master *master)
286{
287 struct component_match *match = master->match;
288 int i;
289
290 list_del(&master->node);
291
292 if (match) {
293 for (i = 0; i < match->num; i++) {
294 struct component *c = match->compare[i].component;
295 if (c)
296 c->master = NULL;
297 }
298 }
299
300 kfree(master);
301}
302
Russell King6955b582014-04-19 11:18:01 +0100303int component_master_add_with_match(struct device *dev,
304 const struct component_master_ops *ops,
305 struct component_match *match)
Russell King2a41e602014-01-10 23:23:37 +0000306{
307 struct master *master;
308 int ret;
309
Russell Kingfae9e2e2014-04-18 22:10:32 +0100310 /* Reallocate the match array for its true size */
Russell Kingce657b12015-11-17 12:08:01 +0000311 ret = component_match_realloc(dev, match, match->num);
312 if (ret)
313 return ret;
Russell King6955b582014-04-19 11:18:01 +0100314
Russell King2a41e602014-01-10 23:23:37 +0000315 master = kzalloc(sizeof(*master), GFP_KERNEL);
316 if (!master)
317 return -ENOMEM;
318
319 master->dev = dev;
320 master->ops = ops;
Russell King6955b582014-04-19 11:18:01 +0100321 master->match = match;
Russell King2a41e602014-01-10 23:23:37 +0000322
323 /* Add to the list of available masters. */
324 mutex_lock(&component_mutex);
325 list_add(&master->node, &masters);
326
327 ret = try_to_bring_up_master(master, NULL);
328
Jon Medhurst (Tixy)57480482016-01-26 17:59:13 +0000329 if (ret < 0)
330 free_master(master);
331
Russell King2a41e602014-01-10 23:23:37 +0000332 mutex_unlock(&component_mutex);
333
334 return ret < 0 ? ret : 0;
335}
Russell King6955b582014-04-19 11:18:01 +0100336EXPORT_SYMBOL_GPL(component_master_add_with_match);
337
Russell King2a41e602014-01-10 23:23:37 +0000338void component_master_del(struct device *dev,
339 const struct component_master_ops *ops)
340{
341 struct master *master;
342
343 mutex_lock(&component_mutex);
344 master = __master_find(dev, ops);
345 if (master) {
346 take_down_master(master);
Jon Medhurst (Tixy)57480482016-01-26 17:59:13 +0000347 free_master(master);
Russell King2a41e602014-01-10 23:23:37 +0000348 }
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;
Russell Kingffc30b72014-04-18 23:05:53 +0100369 size_t i;
Russell King2a41e602014-01-10 23:23:37 +0000370
371 WARN_ON(!mutex_is_locked(&component_mutex));
372
373 master = __master_find(master_dev, NULL);
374 if (!master)
375 return;
376
Russell Kingffc30b72014-04-18 23:05:53 +0100377 /* 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 }
Russell King2a41e602014-01-10 23:23:37 +0000383}
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;
Russell Kingffc30b72014-04-18 23:05:53 +0100442 size_t i;
Russell King2a41e602014-01-10 23:23:37 +0000443 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
Russell Kingffc30b72014-04-18 23:05:53 +0100451 /* 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 }
Russell King2a41e602014-01-10 23:23:37 +0000459
460 if (ret != 0) {
Russell Kingffc30b72014-04-18 23:05:53 +0100461 for (; i--; )
462 if (!master->match->compare[i].duplicate) {
463 c = master->match->compare[i].component;
464 component_unbind(c, master, data);
465 }
Russell King2a41e602014-01-10 23:23:37 +0000466 }
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) {
Daniel Stone8e7199c2016-02-08 21:12:58 +0000491 if (component->master)
492 remove_component(component->master, component);
Russell King2a41e602014-01-10 23:23:37 +0000493 list_del(&component->node);
494
495 kfree(component);
496 }
497 mutex_unlock(&component_mutex);
498
499 return ret < 0 ? ret : 0;
500}
501EXPORT_SYMBOL_GPL(component_add);
502
503void component_del(struct device *dev, const struct component_ops *ops)
504{
505 struct component *c, *component = NULL;
506
507 mutex_lock(&component_mutex);
508 list_for_each_entry(c, &component_list, node)
509 if (c->dev == dev && c->ops == ops) {
510 list_del(&c->node);
511 component = c;
512 break;
513 }
514
Russell Kingffc30b72014-04-18 23:05:53 +0100515 if (component && component->master) {
Russell King2a41e602014-01-10 23:23:37 +0000516 take_down_master(component->master);
Russell Kingffc30b72014-04-18 23:05:53 +0100517 remove_component(component->master, component);
518 }
Russell King2a41e602014-01-10 23:23:37 +0000519
520 mutex_unlock(&component_mutex);
521
522 WARN_ON(!component);
523 kfree(component);
524}
525EXPORT_SYMBOL_GPL(component_del);
526
527MODULE_LICENSE("GPL v2");