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