Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Ben Dooks | a1bdc7a | 2005-10-13 17:54:41 +0100 | [diff] [blame] | 22 | #include "base.h" |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 { |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 27 | struct klist_node node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | struct attribute_container *cont; |
| 29 | struct class_device classdev; |
| 30 | }; |
| 31 | |
Linus Torvalds | caf39e8 | 2005-09-07 18:44:33 -0700 | [diff] [blame] | 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); |
| 36 | class_device_get(&ic->classdev); |
| 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); |
| 43 | class_device_put(&ic->classdev); |
| 44 | } |
| 45 | |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 * |
| 55 | attribute_container_classdev_to_container(struct class_device *classdev) |
| 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 | |
| 63 | static struct list_head attribute_container_list; |
| 64 | |
| 65 | static DECLARE_MUTEX(attribute_container_mutex); |
| 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); |
Linus Torvalds | caf39e8 | 2005-09-07 18:44:33 -0700 | [diff] [blame] | 77 | klist_init(&cont->containers,internal_container_klist_get, |
| 78 | internal_container_klist_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | down(&attribute_container_mutex); |
| 81 | list_add_tail(&cont->node, &attribute_container_list); |
| 82 | up(&attribute_container_mutex); |
| 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; |
| 97 | down(&attribute_container_mutex); |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 98 | spin_lock(&cont->containers.k_lock); |
| 99 | if (!list_empty(&cont->containers.k_list)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | goto out; |
| 101 | retval = 0; |
| 102 | list_del(&cont->node); |
| 103 | out: |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 104 | spin_unlock(&cont->containers.k_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | up(&attribute_container_mutex); |
| 106 | return retval; |
| 107 | |
| 108 | } |
| 109 | EXPORT_SYMBOL_GPL(attribute_container_unregister); |
| 110 | |
| 111 | /* private function used as class release */ |
| 112 | static void attribute_container_release(struct class_device *classdev) |
| 113 | { |
| 114 | struct internal_container *ic |
| 115 | = container_of(classdev, struct internal_container, classdev); |
| 116 | struct device *dev = classdev->dev; |
| 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 |
| 131 | * class_device_add. If a function is provided, it is expected to add |
| 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 |
| 136 | * attribute_container_device_trigger() to call class_device_add() on |
| 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 *, |
| 144 | struct class_device *)) |
| 145 | { |
| 146 | struct attribute_container *cont; |
| 147 | |
| 148 | down(&attribute_container_mutex); |
| 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; |
Jiri Slaby | 4aed064 | 2005-09-13 01:25:01 -0700 | [diff] [blame] | 157 | |
| 158 | ic = kzalloc(sizeof(*ic), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | if (!ic) { |
| 160 | dev_printk(KERN_ERR, dev, "failed to allocate class container\n"); |
| 161 | continue; |
| 162 | } |
Jiri Slaby | 4aed064 | 2005-09-13 01:25:01 -0700 | [diff] [blame] | 163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | ic->cont = cont; |
| 165 | class_device_initialize(&ic->classdev); |
| 166 | ic->classdev.dev = get_device(dev); |
| 167 | ic->classdev.class = cont->class; |
| 168 | cont->class->release = attribute_container_release; |
| 169 | strcpy(ic->classdev.class_id, dev->bus_id); |
| 170 | if (fn) |
| 171 | fn(cont, dev, &ic->classdev); |
| 172 | else |
| 173 | attribute_container_add_class_device(&ic->classdev); |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 174 | klist_add_tail(&ic->node, &cont->containers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | up(&attribute_container_mutex); |
| 177 | } |
| 178 | |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 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); \ |
Linus Torvalds | caf39e8 | 2005-09-07 18:44:33 -0700 | [diff] [blame] | 185 | n ? container_of(n, typeof(*pos), member) : \ |
| 186 | ({ klist_iter_exit(iter) ; NULL; }); \ |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 187 | }) ) != NULL; ) |
| 188 | |
| 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
| 197 | * simply done via class_device_unregister (note that if something |
| 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 |
| 202 | * class_device_del() and then use |
| 203 | * attribute_container_device_trigger() to do the final put on the |
| 204 | * classdev. |
| 205 | */ |
| 206 | void |
| 207 | attribute_container_remove_device(struct device *dev, |
| 208 | void (*fn)(struct attribute_container *, |
| 209 | struct device *, |
| 210 | struct class_device *)) |
| 211 | { |
| 212 | struct attribute_container *cont; |
| 213 | |
| 214 | down(&attribute_container_mutex); |
| 215 | list_for_each_entry(cont, &attribute_container_list, node) { |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 216 | struct internal_container *ic; |
| 217 | struct klist_iter iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
| 219 | if (attribute_container_no_classdevs(cont)) |
| 220 | continue; |
| 221 | |
| 222 | if (!cont->match(cont, dev)) |
| 223 | continue; |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 224 | |
| 225 | klist_for_each_entry(ic, &cont->containers, node, &iter) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | if (dev != ic->classdev.dev) |
| 227 | continue; |
Linus Torvalds | caf39e8 | 2005-09-07 18:44:33 -0700 | [diff] [blame] | 228 | klist_del(&ic->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | if (fn) |
| 230 | fn(cont, dev, &ic->classdev); |
| 231 | else { |
| 232 | attribute_container_remove_attrs(&ic->classdev); |
| 233 | class_device_unregister(&ic->classdev); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | up(&attribute_container_mutex); |
| 238 | } |
| 239 | EXPORT_SYMBOL_GPL(attribute_container_remove_device); |
| 240 | |
| 241 | /** |
| 242 | * attribute_container_device_trigger - execute a trigger for each matching classdev |
| 243 | * |
| 244 | * @dev: The generic device to run the trigger for |
| 245 | * @fn the function to execute for each classdev. |
| 246 | * |
| 247 | * This funcion is for executing a trigger when you need to know both |
| 248 | * the container and the classdev. If you only care about the |
| 249 | * container, then use attribute_container_trigger() instead. |
| 250 | */ |
| 251 | void |
| 252 | attribute_container_device_trigger(struct device *dev, |
| 253 | int (*fn)(struct attribute_container *, |
| 254 | struct device *, |
| 255 | struct class_device *)) |
| 256 | { |
| 257 | struct attribute_container *cont; |
| 258 | |
| 259 | down(&attribute_container_mutex); |
| 260 | list_for_each_entry(cont, &attribute_container_list, node) { |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 261 | struct internal_container *ic; |
| 262 | struct klist_iter iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
| 264 | if (!cont->match(cont, dev)) |
| 265 | continue; |
| 266 | |
James Bottomley | ebd8bb7 | 2005-08-15 16:13:19 -0500 | [diff] [blame] | 267 | if (attribute_container_no_classdevs(cont)) { |
| 268 | fn(cont, dev, NULL); |
| 269 | continue; |
| 270 | } |
| 271 | |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 272 | klist_for_each_entry(ic, &cont->containers, node, &iter) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | if (dev == ic->classdev.dev) |
| 274 | fn(cont, dev, &ic->classdev); |
| 275 | } |
| 276 | } |
| 277 | up(&attribute_container_mutex); |
| 278 | } |
| 279 | EXPORT_SYMBOL_GPL(attribute_container_device_trigger); |
| 280 | |
| 281 | /** |
| 282 | * attribute_container_trigger - trigger a function for each matching container |
| 283 | * |
| 284 | * @dev: The generic device to activate the trigger for |
| 285 | * @fn: the function to trigger |
| 286 | * |
| 287 | * This routine triggers a function that only needs to know the |
| 288 | * matching containers (not the classdev) associated with a device. |
| 289 | * It is more lightweight than attribute_container_device_trigger, so |
| 290 | * should be used in preference unless the triggering function |
| 291 | * actually needs to know the classdev. |
| 292 | */ |
| 293 | void |
| 294 | attribute_container_trigger(struct device *dev, |
| 295 | int (*fn)(struct attribute_container *, |
| 296 | struct device *)) |
| 297 | { |
| 298 | struct attribute_container *cont; |
| 299 | |
| 300 | down(&attribute_container_mutex); |
| 301 | list_for_each_entry(cont, &attribute_container_list, node) { |
| 302 | if (cont->match(cont, dev)) |
| 303 | fn(cont, dev); |
| 304 | } |
| 305 | up(&attribute_container_mutex); |
| 306 | } |
| 307 | EXPORT_SYMBOL_GPL(attribute_container_trigger); |
| 308 | |
| 309 | /** |
| 310 | * attribute_container_add_attrs - add attributes |
| 311 | * |
| 312 | * @classdev: The class device |
| 313 | * |
| 314 | * This simply creates all the class device sysfs files from the |
| 315 | * attributes listed in the container |
| 316 | */ |
| 317 | int |
| 318 | attribute_container_add_attrs(struct class_device *classdev) |
| 319 | { |
| 320 | struct attribute_container *cont = |
| 321 | attribute_container_classdev_to_container(classdev); |
| 322 | struct class_device_attribute **attrs = cont->attrs; |
| 323 | int i, error; |
| 324 | |
| 325 | if (!attrs) |
| 326 | return 0; |
| 327 | |
| 328 | for (i = 0; attrs[i]; i++) { |
| 329 | error = class_device_create_file(classdev, attrs[i]); |
| 330 | if (error) |
| 331 | return error; |
| 332 | } |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | EXPORT_SYMBOL_GPL(attribute_container_add_attrs); |
| 337 | |
| 338 | /** |
| 339 | * attribute_container_add_class_device - same function as class_device_add |
| 340 | * |
| 341 | * @classdev: the class device to add |
| 342 | * |
| 343 | * This performs essentially the same function as class_device_add except for |
| 344 | * attribute containers, namely add the classdev to the system and then |
| 345 | * create the attribute files |
| 346 | */ |
| 347 | int |
| 348 | attribute_container_add_class_device(struct class_device *classdev) |
| 349 | { |
| 350 | int error = class_device_add(classdev); |
| 351 | if (error) |
| 352 | return error; |
| 353 | return attribute_container_add_attrs(classdev); |
| 354 | } |
| 355 | EXPORT_SYMBOL_GPL(attribute_container_add_class_device); |
| 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, |
| 366 | struct class_device *classdev) |
| 367 | { |
| 368 | return attribute_container_add_class_device(classdev); |
| 369 | } |
| 370 | EXPORT_SYMBOL_GPL(attribute_container_add_class_device_adapter); |
| 371 | |
| 372 | /** |
| 373 | * attribute_container_remove_attrs - remove any attribute files |
| 374 | * |
| 375 | * @classdev: The class device to remove the files from |
| 376 | * |
| 377 | */ |
| 378 | void |
| 379 | attribute_container_remove_attrs(struct class_device *classdev) |
| 380 | { |
| 381 | struct attribute_container *cont = |
| 382 | attribute_container_classdev_to_container(classdev); |
| 383 | struct class_device_attribute **attrs = cont->attrs; |
| 384 | int i; |
| 385 | |
| 386 | if (!attrs) |
| 387 | return; |
| 388 | |
| 389 | for (i = 0; attrs[i]; i++) |
| 390 | class_device_remove_file(classdev, attrs[i]); |
| 391 | } |
| 392 | EXPORT_SYMBOL_GPL(attribute_container_remove_attrs); |
| 393 | |
| 394 | /** |
| 395 | * attribute_container_class_device_del - equivalent of class_device_del |
| 396 | * |
| 397 | * @classdev: the class device |
| 398 | * |
| 399 | * This function simply removes all the attribute files and then calls |
| 400 | * class_device_del. |
| 401 | */ |
| 402 | void |
| 403 | attribute_container_class_device_del(struct class_device *classdev) |
| 404 | { |
| 405 | attribute_container_remove_attrs(classdev); |
| 406 | class_device_del(classdev); |
| 407 | } |
| 408 | EXPORT_SYMBOL_GPL(attribute_container_class_device_del); |
| 409 | |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 410 | /** |
| 411 | * attribute_container_find_class_device - find the corresponding class_device |
| 412 | * |
| 413 | * @cont: the container |
| 414 | * @dev: the generic device |
| 415 | * |
| 416 | * Looks up the device in the container's list of class devices and returns |
| 417 | * the corresponding class_device. |
| 418 | */ |
| 419 | struct class_device * |
| 420 | attribute_container_find_class_device(struct attribute_container *cont, |
| 421 | struct device *dev) |
| 422 | { |
| 423 | struct class_device *cdev = NULL; |
| 424 | struct internal_container *ic; |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 425 | struct klist_iter iter; |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 426 | |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 427 | klist_for_each_entry(ic, &cont->containers, node, &iter) { |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 428 | if (ic->classdev.dev == dev) { |
| 429 | cdev = &ic->classdev; |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 430 | /* FIXME: must exit iterator then break */ |
| 431 | klist_iter_exit(&iter); |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 432 | break; |
| 433 | } |
| 434 | } |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 435 | |
| 436 | return cdev; |
| 437 | } |
| 438 | EXPORT_SYMBOL_GPL(attribute_container_find_class_device); |
| 439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | int __init |
| 441 | attribute_container_init(void) |
| 442 | { |
| 443 | INIT_LIST_HEAD(&attribute_container_list); |
| 444 | return 0; |
| 445 | } |