1818#include <linux/mutex.h>
1919#include <linux/slab.h>
2020
21+ struct component_match {
22+ size_t alloc ;
23+ size_t num ;
24+ struct {
25+ void * data ;
26+ int (* fn )(struct device * , void * );
27+ } compare [0 ];
28+ };
29+
2130struct master {
2231 struct list_head node ;
2332 struct list_head components ;
2433 bool bound ;
2534
2635 const struct component_master_ops * ops ;
2736 struct device * dev ;
37+ struct component_match * match ;
2838};
2939
3040struct component {
@@ -69,18 +79,24 @@ static void component_detach_master(struct master *master, struct component *c)
6979 c -> master = NULL ;
7080}
7181
82+ /*
83+ * Add a component to a master, finding the component via the compare
84+ * function and compare data. This is safe to call for duplicate matches
85+ * and will not result in the same component being added multiple times.
86+ */
7287int component_master_add_child (struct master * master ,
7388 int (* compare )(struct device * , void * ), void * compare_data )
7489{
7590 struct component * c ;
7691 int ret = - ENXIO ;
7792
7893 list_for_each_entry (c , & component_list , node ) {
79- if (c -> master )
94+ if (c -> master && c -> master != master )
8095 continue ;
8196
8297 if (compare (c -> dev , compare_data )) {
83- component_attach_master (master , c );
98+ if (!c -> master )
99+ component_attach_master (master , c );
84100 ret = 0 ;
85101 break ;
86102 }
@@ -90,6 +106,34 @@ int component_master_add_child(struct master *master,
90106}
91107EXPORT_SYMBOL_GPL (component_master_add_child );
92108
109+ static int find_components (struct master * master )
110+ {
111+ struct component_match * match = master -> match ;
112+ size_t i ;
113+ int ret = 0 ;
114+
115+ if (!match ) {
116+ /*
117+ * Search the list of components, looking for components that
118+ * belong to this master, and attach them to the master.
119+ */
120+ return master -> ops -> add_components (master -> dev , master );
121+ }
122+
123+ /*
124+ * Scan the array of match functions and attach
125+ * any components which are found to this master.
126+ */
127+ for (i = 0 ; i < match -> num ; i ++ ) {
128+ ret = component_master_add_child (master ,
129+ match -> compare [i ].fn ,
130+ match -> compare [i ].data );
131+ if (ret )
132+ break ;
133+ }
134+ return ret ;
135+ }
136+
93137/* Detach all attached components from this master */
94138static void master_remove_components (struct master * master )
95139{
@@ -113,44 +157,44 @@ static void master_remove_components(struct master *master)
113157static int try_to_bring_up_master (struct master * master ,
114158 struct component * component )
115159{
116- int ret = 0 ;
160+ int ret ;
117161
118- if (!master -> bound ) {
119- /*
120- * Search the list of components, looking for components that
121- * belong to this master, and attach them to the master.
122- */
123- if (master -> ops -> add_components (master -> dev , master )) {
124- /* Failed to find all components */
125- master_remove_components (master );
126- ret = 0 ;
127- goto out ;
128- }
162+ if (master -> bound )
163+ return 0 ;
129164
130- if (component && component -> master != master ) {
131- master_remove_components (master );
132- ret = 0 ;
133- goto out ;
134- }
165+ /*
166+ * Search the list of components, looking for components that
167+ * belong to this master, and attach them to the master.
168+ */
169+ if (find_components (master )) {
170+ /* Failed to find all components */
171+ ret = 0 ;
172+ goto out ;
173+ }
135174
136- if (! devres_open_group ( master -> dev , NULL , GFP_KERNEL ) ) {
137- ret = - ENOMEM ;
138- goto out ;
139- }
175+ if (component && component -> master != master ) {
176+ ret = 0 ;
177+ goto out ;
178+ }
140179
141- /* Found all components */
142- ret = master -> ops -> bind (master -> dev );
143- if (ret < 0 ) {
144- devres_release_group (master -> dev , NULL );
145- dev_info (master -> dev , "master bind failed: %d\n" , ret );
146- master_remove_components (master );
147- goto out ;
148- }
180+ if (!devres_open_group (master -> dev , NULL , GFP_KERNEL )) {
181+ ret = - ENOMEM ;
182+ goto out ;
183+ }
149184
150- master -> bound = true;
151- ret = 1 ;
185+ /* Found all components */
186+ ret = master -> ops -> bind (master -> dev );
187+ if (ret < 0 ) {
188+ devres_release_group (master -> dev , NULL );
189+ dev_info (master -> dev , "master bind failed: %d\n" , ret );
190+ goto out ;
152191 }
192+
193+ master -> bound = true;
194+ return 1 ;
195+
153196out :
197+ master_remove_components (master );
154198
155199 return ret ;
156200}
@@ -180,18 +224,89 @@ static void take_down_master(struct master *master)
180224 master_remove_components (master );
181225}
182226
183- int component_master_add (struct device * dev ,
184- const struct component_master_ops * ops )
227+ static size_t component_match_size (size_t num )
228+ {
229+ return offsetof(struct component_match , compare [num ]);
230+ }
231+
232+ static struct component_match * component_match_realloc (struct device * dev ,
233+ struct component_match * match , size_t num )
234+ {
235+ struct component_match * new ;
236+
237+ if (match && match -> alloc == num )
238+ return match ;
239+
240+ new = devm_kmalloc (dev , component_match_size (num ), GFP_KERNEL );
241+ if (!new )
242+ return ERR_PTR (- ENOMEM );
243+
244+ if (match ) {
245+ memcpy (new , match , component_match_size (min (match -> num , num )));
246+ devm_kfree (dev , match );
247+ } else {
248+ new -> num = 0 ;
249+ }
250+
251+ new -> alloc = num ;
252+
253+ return new ;
254+ }
255+
256+ /*
257+ * Add a component to be matched.
258+ *
259+ * The match array is first created or extended if necessary.
260+ */
261+ void component_match_add (struct device * dev , struct component_match * * matchptr ,
262+ int (* compare )(struct device * , void * ), void * compare_data )
263+ {
264+ struct component_match * match = * matchptr ;
265+
266+ if (IS_ERR (match ))
267+ return ;
268+
269+ if (!match || match -> num == match -> alloc ) {
270+ size_t new_size = match ? match -> alloc + 16 : 15 ;
271+
272+ match = component_match_realloc (dev , match , new_size );
273+
274+ * matchptr = match ;
275+
276+ if (IS_ERR (match ))
277+ return ;
278+ }
279+
280+ match -> compare [match -> num ].fn = compare ;
281+ match -> compare [match -> num ].data = compare_data ;
282+ match -> num ++ ;
283+ }
284+ EXPORT_SYMBOL (component_match_add );
285+
286+ int component_master_add_with_match (struct device * dev ,
287+ const struct component_master_ops * ops ,
288+ struct component_match * match )
185289{
186290 struct master * master ;
187291 int ret ;
188292
293+ if (ops -> add_components && match )
294+ return - EINVAL ;
295+
296+ if (match ) {
297+ /* Reallocate the match array for its true size */
298+ match = component_match_realloc (dev , match , match -> num );
299+ if (IS_ERR (match ))
300+ return PTR_ERR (match );
301+ }
302+
189303 master = kzalloc (sizeof (* master ), GFP_KERNEL );
190304 if (!master )
191305 return - ENOMEM ;
192306
193307 master -> dev = dev ;
194308 master -> ops = ops ;
309+ master -> match = match ;
195310 INIT_LIST_HEAD (& master -> components );
196311
197312 /* Add to the list of available masters. */
@@ -209,6 +324,13 @@ int component_master_add(struct device *dev,
209324
210325 return ret < 0 ? ret : 0 ;
211326}
327+ EXPORT_SYMBOL_GPL (component_master_add_with_match );
328+
329+ int component_master_add (struct device * dev ,
330+ const struct component_master_ops * ops )
331+ {
332+ return component_master_add_with_match (dev , ops , NULL );
333+ }
212334EXPORT_SYMBOL_GPL (component_master_add );
213335
214336void component_master_del (struct device * dev ,
0 commit comments