2020
2121struct component ;
2222
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+
2331struct component_match {
2432 size_t alloc ;
2533 size_t num ;
26- struct {
27- void * data ;
28- int (* fn )(struct device * , void * );
29- struct component * component ;
30- bool duplicate ;
31- } compare [0 ];
34+ struct component_match_array * compare ;
3235};
3336
3437struct master {
@@ -92,15 +95,15 @@ static int find_components(struct master *master)
9295 * any components which are found to this master.
9396 */
9497 for (i = 0 ; i < match -> num ; i ++ ) {
98+ struct component_match_array * mc = & match -> compare [i ];
9599 struct component * c ;
96100
97101 dev_dbg (master -> dev , "Looking for component %zu\n" , i );
98102
99103 if (match -> compare [i ].component )
100104 continue ;
101105
102- c = find_component (master , match -> compare [i ].fn ,
103- match -> compare [i ].data );
106+ c = find_component (master , mc -> compare , mc -> data );
104107 if (!c ) {
105108 ret = - ENXIO ;
106109 break ;
@@ -192,65 +195,93 @@ static void take_down_master(struct master *master)
192195 }
193196}
194197
195- static size_t component_match_size (size_t num )
198+ static void component_match_release (struct device * master ,
199+ struct component_match * match )
200+ {
201+ unsigned int i ;
202+
203+ for (i = 0 ; i < match -> num ; i ++ ) {
204+ struct component_match_array * mc = & match -> compare [i ];
205+
206+ if (mc -> release )
207+ mc -> release (master , mc -> data );
208+ }
209+ }
210+
211+ static void devm_component_match_release (struct device * dev , void * res )
196212{
197- return offsetof( struct component_match , compare [ num ] );
213+ component_match_release ( dev , res );
198214}
199215
200- static struct component_match * component_match_realloc (struct device * dev ,
216+ static int component_match_realloc (struct device * dev ,
201217 struct component_match * match , size_t num )
202218{
203- struct component_match * new ;
219+ struct component_match_array * new ;
204220
205- if (match && match -> alloc == num )
206- return match ;
221+ if (match -> alloc == num )
222+ return 0 ;
207223
208- new = devm_kmalloc (dev , component_match_size ( num ), GFP_KERNEL );
224+ new = devm_kmalloc_array (dev , num , sizeof ( * new ), GFP_KERNEL );
209225 if (!new )
210- return ERR_PTR ( - ENOMEM ) ;
226+ return - ENOMEM ;
211227
212- if (match ) {
213- memcpy (new , match , component_match_size (min (match -> num , num )));
214- devm_kfree (dev , match );
215- } else {
216- new -> num = 0 ;
228+ if (match -> compare ) {
229+ memcpy (new , match -> compare , sizeof (* new ) *
230+ min (match -> num , num ));
231+ devm_kfree (dev , match -> compare );
217232 }
233+ match -> compare = new ;
234+ match -> alloc = num ;
218235
219- new -> alloc = num ;
220-
221- return new ;
236+ return 0 ;
222237}
223238
224239/*
225- * Add a component to be matched.
240+ * Add a component to be matched, with a release function .
226241 *
227242 * The match array is first created or extended if necessary.
228243 */
229- void component_match_add (struct device * dev , struct component_match * * matchptr ,
244+ void component_match_add_release (struct device * master ,
245+ struct component_match * * matchptr ,
246+ void (* release )(struct device * , void * ),
230247 int (* compare )(struct device * , void * ), void * compare_data )
231248{
232249 struct component_match * match = * matchptr ;
233250
234251 if (IS_ERR (match ))
235252 return ;
236253
237- if (!match || match -> num == match -> alloc ) {
238- size_t new_size = match ? match -> alloc + 16 : 15 ;
254+ if (!match ) {
255+ match = devres_alloc (devm_component_match_release ,
256+ sizeof (* match ), GFP_KERNEL );
257+ if (!match ) {
258+ * matchptr = ERR_PTR (- ENOMEM );
259+ return ;
260+ }
239261
240- match = component_match_realloc ( dev , match , new_size );
262+ devres_add ( master , match );
241263
242264 * matchptr = match ;
265+ }
266+
267+ if (match -> num == match -> alloc ) {
268+ size_t new_size = match ? match -> alloc + 16 : 15 ;
269+ int ret ;
243270
244- if (IS_ERR (match ))
271+ ret = component_match_realloc (master , match , new_size );
272+ if (ret ) {
273+ * matchptr = ERR_PTR (ret );
245274 return ;
275+ }
246276 }
247277
248- match -> compare [match -> num ].fn = compare ;
278+ match -> compare [match -> num ].compare = compare ;
279+ match -> compare [match -> num ].release = release ;
249280 match -> compare [match -> num ].data = compare_data ;
250281 match -> compare [match -> num ].component = NULL ;
251282 match -> num ++ ;
252283}
253- EXPORT_SYMBOL (component_match_add );
284+ EXPORT_SYMBOL (component_match_add_release );
254285
255286int component_master_add_with_match (struct device * dev ,
256287 const struct component_master_ops * ops ,
@@ -260,9 +291,9 @@ int component_master_add_with_match(struct device *dev,
260291 int ret ;
261292
262293 /* Reallocate the match array for its true size */
263- match = component_match_realloc (dev , match , match -> num );
264- if (IS_ERR ( match ) )
265- return PTR_ERR ( match ) ;
294+ ret = component_match_realloc (dev , match , match -> num );
295+ if (ret )
296+ return ret ;
266297
267298 master = kzalloc (sizeof (* master ), GFP_KERNEL );
268299 if (!master )
0 commit comments