xref: /linux-6.15/include/linux/component.h (revision 4d69c80e)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef COMPONENT_H
3 #define COMPONENT_H
4 
5 #include <linux/stddef.h>
6 
7 
8 struct device;
9 
10 /**
11  * struct component_ops - callbacks for component drivers
12  *
13  * Components are registered with component_add() and unregistered with
14  * component_del().
15  */
16 struct component_ops {
17 	/**
18 	 * @bind:
19 	 *
20 	 * Called through component_bind_all() when the aggregate driver is
21 	 * ready to bind the overall driver.
22 	 */
23 	int (*bind)(struct device *comp, struct device *master,
24 		    void *master_data);
25 	/**
26 	 * @unbind:
27 	 *
28 	 * Called through component_unbind_all() when the aggregate driver is
29 	 * ready to bind the overall driver, or when component_bind_all() fails
30 	 * part-ways through and needs to unbind some already bound components.
31 	 */
32 	void (*unbind)(struct device *comp, struct device *master,
33 		       void *master_data);
34 };
35 
36 int component_add(struct device *, const struct component_ops *);
37 void component_del(struct device *, const struct component_ops *);
38 
39 int component_bind_all(struct device *master, void *master_data);
40 void component_unbind_all(struct device *master, void *master_data);
41 
42 struct master;
43 
44 /**
45  * struct component_master_ops - callback for the aggregate driver
46  *
47  * Aggregate drivers are registered with component_master_add_with_match() and
48  * unregistered with component_master_del().
49  */
50 struct component_master_ops {
51 	/**
52 	 * @bind:
53 	 *
54 	 * Called when all components or the aggregate driver, as specified in
55 	 * the match list passed to component_master_add_with_match(), are
56 	 * ready. Usually there are 3 steps to bind an aggregate driver:
57 	 *
58 	 * 1. Allocate a structure for the aggregate driver.
59 	 *
60 	 * 2. Bind all components to the aggregate driver by calling
61 	 *    component_bind_all() with the aggregate driver structure as opaque
62 	 *    pointer data.
63 	 *
64 	 * 3. Register the aggregate driver with the subsystem to publish its
65 	 *    interfaces.
66 	 *
67 	 * Note that the lifetime of the aggregate driver does not align with
68 	 * any of the underlying &struct device instances. Therefore devm cannot
69 	 * be used and all resources acquired or allocated in this callback must
70 	 * be explicitly released in the @unbind callback.
71 	 */
72 	int (*bind)(struct device *master);
73 	/**
74 	 * @unbind:
75 	 *
76 	 * Called when either the aggregate driver, using
77 	 * component_master_del(), or one of its components, using
78 	 * component_del(), is unregistered.
79 	 */
80 	void (*unbind)(struct device *master);
81 };
82 
83 void component_master_del(struct device *,
84 	const struct component_master_ops *);
85 
86 struct component_match;
87 
88 int component_master_add_with_match(struct device *,
89 	const struct component_master_ops *, struct component_match *);
90 void component_match_add_release(struct device *master,
91 	struct component_match **matchptr,
92 	void (*release)(struct device *, void *),
93 	int (*compare)(struct device *, void *), void *compare_data);
94 
95 /**
96  * component_match_add - add a compent match
97  * @master: device with the aggregate driver
98  * @matchptr: pointer to the list of component matches
99  * @compare: compare function to match against all components
100  * @compare_data: opaque pointer passed to the @compare function
101  *
102  * Adds a new component match to the list stored in @matchptr, which the @master
103  * aggregate driver needs to function. The list of component matches pointed to
104  * by @matchptr must be initialized to NULL before adding the first match.
105  *
106  * The allocated match list in @matchptr is automatically released using devm
107  * actions.
108  *
109  * See also component_match_add_release().
110  */
111 static inline void component_match_add(struct device *master,
112 	struct component_match **matchptr,
113 	int (*compare)(struct device *, void *), void *compare_data)
114 {
115 	component_match_add_release(master, matchptr, NULL, compare,
116 				    compare_data);
117 }
118 
119 #endif
120