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