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