62c093db11e6e853c7b8d5c22ca1603f904aea07
[powerpc.git] / drivers / base / attribute_container.c
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
22 /* This is a private structure used to tie the classdev and the
23  * container .. it should never be visible outside this file */
24 struct internal_container {
25         struct list_head node;
26         struct attribute_container *cont;
27         struct class_device classdev;
28 };
29
30 /**
31  * attribute_container_classdev_to_container - given a classdev, return the container
32  *
33  * @classdev: the class device created by attribute_container_add_device.
34  *
35  * Returns the container associated with this classdev.
36  */
37 struct attribute_container *
38 attribute_container_classdev_to_container(struct class_device *classdev)
39 {
40         struct internal_container *ic =
41                 container_of(classdev, struct internal_container, classdev);
42         return ic->cont;
43 }
44 EXPORT_SYMBOL_GPL(attribute_container_classdev_to_container);
45
46 static struct list_head attribute_container_list;
47
48 static DECLARE_MUTEX(attribute_container_mutex);
49
50 /**
51  * attribute_container_register - register an attribute container
52  *
53  * @cont: The container to register.  This must be allocated by the
54  *        callee and should also be zeroed by it.
55  */
56 int
57 attribute_container_register(struct attribute_container *cont)
58 {
59         INIT_LIST_HEAD(&cont->node);
60         INIT_LIST_HEAD(&cont->containers);
61         spin_lock_init(&cont->containers_lock);
62                 
63         down(&attribute_container_mutex);
64         list_add_tail(&cont->node, &attribute_container_list);
65         up(&attribute_container_mutex);
66
67         return 0;
68 }
69 EXPORT_SYMBOL_GPL(attribute_container_register);
70
71 /**
72  * attribute_container_unregister - remove a container registration
73  *
74  * @cont: previously registered container to remove
75  */
76 int
77 attribute_container_unregister(struct attribute_container *cont)
78 {
79         int retval = -EBUSY;
80         down(&attribute_container_mutex);
81         spin_lock(&cont->containers_lock);
82         if (!list_empty(&cont->containers))
83                 goto out;
84         retval = 0;
85         list_del(&cont->node);
86  out:
87         spin_unlock(&cont->containers_lock);
88         up(&attribute_container_mutex);
89         return retval;
90                 
91 }
92 EXPORT_SYMBOL_GPL(attribute_container_unregister);
93
94 /* private function used as class release */
95 static void attribute_container_release(struct class_device *classdev)
96 {
97         struct internal_container *ic 
98                 = container_of(classdev, struct internal_container, classdev);
99         struct device *dev = classdev->dev;
100
101         kfree(ic);
102         put_device(dev);
103 }
104
105 /**
106  * attribute_container_add_device - see if any container is interested in dev
107  *
108  * @dev: device to add attributes to
109  * @fn:  function to trigger addition of class device.
110  *
111  * This function allocates storage for the class device(s) to be
112  * attached to dev (one for each matching attribute_container).  If no
113  * fn is provided, the code will simply register the class device via
114  * class_device_add.  If a function is provided, it is expected to add
115  * the class device at the appropriate time.  One of the things that
116  * might be necessary is to allocate and initialise the classdev and
117  * then add it a later time.  To do this, call this routine for
118  * allocation and initialisation and then use
119  * attribute_container_device_trigger() to call class_device_add() on
120  * it.  Note: after this, the class device contains a reference to dev
121  * which is not relinquished until the release of the classdev.
122  */
123 void
124 attribute_container_add_device(struct device *dev,
125                                int (*fn)(struct attribute_container *,
126                                          struct device *,
127                                          struct class_device *))
128 {
129         struct attribute_container *cont;
130
131         down(&attribute_container_mutex);
132         list_for_each_entry(cont, &attribute_container_list, node) {
133                 struct internal_container *ic;
134
135                 if (attribute_container_no_classdevs(cont))
136                         continue;
137
138                 if (!cont->match(cont, dev))
139                         continue;
140                 ic = kmalloc(sizeof(struct internal_container), GFP_KERNEL);
141                 if (!ic) {
142                         dev_printk(KERN_ERR, dev, "failed to allocate class container\n");
143                         continue;
144                 }
145                 memset(ic, 0, sizeof(struct internal_container));
146                 INIT_LIST_HEAD(&ic->node);
147                 ic->cont = cont;
148                 class_device_initialize(&ic->classdev);
149                 ic->classdev.dev = get_device(dev);
150                 ic->classdev.class = cont->class;
151                 cont->class->release = attribute_container_release;
152                 strcpy(ic->classdev.class_id, dev->bus_id);
153                 if (fn)
154                         fn(cont, dev, &ic->classdev);
155                 else
156                         attribute_container_add_class_device(&ic->classdev);
157                 spin_lock(&cont->containers_lock);
158                 list_add_tail(&ic->node, &cont->containers);
159                 spin_unlock(&cont->containers_lock);
160         }
161         up(&attribute_container_mutex);
162 }
163
164 /**
165  * attribute_container_remove_device - make device eligible for removal.
166  *
167  * @dev:  The generic device
168  * @fn:   A function to call to remove the device
169  *
170  * This routine triggers device removal.  If fn is NULL, then it is
171  * simply done via class_device_unregister (note that if something
172  * still has a reference to the classdev, then the memory occupied
173  * will not be freed until the classdev is released).  If you want a
174  * two phase release: remove from visibility and then delete the
175  * device, then you should use this routine with a fn that calls
176  * class_device_del() and then use
177  * attribute_container_device_trigger() to do the final put on the
178  * classdev.
179  */
180 void
181 attribute_container_remove_device(struct device *dev,
182                                   void (*fn)(struct attribute_container *,
183                                              struct device *,
184                                              struct class_device *))
185 {
186         struct attribute_container *cont;
187
188         down(&attribute_container_mutex);
189         list_for_each_entry(cont, &attribute_container_list, node) {
190                 struct internal_container *ic, *tmp;
191
192                 if (attribute_container_no_classdevs(cont))
193                         continue;
194
195                 if (!cont->match(cont, dev))
196                         continue;
197                 spin_lock(&cont->containers_lock);
198                 list_for_each_entry_safe(ic, tmp, &cont->containers, node) {
199                         if (dev != ic->classdev.dev)
200                                 continue;
201                         list_del(&ic->node);
202                         if (fn)
203                                 fn(cont, dev, &ic->classdev);
204                         else {
205                                 attribute_container_remove_attrs(&ic->classdev);
206                                 class_device_unregister(&ic->classdev);
207                         }
208                 }
209                 spin_unlock(&cont->containers_lock);
210         }
211         up(&attribute_container_mutex);
212 }
213 EXPORT_SYMBOL_GPL(attribute_container_remove_device);
214
215 /**
216  * attribute_container_device_trigger - execute a trigger for each matching classdev
217  *
218  * @dev:  The generic device to run the trigger for
219  * @fn    the function to execute for each classdev.
220  *
221  * This funcion is for executing a trigger when you need to know both
222  * the container and the classdev.  If you only care about the
223  * container, then use attribute_container_trigger() instead.
224  */
225 void
226 attribute_container_device_trigger(struct device *dev, 
227                                    int (*fn)(struct attribute_container *,
228                                              struct device *,
229                                              struct class_device *))
230 {
231         struct attribute_container *cont;
232
233         down(&attribute_container_mutex);
234         list_for_each_entry(cont, &attribute_container_list, node) {
235                 struct internal_container *ic, *tmp;
236
237                 if (!cont->match(cont, dev))
238                         continue;
239
240                 spin_lock(&cont->containers_lock);
241                 list_for_each_entry_safe(ic, tmp, &cont->containers, node) {
242                         if (dev == ic->classdev.dev)
243                                 fn(cont, dev, &ic->classdev);
244                 }
245                 spin_unlock(&cont->containers_lock);
246         }
247         up(&attribute_container_mutex);
248 }
249 EXPORT_SYMBOL_GPL(attribute_container_device_trigger);
250
251 /**
252  * attribute_container_trigger - trigger a function for each matching container
253  *
254  * @dev:  The generic device to activate the trigger for
255  * @fn:   the function to trigger
256  *
257  * This routine triggers a function that only needs to know the
258  * matching containers (not the classdev) associated with a device.
259  * It is more lightweight than attribute_container_device_trigger, so
260  * should be used in preference unless the triggering function
261  * actually needs to know the classdev.
262  */
263 void
264 attribute_container_trigger(struct device *dev,
265                             int (*fn)(struct attribute_container *,
266                                       struct device *))
267 {
268         struct attribute_container *cont;
269
270         down(&attribute_container_mutex);
271         list_for_each_entry(cont, &attribute_container_list, node) {
272                 if (cont->match(cont, dev))
273                         fn(cont, dev);
274         }
275         up(&attribute_container_mutex);
276 }
277 EXPORT_SYMBOL_GPL(attribute_container_trigger);
278
279 /**
280  * attribute_container_add_attrs - add attributes
281  *
282  * @classdev: The class device
283  *
284  * This simply creates all the class device sysfs files from the
285  * attributes listed in the container
286  */
287 int
288 attribute_container_add_attrs(struct class_device *classdev)
289 {
290         struct attribute_container *cont =
291                 attribute_container_classdev_to_container(classdev);
292         struct class_device_attribute **attrs = cont->attrs;
293         int i, error;
294
295         if (!attrs)
296                 return 0;
297
298         for (i = 0; attrs[i]; i++) {
299                 error = class_device_create_file(classdev, attrs[i]);
300                 if (error)
301                         return error;
302         }
303
304         return 0;
305 }
306 EXPORT_SYMBOL_GPL(attribute_container_add_attrs);
307
308 /**
309  * attribute_container_add_class_device - same function as class_device_add
310  *
311  * @classdev:   the class device to add
312  *
313  * This performs essentially the same function as class_device_add except for
314  * attribute containers, namely add the classdev to the system and then
315  * create the attribute files
316  */
317 int
318 attribute_container_add_class_device(struct class_device *classdev)
319 {
320         int error = class_device_add(classdev);
321         if (error)
322                 return error;
323         return attribute_container_add_attrs(classdev);
324 }
325 EXPORT_SYMBOL_GPL(attribute_container_add_class_device);
326
327 /**
328  * attribute_container_add_class_device_adapter - simple adapter for triggers
329  *
330  * This function is identical to attribute_container_add_class_device except
331  * that it is designed to be called from the triggers
332  */
333 int
334 attribute_container_add_class_device_adapter(struct attribute_container *cont,
335                                              struct device *dev,
336                                              struct class_device *classdev)
337 {
338         return attribute_container_add_class_device(classdev);
339 }
340 EXPORT_SYMBOL_GPL(attribute_container_add_class_device_adapter);
341
342 /**
343  * attribute_container_remove_attrs - remove any attribute files
344  *
345  * @classdev: The class device to remove the files from
346  *
347  */
348 void
349 attribute_container_remove_attrs(struct class_device *classdev)
350 {
351         struct attribute_container *cont =
352                 attribute_container_classdev_to_container(classdev);
353         struct class_device_attribute **attrs = cont->attrs;
354         int i;
355
356         if (!attrs)
357                 return;
358
359         for (i = 0; attrs[i]; i++)
360                 class_device_remove_file(classdev, attrs[i]);
361 }
362 EXPORT_SYMBOL_GPL(attribute_container_remove_attrs);
363
364 /**
365  * attribute_container_class_device_del - equivalent of class_device_del
366  *
367  * @classdev: the class device
368  *
369  * This function simply removes all the attribute files and then calls
370  * class_device_del.
371  */
372 void
373 attribute_container_class_device_del(struct class_device *classdev)
374 {
375         attribute_container_remove_attrs(classdev);
376         class_device_del(classdev);
377 }
378 EXPORT_SYMBOL_GPL(attribute_container_class_device_del);
379
380 /**
381  * attribute_container_find_class_device - find the corresponding class_device
382  *
383  * @cont:       the container
384  * @dev:        the generic device
385  *
386  * Looks up the device in the container's list of class devices and returns
387  * the corresponding class_device.
388  */
389 struct class_device *
390 attribute_container_find_class_device(struct attribute_container *cont,
391                                       struct device *dev)
392 {
393         struct class_device *cdev = NULL;
394         struct internal_container *ic;
395
396         spin_lock(&cont->containers_lock);
397         list_for_each_entry(ic, &cont->containers, node) {
398                 if (ic->classdev.dev == dev) {
399                         cdev = &ic->classdev;
400                         break;
401                 }
402         }
403         spin_unlock(&cont->containers_lock);
404
405         return cdev;
406 }
407 EXPORT_SYMBOL_GPL(attribute_container_find_class_device);
408
409 int __init
410 attribute_container_init(void)
411 {
412         INIT_LIST_HEAD(&attribute_container_list);
413         return 0;
414 }