blob: 6b2eb6f39b4dea6ec809acb715cf1c2ab1fc0b45 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
24struct internal_container {
James Bottomley53c165e2005-08-22 10:06:19 -050025 struct klist_node node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 struct attribute_container *cont;
27 struct class_device classdev;
28};
29
Linus Torvaldscaf39e82005-09-07 18:44:33 -070030static void internal_container_klist_get(struct klist_node *n)
31{
32 struct internal_container *ic =
33 container_of(n, struct internal_container, node);
34 class_device_get(&ic->classdev);
35}
36
37static void internal_container_klist_put(struct klist_node *n)
38{
39 struct internal_container *ic =
40 container_of(n, struct internal_container, node);
41 class_device_put(&ic->classdev);
42}
43
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/**
46 * attribute_container_classdev_to_container - given a classdev, return the container
47 *
48 * @classdev: the class device created by attribute_container_add_device.
49 *
50 * Returns the container associated with this classdev.
51 */
52struct attribute_container *
53attribute_container_classdev_to_container(struct class_device *classdev)
54{
55 struct internal_container *ic =
56 container_of(classdev, struct internal_container, classdev);
57 return ic->cont;
58}
59EXPORT_SYMBOL_GPL(attribute_container_classdev_to_container);
60
61static struct list_head attribute_container_list;
62
63static DECLARE_MUTEX(attribute_container_mutex);
64
65/**
66 * attribute_container_register - register an attribute container
67 *
68 * @cont: The container to register. This must be allocated by the
69 * callee and should also be zeroed by it.
70 */
71int
72attribute_container_register(struct attribute_container *cont)
73{
74 INIT_LIST_HEAD(&cont->node);
Linus Torvaldscaf39e82005-09-07 18:44:33 -070075 klist_init(&cont->containers,internal_container_klist_get,
76 internal_container_klist_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 down(&attribute_container_mutex);
79 list_add_tail(&cont->node, &attribute_container_list);
80 up(&attribute_container_mutex);
81
82 return 0;
83}
84EXPORT_SYMBOL_GPL(attribute_container_register);
85
86/**
87 * attribute_container_unregister - remove a container registration
88 *
89 * @cont: previously registered container to remove
90 */
91int
92attribute_container_unregister(struct attribute_container *cont)
93{
94 int retval = -EBUSY;
95 down(&attribute_container_mutex);
James Bottomley53c165e2005-08-22 10:06:19 -050096 spin_lock(&cont->containers.k_lock);
97 if (!list_empty(&cont->containers.k_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 goto out;
99 retval = 0;
100 list_del(&cont->node);
101 out:
James Bottomley53c165e2005-08-22 10:06:19 -0500102 spin_unlock(&cont->containers.k_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 up(&attribute_container_mutex);
104 return retval;
105
106}
107EXPORT_SYMBOL_GPL(attribute_container_unregister);
108
109/* private function used as class release */
110static void attribute_container_release(struct class_device *classdev)
111{
112 struct internal_container *ic
113 = container_of(classdev, struct internal_container, classdev);
114 struct device *dev = classdev->dev;
115
116 kfree(ic);
117 put_device(dev);
118}
119
120/**
121 * attribute_container_add_device - see if any container is interested in dev
122 *
123 * @dev: device to add attributes to
124 * @fn: function to trigger addition of class device.
125 *
126 * This function allocates storage for the class device(s) to be
127 * attached to dev (one for each matching attribute_container). If no
128 * fn is provided, the code will simply register the class device via
129 * class_device_add. If a function is provided, it is expected to add
130 * the class device at the appropriate time. One of the things that
131 * might be necessary is to allocate and initialise the classdev and
132 * then add it a later time. To do this, call this routine for
133 * allocation and initialisation and then use
134 * attribute_container_device_trigger() to call class_device_add() on
135 * it. Note: after this, the class device contains a reference to dev
136 * which is not relinquished until the release of the classdev.
137 */
138void
139attribute_container_add_device(struct device *dev,
140 int (*fn)(struct attribute_container *,
141 struct device *,
142 struct class_device *))
143{
144 struct attribute_container *cont;
145
146 down(&attribute_container_mutex);
147 list_for_each_entry(cont, &attribute_container_list, node) {
148 struct internal_container *ic;
149
150 if (attribute_container_no_classdevs(cont))
151 continue;
152
153 if (!cont->match(cont, dev))
154 continue;
Jiri Slaby4aed0642005-09-13 01:25:01 -0700155
156 ic = kzalloc(sizeof(*ic), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (!ic) {
158 dev_printk(KERN_ERR, dev, "failed to allocate class container\n");
159 continue;
160 }
Jiri Slaby4aed0642005-09-13 01:25:01 -0700161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 ic->cont = cont;
163 class_device_initialize(&ic->classdev);
164 ic->classdev.dev = get_device(dev);
165 ic->classdev.class = cont->class;
166 cont->class->release = attribute_container_release;
167 strcpy(ic->classdev.class_id, dev->bus_id);
168 if (fn)
169 fn(cont, dev, &ic->classdev);
170 else
171 attribute_container_add_class_device(&ic->classdev);
James Bottomley53c165e2005-08-22 10:06:19 -0500172 klist_add_tail(&ic->node, &cont->containers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174 up(&attribute_container_mutex);
175}
176
James Bottomley53c165e2005-08-22 10:06:19 -0500177/* FIXME: can't break out of this unless klist_iter_exit is also
178 * called before doing the break
179 */
180#define klist_for_each_entry(pos, head, member, iter) \
181 for (klist_iter_init(head, iter); (pos = ({ \
182 struct klist_node *n = klist_next(iter); \
Linus Torvaldscaf39e82005-09-07 18:44:33 -0700183 n ? container_of(n, typeof(*pos), member) : \
184 ({ klist_iter_exit(iter) ; NULL; }); \
James Bottomley53c165e2005-08-22 10:06:19 -0500185 }) ) != NULL; )
186
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/**
189 * attribute_container_remove_device - make device eligible for removal.
190 *
191 * @dev: The generic device
192 * @fn: A function to call to remove the device
193 *
194 * This routine triggers device removal. If fn is NULL, then it is
195 * simply done via class_device_unregister (note that if something
196 * still has a reference to the classdev, then the memory occupied
197 * will not be freed until the classdev is released). If you want a
198 * two phase release: remove from visibility and then delete the
199 * device, then you should use this routine with a fn that calls
200 * class_device_del() and then use
201 * attribute_container_device_trigger() to do the final put on the
202 * classdev.
203 */
204void
205attribute_container_remove_device(struct device *dev,
206 void (*fn)(struct attribute_container *,
207 struct device *,
208 struct class_device *))
209{
210 struct attribute_container *cont;
211
212 down(&attribute_container_mutex);
213 list_for_each_entry(cont, &attribute_container_list, node) {
James Bottomley53c165e2005-08-22 10:06:19 -0500214 struct internal_container *ic;
215 struct klist_iter iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 if (attribute_container_no_classdevs(cont))
218 continue;
219
220 if (!cont->match(cont, dev))
221 continue;
James Bottomley53c165e2005-08-22 10:06:19 -0500222
223 klist_for_each_entry(ic, &cont->containers, node, &iter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (dev != ic->classdev.dev)
225 continue;
Linus Torvaldscaf39e82005-09-07 18:44:33 -0700226 klist_del(&ic->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (fn)
228 fn(cont, dev, &ic->classdev);
229 else {
230 attribute_container_remove_attrs(&ic->classdev);
231 class_device_unregister(&ic->classdev);
232 }
233 }
234 }
235 up(&attribute_container_mutex);
236}
237EXPORT_SYMBOL_GPL(attribute_container_remove_device);
238
239/**
240 * attribute_container_device_trigger - execute a trigger for each matching classdev
241 *
242 * @dev: The generic device to run the trigger for
243 * @fn the function to execute for each classdev.
244 *
245 * This funcion is for executing a trigger when you need to know both
246 * the container and the classdev. If you only care about the
247 * container, then use attribute_container_trigger() instead.
248 */
249void
250attribute_container_device_trigger(struct device *dev,
251 int (*fn)(struct attribute_container *,
252 struct device *,
253 struct class_device *))
254{
255 struct attribute_container *cont;
256
257 down(&attribute_container_mutex);
258 list_for_each_entry(cont, &attribute_container_list, node) {
James Bottomley53c165e2005-08-22 10:06:19 -0500259 struct internal_container *ic;
260 struct klist_iter iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 if (!cont->match(cont, dev))
263 continue;
264
James Bottomleyebd8bb72005-08-15 16:13:19 -0500265 if (attribute_container_no_classdevs(cont)) {
266 fn(cont, dev, NULL);
267 continue;
268 }
269
James Bottomley53c165e2005-08-22 10:06:19 -0500270 klist_for_each_entry(ic, &cont->containers, node, &iter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (dev == ic->classdev.dev)
272 fn(cont, dev, &ic->classdev);
273 }
274 }
275 up(&attribute_container_mutex);
276}
277EXPORT_SYMBOL_GPL(attribute_container_device_trigger);
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
298 down(&attribute_container_mutex);
299 list_for_each_entry(cont, &attribute_container_list, node) {
300 if (cont->match(cont, dev))
301 fn(cont, dev);
302 }
303 up(&attribute_container_mutex);
304}
305EXPORT_SYMBOL_GPL(attribute_container_trigger);
306
307/**
308 * attribute_container_add_attrs - add attributes
309 *
310 * @classdev: The class device
311 *
312 * This simply creates all the class device sysfs files from the
313 * attributes listed in the container
314 */
315int
316attribute_container_add_attrs(struct class_device *classdev)
317{
318 struct attribute_container *cont =
319 attribute_container_classdev_to_container(classdev);
320 struct class_device_attribute **attrs = cont->attrs;
321 int i, error;
322
323 if (!attrs)
324 return 0;
325
326 for (i = 0; attrs[i]; i++) {
327 error = class_device_create_file(classdev, attrs[i]);
328 if (error)
329 return error;
330 }
331
332 return 0;
333}
334EXPORT_SYMBOL_GPL(attribute_container_add_attrs);
335
336/**
337 * attribute_container_add_class_device - same function as class_device_add
338 *
339 * @classdev: the class device to add
340 *
341 * This performs essentially the same function as class_device_add except for
342 * attribute containers, namely add the classdev to the system and then
343 * create the attribute files
344 */
345int
346attribute_container_add_class_device(struct class_device *classdev)
347{
348 int error = class_device_add(classdev);
349 if (error)
350 return error;
351 return attribute_container_add_attrs(classdev);
352}
353EXPORT_SYMBOL_GPL(attribute_container_add_class_device);
354
355/**
356 * attribute_container_add_class_device_adapter - simple adapter for triggers
357 *
358 * This function is identical to attribute_container_add_class_device except
359 * that it is designed to be called from the triggers
360 */
361int
362attribute_container_add_class_device_adapter(struct attribute_container *cont,
363 struct device *dev,
364 struct class_device *classdev)
365{
366 return attribute_container_add_class_device(classdev);
367}
368EXPORT_SYMBOL_GPL(attribute_container_add_class_device_adapter);
369
370/**
371 * attribute_container_remove_attrs - remove any attribute files
372 *
373 * @classdev: The class device to remove the files from
374 *
375 */
376void
377attribute_container_remove_attrs(struct class_device *classdev)
378{
379 struct attribute_container *cont =
380 attribute_container_classdev_to_container(classdev);
381 struct class_device_attribute **attrs = cont->attrs;
382 int i;
383
384 if (!attrs)
385 return;
386
387 for (i = 0; attrs[i]; i++)
388 class_device_remove_file(classdev, attrs[i]);
389}
390EXPORT_SYMBOL_GPL(attribute_container_remove_attrs);
391
392/**
393 * attribute_container_class_device_del - equivalent of class_device_del
394 *
395 * @classdev: the class device
396 *
397 * This function simply removes all the attribute files and then calls
398 * class_device_del.
399 */
400void
401attribute_container_class_device_del(struct class_device *classdev)
402{
403 attribute_container_remove_attrs(classdev);
404 class_device_del(classdev);
405}
406EXPORT_SYMBOL_GPL(attribute_container_class_device_del);
407
James Bottomleyd0a7e572005-08-14 17:09:01 -0500408/**
409 * attribute_container_find_class_device - find the corresponding class_device
410 *
411 * @cont: the container
412 * @dev: the generic device
413 *
414 * Looks up the device in the container's list of class devices and returns
415 * the corresponding class_device.
416 */
417struct class_device *
418attribute_container_find_class_device(struct attribute_container *cont,
419 struct device *dev)
420{
421 struct class_device *cdev = NULL;
422 struct internal_container *ic;
James Bottomley53c165e2005-08-22 10:06:19 -0500423 struct klist_iter iter;
James Bottomleyd0a7e572005-08-14 17:09:01 -0500424
James Bottomley53c165e2005-08-22 10:06:19 -0500425 klist_for_each_entry(ic, &cont->containers, node, &iter) {
James Bottomleyd0a7e572005-08-14 17:09:01 -0500426 if (ic->classdev.dev == dev) {
427 cdev = &ic->classdev;
James Bottomley53c165e2005-08-22 10:06:19 -0500428 /* FIXME: must exit iterator then break */
429 klist_iter_exit(&iter);
James Bottomleyd0a7e572005-08-14 17:09:01 -0500430 break;
431 }
432 }
James Bottomleyd0a7e572005-08-14 17:09:01 -0500433
434 return cdev;
435}
436EXPORT_SYMBOL_GPL(attribute_container_find_class_device);
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438int __init
439attribute_container_init(void)
440{
441 INIT_LIST_HEAD(&attribute_container_list);
442 return 0;
443}