PM: Simplify suspend_device
[deliverable/linux.git] / drivers / base / attribute_container.c
CommitLineData
1da177e4
LT
1/*
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>
21
a1bdc7aa
BD
22#include "base.h"
23
1da177e4
LT
24/* 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 {
53c165e0 27 struct klist_node node;
1da177e4
LT
28 struct attribute_container *cont;
29 struct class_device classdev;
30};
31
caf39e87
LT
32static void internal_container_klist_get(struct klist_node *n)
33{
34 struct internal_container *ic =
35 container_of(n, struct internal_container, node);
36 class_device_get(&ic->classdev);
37}
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);
43 class_device_put(&ic->classdev);
44}
45
46
1da177e4
LT
47/**
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 *
55attribute_container_classdev_to_container(struct class_device *classdev)
56{
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
63static struct list_head attribute_container_list;
64
61a2f59a 65static DEFINE_MUTEX(attribute_container_mutex);
1da177e4
LT
66
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);
caf39e87
LT
77 klist_init(&cont->containers,internal_container_klist_get,
78 internal_container_klist_put);
1da177e4 79
61a2f59a 80 mutex_lock(&attribute_container_mutex);
1da177e4 81 list_add_tail(&cont->node, &attribute_container_list);
61a2f59a 82 mutex_unlock(&attribute_container_mutex);
1da177e4
LT
83
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;
61a2f59a 97 mutex_lock(&attribute_container_mutex);
53c165e0
JB
98 spin_lock(&cont->containers.k_lock);
99 if (!list_empty(&cont->containers.k_list))
1da177e4
LT
100 goto out;
101 retval = 0;
102 list_del(&cont->node);
103 out:
53c165e0 104 spin_unlock(&cont->containers.k_lock);
61a2f59a 105 mutex_unlock(&attribute_container_mutex);
1da177e4
LT
106 return retval;
107
108}
109EXPORT_SYMBOL_GPL(attribute_container_unregister);
110
111/* private function used as class release */
112static void attribute_container_release(struct class_device *classdev)
113{
114 struct internal_container *ic
115 = container_of(classdev, struct internal_container, classdev);
116 struct device *dev = classdev->dev;
117
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
131 * class_device_add. If a function is provided, it is expected to add
132 * 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
136 * attribute_container_device_trigger() to call class_device_add() on
137 * 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 *,
144 struct class_device *))
145{
146 struct attribute_container *cont;
147
61a2f59a 148 mutex_lock(&attribute_container_mutex);
1da177e4
LT
149 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;
4aed0644
JS
157
158 ic = kzalloc(sizeof(*ic), GFP_KERNEL);
1da177e4
LT
159 if (!ic) {
160 dev_printk(KERN_ERR, dev, "failed to allocate class container\n");
161 continue;
162 }
4aed0644 163
1da177e4
LT
164 ic->cont = cont;
165 class_device_initialize(&ic->classdev);
166 ic->classdev.dev = get_device(dev);
167 ic->classdev.class = cont->class;
168 cont->class->release = attribute_container_release;
169 strcpy(ic->classdev.class_id, dev->bus_id);
170 if (fn)
171 fn(cont, dev, &ic->classdev);
172 else
173 attribute_container_add_class_device(&ic->classdev);
53c165e0 174 klist_add_tail(&ic->node, &cont->containers);
1da177e4 175 }
61a2f59a 176 mutex_unlock(&attribute_container_mutex);
1da177e4
LT
177}
178
53c165e0
JB
179/* 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); \
caf39e87
LT
185 n ? container_of(n, typeof(*pos), member) : \
186 ({ klist_iter_exit(iter) ; NULL; }); \
53c165e0
JB
187 }) ) != NULL; )
188
189
1da177e4
LT
190/**
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
197 * simply done via class_device_unregister (note that if something
198 * 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
202 * class_device_del() and then use
203 * attribute_container_device_trigger() to do the final put on the
204 * classdev.
205 */
206void
207attribute_container_remove_device(struct device *dev,
208 void (*fn)(struct attribute_container *,
209 struct device *,
210 struct class_device *))
211{
212 struct attribute_container *cont;
213
61a2f59a 214 mutex_lock(&attribute_container_mutex);
1da177e4 215 list_for_each_entry(cont, &attribute_container_list, node) {
53c165e0
JB
216 struct internal_container *ic;
217 struct klist_iter iter;
1da177e4
LT
218
219 if (attribute_container_no_classdevs(cont))
220 continue;
221
222 if (!cont->match(cont, dev))
223 continue;
53c165e0
JB
224
225 klist_for_each_entry(ic, &cont->containers, node, &iter) {
1da177e4
LT
226 if (dev != ic->classdev.dev)
227 continue;
caf39e87 228 klist_del(&ic->node);
1da177e4
LT
229 if (fn)
230 fn(cont, dev, &ic->classdev);
231 else {
232 attribute_container_remove_attrs(&ic->classdev);
233 class_device_unregister(&ic->classdev);
234 }
235 }
236 }
61a2f59a 237 mutex_unlock(&attribute_container_mutex);
1da177e4 238}
1da177e4
LT
239
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
251attribute_container_device_trigger(struct device *dev,
252 int (*fn)(struct attribute_container *,
253 struct device *,
254 struct class_device *))
255{
256 struct attribute_container *cont;
257
61a2f59a 258 mutex_lock(&attribute_container_mutex);
1da177e4 259 list_for_each_entry(cont, &attribute_container_list, node) {
53c165e0
JB
260 struct internal_container *ic;
261 struct klist_iter iter;
1da177e4
LT
262
263 if (!cont->match(cont, dev))
264 continue;
265
ebd8bb76
JB
266 if (attribute_container_no_classdevs(cont)) {
267 fn(cont, dev, NULL);
268 continue;
269 }
270
53c165e0 271 klist_for_each_entry(ic, &cont->containers, node, &iter) {
1da177e4
LT
272 if (dev == ic->classdev.dev)
273 fn(cont, dev, &ic->classdev);
274 }
275 }
61a2f59a 276 mutex_unlock(&attribute_container_mutex);
1da177e4 277}
1da177e4
LT
278
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
61a2f59a 298 mutex_lock(&attribute_container_mutex);
1da177e4
LT
299 list_for_each_entry(cont, &attribute_container_list, node) {
300 if (cont->match(cont, dev))
301 fn(cont, dev);
302 }
61a2f59a 303 mutex_unlock(&attribute_container_mutex);
1da177e4 304}
1da177e4
LT
305
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
315attribute_container_add_attrs(struct class_device *classdev)
316{
317 struct attribute_container *cont =
318 attribute_container_classdev_to_container(classdev);
319 struct class_device_attribute **attrs = cont->attrs;
320 int i, error;
321
322 if (!attrs)
323 return 0;
324
325 for (i = 0; attrs[i]; i++) {
326 error = class_device_create_file(classdev, attrs[i]);
327 if (error)
328 return error;
329 }
330
331 return 0;
332}
1da177e4
LT
333
334/**
335 * attribute_container_add_class_device - same function as class_device_add
336 *
337 * @classdev: the class device to add
338 *
339 * This performs essentially the same function as class_device_add except for
340 * attribute containers, namely add the classdev to the system and then
341 * create the attribute files
342 */
343int
344attribute_container_add_class_device(struct class_device *classdev)
345{
346 int error = class_device_add(classdev);
347 if (error)
348 return error;
349 return attribute_container_add_attrs(classdev);
350}
1da177e4
LT
351
352/**
353 * attribute_container_add_class_device_adapter - simple adapter for triggers
354 *
355 * This function is identical to attribute_container_add_class_device except
356 * that it is designed to be called from the triggers
357 */
358int
359attribute_container_add_class_device_adapter(struct attribute_container *cont,
360 struct device *dev,
361 struct class_device *classdev)
362{
363 return attribute_container_add_class_device(classdev);
364}
1da177e4
LT
365
366/**
367 * attribute_container_remove_attrs - remove any attribute files
368 *
369 * @classdev: The class device to remove the files from
370 *
371 */
372void
373attribute_container_remove_attrs(struct class_device *classdev)
374{
375 struct attribute_container *cont =
376 attribute_container_classdev_to_container(classdev);
377 struct class_device_attribute **attrs = cont->attrs;
378 int i;
379
380 if (!attrs)
381 return;
382
383 for (i = 0; attrs[i]; i++)
384 class_device_remove_file(classdev, attrs[i]);
385}
1da177e4
LT
386
387/**
388 * attribute_container_class_device_del - equivalent of class_device_del
389 *
390 * @classdev: the class device
391 *
392 * This function simply removes all the attribute files and then calls
393 * class_device_del.
394 */
395void
396attribute_container_class_device_del(struct class_device *classdev)
397{
398 attribute_container_remove_attrs(classdev);
399 class_device_del(classdev);
400}
1da177e4 401
d0a7e574
JB
402/**
403 * attribute_container_find_class_device - find the corresponding class_device
404 *
405 * @cont: the container
406 * @dev: the generic device
407 *
408 * Looks up the device in the container's list of class devices and returns
409 * the corresponding class_device.
410 */
411struct class_device *
412attribute_container_find_class_device(struct attribute_container *cont,
413 struct device *dev)
414{
415 struct class_device *cdev = NULL;
416 struct internal_container *ic;
53c165e0 417 struct klist_iter iter;
d0a7e574 418
53c165e0 419 klist_for_each_entry(ic, &cont->containers, node, &iter) {
d0a7e574
JB
420 if (ic->classdev.dev == dev) {
421 cdev = &ic->classdev;
53c165e0
JB
422 /* FIXME: must exit iterator then break */
423 klist_iter_exit(&iter);
d0a7e574
JB
424 break;
425 }
426 }
d0a7e574
JB
427
428 return cdev;
429}
430EXPORT_SYMBOL_GPL(attribute_container_find_class_device);
431
1da177e4
LT
432int __init
433attribute_container_init(void)
434{
435 INIT_LIST_HEAD(&attribute_container_list);
436 return 0;
437}
This page took 0.219127 seconds and 5 git commands to generate.