blob: 20736aaa0e69ccb69660c81b7b9ae9bee0332641 [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/device.h>
15#include <linux/kernel.h>
16#include <linux/slab.h>
17#include <linux/list.h>
18#include <linux/module.h>
Michael S. Tsirkinf8916c12007-06-10 22:39:12 +030019#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010021#include "base.h"
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023/* This is a private structure used to tie the classdev and the
24 * container .. it should never be visible outside this file */
25struct internal_container {
James Bottomley53c165e2005-08-22 10:06:19 -050026 struct klist_node node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 struct attribute_container *cont;
Tony Jonesee959b02008-02-22 00:13:36 +010028 struct device classdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029};
30
Linus Torvaldscaf39e82005-09-07 18:44:33 -070031static void internal_container_klist_get(struct klist_node *n)
32{
33 struct internal_container *ic =
34 container_of(n, struct internal_container, node);
Tony Jonesee959b02008-02-22 00:13:36 +010035 get_device(&ic->classdev);
Linus Torvaldscaf39e82005-09-07 18:44:33 -070036}
37
38static void internal_container_klist_put(struct klist_node *n)
39{
40 struct internal_container *ic =
41 container_of(n, struct internal_container, node);
Tony Jonesee959b02008-02-22 00:13:36 +010042 put_device(&ic->classdev);
Linus Torvaldscaf39e82005-09-07 18:44:33 -070043}
44
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/**
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 */
53struct attribute_container *
Tony Jonesee959b02008-02-22 00:13:36 +010054attribute_container_classdev_to_container(struct device *classdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 struct internal_container *ic =
57 container_of(classdev, struct internal_container, classdev);
58 return ic->cont;
59}
60EXPORT_SYMBOL_GPL(attribute_container_classdev_to_container);
61
Denis Chengdb1118a2007-12-06 02:24:40 +080062static LIST_HEAD(attribute_container_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -070064static DEFINE_MUTEX(attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
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 */
72int
73attribute_container_register(struct attribute_container *cont)
74{
75 INIT_LIST_HEAD(&cont->node);
Tina Johnson287f9bd2014-08-12 02:54:09 +053076 klist_init(&cont->containers, internal_container_klist_get,
Linus Torvaldscaf39e82005-09-07 18:44:33 -070077 internal_container_klist_put);
Tina Johnson24a7d362014-08-12 02:54:08 +053078
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -070079 mutex_lock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 list_add_tail(&cont->node, &attribute_container_list);
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -070081 mutex_unlock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 return 0;
84}
85EXPORT_SYMBOL_GPL(attribute_container_register);
86
87/**
88 * attribute_container_unregister - remove a container registration
89 *
90 * @cont: previously registered container to remove
91 */
92int
93attribute_container_unregister(struct attribute_container *cont)
94{
95 int retval = -EBUSY;
Cosmin Dragomir481026d2015-03-08 12:30:14 +020096
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -070097 mutex_lock(&attribute_container_mutex);
James Bottomley53c165e2005-08-22 10:06:19 -050098 spin_lock(&cont->containers.k_lock);
99 if (!list_empty(&cont->containers.k_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 goto out;
101 retval = 0;
102 list_del(&cont->node);
103 out:
James Bottomley53c165e2005-08-22 10:06:19 -0500104 spin_unlock(&cont->containers.k_lock);
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700105 mutex_unlock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return retval;
Tina Johnson24a7d362014-08-12 02:54:08 +0530107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109EXPORT_SYMBOL_GPL(attribute_container_unregister);
110
111/* private function used as class release */
Tony Jonesee959b02008-02-22 00:13:36 +0100112static void attribute_container_release(struct device *classdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Tina Johnson24a7d362014-08-12 02:54:08 +0530114 struct internal_container *ic
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 = container_of(classdev, struct internal_container, classdev);
Tony Jonesee959b02008-02-22 00:13:36 +0100116 struct device *dev = classdev->parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 kfree(ic);
119 put_device(dev);
120}
121
122/**
123 * attribute_container_add_device - see if any container is interested in dev
124 *
125 * @dev: device to add attributes to
126 * @fn: function to trigger addition of class device.
127 *
128 * This function allocates storage for the class device(s) to be
129 * attached to dev (one for each matching attribute_container). If no
130 * fn is provided, the code will simply register the class device via
Tony Jonesee959b02008-02-22 00:13:36 +0100131 * device_add. If a function is provided, it is expected to add
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * the class device at the appropriate time. One of the things that
133 * might be necessary is to allocate and initialise the classdev and
134 * then add it a later time. To do this, call this routine for
135 * allocation and initialisation and then use
Tony Jonesee959b02008-02-22 00:13:36 +0100136 * attribute_container_device_trigger() to call device_add() on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 * it. Note: after this, the class device contains a reference to dev
138 * which is not relinquished until the release of the classdev.
139 */
140void
141attribute_container_add_device(struct device *dev,
142 int (*fn)(struct attribute_container *,
143 struct device *,
Tony Jonesee959b02008-02-22 00:13:36 +0100144 struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 struct attribute_container *cont;
147
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700148 mutex_lock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 list_for_each_entry(cont, &attribute_container_list, node) {
150 struct internal_container *ic;
151
152 if (attribute_container_no_classdevs(cont))
153 continue;
154
155 if (!cont->match(cont, dev))
156 continue;
Jiri Slaby4aed0642005-09-13 01:25:01 -0700157
158 ic = kzalloc(sizeof(*ic), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (!ic) {
Joe Perchesa369a7e2012-10-28 01:05:41 -0700160 dev_err(dev, "failed to allocate class container\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 continue;
162 }
Jiri Slaby4aed0642005-09-13 01:25:01 -0700163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 ic->cont = cont;
Tony Jonesee959b02008-02-22 00:13:36 +0100165 device_initialize(&ic->classdev);
166 ic->classdev.parent = get_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 ic->classdev.class = cont->class;
Tony Jonesee959b02008-02-22 00:13:36 +0100168 cont->class->dev_release = attribute_container_release;
Kees Cook02aa2a32013-07-03 15:04:56 -0700169 dev_set_name(&ic->classdev, "%s", dev_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (fn)
171 fn(cont, dev, &ic->classdev);
172 else
173 attribute_container_add_class_device(&ic->classdev);
James Bottomley53c165e2005-08-22 10:06:19 -0500174 klist_add_tail(&ic->node, &cont->containers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700176 mutex_unlock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
James Bottomley53c165e2005-08-22 10:06:19 -0500179/* FIXME: can't break out of this unless klist_iter_exit is also
180 * called before doing the break
181 */
182#define klist_for_each_entry(pos, head, member, iter) \
183 for (klist_iter_init(head, iter); (pos = ({ \
184 struct klist_node *n = klist_next(iter); \
Linus Torvaldscaf39e82005-09-07 18:44:33 -0700185 n ? container_of(n, typeof(*pos), member) : \
186 ({ klist_iter_exit(iter) ; NULL; }); \
Tina Johnson287f9bd2014-08-12 02:54:09 +0530187 })) != NULL;)
Tina Johnson24a7d362014-08-12 02:54:08 +0530188
James Bottomley53c165e2005-08-22 10:06:19 -0500189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/**
191 * attribute_container_remove_device - make device eligible for removal.
192 *
193 * @dev: The generic device
194 * @fn: A function to call to remove the device
195 *
196 * This routine triggers device removal. If fn is NULL, then it is
Tony Jonesee959b02008-02-22 00:13:36 +0100197 * simply done via device_unregister (note that if something
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * still has a reference to the classdev, then the memory occupied
199 * will not be freed until the classdev is released). If you want a
200 * two phase release: remove from visibility and then delete the
201 * device, then you should use this routine with a fn that calls
Tony Jonesee959b02008-02-22 00:13:36 +0100202 * device_del() and then use attribute_container_device_trigger()
203 * to do the final put on the classdev.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 */
205void
206attribute_container_remove_device(struct device *dev,
207 void (*fn)(struct attribute_container *,
208 struct device *,
Tony Jonesee959b02008-02-22 00:13:36 +0100209 struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 struct attribute_container *cont;
212
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700213 mutex_lock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 list_for_each_entry(cont, &attribute_container_list, node) {
James Bottomley53c165e2005-08-22 10:06:19 -0500215 struct internal_container *ic;
216 struct klist_iter iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 if (attribute_container_no_classdevs(cont))
219 continue;
220
221 if (!cont->match(cont, dev))
222 continue;
James Bottomley53c165e2005-08-22 10:06:19 -0500223
224 klist_for_each_entry(ic, &cont->containers, node, &iter) {
Tony Jonesee959b02008-02-22 00:13:36 +0100225 if (dev != ic->classdev.parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 continue;
Linus Torvaldscaf39e82005-09-07 18:44:33 -0700227 klist_del(&ic->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (fn)
229 fn(cont, dev, &ic->classdev);
230 else {
231 attribute_container_remove_attrs(&ic->classdev);
Tony Jonesee959b02008-02-22 00:13:36 +0100232 device_unregister(&ic->classdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234 }
235 }
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700236 mutex_unlock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239/**
240 * attribute_container_device_trigger - execute a trigger for each matching classdev
241 *
242 * @dev: The generic device to run the trigger for
243 * @fn the function to execute for each classdev.
244 *
Xiubo Li03aca7b2016-06-01 09:18:40 +0800245 * This function is for executing a trigger when you need to know both
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 * the container and the classdev. If you only care about the
247 * container, then use attribute_container_trigger() instead.
248 */
249void
Tina Johnson24a7d362014-08-12 02:54:08 +0530250attribute_container_device_trigger(struct device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 int (*fn)(struct attribute_container *,
252 struct device *,
Tony Jonesee959b02008-02-22 00:13:36 +0100253 struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 struct attribute_container *cont;
256
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700257 mutex_lock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 list_for_each_entry(cont, &attribute_container_list, node) {
James Bottomley53c165e2005-08-22 10:06:19 -0500259 struct internal_container *ic;
260 struct klist_iter iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 if (!cont->match(cont, dev))
263 continue;
264
James Bottomleyebd8bb72005-08-15 16:13:19 -0500265 if (attribute_container_no_classdevs(cont)) {
266 fn(cont, dev, NULL);
267 continue;
268 }
269
James Bottomley53c165e2005-08-22 10:06:19 -0500270 klist_for_each_entry(ic, &cont->containers, node, &iter) {
Tony Jonesee959b02008-02-22 00:13:36 +0100271 if (dev == ic->classdev.parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 fn(cont, dev, &ic->classdev);
273 }
274 }
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700275 mutex_unlock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278/**
279 * attribute_container_trigger - trigger a function for each matching container
280 *
281 * @dev: The generic device to activate the trigger for
282 * @fn: the function to trigger
283 *
284 * This routine triggers a function that only needs to know the
285 * matching containers (not the classdev) associated with a device.
286 * It is more lightweight than attribute_container_device_trigger, so
287 * should be used in preference unless the triggering function
288 * actually needs to know the classdev.
289 */
290void
291attribute_container_trigger(struct device *dev,
292 int (*fn)(struct attribute_container *,
293 struct device *))
294{
295 struct attribute_container *cont;
296
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700297 mutex_lock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 list_for_each_entry(cont, &attribute_container_list, node) {
299 if (cont->match(cont, dev))
300 fn(cont, dev);
301 }
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700302 mutex_unlock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305/**
306 * attribute_container_add_attrs - add attributes
307 *
308 * @classdev: The class device
309 *
310 * This simply creates all the class device sysfs files from the
311 * attributes listed in the container
312 */
313int
Tony Jonesee959b02008-02-22 00:13:36 +0100314attribute_container_add_attrs(struct device *classdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 struct attribute_container *cont =
317 attribute_container_classdev_to_container(classdev);
Tony Jonesee959b02008-02-22 00:13:36 +0100318 struct device_attribute **attrs = cont->attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 int i, error;
320
James Bottomleyfd110972008-01-02 18:48:47 -0600321 BUG_ON(attrs && cont->grp);
322
323 if (!attrs && !cont->grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return 0;
325
James Bottomleyfd110972008-01-02 18:48:47 -0600326 if (cont->grp)
327 return sysfs_create_group(&classdev->kobj, cont->grp);
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 for (i = 0; attrs[i]; i++) {
James Bottomleyebd09ec2010-03-20 12:44:12 -0500330 sysfs_attr_init(&attrs[i]->attr);
Tony Jonesee959b02008-02-22 00:13:36 +0100331 error = device_create_file(classdev, attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (error)
333 return error;
334 }
335
336 return 0;
337}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339/**
Tony Jonesee959b02008-02-22 00:13:36 +0100340 * attribute_container_add_class_device - same function as device_add
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 *
342 * @classdev: the class device to add
343 *
Tony Jonesee959b02008-02-22 00:13:36 +0100344 * This performs essentially the same function as device_add except for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 * attribute containers, namely add the classdev to the system and then
346 * create the attribute files
347 */
348int
Tony Jonesee959b02008-02-22 00:13:36 +0100349attribute_container_add_class_device(struct device *classdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Tony Jonesee959b02008-02-22 00:13:36 +0100351 int error = device_add(classdev);
Cosmin Dragomir481026d2015-03-08 12:30:14 +0200352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (error)
354 return error;
355 return attribute_container_add_attrs(classdev);
356}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358/**
359 * attribute_container_add_class_device_adapter - simple adapter for triggers
360 *
361 * This function is identical to attribute_container_add_class_device except
362 * that it is designed to be called from the triggers
363 */
364int
365attribute_container_add_class_device_adapter(struct attribute_container *cont,
366 struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100367 struct device *classdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 return attribute_container_add_class_device(classdev);
370}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372/**
373 * attribute_container_remove_attrs - remove any attribute files
374 *
375 * @classdev: The class device to remove the files from
376 *
377 */
378void
Tony Jonesee959b02008-02-22 00:13:36 +0100379attribute_container_remove_attrs(struct device *classdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 struct attribute_container *cont =
382 attribute_container_classdev_to_container(classdev);
Tony Jonesee959b02008-02-22 00:13:36 +0100383 struct device_attribute **attrs = cont->attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 int i;
385
James Bottomleyfd110972008-01-02 18:48:47 -0600386 if (!attrs && !cont->grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return;
388
James Bottomleyfd110972008-01-02 18:48:47 -0600389 if (cont->grp) {
390 sysfs_remove_group(&classdev->kobj, cont->grp);
391 return ;
392 }
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 for (i = 0; attrs[i]; i++)
Tony Jonesee959b02008-02-22 00:13:36 +0100395 device_remove_file(classdev, attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398/**
399 * attribute_container_class_device_del - equivalent of class_device_del
400 *
401 * @classdev: the class device
402 *
403 * This function simply removes all the attribute files and then calls
Tony Jonesee959b02008-02-22 00:13:36 +0100404 * device_del.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 */
406void
Tony Jonesee959b02008-02-22 00:13:36 +0100407attribute_container_class_device_del(struct device *classdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 attribute_container_remove_attrs(classdev);
Tony Jonesee959b02008-02-22 00:13:36 +0100410 device_del(classdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
James Bottomleyd0a7e572005-08-14 17:09:01 -0500413/**
414 * attribute_container_find_class_device - find the corresponding class_device
415 *
416 * @cont: the container
417 * @dev: the generic device
418 *
419 * Looks up the device in the container's list of class devices and returns
420 * the corresponding class_device.
421 */
Tony Jonesee959b02008-02-22 00:13:36 +0100422struct device *
James Bottomleyd0a7e572005-08-14 17:09:01 -0500423attribute_container_find_class_device(struct attribute_container *cont,
424 struct device *dev)
425{
Tony Jonesee959b02008-02-22 00:13:36 +0100426 struct device *cdev = NULL;
James Bottomleyd0a7e572005-08-14 17:09:01 -0500427 struct internal_container *ic;
James Bottomley53c165e2005-08-22 10:06:19 -0500428 struct klist_iter iter;
James Bottomleyd0a7e572005-08-14 17:09:01 -0500429
James Bottomley53c165e2005-08-22 10:06:19 -0500430 klist_for_each_entry(ic, &cont->containers, node, &iter) {
Tony Jonesee959b02008-02-22 00:13:36 +0100431 if (ic->classdev.parent == dev) {
James Bottomleyd0a7e572005-08-14 17:09:01 -0500432 cdev = &ic->classdev;
James Bottomley53c165e2005-08-22 10:06:19 -0500433 /* FIXME: must exit iterator then break */
434 klist_iter_exit(&iter);
James Bottomleyd0a7e572005-08-14 17:09:01 -0500435 break;
436 }
437 }
James Bottomleyd0a7e572005-08-14 17:09:01 -0500438
439 return cdev;
440}
441EXPORT_SYMBOL_GPL(attribute_container_find_class_device);