xref: /linux/drivers/base/attribute_container.c (revision eb3dad518e4da48ab6c6df16aa8895b8b0bd6ecf)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * attribute_container.c - implementation of a simple container for classes
4  *
5  * Copyright (c) 2005 - James Bottomley <James.Bottomley@steeleye.com>
6  *
7  * The basic idea here is to enable a device to be attached to an
8  * aritrary numer of classes without having to allocate storage for them.
9  * Instead, the contained classes select the devices they need to attach
10  * to via a matching function.
11  */
12 
13 #include <linux/attribute_container.h>
14 #include <linux/device.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 
21 #include "base.h"
22 
23 /* This is a private structure used to tie the classdev and the
24  * container .. it should never be visible outside this file */
25 struct internal_container {
26 	struct klist_node node;
27 	struct attribute_container *cont;
28 	struct device classdev;
29 };
30 
31 static void internal_container_klist_get(struct klist_node *n)
32 {
33 	struct internal_container *ic =
34 		container_of(n, struct internal_container, node);
35 	get_device(&ic->classdev);
36 }
37 
38 static void internal_container_klist_put(struct klist_node *n)
39 {
40 	struct internal_container *ic =
41 		container_of(n, struct internal_container, node);
42 	put_device(&ic->classdev);
43 }
44 
45 
46 /**
47  * attribute_container_classdev_to_container - given a classdev, return the container
48  *
49  * @classdev: the class device created by attribute_container_add_device.
50  *
51  * Returns the container associated with this classdev.
52  */
53 struct attribute_container *
54 attribute_container_classdev_to_container(struct device *classdev)
55 {
56 	struct internal_container *ic =
57 		container_of(classdev, struct internal_container, classdev);
58 	return ic->cont;
59 }
60 EXPORT_SYMBOL_GPL(attribute_container_classdev_to_container);
61 
62 static LIST_HEAD(attribute_container_list);
63 
64 static DEFINE_MUTEX(attribute_container_mutex);
65 
66 /**
67  * attribute_container_register - register an attribute container
68  *
69  * @cont: The container to register.  This must be allocated by the
70  *        callee and should also be zeroed by it.
71  */
72 void
73 attribute_container_register(struct attribute_container *cont)
74 {
75 	INIT_LIST_HEAD(&cont->node);
76 	klist_init(&cont->containers, internal_container_klist_get,
77 		   internal_container_klist_put);
78 
79 	mutex_lock(&attribute_container_mutex);
80 	list_add_tail(&cont->node, &attribute_container_list);
81 	mutex_unlock(&attribute_container_mutex);
82 }
83 EXPORT_SYMBOL_GPL(attribute_container_register);
84 
85 /**
86  * attribute_container_unregister - remove a container registration
87  *
88  * @cont: previously registered container to remove
89  */
90 int
91 attribute_container_unregister(struct attribute_container *cont)
92 {
93 	int retval = -EBUSY;
94 
95 	mutex_lock(&attribute_container_mutex);
96 	spin_lock(&cont->containers.k_lock);
97 	if (!list_empty(&cont->containers.k_list))
98 		goto out;
99 	retval = 0;
100 	list_del(&cont->node);
101  out:
102 	spin_unlock(&cont->containers.k_lock);
103 	mutex_unlock(&attribute_container_mutex);
104 	return retval;
105 
106 }
107 EXPORT_SYMBOL_GPL(attribute_container_unregister);
108 
109 /* private function used as class release */
110 static void attribute_container_release(struct device *classdev)
111 {
112 	struct internal_container *ic
113 		= container_of(classdev, struct internal_container, classdev);
114 	struct device *dev = classdev->parent;
115 
116 	kfree(ic);
117 	put_device(dev);
118 }
119 
120 /**
121  * attribute_container_add_device - see if any container is interested in dev
122  *
123  * @dev: device to add attributes to
124  * @fn:	 function to trigger addition of class device.
125  *
126  * This function allocates storage for the class device(s) to be
127  * attached to dev (one for each matching attribute_container).  If no
128  * fn is provided, the code will simply register the class device via
129  * device_add.  If a function is provided, it is expected to add
130  * the class device at the appropriate time.  One of the things that
131  * might be necessary is to allocate and initialise the classdev and
132  * then add it a later time.  To do this, call this routine for
133  * allocation and initialisation and then use
134  * attribute_container_device_trigger() to call device_add() on
135  * it.  Note: after this, the class device contains a reference to dev
136  * which is not relinquished until the release of the classdev.
137  */
138 void
139 attribute_container_add_device(struct device *dev,
140 			       int (*fn)(struct attribute_container *,
141 					 struct device *,
142 					 struct device *))
143 {
144 	struct attribute_container *cont;
145 
146 	mutex_lock(&attribute_container_mutex);
147 	list_for_each_entry(cont, &attribute_container_list, node) {
148 		struct internal_container *ic;
149 
150 		if (attribute_container_no_classdevs(cont))
151 			continue;
152 
153 		if (!cont->match(cont, dev))
154 			continue;
155 
156 		ic = kzalloc(sizeof(*ic), GFP_KERNEL);
157 		if (!ic) {
158 			dev_err(dev, "failed to allocate class container\n");
159 			continue;
160 		}
161 
162 		ic->cont = cont;
163 		device_initialize(&ic->classdev);
164 		ic->classdev.parent = get_device(dev);
165 		ic->classdev.class = cont->class;
166 		cont->class->dev_release = attribute_container_release;
167 		dev_set_name(&ic->classdev, "%s", dev_name(dev));
168 		if (fn)
169 			fn(cont, dev, &ic->classdev);
170 		else
171 			attribute_container_add_class_device(&ic->classdev);
172 		klist_add_tail(&ic->node, &cont->containers);
173 	}
174 	mutex_unlock(&attribute_container_mutex);
175 }
176 
177 /* FIXME: can't break out of this unless klist_iter_exit is also
178  * called before doing the break
179  */
180 #define klist_for_each_entry(pos, head, member, iter) \
181 	for (klist_iter_init(head, iter); (pos = ({ \
182 		struct klist_node *n = klist_next(iter); \
183 		n ? container_of(n, typeof(*pos), member) : \
184 			({ klist_iter_exit(iter) ; NULL; }); \
185 	})) != NULL;)
186 
187 
188 /**
189  * attribute_container_remove_device - make device eligible for removal.
190  *
191  * @dev:  The generic device
192  * @fn:	  A function to call to remove the device
193  *
194  * This routine triggers device removal.  If fn is NULL, then it is
195  * simply done via device_unregister (note that if something
196  * still has a reference to the classdev, then the memory occupied
197  * will not be freed until the classdev is released).  If you want a
198  * two phase release: remove from visibility and then delete the
199  * device, then you should use this routine with a fn that calls
200  * device_del() and then use attribute_container_device_trigger()
201  * to do the final put on the classdev.
202  */
203 void
204 attribute_container_remove_device(struct device *dev,
205 				  void (*fn)(struct attribute_container *,
206 					     struct device *,
207 					     struct device *))
208 {
209 	struct attribute_container *cont;
210 
211 	mutex_lock(&attribute_container_mutex);
212 	list_for_each_entry(cont, &attribute_container_list, node) {
213 		struct internal_container *ic;
214 		struct klist_iter iter;
215 
216 		if (attribute_container_no_classdevs(cont))
217 			continue;
218 
219 		if (!cont->match(cont, dev))
220 			continue;
221 
222 		klist_for_each_entry(ic, &cont->containers, node, &iter) {
223 			if (dev != ic->classdev.parent)
224 				continue;
225 			klist_del(&ic->node);
226 			if (fn)
227 				fn(cont, dev, &ic->classdev);
228 			else {
229 				attribute_container_remove_attrs(&ic->classdev);
230 				device_unregister(&ic->classdev);
231 			}
232 		}
233 	}
234 	mutex_unlock(&attribute_container_mutex);
235 }
236 
237 static int
238 do_attribute_container_device_trigger_safe(struct device *dev,
239 					   struct attribute_container *cont,
240 					   int (*fn)(struct attribute_container *,
241 						     struct device *, struct device *),
242 					   int (*undo)(struct attribute_container *,
243 						       struct device *, struct device *))
244 {
245 	int ret;
246 	struct internal_container *ic, *failed;
247 	struct klist_iter iter;
248 
249 	if (attribute_container_no_classdevs(cont))
250 		return fn(cont, dev, NULL);
251 
252 	klist_for_each_entry(ic, &cont->containers, node, &iter) {
253 		if (dev == ic->classdev.parent) {
254 			ret = fn(cont, dev, &ic->classdev);
255 			if (ret) {
256 				failed = ic;
257 				klist_iter_exit(&iter);
258 				goto fail;
259 			}
260 		}
261 	}
262 	return 0;
263 
264 fail:
265 	if (!undo)
266 		return ret;
267 
268 	/* Attempt to undo the work partially done. */
269 	klist_for_each_entry(ic, &cont->containers, node, &iter) {
270 		if (ic == failed) {
271 			klist_iter_exit(&iter);
272 			break;
273 		}
274 		if (dev == ic->classdev.parent)
275 			undo(cont, dev, &ic->classdev);
276 	}
277 	return ret;
278 }
279 
280 /**
281  * attribute_container_device_trigger_safe - execute a trigger for each
282  * matching classdev or fail all of them.
283  *
284  * @dev:  The generic device to run the trigger for
285  * @fn:   the function to execute for each classdev.
286  * @undo: A function to undo the work previously done in case of error
287  *
288  * This function is a safe version of
289  * attribute_container_device_trigger. It stops on the first error and
290  * undo the partial work that has been done, on previous classdev.  It
291  * is guaranteed that either they all succeeded, or none of them
292  * succeeded.
293  */
294 int
295 attribute_container_device_trigger_safe(struct device *dev,
296 					int (*fn)(struct attribute_container *,
297 						  struct device *,
298 						  struct device *),
299 					int (*undo)(struct attribute_container *,
300 						    struct device *,
301 						    struct device *))
302 {
303 	struct attribute_container *cont, *failed = NULL;
304 	int ret = 0;
305 
306 	mutex_lock(&attribute_container_mutex);
307 
308 	list_for_each_entry(cont, &attribute_container_list, node) {
309 
310 		if (!cont->match(cont, dev))
311 			continue;
312 
313 		ret = do_attribute_container_device_trigger_safe(dev, cont,
314 								 fn, undo);
315 		if (ret) {
316 			failed = cont;
317 			break;
318 		}
319 	}
320 
321 	if (ret && !WARN_ON(!undo)) {
322 		list_for_each_entry(cont, &attribute_container_list, node) {
323 
324 			if (failed == cont)
325 				break;
326 
327 			if (!cont->match(cont, dev))
328 				continue;
329 
330 			do_attribute_container_device_trigger_safe(dev, cont,
331 								   undo, NULL);
332 		}
333 	}
334 
335 	mutex_unlock(&attribute_container_mutex);
336 	return ret;
337 
338 }
339 
340 /**
341  * attribute_container_device_trigger - execute a trigger for each matching classdev
342  *
343  * @dev:  The generic device to run the trigger for
344  * @fn:   the function to execute for each classdev.
345  *
346  * This function is for executing a trigger when you need to know both
347  * the container and the classdev.
348  */
349 void
350 attribute_container_device_trigger(struct device *dev,
351 				   int (*fn)(struct attribute_container *,
352 					     struct device *,
353 					     struct device *))
354 {
355 	struct attribute_container *cont;
356 
357 	mutex_lock(&attribute_container_mutex);
358 	list_for_each_entry(cont, &attribute_container_list, node) {
359 		struct internal_container *ic;
360 		struct klist_iter iter;
361 
362 		if (!cont->match(cont, dev))
363 			continue;
364 
365 		if (attribute_container_no_classdevs(cont)) {
366 			fn(cont, dev, NULL);
367 			continue;
368 		}
369 
370 		klist_for_each_entry(ic, &cont->containers, node, &iter) {
371 			if (dev == ic->classdev.parent)
372 				fn(cont, dev, &ic->classdev);
373 		}
374 	}
375 	mutex_unlock(&attribute_container_mutex);
376 }
377 
378 /**
379  * attribute_container_add_attrs - add attributes
380  *
381  * @classdev: The class device
382  *
383  * This simply creates all the class device sysfs files from the
384  * attributes listed in the container
385  */
386 int
387 attribute_container_add_attrs(struct device *classdev)
388 {
389 	struct attribute_container *cont =
390 		attribute_container_classdev_to_container(classdev);
391 	struct device_attribute **attrs = cont->attrs;
392 	int i, error;
393 
394 	BUG_ON(attrs && cont->grp);
395 
396 	if (!attrs && !cont->grp)
397 		return 0;
398 
399 	if (cont->grp)
400 		return sysfs_create_group(&classdev->kobj, cont->grp);
401 
402 	for (i = 0; attrs[i]; i++) {
403 		sysfs_attr_init(&attrs[i]->attr);
404 		error = device_create_file(classdev, attrs[i]);
405 		if (error)
406 			return error;
407 	}
408 
409 	return 0;
410 }
411 
412 /**
413  * attribute_container_add_class_device - same function as device_add
414  *
415  * @classdev:	the class device to add
416  *
417  * This performs essentially the same function as device_add except for
418  * attribute containers, namely add the classdev to the system and then
419  * create the attribute files
420  */
421 int
422 attribute_container_add_class_device(struct device *classdev)
423 {
424 	int error = device_add(classdev);
425 
426 	if (error)
427 		return error;
428 	return attribute_container_add_attrs(classdev);
429 }
430 
431 /**
432  * attribute_container_remove_attrs - remove any attribute files
433  *
434  * @classdev: The class device to remove the files from
435  *
436  */
437 void
438 attribute_container_remove_attrs(struct device *classdev)
439 {
440 	struct attribute_container *cont =
441 		attribute_container_classdev_to_container(classdev);
442 	struct device_attribute **attrs = cont->attrs;
443 	int i;
444 
445 	if (!attrs && !cont->grp)
446 		return;
447 
448 	if (cont->grp) {
449 		sysfs_remove_group(&classdev->kobj, cont->grp);
450 		return ;
451 	}
452 
453 	for (i = 0; attrs[i]; i++)
454 		device_remove_file(classdev, attrs[i]);
455 }
456 
457 /**
458  * attribute_container_class_device_del - equivalent of class_device_del
459  *
460  * @classdev: the class device
461  *
462  * This function simply removes all the attribute files and then calls
463  * device_del.
464  */
465 void
466 attribute_container_class_device_del(struct device *classdev)
467 {
468 	attribute_container_remove_attrs(classdev);
469 	device_del(classdev);
470 }
471 
472 /**
473  * attribute_container_find_class_device - find the corresponding class_device
474  *
475  * @cont:	the container
476  * @dev:	the generic device
477  *
478  * Looks up the device in the container's list of class devices and returns
479  * the corresponding class_device.
480  */
481 struct device *
482 attribute_container_find_class_device(struct attribute_container *cont,
483 				      struct device *dev)
484 {
485 	struct device *cdev = NULL;
486 	struct internal_container *ic;
487 	struct klist_iter iter;
488 
489 	klist_for_each_entry(ic, &cont->containers, node, &iter) {
490 		if (ic->classdev.parent == dev) {
491 			cdev = &ic->classdev;
492 			/* FIXME: must exit iterator then break */
493 			klist_iter_exit(&iter);
494 			break;
495 		}
496 	}
497 
498 	return cdev;
499 }
500 EXPORT_SYMBOL_GPL(attribute_container_find_class_device);
501