blob: 218b9deb4f0b3251a0361ecfeeaeb730f4e5ca11 [file] [log] [blame]
Greg Kroah-Hartman989d42e2017-11-07 17:30:07 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * attribute_container.c - implementation of a simple container for classes
4 *
5 * Copyright (c) 2005 - James Bottomley <James.Bottomley@steeleye.com>
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/device.h>
17#include <linux/kernel.h>
18#include <linux/slab.h>
19#include <linux/list.h>
20#include <linux/module.h>
Michael S. Tsirkinf8916c12007-06-10 22:39:12 +030021#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010023#include "base.h"
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/* This is a private structure used to tie the classdev and the
26 * container .. it should never be visible outside this file */
27struct internal_container {
James Bottomley53c165e2005-08-22 10:06:19 -050028 struct klist_node node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 struct attribute_container *cont;
Tony Jonesee959b02008-02-22 00:13:36 +010030 struct device classdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031};
32
Linus Torvaldscaf39e82005-09-07 18:44:33 -070033static void internal_container_klist_get(struct klist_node *n)
34{
35 struct internal_container *ic =
36 container_of(n, struct internal_container, node);
Tony Jonesee959b02008-02-22 00:13:36 +010037 get_device(&ic->classdev);
Linus Torvaldscaf39e82005-09-07 18:44:33 -070038}
39
40static void internal_container_klist_put(struct klist_node *n)
41{
42 struct internal_container *ic =
43 container_of(n, struct internal_container, node);
Tony Jonesee959b02008-02-22 00:13:36 +010044 put_device(&ic->classdev);
Linus Torvaldscaf39e82005-09-07 18:44:33 -070045}
46
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/**
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 */
55struct attribute_container *
Tony Jonesee959b02008-02-22 00:13:36 +010056attribute_container_classdev_to_container(struct device *classdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 struct internal_container *ic =
59 container_of(classdev, struct internal_container, classdev);
60 return ic->cont;
61}
62EXPORT_SYMBOL_GPL(attribute_container_classdev_to_container);
63
Denis Chengdb1118a2007-12-06 02:24:40 +080064static LIST_HEAD(attribute_container_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -070066static DEFINE_MUTEX(attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
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 */
74int
75attribute_container_register(struct attribute_container *cont)
76{
77 INIT_LIST_HEAD(&cont->node);
Tina Johnson287f9bd2014-08-12 02:54:09 +053078 klist_init(&cont->containers, internal_container_klist_get,
Linus Torvaldscaf39e82005-09-07 18:44:33 -070079 internal_container_klist_put);
Tina Johnson24a7d362014-08-12 02:54:08 +053080
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -070081 mutex_lock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 list_add_tail(&cont->node, &attribute_container_list);
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -070083 mutex_unlock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 return 0;
86}
87EXPORT_SYMBOL_GPL(attribute_container_register);
88
89/**
90 * attribute_container_unregister - remove a container registration
91 *
92 * @cont: previously registered container to remove
93 */
94int
95attribute_container_unregister(struct attribute_container *cont)
96{
97 int retval = -EBUSY;
Cosmin Dragomir481026d2015-03-08 12:30:14 +020098
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -070099 mutex_lock(&attribute_container_mutex);
James Bottomley53c165e2005-08-22 10:06:19 -0500100 spin_lock(&cont->containers.k_lock);
101 if (!list_empty(&cont->containers.k_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 goto out;
103 retval = 0;
104 list_del(&cont->node);
105 out:
James Bottomley53c165e2005-08-22 10:06:19 -0500106 spin_unlock(&cont->containers.k_lock);
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700107 mutex_unlock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return retval;
Tina Johnson24a7d362014-08-12 02:54:08 +0530109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111EXPORT_SYMBOL_GPL(attribute_container_unregister);
112
113/* private function used as class release */
Tony Jonesee959b02008-02-22 00:13:36 +0100114static void attribute_container_release(struct device *classdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Tina Johnson24a7d362014-08-12 02:54:08 +0530116 struct internal_container *ic
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 = container_of(classdev, struct internal_container, classdev);
Tony Jonesee959b02008-02-22 00:13:36 +0100118 struct device *dev = classdev->parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
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
Tony Jonesee959b02008-02-22 00:13:36 +0100133 * device_add. If a function is provided, it is expected to add
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * 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
Tony Jonesee959b02008-02-22 00:13:36 +0100138 * attribute_container_device_trigger() to call device_add() on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 * it. Note: after this, the class device contains a reference to dev
140 * which is not relinquished until the release of the classdev.
141 */
142void
143attribute_container_add_device(struct device *dev,
144 int (*fn)(struct attribute_container *,
145 struct device *,
Tony Jonesee959b02008-02-22 00:13:36 +0100146 struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 struct attribute_container *cont;
149
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700150 mutex_lock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 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;
Jiri Slaby4aed0642005-09-13 01:25:01 -0700159
160 ic = kzalloc(sizeof(*ic), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (!ic) {
Joe Perchesa369a7e2012-10-28 01:05:41 -0700162 dev_err(dev, "failed to allocate class container\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 continue;
164 }
Jiri Slaby4aed0642005-09-13 01:25:01 -0700165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 ic->cont = cont;
Tony Jonesee959b02008-02-22 00:13:36 +0100167 device_initialize(&ic->classdev);
168 ic->classdev.parent = get_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 ic->classdev.class = cont->class;
Tony Jonesee959b02008-02-22 00:13:36 +0100170 cont->class->dev_release = attribute_container_release;
Kees Cook02aa2a32013-07-03 15:04:56 -0700171 dev_set_name(&ic->classdev, "%s", dev_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (fn)
173 fn(cont, dev, &ic->classdev);
174 else
175 attribute_container_add_class_device(&ic->classdev);
James Bottomley53c165e2005-08-22 10:06:19 -0500176 klist_add_tail(&ic->node, &cont->containers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700178 mutex_unlock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
James Bottomley53c165e2005-08-22 10:06:19 -0500181/* 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); \
Linus Torvaldscaf39e82005-09-07 18:44:33 -0700187 n ? container_of(n, typeof(*pos), member) : \
188 ({ klist_iter_exit(iter) ; NULL; }); \
Tina Johnson287f9bd2014-08-12 02:54:09 +0530189 })) != NULL;)
Tina Johnson24a7d362014-08-12 02:54:08 +0530190
James Bottomley53c165e2005-08-22 10:06:19 -0500191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/**
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
Tony Jonesee959b02008-02-22 00:13:36 +0100199 * simply done via device_unregister (note that if something
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * 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
Tony Jonesee959b02008-02-22 00:13:36 +0100204 * device_del() and then use attribute_container_device_trigger()
205 * to do the final put on the classdev.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 */
207void
208attribute_container_remove_device(struct device *dev,
209 void (*fn)(struct attribute_container *,
210 struct device *,
Tony Jonesee959b02008-02-22 00:13:36 +0100211 struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 struct attribute_container *cont;
214
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700215 mutex_lock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 list_for_each_entry(cont, &attribute_container_list, node) {
James Bottomley53c165e2005-08-22 10:06:19 -0500217 struct internal_container *ic;
218 struct klist_iter iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 if (attribute_container_no_classdevs(cont))
221 continue;
222
223 if (!cont->match(cont, dev))
224 continue;
James Bottomley53c165e2005-08-22 10:06:19 -0500225
226 klist_for_each_entry(ic, &cont->containers, node, &iter) {
Tony Jonesee959b02008-02-22 00:13:36 +0100227 if (dev != ic->classdev.parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 continue;
Linus Torvaldscaf39e82005-09-07 18:44:33 -0700229 klist_del(&ic->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (fn)
231 fn(cont, dev, &ic->classdev);
232 else {
233 attribute_container_remove_attrs(&ic->classdev);
Tony Jonesee959b02008-02-22 00:13:36 +0100234 device_unregister(&ic->classdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236 }
237 }
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700238 mutex_unlock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
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 *
Xiubo Li03aca7b2016-06-01 09:18:40 +0800247 * This function is for executing a trigger when you need to know both
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 * the container and the classdev. If you only care about the
249 * container, then use attribute_container_trigger() instead.
250 */
251void
Tina Johnson24a7d362014-08-12 02:54:08 +0530252attribute_container_device_trigger(struct device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 int (*fn)(struct attribute_container *,
254 struct device *,
Tony Jonesee959b02008-02-22 00:13:36 +0100255 struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 struct attribute_container *cont;
258
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700259 mutex_lock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 list_for_each_entry(cont, &attribute_container_list, node) {
James Bottomley53c165e2005-08-22 10:06:19 -0500261 struct internal_container *ic;
262 struct klist_iter iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 if (!cont->match(cont, dev))
265 continue;
266
James Bottomleyebd8bb72005-08-15 16:13:19 -0500267 if (attribute_container_no_classdevs(cont)) {
268 fn(cont, dev, NULL);
269 continue;
270 }
271
James Bottomley53c165e2005-08-22 10:06:19 -0500272 klist_for_each_entry(ic, &cont->containers, node, &iter) {
Tony Jonesee959b02008-02-22 00:13:36 +0100273 if (dev == ic->classdev.parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 fn(cont, dev, &ic->classdev);
275 }
276 }
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700277 mutex_unlock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
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 */
292void
293attribute_container_trigger(struct device *dev,
294 int (*fn)(struct attribute_container *,
295 struct device *))
296{
297 struct attribute_container *cont;
298
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700299 mutex_lock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 list_for_each_entry(cont, &attribute_container_list, node) {
301 if (cont->match(cont, dev))
302 fn(cont, dev);
303 }
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700304 mutex_unlock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
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 */
315int
Tony Jonesee959b02008-02-22 00:13:36 +0100316attribute_container_add_attrs(struct device *classdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 struct attribute_container *cont =
319 attribute_container_classdev_to_container(classdev);
Tony Jonesee959b02008-02-22 00:13:36 +0100320 struct device_attribute **attrs = cont->attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 int i, error;
322
James Bottomleyfd110972008-01-02 18:48:47 -0600323 BUG_ON(attrs && cont->grp);
324
325 if (!attrs && !cont->grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return 0;
327
James Bottomleyfd110972008-01-02 18:48:47 -0600328 if (cont->grp)
329 return sysfs_create_group(&classdev->kobj, cont->grp);
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 for (i = 0; attrs[i]; i++) {
James Bottomleyebd09ec2010-03-20 12:44:12 -0500332 sysfs_attr_init(&attrs[i]->attr);
Tony Jonesee959b02008-02-22 00:13:36 +0100333 error = device_create_file(classdev, attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (error)
335 return error;
336 }
337
338 return 0;
339}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341/**
Tony Jonesee959b02008-02-22 00:13:36 +0100342 * attribute_container_add_class_device - same function as device_add
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 *
344 * @classdev: the class device to add
345 *
Tony Jonesee959b02008-02-22 00:13:36 +0100346 * This performs essentially the same function as device_add except for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 * attribute containers, namely add the classdev to the system and then
348 * create the attribute files
349 */
350int
Tony Jonesee959b02008-02-22 00:13:36 +0100351attribute_container_add_class_device(struct device *classdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Tony Jonesee959b02008-02-22 00:13:36 +0100353 int error = device_add(classdev);
Cosmin Dragomir481026d2015-03-08 12:30:14 +0200354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (error)
356 return error;
357 return attribute_container_add_attrs(classdev);
358}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
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 */
366int
367attribute_container_add_class_device_adapter(struct attribute_container *cont,
368 struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100369 struct device *classdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 return attribute_container_add_class_device(classdev);
372}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374/**
375 * attribute_container_remove_attrs - remove any attribute files
376 *
377 * @classdev: The class device to remove the files from
378 *
379 */
380void
Tony Jonesee959b02008-02-22 00:13:36 +0100381attribute_container_remove_attrs(struct device *classdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 struct attribute_container *cont =
384 attribute_container_classdev_to_container(classdev);
Tony Jonesee959b02008-02-22 00:13:36 +0100385 struct device_attribute **attrs = cont->attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 int i;
387
James Bottomleyfd110972008-01-02 18:48:47 -0600388 if (!attrs && !cont->grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return;
390
James Bottomleyfd110972008-01-02 18:48:47 -0600391 if (cont->grp) {
392 sysfs_remove_group(&classdev->kobj, cont->grp);
393 return ;
394 }
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 for (i = 0; attrs[i]; i++)
Tony Jonesee959b02008-02-22 00:13:36 +0100397 device_remove_file(classdev, attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
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
Tony Jonesee959b02008-02-22 00:13:36 +0100406 * device_del.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 */
408void
Tony Jonesee959b02008-02-22 00:13:36 +0100409attribute_container_class_device_del(struct device *classdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 attribute_container_remove_attrs(classdev);
Tony Jonesee959b02008-02-22 00:13:36 +0100412 device_del(classdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
James Bottomleyd0a7e572005-08-14 17:09:01 -0500415/**
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 */
Tony Jonesee959b02008-02-22 00:13:36 +0100424struct device *
James Bottomleyd0a7e572005-08-14 17:09:01 -0500425attribute_container_find_class_device(struct attribute_container *cont,
426 struct device *dev)
427{
Tony Jonesee959b02008-02-22 00:13:36 +0100428 struct device *cdev = NULL;
James Bottomleyd0a7e572005-08-14 17:09:01 -0500429 struct internal_container *ic;
James Bottomley53c165e2005-08-22 10:06:19 -0500430 struct klist_iter iter;
James Bottomleyd0a7e572005-08-14 17:09:01 -0500431
James Bottomley53c165e2005-08-22 10:06:19 -0500432 klist_for_each_entry(ic, &cont->containers, node, &iter) {
Tony Jonesee959b02008-02-22 00:13:36 +0100433 if (ic->classdev.parent == dev) {
James Bottomleyd0a7e572005-08-14 17:09:01 -0500434 cdev = &ic->classdev;
James Bottomley53c165e2005-08-22 10:06:19 -0500435 /* FIXME: must exit iterator then break */
436 klist_iter_exit(&iter);
James Bottomleyd0a7e572005-08-14 17:09:01 -0500437 break;
438 }
439 }
James Bottomleyd0a7e572005-08-14 17:09:01 -0500440
441 return cdev;
442}
443EXPORT_SYMBOL_GPL(attribute_container_find_class_device);