2 * attribute_container.c - implementation of a simple container for classes
4 * Copyright (c) 2005 - James Bottomley <James.Bottomley@steeleye.com>
6 * This file is licensed under GPLv2
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.
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>
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 klist_node node;
26 struct attribute_container *cont;
27 struct class_device classdev;
31 * attribute_container_classdev_to_container - given a classdev, return the container
33 * @classdev: the class device created by attribute_container_add_device.
35 * Returns the container associated with this classdev.
37 struct attribute_container *
38 attribute_container_classdev_to_container(struct class_device *classdev)
40 struct internal_container *ic =
41 container_of(classdev, struct internal_container, classdev);
44 EXPORT_SYMBOL_GPL(attribute_container_classdev_to_container);
46 static struct list_head attribute_container_list;
48 static DECLARE_MUTEX(attribute_container_mutex);
51 * attribute_container_register - register an attribute container
53 * @cont: The container to register. This must be allocated by the
54 * callee and should also be zeroed by it.
57 attribute_container_register(struct attribute_container *cont)
59 INIT_LIST_HEAD(&cont->node);
60 klist_init(&cont->containers);
62 down(&attribute_container_mutex);
63 list_add_tail(&cont->node, &attribute_container_list);
64 up(&attribute_container_mutex);
68 EXPORT_SYMBOL_GPL(attribute_container_register);
71 * attribute_container_unregister - remove a container registration
73 * @cont: previously registered container to remove
76 attribute_container_unregister(struct attribute_container *cont)
79 down(&attribute_container_mutex);
80 spin_lock(&cont->containers.k_lock);
81 if (!list_empty(&cont->containers.k_list))
84 list_del(&cont->node);
86 spin_unlock(&cont->containers.k_lock);
87 up(&attribute_container_mutex);
91 EXPORT_SYMBOL_GPL(attribute_container_unregister);
93 /* private function used as class release */
94 static void attribute_container_release(struct class_device *classdev)
96 struct internal_container *ic
97 = container_of(classdev, struct internal_container, classdev);
98 struct device *dev = classdev->dev;
105 * attribute_container_add_device - see if any container is interested in dev
107 * @dev: device to add attributes to
108 * @fn: function to trigger addition of class device.
110 * This function allocates storage for the class device(s) to be
111 * attached to dev (one for each matching attribute_container). If no
112 * fn is provided, the code will simply register the class device via
113 * class_device_add. If a function is provided, it is expected to add
114 * the class device at the appropriate time. One of the things that
115 * might be necessary is to allocate and initialise the classdev and
116 * then add it a later time. To do this, call this routine for
117 * allocation and initialisation and then use
118 * attribute_container_device_trigger() to call class_device_add() on
119 * it. Note: after this, the class device contains a reference to dev
120 * which is not relinquished until the release of the classdev.
123 attribute_container_add_device(struct device *dev,
124 int (*fn)(struct attribute_container *,
126 struct class_device *))
128 struct attribute_container *cont;
130 down(&attribute_container_mutex);
131 list_for_each_entry(cont, &attribute_container_list, node) {
132 struct internal_container *ic;
134 if (attribute_container_no_classdevs(cont))
137 if (!cont->match(cont, dev))
139 ic = kmalloc(sizeof(struct internal_container), GFP_KERNEL);
141 dev_printk(KERN_ERR, dev, "failed to allocate class container\n");
144 memset(ic, 0, sizeof(struct internal_container));
146 class_device_initialize(&ic->classdev);
147 ic->classdev.dev = get_device(dev);
148 ic->classdev.class = cont->class;
149 cont->class->release = attribute_container_release;
150 strcpy(ic->classdev.class_id, dev->bus_id);
152 fn(cont, dev, &ic->classdev);
154 attribute_container_add_class_device(&ic->classdev);
155 klist_add_tail(&ic->node, &cont->containers);
157 up(&attribute_container_mutex);
160 /* FIXME: can't break out of this unless klist_iter_exit is also
161 * called before doing the break
163 #define klist_for_each_entry(pos, head, member, iter) \
164 for (klist_iter_init(head, iter); (pos = ({ \
165 struct klist_node *n = klist_next(iter); \
166 n ? ({ klist_iter_exit(iter) ; NULL; }) : \
167 container_of(n, typeof(*pos), member);\
172 * attribute_container_remove_device - make device eligible for removal.
174 * @dev: The generic device
175 * @fn: A function to call to remove the device
177 * This routine triggers device removal. If fn is NULL, then it is
178 * simply done via class_device_unregister (note that if something
179 * still has a reference to the classdev, then the memory occupied
180 * will not be freed until the classdev is released). If you want a
181 * two phase release: remove from visibility and then delete the
182 * device, then you should use this routine with a fn that calls
183 * class_device_del() and then use
184 * attribute_container_device_trigger() to do the final put on the
188 attribute_container_remove_device(struct device *dev,
189 void (*fn)(struct attribute_container *,
191 struct class_device *))
193 struct attribute_container *cont;
195 down(&attribute_container_mutex);
196 list_for_each_entry(cont, &attribute_container_list, node) {
197 struct internal_container *ic;
198 struct klist_iter iter;
200 if (attribute_container_no_classdevs(cont))
203 if (!cont->match(cont, dev))
206 klist_for_each_entry(ic, &cont->containers, node, &iter) {
207 if (dev != ic->classdev.dev)
209 klist_remove(&ic->node);
211 fn(cont, dev, &ic->classdev);
213 attribute_container_remove_attrs(&ic->classdev);
214 class_device_unregister(&ic->classdev);
218 up(&attribute_container_mutex);
220 EXPORT_SYMBOL_GPL(attribute_container_remove_device);
223 * attribute_container_device_trigger - execute a trigger for each matching classdev
225 * @dev: The generic device to run the trigger for
226 * @fn the function to execute for each classdev.
228 * This funcion is for executing a trigger when you need to know both
229 * the container and the classdev. If you only care about the
230 * container, then use attribute_container_trigger() instead.
233 attribute_container_device_trigger(struct device *dev,
234 int (*fn)(struct attribute_container *,
236 struct class_device *))
238 struct attribute_container *cont;
240 down(&attribute_container_mutex);
241 list_for_each_entry(cont, &attribute_container_list, node) {
242 struct internal_container *ic;
243 struct klist_iter iter;
245 if (!cont->match(cont, dev))
248 if (attribute_container_no_classdevs(cont)) {
253 klist_for_each_entry(ic, &cont->containers, node, &iter) {
254 if (dev == ic->classdev.dev)
255 fn(cont, dev, &ic->classdev);
258 up(&attribute_container_mutex);
260 EXPORT_SYMBOL_GPL(attribute_container_device_trigger);
263 * attribute_container_trigger - trigger a function for each matching container
265 * @dev: The generic device to activate the trigger for
266 * @fn: the function to trigger
268 * This routine triggers a function that only needs to know the
269 * matching containers (not the classdev) associated with a device.
270 * It is more lightweight than attribute_container_device_trigger, so
271 * should be used in preference unless the triggering function
272 * actually needs to know the classdev.
275 attribute_container_trigger(struct device *dev,
276 int (*fn)(struct attribute_container *,
279 struct attribute_container *cont;
281 down(&attribute_container_mutex);
282 list_for_each_entry(cont, &attribute_container_list, node) {
283 if (cont->match(cont, dev))
286 up(&attribute_container_mutex);
288 EXPORT_SYMBOL_GPL(attribute_container_trigger);
291 * attribute_container_add_attrs - add attributes
293 * @classdev: The class device
295 * This simply creates all the class device sysfs files from the
296 * attributes listed in the container
299 attribute_container_add_attrs(struct class_device *classdev)
301 struct attribute_container *cont =
302 attribute_container_classdev_to_container(classdev);
303 struct class_device_attribute **attrs = cont->attrs;
309 for (i = 0; attrs[i]; i++) {
310 error = class_device_create_file(classdev, attrs[i]);
317 EXPORT_SYMBOL_GPL(attribute_container_add_attrs);
320 * attribute_container_add_class_device - same function as class_device_add
322 * @classdev: the class device to add
324 * This performs essentially the same function as class_device_add except for
325 * attribute containers, namely add the classdev to the system and then
326 * create the attribute files
329 attribute_container_add_class_device(struct class_device *classdev)
331 int error = class_device_add(classdev);
334 return attribute_container_add_attrs(classdev);
336 EXPORT_SYMBOL_GPL(attribute_container_add_class_device);
339 * attribute_container_add_class_device_adapter - simple adapter for triggers
341 * This function is identical to attribute_container_add_class_device except
342 * that it is designed to be called from the triggers
345 attribute_container_add_class_device_adapter(struct attribute_container *cont,
347 struct class_device *classdev)
349 return attribute_container_add_class_device(classdev);
351 EXPORT_SYMBOL_GPL(attribute_container_add_class_device_adapter);
354 * attribute_container_remove_attrs - remove any attribute files
356 * @classdev: The class device to remove the files from
360 attribute_container_remove_attrs(struct class_device *classdev)
362 struct attribute_container *cont =
363 attribute_container_classdev_to_container(classdev);
364 struct class_device_attribute **attrs = cont->attrs;
370 for (i = 0; attrs[i]; i++)
371 class_device_remove_file(classdev, attrs[i]);
373 EXPORT_SYMBOL_GPL(attribute_container_remove_attrs);
376 * attribute_container_class_device_del - equivalent of class_device_del
378 * @classdev: the class device
380 * This function simply removes all the attribute files and then calls
384 attribute_container_class_device_del(struct class_device *classdev)
386 attribute_container_remove_attrs(classdev);
387 class_device_del(classdev);
389 EXPORT_SYMBOL_GPL(attribute_container_class_device_del);
392 * attribute_container_find_class_device - find the corresponding class_device
394 * @cont: the container
395 * @dev: the generic device
397 * Looks up the device in the container's list of class devices and returns
398 * the corresponding class_device.
400 struct class_device *
401 attribute_container_find_class_device(struct attribute_container *cont,
404 struct class_device *cdev = NULL;
405 struct internal_container *ic;
406 struct klist_iter iter;
408 klist_for_each_entry(ic, &cont->containers, node, &iter) {
409 if (ic->classdev.dev == dev) {
410 cdev = &ic->classdev;
411 /* FIXME: must exit iterator then break */
412 klist_iter_exit(&iter);
419 EXPORT_SYMBOL_GPL(attribute_container_find_class_device);
422 attribute_container_init(void)
424 INIT_LIST_HEAD(&attribute_container_list);