blob: 6c0f49340eb2a98cf7a015f390833aede2615cb9 [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
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 */
37struct attribute_container *
38attribute_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}
44EXPORT_SYMBOL_GPL(attribute_container_classdev_to_container);
45
46static struct list_head attribute_container_list;
47
48static 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 */
56int
57attribute_container_register(struct attribute_container *cont)
58{
59 INIT_LIST_HEAD(&cont->node);
James Bottomley53c165e2005-08-22 10:06:19 -050060 klist_init(&cont->containers);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 down(&attribute_container_mutex);
63 list_add_tail(&cont->node, &attribute_container_list);
64 up(&attribute_container_mutex);
65
66 return 0;
67}
68EXPORT_SYMBOL_GPL(attribute_container_register);
69
70/**
71 * attribute_container_unregister - remove a container registration
72 *
73 * @cont: previously registered container to remove
74 */
75int
76attribute_container_unregister(struct attribute_container *cont)
77{
78 int retval = -EBUSY;
79 down(&attribute_container_mutex);
James Bottomley53c165e2005-08-22 10:06:19 -050080 spin_lock(&cont->containers.k_lock);
81 if (!list_empty(&cont->containers.k_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 goto out;
83 retval = 0;
84 list_del(&cont->node);
85 out:
James Bottomley53c165e2005-08-22 10:06:19 -050086 spin_unlock(&cont->containers.k_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 up(&attribute_container_mutex);
88 return retval;
89
90}
91EXPORT_SYMBOL_GPL(attribute_container_unregister);
92
93/* private function used as class release */
94static 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 */
122void
123attribute_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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 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);
James Bottomley53c165e2005-08-22 10:06:19 -0500155 klist_add_tail(&ic->node, &cont->containers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157 up(&attribute_container_mutex);
158}
159
James Bottomley53c165e2005-08-22 10:06:19 -0500160/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/**
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 */
187void
188attribute_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) {
James Bottomley53c165e2005-08-22 10:06:19 -0500197 struct internal_container *ic;
198 struct klist_iter iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 if (attribute_container_no_classdevs(cont))
201 continue;
202
203 if (!cont->match(cont, dev))
204 continue;
James Bottomley53c165e2005-08-22 10:06:19 -0500205
206 klist_for_each_entry(ic, &cont->containers, node, &iter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (dev != ic->classdev.dev)
208 continue;
James Bottomley53c165e2005-08-22 10:06:19 -0500209 klist_remove(&ic->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 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}
220EXPORT_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 */
232void
233attribute_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) {
James Bottomley53c165e2005-08-22 10:06:19 -0500242 struct internal_container *ic;
243 struct klist_iter iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 if (!cont->match(cont, dev))
246 continue;
247
James Bottomleyebd8bb72005-08-15 16:13:19 -0500248 if (attribute_container_no_classdevs(cont)) {
249 fn(cont, dev, NULL);
250 continue;
251 }
252
James Bottomley53c165e2005-08-22 10:06:19 -0500253 klist_for_each_entry(ic, &cont->containers, node, &iter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (dev == ic->classdev.dev)
255 fn(cont, dev, &ic->classdev);
256 }
257 }
258 up(&attribute_container_mutex);
259}
260EXPORT_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 */
274void
275attribute_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}
288EXPORT_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 */
298int
299attribute_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}
317EXPORT_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 */
328int
329attribute_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}
336EXPORT_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 */
344int
345attribute_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}
351EXPORT_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 */
359void
360attribute_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}
373EXPORT_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 */
383void
384attribute_container_class_device_del(struct class_device *classdev)
385{
386 attribute_container_remove_attrs(classdev);
387 class_device_del(classdev);
388}
389EXPORT_SYMBOL_GPL(attribute_container_class_device_del);
390
James Bottomleyd0a7e572005-08-14 17:09:01 -0500391/**
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 */
400struct class_device *
401attribute_container_find_class_device(struct attribute_container *cont,
402 struct device *dev)
403{
404 struct class_device *cdev = NULL;
405 struct internal_container *ic;
James Bottomley53c165e2005-08-22 10:06:19 -0500406 struct klist_iter iter;
James Bottomleyd0a7e572005-08-14 17:09:01 -0500407
James Bottomley53c165e2005-08-22 10:06:19 -0500408 klist_for_each_entry(ic, &cont->containers, node, &iter) {
James Bottomleyd0a7e572005-08-14 17:09:01 -0500409 if (ic->classdev.dev == dev) {
410 cdev = &ic->classdev;
James Bottomley53c165e2005-08-22 10:06:19 -0500411 /* FIXME: must exit iterator then break */
412 klist_iter_exit(&iter);
James Bottomleyd0a7e572005-08-14 17:09:01 -0500413 break;
414 }
415 }
James Bottomleyd0a7e572005-08-14 17:09:01 -0500416
417 return cdev;
418}
419EXPORT_SYMBOL_GPL(attribute_container_find_class_device);
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421int __init
422attribute_container_init(void)
423{
424 INIT_LIST_HEAD(&attribute_container_list);
425 return 0;
426}