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; | |
61a2f59a | 97 | mutex_lock(&attribute_container_mutex); |
53c165e0 JB |
98 | spin_lock(&cont->containers.k_lock); |
99 | if (!list_empty(&cont->containers.k_list)) | |
1da177e4 LT |
100 | goto out; |
101 | retval = 0; | |
102 | list_del(&cont->node); | |
103 | out: | |
53c165e0 | 104 | spin_unlock(&cont->containers.k_lock); |
61a2f59a | 105 | mutex_unlock(&attribute_container_mutex); |
1da177e4 | 106 | return retval; |
24a7d36a | 107 | |
1da177e4 LT |
108 | } |
109 | EXPORT_SYMBOL_GPL(attribute_container_unregister); | |
110 | ||
111 | /* private function used as class release */ | |
ee959b00 | 112 | static void attribute_container_release(struct device *classdev) |
1da177e4 | 113 | { |
24a7d36a | 114 | struct internal_container *ic |
1da177e4 | 115 | = container_of(classdev, struct internal_container, classdev); |
ee959b00 | 116 | struct device *dev = classdev->parent; |
1da177e4 LT |
117 | |
118 | kfree(ic); | |
119 | put_device(dev); | |
120 | } | |
121 | ||
122 | /** | |
123 | * attribute_container_add_device - see if any container is interested in dev | |
124 | * | |
125 | * @dev: device to add attributes to | |
126 | * @fn: function to trigger addition of class device. | |
127 | * | |
128 | * This function allocates storage for the class device(s) to be | |
129 | * attached to dev (one for each matching attribute_container). If no | |
130 | * fn is provided, the code will simply register the class device via | |
ee959b00 | 131 | * device_add. If a function is provided, it is expected to add |
1da177e4 LT |
132 | * the class device at the appropriate time. One of the things that |
133 | * might be necessary is to allocate and initialise the classdev and | |
134 | * then add it a later time. To do this, call this routine for | |
135 | * allocation and initialisation and then use | |
ee959b00 | 136 | * attribute_container_device_trigger() to call device_add() on |
1da177e4 LT |
137 | * it. Note: after this, the class device contains a reference to dev |
138 | * which is not relinquished until the release of the classdev. | |
139 | */ | |
140 | void | |
141 | attribute_container_add_device(struct device *dev, | |
142 | int (*fn)(struct attribute_container *, | |
143 | struct device *, | |
ee959b00 | 144 | struct device *)) |
1da177e4 LT |
145 | { |
146 | struct attribute_container *cont; | |
147 | ||
61a2f59a | 148 | mutex_lock(&attribute_container_mutex); |
1da177e4 LT |
149 | list_for_each_entry(cont, &attribute_container_list, node) { |
150 | struct internal_container *ic; | |
151 | ||
152 | if (attribute_container_no_classdevs(cont)) | |
153 | continue; | |
154 | ||
155 | if (!cont->match(cont, dev)) | |
156 | continue; | |
4aed0644 JS |
157 | |
158 | ic = kzalloc(sizeof(*ic), GFP_KERNEL); | |
1da177e4 | 159 | if (!ic) { |
a369a7eb | 160 | dev_err(dev, "failed to allocate class container\n"); |
1da177e4 LT |
161 | continue; |
162 | } | |
4aed0644 | 163 | |
1da177e4 | 164 | ic->cont = cont; |
ee959b00 TJ |
165 | device_initialize(&ic->classdev); |
166 | ic->classdev.parent = get_device(dev); | |
1da177e4 | 167 | ic->classdev.class = cont->class; |
ee959b00 | 168 | cont->class->dev_release = attribute_container_release; |
02aa2a37 | 169 | dev_set_name(&ic->classdev, "%s", dev_name(dev)); |
1da177e4 LT |
170 | if (fn) |
171 | fn(cont, dev, &ic->classdev); | |
172 | else | |
173 | attribute_container_add_class_device(&ic->classdev); | |
53c165e0 | 174 | klist_add_tail(&ic->node, &cont->containers); |
1da177e4 | 175 | } |
61a2f59a | 176 | mutex_unlock(&attribute_container_mutex); |
1da177e4 LT |
177 | } |
178 | ||
53c165e0 JB |
179 | /* FIXME: can't break out of this unless klist_iter_exit is also |
180 | * called before doing the break | |
181 | */ | |
182 | #define klist_for_each_entry(pos, head, member, iter) \ | |
183 | for (klist_iter_init(head, iter); (pos = ({ \ | |
184 | struct klist_node *n = klist_next(iter); \ | |
caf39e87 LT |
185 | n ? container_of(n, typeof(*pos), member) : \ |
186 | ({ klist_iter_exit(iter) ; NULL; }); \ | |
287f9bd9 | 187 | })) != NULL;) |
24a7d36a | 188 | |
53c165e0 | 189 | |
1da177e4 LT |
190 | /** |
191 | * attribute_container_remove_device - make device eligible for removal. | |
192 | * | |
193 | * @dev: The generic device | |
194 | * @fn: A function to call to remove the device | |
195 | * | |
196 | * This routine triggers device removal. If fn is NULL, then it is | |
ee959b00 | 197 | * simply done via device_unregister (note that if something |
1da177e4 LT |
198 | * still has a reference to the classdev, then the memory occupied |
199 | * will not be freed until the classdev is released). If you want a | |
200 | * two phase release: remove from visibility and then delete the | |
201 | * device, then you should use this routine with a fn that calls | |
ee959b00 TJ |
202 | * device_del() and then use attribute_container_device_trigger() |
203 | * to do the final put on the classdev. | |
1da177e4 LT |
204 | */ |
205 | void | |
206 | attribute_container_remove_device(struct device *dev, | |
207 | void (*fn)(struct attribute_container *, | |
208 | struct device *, | |
ee959b00 | 209 | struct device *)) |
1da177e4 LT |
210 | { |
211 | struct attribute_container *cont; | |
212 | ||
61a2f59a | 213 | mutex_lock(&attribute_container_mutex); |
1da177e4 | 214 | list_for_each_entry(cont, &attribute_container_list, node) { |
53c165e0 JB |
215 | struct internal_container *ic; |
216 | struct klist_iter iter; | |
1da177e4 LT |
217 | |
218 | if (attribute_container_no_classdevs(cont)) | |
219 | continue; | |
220 | ||
221 | if (!cont->match(cont, dev)) | |
222 | continue; | |
53c165e0 JB |
223 | |
224 | klist_for_each_entry(ic, &cont->containers, node, &iter) { | |
ee959b00 | 225 | if (dev != ic->classdev.parent) |
1da177e4 | 226 | continue; |
caf39e87 | 227 | klist_del(&ic->node); |
1da177e4 LT |
228 | if (fn) |
229 | fn(cont, dev, &ic->classdev); | |
230 | else { | |
231 | attribute_container_remove_attrs(&ic->classdev); | |
ee959b00 | 232 | device_unregister(&ic->classdev); |
1da177e4 LT |
233 | } |
234 | } | |
235 | } | |
61a2f59a | 236 | mutex_unlock(&attribute_container_mutex); |
1da177e4 | 237 | } |
1da177e4 LT |
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 | */ | |
249 | void | |
24a7d36a | 250 | attribute_container_device_trigger(struct device *dev, |
1da177e4 LT |
251 | int (*fn)(struct attribute_container *, |
252 | struct device *, | |
ee959b00 | 253 | struct device *)) |
1da177e4 LT |
254 | { |
255 | struct attribute_container *cont; | |
256 | ||
61a2f59a | 257 | mutex_lock(&attribute_container_mutex); |
1da177e4 | 258 | list_for_each_entry(cont, &attribute_container_list, node) { |
53c165e0 JB |
259 | struct internal_container *ic; |
260 | struct klist_iter iter; | |
1da177e4 LT |
261 | |
262 | if (!cont->match(cont, dev)) | |
263 | continue; | |
264 | ||
ebd8bb76 JB |
265 | if (attribute_container_no_classdevs(cont)) { |
266 | fn(cont, dev, NULL); | |
267 | continue; | |
268 | } | |
269 | ||
53c165e0 | 270 | klist_for_each_entry(ic, &cont->containers, node, &iter) { |
ee959b00 | 271 | if (dev == ic->classdev.parent) |
1da177e4 LT |
272 | fn(cont, dev, &ic->classdev); |
273 | } | |
274 | } | |
61a2f59a | 275 | mutex_unlock(&attribute_container_mutex); |
1da177e4 | 276 | } |
1da177e4 LT |
277 | |
278 | /** | |
279 | * attribute_container_trigger - trigger a function for each matching container | |
280 | * | |
281 | * @dev: The generic device to activate the trigger for | |
282 | * @fn: the function to trigger | |
283 | * | |
284 | * This routine triggers a function that only needs to know the | |
285 | * matching containers (not the classdev) associated with a device. | |
286 | * It is more lightweight than attribute_container_device_trigger, so | |
287 | * should be used in preference unless the triggering function | |
288 | * actually needs to know the classdev. | |
289 | */ | |
290 | void | |
291 | attribute_container_trigger(struct device *dev, | |
292 | int (*fn)(struct attribute_container *, | |
293 | struct device *)) | |
294 | { | |
295 | struct attribute_container *cont; | |
296 | ||
61a2f59a | 297 | mutex_lock(&attribute_container_mutex); |
1da177e4 LT |
298 | list_for_each_entry(cont, &attribute_container_list, node) { |
299 | if (cont->match(cont, dev)) | |
300 | fn(cont, dev); | |
301 | } | |
61a2f59a | 302 | mutex_unlock(&attribute_container_mutex); |
1da177e4 | 303 | } |
1da177e4 LT |
304 | |
305 | /** | |
306 | * attribute_container_add_attrs - add attributes | |
307 | * | |
308 | * @classdev: The class device | |
309 | * | |
310 | * This simply creates all the class device sysfs files from the | |
311 | * attributes listed in the container | |
312 | */ | |
313 | int | |
ee959b00 | 314 | attribute_container_add_attrs(struct device *classdev) |
1da177e4 LT |
315 | { |
316 | struct attribute_container *cont = | |
317 | attribute_container_classdev_to_container(classdev); | |
ee959b00 | 318 | struct device_attribute **attrs = cont->attrs; |
1da177e4 LT |
319 | int i, error; |
320 | ||
fd110971 JB |
321 | BUG_ON(attrs && cont->grp); |
322 | ||
323 | if (!attrs && !cont->grp) | |
1da177e4 LT |
324 | return 0; |
325 | ||
fd110971 JB |
326 | if (cont->grp) |
327 | return sysfs_create_group(&classdev->kobj, cont->grp); | |
328 | ||
1da177e4 | 329 | for (i = 0; attrs[i]; i++) { |
ebd09ec9 | 330 | sysfs_attr_init(&attrs[i]->attr); |
ee959b00 | 331 | error = device_create_file(classdev, attrs[i]); |
1da177e4 LT |
332 | if (error) |
333 | return error; | |
334 | } | |
335 | ||
336 | return 0; | |
337 | } | |
1da177e4 LT |
338 | |
339 | /** | |
ee959b00 | 340 | * attribute_container_add_class_device - same function as device_add |
1da177e4 LT |
341 | * |
342 | * @classdev: the class device to add | |
343 | * | |
ee959b00 | 344 | * This performs essentially the same function as device_add except for |
1da177e4 LT |
345 | * attribute containers, namely add the classdev to the system and then |
346 | * create the attribute files | |
347 | */ | |
348 | int | |
ee959b00 | 349 | attribute_container_add_class_device(struct device *classdev) |
1da177e4 | 350 | { |
ee959b00 | 351 | int error = device_add(classdev); |
1da177e4 LT |
352 | if (error) |
353 | return error; | |
354 | return attribute_container_add_attrs(classdev); | |
355 | } | |
1da177e4 LT |
356 | |
357 | /** | |
358 | * attribute_container_add_class_device_adapter - simple adapter for triggers | |
359 | * | |
360 | * This function is identical to attribute_container_add_class_device except | |
361 | * that it is designed to be called from the triggers | |
362 | */ | |
363 | int | |
364 | attribute_container_add_class_device_adapter(struct attribute_container *cont, | |
365 | struct device *dev, | |
ee959b00 | 366 | struct device *classdev) |
1da177e4 LT |
367 | { |
368 | return attribute_container_add_class_device(classdev); | |
369 | } | |
1da177e4 LT |
370 | |
371 | /** | |
372 | * attribute_container_remove_attrs - remove any attribute files | |
373 | * | |
374 | * @classdev: The class device to remove the files from | |
375 | * | |
376 | */ | |
377 | void | |
ee959b00 | 378 | attribute_container_remove_attrs(struct device *classdev) |
1da177e4 LT |
379 | { |
380 | struct attribute_container *cont = | |
381 | attribute_container_classdev_to_container(classdev); | |
ee959b00 | 382 | struct device_attribute **attrs = cont->attrs; |
1da177e4 LT |
383 | int i; |
384 | ||
fd110971 | 385 | if (!attrs && !cont->grp) |
1da177e4 LT |
386 | return; |
387 | ||
fd110971 JB |
388 | if (cont->grp) { |
389 | sysfs_remove_group(&classdev->kobj, cont->grp); | |
390 | return ; | |
391 | } | |
392 | ||
1da177e4 | 393 | for (i = 0; attrs[i]; i++) |
ee959b00 | 394 | device_remove_file(classdev, attrs[i]); |
1da177e4 | 395 | } |
1da177e4 LT |
396 | |
397 | /** | |
398 | * attribute_container_class_device_del - equivalent of class_device_del | |
399 | * | |
400 | * @classdev: the class device | |
401 | * | |
402 | * This function simply removes all the attribute files and then calls | |
ee959b00 | 403 | * device_del. |
1da177e4 LT |
404 | */ |
405 | void | |
ee959b00 | 406 | attribute_container_class_device_del(struct device *classdev) |
1da177e4 LT |
407 | { |
408 | attribute_container_remove_attrs(classdev); | |
ee959b00 | 409 | device_del(classdev); |
1da177e4 | 410 | } |
1da177e4 | 411 | |
d0a7e574 JB |
412 | /** |
413 | * attribute_container_find_class_device - find the corresponding class_device | |
414 | * | |
415 | * @cont: the container | |
416 | * @dev: the generic device | |
417 | * | |
418 | * Looks up the device in the container's list of class devices and returns | |
419 | * the corresponding class_device. | |
420 | */ | |
ee959b00 | 421 | struct device * |
d0a7e574 JB |
422 | attribute_container_find_class_device(struct attribute_container *cont, |
423 | struct device *dev) | |
424 | { | |
ee959b00 | 425 | struct device *cdev = NULL; |
d0a7e574 | 426 | struct internal_container *ic; |
53c165e0 | 427 | struct klist_iter iter; |
d0a7e574 | 428 | |
53c165e0 | 429 | klist_for_each_entry(ic, &cont->containers, node, &iter) { |
ee959b00 | 430 | if (ic->classdev.parent == dev) { |
d0a7e574 | 431 | cdev = &ic->classdev; |
53c165e0 JB |
432 | /* FIXME: must exit iterator then break */ |
433 | klist_iter_exit(&iter); | |
d0a7e574 JB |
434 | break; |
435 | } | |
436 | } | |
d0a7e574 JB |
437 | |
438 | return cdev; | |
439 | } | |
440 | EXPORT_SYMBOL_GPL(attribute_container_find_class_device); |