blob: 3b43e8a9f87e45e0763c8ae3d7b7d5b539616151 [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>
15#include <linux/init.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>
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;
30 struct class_device classdev;
31};
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);
37 class_device_get(&ic->classdev);
38}
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);
44 class_device_put(&ic->classdev);
45}
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 *
56attribute_container_classdev_to_container(struct class_device *classdev)
57{
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);
Linus Torvaldscaf39e82005-09-07 18:44:33 -070078 klist_init(&cont->containers,internal_container_klist_get,
79 internal_container_klist_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
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;
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;
108
109}
110EXPORT_SYMBOL_GPL(attribute_container_unregister);
111
112/* private function used as class release */
113static void attribute_container_release(struct class_device *classdev)
114{
115 struct internal_container *ic
116 = container_of(classdev, struct internal_container, classdev);
117 struct device *dev = classdev->dev;
118
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
132 * class_device_add. If a function is provided, it is expected to add
133 * 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
137 * attribute_container_device_trigger() to call class_device_add() on
138 * 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 *,
145 struct class_device *))
146{
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) {
161 dev_printk(KERN_ERR, dev, "failed to allocate class container\n");
162 continue;
163 }
Jiri Slaby4aed0642005-09-13 01:25:01 -0700164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 ic->cont = cont;
166 class_device_initialize(&ic->classdev);
167 ic->classdev.dev = get_device(dev);
168 ic->classdev.class = cont->class;
169 cont->class->release = attribute_container_release;
170 strcpy(ic->classdev.class_id, dev->bus_id);
171 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; }); \
James Bottomley53c165e2005-08-22 10:06:19 -0500188 }) ) != NULL; )
189
190
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
198 * simply done via class_device_unregister (note that if something
199 * 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
203 * class_device_del() and then use
204 * attribute_container_device_trigger() to do the final put on the
205 * classdev.
206 */
207void
208attribute_container_remove_device(struct device *dev,
209 void (*fn)(struct attribute_container *,
210 struct device *,
211 struct class_device *))
212{
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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (dev != ic->classdev.dev)
228 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);
234 class_device_unregister(&ic->classdev);
235 }
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 *
247 * This funcion 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 */
251void
252attribute_container_device_trigger(struct device *dev,
253 int (*fn)(struct attribute_container *,
254 struct device *,
255 struct class_device *))
256{
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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (dev == ic->classdev.dev)
274 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
316attribute_container_add_attrs(struct class_device *classdev)
317{
318 struct attribute_container *cont =
319 attribute_container_classdev_to_container(classdev);
320 struct class_device_attribute **attrs = cont->attrs;
321 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++) {
332 error = class_device_create_file(classdev, attrs[i]);
333 if (error)
334 return error;
335 }
336
337 return 0;
338}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340/**
341 * attribute_container_add_class_device - same function as class_device_add
342 *
343 * @classdev: the class device to add
344 *
345 * This performs essentially the same function as class_device_add except for
346 * attribute containers, namely add the classdev to the system and then
347 * create the attribute files
348 */
349int
350attribute_container_add_class_device(struct class_device *classdev)
351{
352 int error = class_device_add(classdev);
353 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,
367 struct class_device *classdev)
368{
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
379attribute_container_remove_attrs(struct class_device *classdev)
380{
381 struct attribute_container *cont =
382 attribute_container_classdev_to_container(classdev);
383 struct class_device_attribute **attrs = cont->attrs;
384 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++)
395 class_device_remove_file(classdev, attrs[i]);
396}
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
404 * class_device_del.
405 */
406void
407attribute_container_class_device_del(struct class_device *classdev)
408{
409 attribute_container_remove_attrs(classdev);
410 class_device_del(classdev);
411}
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 */
422struct class_device *
423attribute_container_find_class_device(struct attribute_container *cont,
424 struct device *dev)
425{
426 struct class_device *cdev = NULL;
427 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) {
James Bottomleyd0a7e572005-08-14 17:09:01 -0500431 if (ic->classdev.dev == dev) {
432 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);