blob: 2ba4cac080c5e79cc8b5056f18aa45b75e816bf6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * attribute_container.c - implementation of a simple container for classes
3 *
4 * Copyright (c) 2005 - James Bottomley <James.Bottomley@steeleye.com>
5 *
6 * This file is licensed under GPLv2
7 *
8 * The basic idea here is to enable a device to be attached to an
9 * aritrary numer of classes without having to allocate storage for them.
10 * Instead, the contained classes select the devices they need to attach
11 * to via a matching function.
12 */
13
14#include <linux/attribute_container.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/device.h>
16#include <linux/kernel.h>
17#include <linux/slab.h>
18#include <linux/list.h>
19#include <linux/module.h>
Michael S. Tsirkinf8916c12007-06-10 22:39:12 +030020#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010022#include "base.h"
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/* This is a private structure used to tie the classdev and the
25 * container .. it should never be visible outside this file */
26struct internal_container {
James Bottomley53c165e2005-08-22 10:06:19 -050027 struct klist_node node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 struct attribute_container *cont;
Tony Jonesee959b02008-02-22 00:13:36 +010029 struct device classdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030};
31
Linus Torvaldscaf39e82005-09-07 18:44:33 -070032static void internal_container_klist_get(struct klist_node *n)
33{
34 struct internal_container *ic =
35 container_of(n, struct internal_container, node);
Tony Jonesee959b02008-02-22 00:13:36 +010036 get_device(&ic->classdev);
Linus Torvaldscaf39e82005-09-07 18:44:33 -070037}
38
39static void internal_container_klist_put(struct klist_node *n)
40{
41 struct internal_container *ic =
42 container_of(n, struct internal_container, node);
Tony Jonesee959b02008-02-22 00:13:36 +010043 put_device(&ic->classdev);
Linus Torvaldscaf39e82005-09-07 18:44:33 -070044}
45
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/**
48 * attribute_container_classdev_to_container - given a classdev, return the container
49 *
50 * @classdev: the class device created by attribute_container_add_device.
51 *
52 * Returns the container associated with this classdev.
53 */
54struct attribute_container *
Tony Jonesee959b02008-02-22 00:13:36 +010055attribute_container_classdev_to_container(struct device *classdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 struct internal_container *ic =
58 container_of(classdev, struct internal_container, classdev);
59 return ic->cont;
60}
61EXPORT_SYMBOL_GPL(attribute_container_classdev_to_container);
62
Denis Chengdb1118a2007-12-06 02:24:40 +080063static LIST_HEAD(attribute_container_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -070065static DEFINE_MUTEX(attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/**
68 * attribute_container_register - register an attribute container
69 *
70 * @cont: The container to register. This must be allocated by the
71 * callee and should also be zeroed by it.
72 */
73int
74attribute_container_register(struct attribute_container *cont)
75{
76 INIT_LIST_HEAD(&cont->node);
Tina Johnson287f9bd2014-08-12 02:54:09 +053077 klist_init(&cont->containers, internal_container_klist_get,
Linus Torvaldscaf39e82005-09-07 18:44:33 -070078 internal_container_klist_put);
Tina Johnson24a7d362014-08-12 02:54:08 +053079
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -070080 mutex_lock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 list_add_tail(&cont->node, &attribute_container_list);
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -070082 mutex_unlock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 return 0;
85}
86EXPORT_SYMBOL_GPL(attribute_container_register);
87
88/**
89 * attribute_container_unregister - remove a container registration
90 *
91 * @cont: previously registered container to remove
92 */
93int
94attribute_container_unregister(struct attribute_container *cont)
95{
96 int retval = -EBUSY;
Cosmin Dragomir481026d2015-03-08 12:30:14 +020097
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -070098 mutex_lock(&attribute_container_mutex);
James Bottomley53c165e2005-08-22 10:06:19 -050099 spin_lock(&cont->containers.k_lock);
100 if (!list_empty(&cont->containers.k_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 goto out;
102 retval = 0;
103 list_del(&cont->node);
104 out:
James Bottomley53c165e2005-08-22 10:06:19 -0500105 spin_unlock(&cont->containers.k_lock);
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700106 mutex_unlock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return retval;
Tina Johnson24a7d362014-08-12 02:54:08 +0530108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110EXPORT_SYMBOL_GPL(attribute_container_unregister);
111
112/* private function used as class release */
Tony Jonesee959b02008-02-22 00:13:36 +0100113static void attribute_container_release(struct device *classdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Tina Johnson24a7d362014-08-12 02:54:08 +0530115 struct internal_container *ic
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 = container_of(classdev, struct internal_container, classdev);
Tony Jonesee959b02008-02-22 00:13:36 +0100117 struct device *dev = classdev->parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 kfree(ic);
120 put_device(dev);
121}
122
123/**
124 * attribute_container_add_device - see if any container is interested in dev
125 *
126 * @dev: device to add attributes to
127 * @fn: function to trigger addition of class device.
128 *
129 * This function allocates storage for the class device(s) to be
130 * attached to dev (one for each matching attribute_container). If no
131 * fn is provided, the code will simply register the class device via
Tony Jonesee959b02008-02-22 00:13:36 +0100132 * device_add. If a function is provided, it is expected to add
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * the class device at the appropriate time. One of the things that
134 * might be necessary is to allocate and initialise the classdev and
135 * then add it a later time. To do this, call this routine for
136 * allocation and initialisation and then use
Tony Jonesee959b02008-02-22 00:13:36 +0100137 * attribute_container_device_trigger() to call device_add() on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * it. Note: after this, the class device contains a reference to dev
139 * which is not relinquished until the release of the classdev.
140 */
141void
142attribute_container_add_device(struct device *dev,
143 int (*fn)(struct attribute_container *,
144 struct device *,
Tony Jonesee959b02008-02-22 00:13:36 +0100145 struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 struct attribute_container *cont;
148
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700149 mutex_lock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 list_for_each_entry(cont, &attribute_container_list, node) {
151 struct internal_container *ic;
152
153 if (attribute_container_no_classdevs(cont))
154 continue;
155
156 if (!cont->match(cont, dev))
157 continue;
Jiri Slaby4aed0642005-09-13 01:25:01 -0700158
159 ic = kzalloc(sizeof(*ic), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (!ic) {
Joe Perchesa369a7e2012-10-28 01:05:41 -0700161 dev_err(dev, "failed to allocate class container\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 continue;
163 }
Jiri Slaby4aed0642005-09-13 01:25:01 -0700164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 ic->cont = cont;
Tony Jonesee959b02008-02-22 00:13:36 +0100166 device_initialize(&ic->classdev);
167 ic->classdev.parent = get_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 ic->classdev.class = cont->class;
Tony Jonesee959b02008-02-22 00:13:36 +0100169 cont->class->dev_release = attribute_container_release;
Kees Cook02aa2a32013-07-03 15:04:56 -0700170 dev_set_name(&ic->classdev, "%s", dev_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (fn)
172 fn(cont, dev, &ic->classdev);
173 else
174 attribute_container_add_class_device(&ic->classdev);
James Bottomley53c165e2005-08-22 10:06:19 -0500175 klist_add_tail(&ic->node, &cont->containers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700177 mutex_unlock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
James Bottomley53c165e2005-08-22 10:06:19 -0500180/* FIXME: can't break out of this unless klist_iter_exit is also
181 * called before doing the break
182 */
183#define klist_for_each_entry(pos, head, member, iter) \
184 for (klist_iter_init(head, iter); (pos = ({ \
185 struct klist_node *n = klist_next(iter); \
Linus Torvaldscaf39e82005-09-07 18:44:33 -0700186 n ? container_of(n, typeof(*pos), member) : \
187 ({ klist_iter_exit(iter) ; NULL; }); \
Tina Johnson287f9bd2014-08-12 02:54:09 +0530188 })) != NULL;)
Tina Johnson24a7d362014-08-12 02:54:08 +0530189
James Bottomley53c165e2005-08-22 10:06:19 -0500190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/**
192 * attribute_container_remove_device - make device eligible for removal.
193 *
194 * @dev: The generic device
195 * @fn: A function to call to remove the device
196 *
197 * This routine triggers device removal. If fn is NULL, then it is
Tony Jonesee959b02008-02-22 00:13:36 +0100198 * simply done via device_unregister (note that if something
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 * still has a reference to the classdev, then the memory occupied
200 * will not be freed until the classdev is released). If you want a
201 * two phase release: remove from visibility and then delete the
202 * device, then you should use this routine with a fn that calls
Tony Jonesee959b02008-02-22 00:13:36 +0100203 * device_del() and then use attribute_container_device_trigger()
204 * to do the final put on the classdev.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 */
206void
207attribute_container_remove_device(struct device *dev,
208 void (*fn)(struct attribute_container *,
209 struct device *,
Tony Jonesee959b02008-02-22 00:13:36 +0100210 struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 struct attribute_container *cont;
213
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700214 mutex_lock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 list_for_each_entry(cont, &attribute_container_list, node) {
James Bottomley53c165e2005-08-22 10:06:19 -0500216 struct internal_container *ic;
217 struct klist_iter iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 if (attribute_container_no_classdevs(cont))
220 continue;
221
222 if (!cont->match(cont, dev))
223 continue;
James Bottomley53c165e2005-08-22 10:06:19 -0500224
225 klist_for_each_entry(ic, &cont->containers, node, &iter) {
Tony Jonesee959b02008-02-22 00:13:36 +0100226 if (dev != ic->classdev.parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 continue;
Linus Torvaldscaf39e82005-09-07 18:44:33 -0700228 klist_del(&ic->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (fn)
230 fn(cont, dev, &ic->classdev);
231 else {
232 attribute_container_remove_attrs(&ic->classdev);
Tony Jonesee959b02008-02-22 00:13:36 +0100233 device_unregister(&ic->classdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
235 }
236 }
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700237 mutex_unlock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240/**
241 * attribute_container_device_trigger - execute a trigger for each matching classdev
242 *
243 * @dev: The generic device to run the trigger for
244 * @fn the function to execute for each classdev.
245 *
246 * This funcion is for executing a trigger when you need to know both
247 * the container and the classdev. If you only care about the
248 * container, then use attribute_container_trigger() instead.
249 */
250void
Tina Johnson24a7d362014-08-12 02:54:08 +0530251attribute_container_device_trigger(struct device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 int (*fn)(struct attribute_container *,
253 struct device *,
Tony Jonesee959b02008-02-22 00:13:36 +0100254 struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 struct attribute_container *cont;
257
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700258 mutex_lock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 list_for_each_entry(cont, &attribute_container_list, node) {
James Bottomley53c165e2005-08-22 10:06:19 -0500260 struct internal_container *ic;
261 struct klist_iter iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 if (!cont->match(cont, dev))
264 continue;
265
James Bottomleyebd8bb72005-08-15 16:13:19 -0500266 if (attribute_container_no_classdevs(cont)) {
267 fn(cont, dev, NULL);
268 continue;
269 }
270
James Bottomley53c165e2005-08-22 10:06:19 -0500271 klist_for_each_entry(ic, &cont->containers, node, &iter) {
Tony Jonesee959b02008-02-22 00:13:36 +0100272 if (dev == ic->classdev.parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 fn(cont, dev, &ic->classdev);
274 }
275 }
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700276 mutex_unlock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279/**
280 * attribute_container_trigger - trigger a function for each matching container
281 *
282 * @dev: The generic device to activate the trigger for
283 * @fn: the function to trigger
284 *
285 * This routine triggers a function that only needs to know the
286 * matching containers (not the classdev) associated with a device.
287 * It is more lightweight than attribute_container_device_trigger, so
288 * should be used in preference unless the triggering function
289 * actually needs to know the classdev.
290 */
291void
292attribute_container_trigger(struct device *dev,
293 int (*fn)(struct attribute_container *,
294 struct device *))
295{
296 struct attribute_container *cont;
297
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700298 mutex_lock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 list_for_each_entry(cont, &attribute_container_list, node) {
300 if (cont->match(cont, dev))
301 fn(cont, dev);
302 }
Matthias Kaehlcke61a2f592007-04-26 00:12:09 -0700303 mutex_unlock(&attribute_container_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306/**
307 * attribute_container_add_attrs - add attributes
308 *
309 * @classdev: The class device
310 *
311 * This simply creates all the class device sysfs files from the
312 * attributes listed in the container
313 */
314int
Tony Jonesee959b02008-02-22 00:13:36 +0100315attribute_container_add_attrs(struct device *classdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 struct attribute_container *cont =
318 attribute_container_classdev_to_container(classdev);
Tony Jonesee959b02008-02-22 00:13:36 +0100319 struct device_attribute **attrs = cont->attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int i, error;
321
James Bottomleyfd110972008-01-02 18:48:47 -0600322 BUG_ON(attrs && cont->grp);
323
324 if (!attrs && !cont->grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return 0;
326
James Bottomleyfd110972008-01-02 18:48:47 -0600327 if (cont->grp)
328 return sysfs_create_group(&classdev->kobj, cont->grp);
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 for (i = 0; attrs[i]; i++) {
James Bottomleyebd09ec2010-03-20 12:44:12 -0500331 sysfs_attr_init(&attrs[i]->attr);
Tony Jonesee959b02008-02-22 00:13:36 +0100332 error = device_create_file(classdev, attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (error)
334 return error;
335 }
336
337 return 0;
338}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340/**
Tony Jonesee959b02008-02-22 00:13:36 +0100341 * attribute_container_add_class_device - same function as device_add
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 *
343 * @classdev: the class device to add
344 *
Tony Jonesee959b02008-02-22 00:13:36 +0100345 * This performs essentially the same function as device_add except for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 * attribute containers, namely add the classdev to the system and then
347 * create the attribute files
348 */
349int
Tony Jonesee959b02008-02-22 00:13:36 +0100350attribute_container_add_class_device(struct device *classdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Tony Jonesee959b02008-02-22 00:13:36 +0100352 int error = device_add(classdev);
Cosmin Dragomir481026d2015-03-08 12:30:14 +0200353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (error)
355 return error;
356 return attribute_container_add_attrs(classdev);
357}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359/**
360 * attribute_container_add_class_device_adapter - simple adapter for triggers
361 *
362 * This function is identical to attribute_container_add_class_device except
363 * that it is designed to be called from the triggers
364 */
365int
366attribute_container_add_class_device_adapter(struct attribute_container *cont,
367 struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100368 struct device *classdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 return attribute_container_add_class_device(classdev);
371}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373/**
374 * attribute_container_remove_attrs - remove any attribute files
375 *
376 * @classdev: The class device to remove the files from
377 *
378 */
379void
Tony Jonesee959b02008-02-22 00:13:36 +0100380attribute_container_remove_attrs(struct device *classdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 struct attribute_container *cont =
383 attribute_container_classdev_to_container(classdev);
Tony Jonesee959b02008-02-22 00:13:36 +0100384 struct device_attribute **attrs = cont->attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 int i;
386
James Bottomleyfd110972008-01-02 18:48:47 -0600387 if (!attrs && !cont->grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return;
389
James Bottomleyfd110972008-01-02 18:48:47 -0600390 if (cont->grp) {
391 sysfs_remove_group(&classdev->kobj, cont->grp);
392 return ;
393 }
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 for (i = 0; attrs[i]; i++)
Tony Jonesee959b02008-02-22 00:13:36 +0100396 device_remove_file(classdev, attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399/**
400 * attribute_container_class_device_del - equivalent of class_device_del
401 *
402 * @classdev: the class device
403 *
404 * This function simply removes all the attribute files and then calls
Tony Jonesee959b02008-02-22 00:13:36 +0100405 * device_del.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 */
407void
Tony Jonesee959b02008-02-22 00:13:36 +0100408attribute_container_class_device_del(struct device *classdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 attribute_container_remove_attrs(classdev);
Tony Jonesee959b02008-02-22 00:13:36 +0100411 device_del(classdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
James Bottomleyd0a7e572005-08-14 17:09:01 -0500414/**
415 * attribute_container_find_class_device - find the corresponding class_device
416 *
417 * @cont: the container
418 * @dev: the generic device
419 *
420 * Looks up the device in the container's list of class devices and returns
421 * the corresponding class_device.
422 */
Tony Jonesee959b02008-02-22 00:13:36 +0100423struct device *
James Bottomleyd0a7e572005-08-14 17:09:01 -0500424attribute_container_find_class_device(struct attribute_container *cont,
425 struct device *dev)
426{
Tony Jonesee959b02008-02-22 00:13:36 +0100427 struct device *cdev = NULL;
James Bottomleyd0a7e572005-08-14 17:09:01 -0500428 struct internal_container *ic;
James Bottomley53c165e2005-08-22 10:06:19 -0500429 struct klist_iter iter;
James Bottomleyd0a7e572005-08-14 17:09:01 -0500430
James Bottomley53c165e2005-08-22 10:06:19 -0500431 klist_for_each_entry(ic, &cont->containers, node, &iter) {
Tony Jonesee959b02008-02-22 00:13:36 +0100432 if (ic->classdev.parent == dev) {
James Bottomleyd0a7e572005-08-14 17:09:01 -0500433 cdev = &ic->classdev;
James Bottomley53c165e2005-08-22 10:06:19 -0500434 /* FIXME: must exit iterator then break */
435 klist_iter_exit(&iter);
James Bottomleyd0a7e572005-08-14 17:09:01 -0500436 break;
437 }
438 }
James Bottomleyd0a7e572005-08-14 17:09:01 -0500439
440 return cdev;
441}
442EXPORT_SYMBOL_GPL(attribute_container_find_class_device);