[SCSI] correct transport class abstraction to work outside SCSI
[linux-2.6/libata-dev.git] / drivers / base / attribute_container.c
blob62c093db11e6e853c7b8d5c22ca1603f904aea07
1 /*
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 list_head node;
26 struct attribute_container *cont;
27 struct class_device classdev;
30 /**
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);
42 return ic->cont;
44 EXPORT_SYMBOL_GPL(attribute_container_classdev_to_container);
46 static struct list_head attribute_container_list;
48 static DECLARE_MUTEX(attribute_container_mutex);
50 /**
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.
56 int
57 attribute_container_register(struct attribute_container *cont)
59 INIT_LIST_HEAD(&cont->node);
60 INIT_LIST_HEAD(&cont->containers);
61 spin_lock_init(&cont->containers_lock);
63 down(&attribute_container_mutex);
64 list_add_tail(&cont->node, &attribute_container_list);
65 up(&attribute_container_mutex);
67 return 0;
69 EXPORT_SYMBOL_GPL(attribute_container_register);
71 /**
72 * attribute_container_unregister - remove a container registration
74 * @cont: previously registered container to remove
76 int
77 attribute_container_unregister(struct attribute_container *cont)
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;
92 EXPORT_SYMBOL_GPL(attribute_container_unregister);
94 /* private function used as class release */
95 static void attribute_container_release(struct class_device *classdev)
97 struct internal_container *ic
98 = container_of(classdev, struct internal_container, classdev);
99 struct device *dev = classdev->dev;
101 kfree(ic);
102 put_device(dev);
106 * attribute_container_add_device - see if any container is interested in dev
108 * @dev: device to add attributes to
109 * @fn: function to trigger addition of class device.
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.
123 void
124 attribute_container_add_device(struct device *dev,
125 int (*fn)(struct attribute_container *,
126 struct device *,
127 struct class_device *))
129 struct attribute_container *cont;
131 down(&attribute_container_mutex);
132 list_for_each_entry(cont, &attribute_container_list, node) {
133 struct internal_container *ic;
135 if (attribute_container_no_classdevs(cont))
136 continue;
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;
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);
161 up(&attribute_container_mutex);
165 * attribute_container_remove_device - make device eligible for removal.
167 * @dev: The generic device
168 * @fn: A function to call to remove the device
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.
180 void
181 attribute_container_remove_device(struct device *dev,
182 void (*fn)(struct attribute_container *,
183 struct device *,
184 struct class_device *))
186 struct attribute_container *cont;
188 down(&attribute_container_mutex);
189 list_for_each_entry(cont, &attribute_container_list, node) {
190 struct internal_container *ic, *tmp;
192 if (attribute_container_no_classdevs(cont))
193 continue;
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);
209 spin_unlock(&cont->containers_lock);
211 up(&attribute_container_mutex);
213 EXPORT_SYMBOL_GPL(attribute_container_remove_device);
216 * attribute_container_device_trigger - execute a trigger for each matching classdev
218 * @dev: The generic device to run the trigger for
219 * @fn the function to execute for each classdev.
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.
225 void
226 attribute_container_device_trigger(struct device *dev,
227 int (*fn)(struct attribute_container *,
228 struct device *,
229 struct class_device *))
231 struct attribute_container *cont;
233 down(&attribute_container_mutex);
234 list_for_each_entry(cont, &attribute_container_list, node) {
235 struct internal_container *ic, *tmp;
237 if (!cont->match(cont, dev))
238 continue;
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);
245 spin_unlock(&cont->containers_lock);
247 up(&attribute_container_mutex);
249 EXPORT_SYMBOL_GPL(attribute_container_device_trigger);
252 * attribute_container_trigger - trigger a function for each matching container
254 * @dev: The generic device to activate the trigger for
255 * @fn: the function to trigger
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.
263 void
264 attribute_container_trigger(struct device *dev,
265 int (*fn)(struct attribute_container *,
266 struct device *))
268 struct attribute_container *cont;
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);
275 up(&attribute_container_mutex);
277 EXPORT_SYMBOL_GPL(attribute_container_trigger);
280 * attribute_container_add_attrs - add attributes
282 * @classdev: The class device
284 * This simply creates all the class device sysfs files from the
285 * attributes listed in the container
288 attribute_container_add_attrs(struct class_device *classdev)
290 struct attribute_container *cont =
291 attribute_container_classdev_to_container(classdev);
292 struct class_device_attribute **attrs = cont->attrs;
293 int i, error;
295 if (!attrs)
296 return 0;
298 for (i = 0; attrs[i]; i++) {
299 error = class_device_create_file(classdev, attrs[i]);
300 if (error)
301 return error;
304 return 0;
306 EXPORT_SYMBOL_GPL(attribute_container_add_attrs);
309 * attribute_container_add_class_device - same function as class_device_add
311 * @classdev: the class device to add
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
318 attribute_container_add_class_device(struct class_device *classdev)
320 int error = class_device_add(classdev);
321 if (error)
322 return error;
323 return attribute_container_add_attrs(classdev);
325 EXPORT_SYMBOL_GPL(attribute_container_add_class_device);
328 * attribute_container_add_class_device_adapter - simple adapter for triggers
330 * This function is identical to attribute_container_add_class_device except
331 * that it is designed to be called from the triggers
334 attribute_container_add_class_device_adapter(struct attribute_container *cont,
335 struct device *dev,
336 struct class_device *classdev)
338 return attribute_container_add_class_device(classdev);
340 EXPORT_SYMBOL_GPL(attribute_container_add_class_device_adapter);
343 * attribute_container_remove_attrs - remove any attribute files
345 * @classdev: The class device to remove the files from
348 void
349 attribute_container_remove_attrs(struct class_device *classdev)
351 struct attribute_container *cont =
352 attribute_container_classdev_to_container(classdev);
353 struct class_device_attribute **attrs = cont->attrs;
354 int i;
356 if (!attrs)
357 return;
359 for (i = 0; attrs[i]; i++)
360 class_device_remove_file(classdev, attrs[i]);
362 EXPORT_SYMBOL_GPL(attribute_container_remove_attrs);
365 * attribute_container_class_device_del - equivalent of class_device_del
367 * @classdev: the class device
369 * This function simply removes all the attribute files and then calls
370 * class_device_del.
372 void
373 attribute_container_class_device_del(struct class_device *classdev)
375 attribute_container_remove_attrs(classdev);
376 class_device_del(classdev);
378 EXPORT_SYMBOL_GPL(attribute_container_class_device_del);
381 * attribute_container_find_class_device - find the corresponding class_device
383 * @cont: the container
384 * @dev: the generic device
386 * Looks up the device in the container's list of class devices and returns
387 * the corresponding class_device.
389 struct class_device *
390 attribute_container_find_class_device(struct attribute_container *cont,
391 struct device *dev)
393 struct class_device *cdev = NULL;
394 struct internal_container *ic;
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;
403 spin_unlock(&cont->containers_lock);
405 return cdev;
407 EXPORT_SYMBOL_GPL(attribute_container_find_class_device);
409 int __init
410 attribute_container_init(void)
412 INIT_LIST_HEAD(&attribute_container_list);
413 return 0;