Commit | Line | Data |
---|---|---|
1da177e4 LT |
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> | |
1da177e4 LT |
15 | #include <linux/device.h> |
16 | #include <linux/kernel.h> | |
17 | #include <linux/slab.h> | |
18 | #include <linux/list.h> | |
19 | #include <linux/module.h> | |
f8916c11 | 20 | #include <linux/mutex.h> |
1da177e4 | 21 | |
a1bdc7aa BD |
22 | #include "base.h" |
23 | ||
1da177e4 LT |
24 | /* This is a private structure used to tie the classdev and the |
25 | * container .. it should never be visible outside this file */ | |
26 | struct internal_container { | |
53c165e0 | 27 | struct klist_node node; |
1da177e4 | 28 | struct attribute_container *cont; |
ee959b00 | 29 | struct device classdev; |
1da177e4 LT |
30 | }; |
31 | ||
caf39e87 LT |
32 | static void internal_container_klist_get(struct klist_node *n) |
33 | { | |
34 | struct internal_container *ic = | |
35 | container_of(n, struct internal_container, node); | |
ee959b00 | 36 | get_device(&ic->classdev); |
caf39e87 LT |
37 | } |
38 | ||
39 | static void internal_container_klist_put(struct klist_node *n) | |
40 | { | |
41 | struct internal_container *ic = | |
42 | container_of(n, struct internal_container, node); | |
ee959b00 | 43 | put_device(&ic->classdev); |
caf39e87 LT |
44 | } |
45 | ||
46 | ||
1da177e4 LT |
47 | /** |
48 | * attribute_container_classdev_to_container - given a classdev, return the container | |
49 | * | |
50 | * @classdev: the class device created by attribute_container_add_device. | |
51 | * | |
52 | * Returns the container associated with this classdev. | |
53 | */ | |
54 | struct attribute_container * | |
ee959b00 | 55 | attribute_container_classdev_to_container(struct device *classdev) |
1da177e4 LT |
56 | { |
57 | struct internal_container *ic = | |
58 | container_of(classdev, struct internal_container, classdev); | |
59 | return ic->cont; | |
60 | } | |
61 | EXPORT_SYMBOL_GPL(attribute_container_classdev_to_container); | |
62 | ||
db1118a4 | 63 | static LIST_HEAD(attribute_container_list); |
1da177e4 | 64 | |
61a2f59a | 65 | static DEFINE_MUTEX(attribute_container_mutex); |
1da177e4 LT |
66 | |
67 | /** | |
68 | * attribute_container_register - register an attribute container | |
69 | * | |
70 | * @cont: The container to register. This must be allocated by the | |
71 | * callee and should also be zeroed by it. | |
72 | */ | |
73 | int | |
74 | attribute_container_register(struct attribute_container *cont) | |
75 | { | |
76 | INIT_LIST_HEAD(&cont->node); | |
287f9bd9 | 77 | klist_init(&cont->containers, internal_container_klist_get, |
caf39e87 | 78 | internal_container_klist_put); |
24a7d36a | 79 | |
61a2f59a | 80 | mutex_lock(&attribute_container_mutex); |
1da177e4 | 81 | list_add_tail(&cont->node, &attribute_container_list); |
61a2f59a | 82 | mutex_unlock(&attribute_container_mutex); |
1da177e4 LT |
83 | |
84 | return 0; | |
85 | } | |
86 | EXPORT_SYMBOL_GPL(attribute_container_register); | |
87 | ||
88 | /** | |
89 | * attribute_container_unregister - remove a container registration | |
90 | * | |
91 | * @cont: previously registered container to remove | |
92 | */ | |
93 | int | |
94 | attribute_container_unregister(struct attribute_container *cont) | |
95 | { | |
96 | int retval = -EBUSY; | |
481026db | 97 | |
61a2f59a | 98 | mutex_lock(&attribute_container_mutex); |
53c165e0 JB |
99 | spin_lock(&cont->containers.k_lock); |
100 | if (!list_empty(&cont->containers.k_list)) | |
1da177e4 LT |
101 | goto out; |
102 | retval = 0; | |
103 | list_del(&cont->node); | |
104 | out: | |
53c165e0 | 105 | spin_unlock(&cont->containers.k_lock); |
61a2f59a | 106 | mutex_unlock(&attribute_container_mutex); |
1da177e4 | 107 | return retval; |
24a7d36a | 108 | |
1da177e4 LT |
109 | } |
110 | EXPORT_SYMBOL_GPL(attribute_container_unregister); | |
111 | ||
112 | /* private function used as class release */ | |
ee959b00 | 113 | static void attribute_container_release(struct device *classdev) |
1da177e4 | 114 | { |
24a7d36a | 115 | struct internal_container *ic |
1da177e4 | 116 | = container_of(classdev, struct internal_container, classdev); |
ee959b00 | 117 | struct device *dev = classdev->parent; |
1da177e4 LT |
118 | |
119 | kfree(ic); | |
120 | put_device(dev); | |
121 | } | |
122 | ||
123 | /** | |
124 | * attribute_container_add_device - see if any container is interested in dev | |
125 | * | |
126 | * @dev: device to add attributes to | |
127 | * @fn: function to trigger addition of class device. | |
128 | * | |
129 | * This function allocates storage for the class device(s) to be | |
130 | * attached to dev (one for each matching attribute_container). If no | |
131 | * fn is provided, the code will simply register the class device via | |
ee959b00 | 132 | * device_add. If a function is provided, it is expected to add |
1da177e4 LT |
133 | * the class device at the appropriate time. One of the things that |
134 | * might be necessary is to allocate and initialise the classdev and | |
135 | * then add it a later time. To do this, call this routine for | |
136 | * allocation and initialisation and then use | |
ee959b00 | 137 | * attribute_container_device_trigger() to call device_add() on |
1da177e4 LT |
138 | * it. Note: after this, the class device contains a reference to dev |
139 | * which is not relinquished until the release of the classdev. | |
140 | */ | |
141 | void | |
142 | attribute_container_add_device(struct device *dev, | |
143 | int (*fn)(struct attribute_container *, | |
144 | struct device *, | |
ee959b00 | 145 | struct device *)) |
1da177e4 LT |
146 | { |
147 | struct attribute_container *cont; | |
148 | ||
61a2f59a | 149 | mutex_lock(&attribute_container_mutex); |
1da177e4 LT |
150 | list_for_each_entry(cont, &attribute_container_list, node) { |
151 | struct internal_container *ic; | |
152 | ||
153 | if (attribute_container_no_classdevs(cont)) | |
154 | continue; | |
155 | ||
156 | if (!cont->match(cont, dev)) | |
157 | continue; | |
4aed0644 JS |
158 | |
159 | ic = kzalloc(sizeof(*ic), GFP_KERNEL); | |
1da177e4 | 160 | if (!ic) { |
a369a7eb | 161 | dev_err(dev, "failed to allocate class container\n"); |
1da177e4 LT |
162 | continue; |
163 | } | |
4aed0644 | 164 | |
1da177e4 | 165 | ic->cont = cont; |
ee959b00 TJ |
166 | device_initialize(&ic->classdev); |
167 | ic->classdev.parent = get_device(dev); | |
1da177e4 | 168 | ic->classdev.class = cont->class; |
ee959b00 | 169 | cont->class->dev_release = attribute_container_release; |
02aa2a37 | 170 | dev_set_name(&ic->classdev, "%s", dev_name(dev)); |
1da177e4 LT |
171 | if (fn) |
172 | fn(cont, dev, &ic->classdev); | |
173 | else | |
174 | attribute_container_add_class_device(&ic->classdev); | |
53c165e0 | 175 | klist_add_tail(&ic->node, &cont->containers); |
1da177e4 | 176 | } |
61a2f59a | 177 | mutex_unlock(&attribute_container_mutex); |
1da177e4 LT |
178 | } |
179 | ||
53c165e0 JB |
180 | /* FIXME: can't break out of this unless klist_iter_exit is also |
181 | * called before doing the break | |
182 | */ | |
183 | #define klist_for_each_entry(pos, head, member, iter) \ | |
184 | for (klist_iter_init(head, iter); (pos = ({ \ | |
185 | struct klist_node *n = klist_next(iter); \ | |
caf39e87 LT |
186 | n ? container_of(n, typeof(*pos), member) : \ |
187 | ({ klist_iter_exit(iter) ; NULL; }); \ | |
287f9bd9 | 188 | })) != NULL;) |
24a7d36a | 189 | |
53c165e0 | 190 | |
1da177e4 LT |
191 | /** |
192 | * attribute_container_remove_device - make device eligible for removal. | |
193 | * | |
194 | * @dev: The generic device | |
195 | * @fn: A function to call to remove the device | |
196 | * | |
197 | * This routine triggers device removal. If fn is NULL, then it is | |
ee959b00 | 198 | * simply done via device_unregister (note that if something |
1da177e4 LT |
199 | * still has a reference to the classdev, then the memory occupied |
200 | * will not be freed until the classdev is released). If you want a | |
201 | * two phase release: remove from visibility and then delete the | |
202 | * device, then you should use this routine with a fn that calls | |
ee959b00 TJ |
203 | * device_del() and then use attribute_container_device_trigger() |
204 | * to do the final put on the classdev. | |
1da177e4 LT |
205 | */ |
206 | void | |
207 | attribute_container_remove_device(struct device *dev, | |
208 | void (*fn)(struct attribute_container *, | |
209 | struct device *, | |
ee959b00 | 210 | struct device *)) |
1da177e4 LT |
211 | { |
212 | struct attribute_container *cont; | |
213 | ||
61a2f59a | 214 | mutex_lock(&attribute_container_mutex); |
1da177e4 | 215 | list_for_each_entry(cont, &attribute_container_list, node) { |
53c165e0 JB |
216 | struct internal_container *ic; |
217 | struct klist_iter iter; | |
1da177e4 LT |
218 | |
219 | if (attribute_container_no_classdevs(cont)) | |
220 | continue; | |
221 | ||
222 | if (!cont->match(cont, dev)) | |
223 | continue; | |
53c165e0 JB |
224 | |
225 | klist_for_each_entry(ic, &cont->containers, node, &iter) { | |
ee959b00 | 226 | if (dev != ic->classdev.parent) |
1da177e4 | 227 | continue; |
caf39e87 | 228 | klist_del(&ic->node); |
1da177e4 LT |
229 | if (fn) |
230 | fn(cont, dev, &ic->classdev); | |
231 | else { | |
232 | attribute_container_remove_attrs(&ic->classdev); | |
ee959b00 | 233 | device_unregister(&ic->classdev); |
1da177e4 LT |
234 | } |
235 | } | |
236 | } | |
61a2f59a | 237 | mutex_unlock(&attribute_container_mutex); |
1da177e4 | 238 | } |
1da177e4 LT |
239 | |
240 | /** | |
241 | * attribute_container_device_trigger - execute a trigger for each matching classdev | |
242 | * | |
243 | * @dev: The generic device to run the trigger for | |
244 | * @fn the function to execute for each classdev. | |
245 | * | |
03aca7b2 | 246 | * This function is for executing a trigger when you need to know both |
1da177e4 LT |
247 | * the container and the classdev. If you only care about the |
248 | * container, then use attribute_container_trigger() instead. | |
249 | */ | |
250 | void | |
24a7d36a | 251 | attribute_container_device_trigger(struct device *dev, |
1da177e4 LT |
252 | int (*fn)(struct attribute_container *, |
253 | struct device *, | |
ee959b00 | 254 | struct device *)) |
1da177e4 LT |
255 | { |
256 | struct attribute_container *cont; | |
257 | ||
61a2f59a | 258 | mutex_lock(&attribute_container_mutex); |
1da177e4 | 259 | list_for_each_entry(cont, &attribute_container_list, node) { |
53c165e0 JB |
260 | struct internal_container *ic; |
261 | struct klist_iter iter; | |
1da177e4 LT |
262 | |
263 | if (!cont->match(cont, dev)) | |
264 | continue; | |
265 | ||
ebd8bb76 JB |
266 | if (attribute_container_no_classdevs(cont)) { |
267 | fn(cont, dev, NULL); | |
268 | continue; | |
269 | } | |
270 | ||
53c165e0 | 271 | klist_for_each_entry(ic, &cont->containers, node, &iter) { |
ee959b00 | 272 | if (dev == ic->classdev.parent) |
1da177e4 LT |
273 | fn(cont, dev, &ic->classdev); |
274 | } | |
275 | } | |
61a2f59a | 276 | mutex_unlock(&attribute_container_mutex); |
1da177e4 | 277 | } |
1da177e4 LT |
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 | */ | |
291 | void | |
292 | attribute_container_trigger(struct device *dev, | |
293 | int (*fn)(struct attribute_container *, | |
294 | struct device *)) | |
295 | { | |
296 | struct attribute_container *cont; | |
297 | ||
61a2f59a | 298 | mutex_lock(&attribute_container_mutex); |
1da177e4 LT |
299 | list_for_each_entry(cont, &attribute_container_list, node) { |
300 | if (cont->match(cont, dev)) | |
301 | fn(cont, dev); | |
302 | } | |
61a2f59a | 303 | mutex_unlock(&attribute_container_mutex); |
1da177e4 | 304 | } |
1da177e4 LT |
305 | |
306 | /** | |
307 | * attribute_container_add_attrs - add attributes | |
308 | * | |
309 | * @classdev: The class device | |
310 | * | |
311 | * This simply creates all the class device sysfs files from the | |
312 | * attributes listed in the container | |
313 | */ | |
314 | int | |
ee959b00 | 315 | attribute_container_add_attrs(struct device *classdev) |
1da177e4 LT |
316 | { |
317 | struct attribute_container *cont = | |
318 | attribute_container_classdev_to_container(classdev); | |
ee959b00 | 319 | struct device_attribute **attrs = cont->attrs; |
1da177e4 LT |
320 | int i, error; |
321 | ||
fd110971 JB |
322 | BUG_ON(attrs && cont->grp); |
323 | ||
324 | if (!attrs && !cont->grp) | |
1da177e4 LT |
325 | return 0; |
326 | ||
fd110971 JB |
327 | if (cont->grp) |
328 | return sysfs_create_group(&classdev->kobj, cont->grp); | |
329 | ||
1da177e4 | 330 | for (i = 0; attrs[i]; i++) { |
ebd09ec9 | 331 | sysfs_attr_init(&attrs[i]->attr); |
ee959b00 | 332 | error = device_create_file(classdev, attrs[i]); |
1da177e4 LT |
333 | if (error) |
334 | return error; | |
335 | } | |
336 | ||
337 | return 0; | |
338 | } | |
1da177e4 LT |
339 | |
340 | /** | |
ee959b00 | 341 | * attribute_container_add_class_device - same function as device_add |
1da177e4 LT |
342 | * |
343 | * @classdev: the class device to add | |
344 | * | |
ee959b00 | 345 | * This performs essentially the same function as device_add except for |
1da177e4 LT |
346 | * attribute containers, namely add the classdev to the system and then |
347 | * create the attribute files | |
348 | */ | |
349 | int | |
ee959b00 | 350 | attribute_container_add_class_device(struct device *classdev) |
1da177e4 | 351 | { |
ee959b00 | 352 | int error = device_add(classdev); |
481026db | 353 | |
1da177e4 LT |
354 | if (error) |
355 | return error; | |
356 | return attribute_container_add_attrs(classdev); | |
357 | } | |
1da177e4 LT |
358 | |
359 | /** | |
360 | * attribute_container_add_class_device_adapter - simple adapter for triggers | |
361 | * | |
362 | * This function is identical to attribute_container_add_class_device except | |
363 | * that it is designed to be called from the triggers | |
364 | */ | |
365 | int | |
366 | attribute_container_add_class_device_adapter(struct attribute_container *cont, | |
367 | struct device *dev, | |
ee959b00 | 368 | struct device *classdev) |
1da177e4 LT |
369 | { |
370 | return attribute_container_add_class_device(classdev); | |
371 | } | |
1da177e4 LT |
372 | |
373 | /** | |
374 | * attribute_container_remove_attrs - remove any attribute files | |
375 | * | |
376 | * @classdev: The class device to remove the files from | |
377 | * | |
378 | */ | |
379 | void | |
ee959b00 | 380 | attribute_container_remove_attrs(struct device *classdev) |
1da177e4 LT |
381 | { |
382 | struct attribute_container *cont = | |
383 | attribute_container_classdev_to_container(classdev); | |
ee959b00 | 384 | struct device_attribute **attrs = cont->attrs; |
1da177e4 LT |
385 | int i; |
386 | ||
fd110971 | 387 | if (!attrs && !cont->grp) |
1da177e4 LT |
388 | return; |
389 | ||
fd110971 JB |
390 | if (cont->grp) { |
391 | sysfs_remove_group(&classdev->kobj, cont->grp); | |
392 | return ; | |
393 | } | |
394 | ||
1da177e4 | 395 | for (i = 0; attrs[i]; i++) |
ee959b00 | 396 | device_remove_file(classdev, attrs[i]); |
1da177e4 | 397 | } |
1da177e4 LT |
398 | |
399 | /** | |
400 | * attribute_container_class_device_del - equivalent of class_device_del | |
401 | * | |
402 | * @classdev: the class device | |
403 | * | |
404 | * This function simply removes all the attribute files and then calls | |
ee959b00 | 405 | * device_del. |
1da177e4 LT |
406 | */ |
407 | void | |
ee959b00 | 408 | attribute_container_class_device_del(struct device *classdev) |
1da177e4 LT |
409 | { |
410 | attribute_container_remove_attrs(classdev); | |
ee959b00 | 411 | device_del(classdev); |
1da177e4 | 412 | } |
1da177e4 | 413 | |
d0a7e574 JB |
414 | /** |
415 | * attribute_container_find_class_device - find the corresponding class_device | |
416 | * | |
417 | * @cont: the container | |
418 | * @dev: the generic device | |
419 | * | |
420 | * Looks up the device in the container's list of class devices and returns | |
421 | * the corresponding class_device. | |
422 | */ | |
ee959b00 | 423 | struct device * |
d0a7e574 JB |
424 | attribute_container_find_class_device(struct attribute_container *cont, |
425 | struct device *dev) | |
426 | { | |
ee959b00 | 427 | struct device *cdev = NULL; |
d0a7e574 | 428 | struct internal_container *ic; |
53c165e0 | 429 | struct klist_iter iter; |
d0a7e574 | 430 | |
53c165e0 | 431 | klist_for_each_entry(ic, &cont->containers, node, &iter) { |
ee959b00 | 432 | if (ic->classdev.parent == dev) { |
d0a7e574 | 433 | cdev = &ic->classdev; |
53c165e0 JB |
434 | /* FIXME: must exit iterator then break */ |
435 | klist_iter_exit(&iter); | |
d0a7e574 JB |
436 | break; |
437 | } | |
438 | } | |
d0a7e574 JB |
439 | |
440 | return cdev; | |
441 | } | |
442 | EXPORT_SYMBOL_GPL(attribute_container_find_class_device); |