]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blob - drivers/base/attribute_container.c
[SCSI] Revert "final klist fixes"
[mirror_ubuntu-kernels.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 klist_node 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 klist_init(&cont->containers);
61
62 down(&attribute_container_mutex);
63 list_add_tail(&cont->node, &attribute_container_list);
64 up(&attribute_container_mutex);
65
66 return 0;
67 }
68 EXPORT_SYMBOL_GPL(attribute_container_register);
69
70 /**
71 * attribute_container_unregister - remove a container registration
72 *
73 * @cont: previously registered container to remove
74 */
75 int
76 attribute_container_unregister(struct attribute_container *cont)
77 {
78 int retval = -EBUSY;
79 down(&attribute_container_mutex);
80 spin_lock(&cont->containers.k_lock);
81 if (!list_empty(&cont->containers.k_list))
82 goto out;
83 retval = 0;
84 list_del(&cont->node);
85 out:
86 spin_unlock(&cont->containers.k_lock);
87 up(&attribute_container_mutex);
88 return retval;
89
90 }
91 EXPORT_SYMBOL_GPL(attribute_container_unregister);
92
93 /* private function used as class release */
94 static void attribute_container_release(struct class_device *classdev)
95 {
96 struct internal_container *ic
97 = container_of(classdev, struct internal_container, classdev);
98 struct device *dev = classdev->dev;
99
100 kfree(ic);
101 put_device(dev);
102 }
103
104 /**
105 * attribute_container_add_device - see if any container is interested in dev
106 *
107 * @dev: device to add attributes to
108 * @fn: function to trigger addition of class device.
109 *
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.
121 */
122 void
123 attribute_container_add_device(struct device *dev,
124 int (*fn)(struct attribute_container *,
125 struct device *,
126 struct class_device *))
127 {
128 struct attribute_container *cont;
129
130 down(&attribute_container_mutex);
131 list_for_each_entry(cont, &attribute_container_list, node) {
132 struct internal_container *ic;
133
134 if (attribute_container_no_classdevs(cont))
135 continue;
136
137 if (!cont->match(cont, dev))
138 continue;
139 ic = kmalloc(sizeof(struct internal_container), GFP_KERNEL);
140 if (!ic) {
141 dev_printk(KERN_ERR, dev, "failed to allocate class container\n");
142 continue;
143 }
144 memset(ic, 0, sizeof(struct internal_container));
145 ic->cont = cont;
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);
151 if (fn)
152 fn(cont, dev, &ic->classdev);
153 else
154 attribute_container_add_class_device(&ic->classdev);
155 klist_add_tail(&ic->node, &cont->containers);
156 }
157 up(&attribute_container_mutex);
158 }
159
160 /* FIXME: can't break out of this unless klist_iter_exit is also
161 * called before doing the break
162 */
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);\
168 }) ) != NULL; )
169
170
171 /**
172 * attribute_container_remove_device - make device eligible for removal.
173 *
174 * @dev: The generic device
175 * @fn: A function to call to remove the device
176 *
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
185 * classdev.
186 */
187 void
188 attribute_container_remove_device(struct device *dev,
189 void (*fn)(struct attribute_container *,
190 struct device *,
191 struct class_device *))
192 {
193 struct attribute_container *cont;
194
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;
199
200 if (attribute_container_no_classdevs(cont))
201 continue;
202
203 if (!cont->match(cont, dev))
204 continue;
205
206 klist_for_each_entry(ic, &cont->containers, node, &iter) {
207 if (dev != ic->classdev.dev)
208 continue;
209 klist_remove(&ic->node);
210 if (fn)
211 fn(cont, dev, &ic->classdev);
212 else {
213 attribute_container_remove_attrs(&ic->classdev);
214 class_device_unregister(&ic->classdev);
215 }
216 }
217 }
218 up(&attribute_container_mutex);
219 }
220 EXPORT_SYMBOL_GPL(attribute_container_remove_device);
221
222 /**
223 * attribute_container_device_trigger - execute a trigger for each matching classdev
224 *
225 * @dev: The generic device to run the trigger for
226 * @fn the function to execute for each classdev.
227 *
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.
231 */
232 void
233 attribute_container_device_trigger(struct device *dev,
234 int (*fn)(struct attribute_container *,
235 struct device *,
236 struct class_device *))
237 {
238 struct attribute_container *cont;
239
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;
244
245 if (!cont->match(cont, dev))
246 continue;
247
248 if (attribute_container_no_classdevs(cont)) {
249 fn(cont, dev, NULL);
250 continue;
251 }
252
253 klist_for_each_entry(ic, &cont->containers, node, &iter) {
254 if (dev == ic->classdev.dev)
255 fn(cont, dev, &ic->classdev);
256 }
257 }
258 up(&attribute_container_mutex);
259 }
260 EXPORT_SYMBOL_GPL(attribute_container_device_trigger);
261
262 /**
263 * attribute_container_trigger - trigger a function for each matching container
264 *
265 * @dev: The generic device to activate the trigger for
266 * @fn: the function to trigger
267 *
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.
273 */
274 void
275 attribute_container_trigger(struct device *dev,
276 int (*fn)(struct attribute_container *,
277 struct device *))
278 {
279 struct attribute_container *cont;
280
281 down(&attribute_container_mutex);
282 list_for_each_entry(cont, &attribute_container_list, node) {
283 if (cont->match(cont, dev))
284 fn(cont, dev);
285 }
286 up(&attribute_container_mutex);
287 }
288 EXPORT_SYMBOL_GPL(attribute_container_trigger);
289
290 /**
291 * attribute_container_add_attrs - add attributes
292 *
293 * @classdev: The class device
294 *
295 * This simply creates all the class device sysfs files from the
296 * attributes listed in the container
297 */
298 int
299 attribute_container_add_attrs(struct class_device *classdev)
300 {
301 struct attribute_container *cont =
302 attribute_container_classdev_to_container(classdev);
303 struct class_device_attribute **attrs = cont->attrs;
304 int i, error;
305
306 if (!attrs)
307 return 0;
308
309 for (i = 0; attrs[i]; i++) {
310 error = class_device_create_file(classdev, attrs[i]);
311 if (error)
312 return error;
313 }
314
315 return 0;
316 }
317 EXPORT_SYMBOL_GPL(attribute_container_add_attrs);
318
319 /**
320 * attribute_container_add_class_device - same function as class_device_add
321 *
322 * @classdev: the class device to add
323 *
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
327 */
328 int
329 attribute_container_add_class_device(struct class_device *classdev)
330 {
331 int error = class_device_add(classdev);
332 if (error)
333 return error;
334 return attribute_container_add_attrs(classdev);
335 }
336 EXPORT_SYMBOL_GPL(attribute_container_add_class_device);
337
338 /**
339 * attribute_container_add_class_device_adapter - simple adapter for triggers
340 *
341 * This function is identical to attribute_container_add_class_device except
342 * that it is designed to be called from the triggers
343 */
344 int
345 attribute_container_add_class_device_adapter(struct attribute_container *cont,
346 struct device *dev,
347 struct class_device *classdev)
348 {
349 return attribute_container_add_class_device(classdev);
350 }
351 EXPORT_SYMBOL_GPL(attribute_container_add_class_device_adapter);
352
353 /**
354 * attribute_container_remove_attrs - remove any attribute files
355 *
356 * @classdev: The class device to remove the files from
357 *
358 */
359 void
360 attribute_container_remove_attrs(struct class_device *classdev)
361 {
362 struct attribute_container *cont =
363 attribute_container_classdev_to_container(classdev);
364 struct class_device_attribute **attrs = cont->attrs;
365 int i;
366
367 if (!attrs)
368 return;
369
370 for (i = 0; attrs[i]; i++)
371 class_device_remove_file(classdev, attrs[i]);
372 }
373 EXPORT_SYMBOL_GPL(attribute_container_remove_attrs);
374
375 /**
376 * attribute_container_class_device_del - equivalent of class_device_del
377 *
378 * @classdev: the class device
379 *
380 * This function simply removes all the attribute files and then calls
381 * class_device_del.
382 */
383 void
384 attribute_container_class_device_del(struct class_device *classdev)
385 {
386 attribute_container_remove_attrs(classdev);
387 class_device_del(classdev);
388 }
389 EXPORT_SYMBOL_GPL(attribute_container_class_device_del);
390
391 /**
392 * attribute_container_find_class_device - find the corresponding class_device
393 *
394 * @cont: the container
395 * @dev: the generic device
396 *
397 * Looks up the device in the container's list of class devices and returns
398 * the corresponding class_device.
399 */
400 struct class_device *
401 attribute_container_find_class_device(struct attribute_container *cont,
402 struct device *dev)
403 {
404 struct class_device *cdev = NULL;
405 struct internal_container *ic;
406 struct klist_iter iter;
407
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);
413 break;
414 }
415 }
416
417 return cdev;
418 }
419 EXPORT_SYMBOL_GPL(attribute_container_find_class_device);
420
421 int __init
422 attribute_container_init(void)
423 {
424 INIT_LIST_HEAD(&attribute_container_list);
425 return 0;
426 }