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 | |
| 22 | /* This is a private structure used to tie the classdev and the |
| 23 | * container .. it should never be visible outside this file */ |
| 24 | struct internal_container { |
| 25 | struct list_head node; |
| 26 | 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 | */ |
| 37 | struct attribute_container * |
| 38 | attribute_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 | } |
| 44 | EXPORT_SYMBOL_GPL(attribute_container_classdev_to_container); |
| 45 | |
| 46 | static struct list_head attribute_container_list; |
| 47 | |
| 48 | static 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 | */ |
| 56 | int |
| 57 | attribute_container_register(struct attribute_container *cont) |
| 58 | { |
| 59 | INIT_LIST_HEAD(&cont->node); |
| 60 | INIT_LIST_HEAD(&cont->containers); |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 61 | spin_lock_init(&cont->containers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | down(&attribute_container_mutex); |
| 64 | list_add_tail(&cont->node, &attribute_container_list); |
| 65 | up(&attribute_container_mutex); |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | EXPORT_SYMBOL_GPL(attribute_container_register); |
| 70 | |
| 71 | /** |
| 72 | * attribute_container_unregister - remove a container registration |
| 73 | * |
| 74 | * @cont: previously registered container to remove |
| 75 | */ |
| 76 | int |
| 77 | attribute_container_unregister(struct attribute_container *cont) |
| 78 | { |
| 79 | int retval = -EBUSY; |
| 80 | down(&attribute_container_mutex); |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 81 | spin_lock(&cont->containers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | if (!list_empty(&cont->containers)) |
| 83 | goto out; |
| 84 | retval = 0; |
| 85 | list_del(&cont->node); |
| 86 | out: |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 87 | spin_unlock(&cont->containers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | up(&attribute_container_mutex); |
| 89 | return retval; |
| 90 | |
| 91 | } |
| 92 | EXPORT_SYMBOL_GPL(attribute_container_unregister); |
| 93 | |
| 94 | /* private function used as class release */ |
| 95 | static void attribute_container_release(struct class_device *classdev) |
| 96 | { |
| 97 | struct internal_container *ic |
| 98 | = container_of(classdev, struct internal_container, classdev); |
| 99 | struct device *dev = classdev->dev; |
| 100 | |
| 101 | kfree(ic); |
| 102 | put_device(dev); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * attribute_container_add_device - see if any container is interested in dev |
| 107 | * |
| 108 | * @dev: device to add attributes to |
| 109 | * @fn: function to trigger addition of class device. |
| 110 | * |
| 111 | * This function allocates storage for the class device(s) to be |
| 112 | * attached to dev (one for each matching attribute_container). If no |
| 113 | * fn is provided, the code will simply register the class device via |
| 114 | * class_device_add. If a function is provided, it is expected to add |
| 115 | * the class device at the appropriate time. One of the things that |
| 116 | * might be necessary is to allocate and initialise the classdev and |
| 117 | * then add it a later time. To do this, call this routine for |
| 118 | * allocation and initialisation and then use |
| 119 | * attribute_container_device_trigger() to call class_device_add() on |
| 120 | * it. Note: after this, the class device contains a reference to dev |
| 121 | * which is not relinquished until the release of the classdev. |
| 122 | */ |
| 123 | void |
| 124 | attribute_container_add_device(struct device *dev, |
| 125 | int (*fn)(struct attribute_container *, |
| 126 | struct device *, |
| 127 | struct class_device *)) |
| 128 | { |
| 129 | struct attribute_container *cont; |
| 130 | |
| 131 | down(&attribute_container_mutex); |
| 132 | list_for_each_entry(cont, &attribute_container_list, node) { |
| 133 | struct internal_container *ic; |
| 134 | |
| 135 | if (attribute_container_no_classdevs(cont)) |
| 136 | continue; |
| 137 | |
| 138 | if (!cont->match(cont, dev)) |
| 139 | continue; |
| 140 | ic = kmalloc(sizeof(struct internal_container), GFP_KERNEL); |
| 141 | if (!ic) { |
| 142 | dev_printk(KERN_ERR, dev, "failed to allocate class container\n"); |
| 143 | continue; |
| 144 | } |
| 145 | memset(ic, 0, sizeof(struct internal_container)); |
| 146 | INIT_LIST_HEAD(&ic->node); |
| 147 | ic->cont = cont; |
| 148 | class_device_initialize(&ic->classdev); |
| 149 | ic->classdev.dev = get_device(dev); |
| 150 | ic->classdev.class = cont->class; |
| 151 | cont->class->release = attribute_container_release; |
| 152 | strcpy(ic->classdev.class_id, dev->bus_id); |
| 153 | if (fn) |
| 154 | fn(cont, dev, &ic->classdev); |
| 155 | else |
| 156 | attribute_container_add_class_device(&ic->classdev); |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 157 | spin_lock(&cont->containers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | list_add_tail(&ic->node, &cont->containers); |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 159 | spin_unlock(&cont->containers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | up(&attribute_container_mutex); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * attribute_container_remove_device - make device eligible for removal. |
| 166 | * |
| 167 | * @dev: The generic device |
| 168 | * @fn: A function to call to remove the device |
| 169 | * |
| 170 | * This routine triggers device removal. If fn is NULL, then it is |
| 171 | * simply done via class_device_unregister (note that if something |
| 172 | * still has a reference to the classdev, then the memory occupied |
| 173 | * will not be freed until the classdev is released). If you want a |
| 174 | * two phase release: remove from visibility and then delete the |
| 175 | * device, then you should use this routine with a fn that calls |
| 176 | * class_device_del() and then use |
| 177 | * attribute_container_device_trigger() to do the final put on the |
| 178 | * classdev. |
| 179 | */ |
| 180 | void |
| 181 | attribute_container_remove_device(struct device *dev, |
| 182 | void (*fn)(struct attribute_container *, |
| 183 | struct device *, |
| 184 | struct class_device *)) |
| 185 | { |
| 186 | struct attribute_container *cont; |
| 187 | |
| 188 | down(&attribute_container_mutex); |
| 189 | list_for_each_entry(cont, &attribute_container_list, node) { |
| 190 | struct internal_container *ic, *tmp; |
| 191 | |
| 192 | if (attribute_container_no_classdevs(cont)) |
| 193 | continue; |
| 194 | |
| 195 | if (!cont->match(cont, dev)) |
| 196 | continue; |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 197 | spin_lock(&cont->containers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | list_for_each_entry_safe(ic, tmp, &cont->containers, node) { |
| 199 | if (dev != ic->classdev.dev) |
| 200 | continue; |
| 201 | list_del(&ic->node); |
| 202 | if (fn) |
| 203 | fn(cont, dev, &ic->classdev); |
| 204 | else { |
| 205 | attribute_container_remove_attrs(&ic->classdev); |
| 206 | class_device_unregister(&ic->classdev); |
| 207 | } |
| 208 | } |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 209 | spin_unlock(&cont->containers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | up(&attribute_container_mutex); |
| 212 | } |
| 213 | EXPORT_SYMBOL_GPL(attribute_container_remove_device); |
| 214 | |
| 215 | /** |
| 216 | * attribute_container_device_trigger - execute a trigger for each matching classdev |
| 217 | * |
| 218 | * @dev: The generic device to run the trigger for |
| 219 | * @fn the function to execute for each classdev. |
| 220 | * |
| 221 | * This funcion is for executing a trigger when you need to know both |
| 222 | * the container and the classdev. If you only care about the |
| 223 | * container, then use attribute_container_trigger() instead. |
| 224 | */ |
| 225 | void |
| 226 | attribute_container_device_trigger(struct device *dev, |
| 227 | int (*fn)(struct attribute_container *, |
| 228 | struct device *, |
| 229 | struct class_device *)) |
| 230 | { |
| 231 | struct attribute_container *cont; |
| 232 | |
| 233 | down(&attribute_container_mutex); |
| 234 | list_for_each_entry(cont, &attribute_container_list, node) { |
| 235 | struct internal_container *ic, *tmp; |
| 236 | |
| 237 | if (!cont->match(cont, dev)) |
| 238 | continue; |
| 239 | |
James Bottomley | ebd8bb7 | 2005-08-15 16:13:19 -0500 | [diff] [blame] | 240 | if (attribute_container_no_classdevs(cont)) { |
| 241 | fn(cont, dev, NULL); |
| 242 | continue; |
| 243 | } |
| 244 | |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 245 | spin_lock(&cont->containers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | list_for_each_entry_safe(ic, tmp, &cont->containers, node) { |
| 247 | if (dev == ic->classdev.dev) |
| 248 | fn(cont, dev, &ic->classdev); |
| 249 | } |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 250 | spin_unlock(&cont->containers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |
| 252 | up(&attribute_container_mutex); |
| 253 | } |
| 254 | EXPORT_SYMBOL_GPL(attribute_container_device_trigger); |
| 255 | |
| 256 | /** |
| 257 | * attribute_container_trigger - trigger a function for each matching container |
| 258 | * |
| 259 | * @dev: The generic device to activate the trigger for |
| 260 | * @fn: the function to trigger |
| 261 | * |
| 262 | * This routine triggers a function that only needs to know the |
| 263 | * matching containers (not the classdev) associated with a device. |
| 264 | * It is more lightweight than attribute_container_device_trigger, so |
| 265 | * should be used in preference unless the triggering function |
| 266 | * actually needs to know the classdev. |
| 267 | */ |
| 268 | void |
| 269 | attribute_container_trigger(struct device *dev, |
| 270 | int (*fn)(struct attribute_container *, |
| 271 | struct device *)) |
| 272 | { |
| 273 | struct attribute_container *cont; |
| 274 | |
| 275 | down(&attribute_container_mutex); |
| 276 | list_for_each_entry(cont, &attribute_container_list, node) { |
| 277 | if (cont->match(cont, dev)) |
| 278 | fn(cont, dev); |
| 279 | } |
| 280 | up(&attribute_container_mutex); |
| 281 | } |
| 282 | EXPORT_SYMBOL_GPL(attribute_container_trigger); |
| 283 | |
| 284 | /** |
| 285 | * attribute_container_add_attrs - add attributes |
| 286 | * |
| 287 | * @classdev: The class device |
| 288 | * |
| 289 | * This simply creates all the class device sysfs files from the |
| 290 | * attributes listed in the container |
| 291 | */ |
| 292 | int |
| 293 | attribute_container_add_attrs(struct class_device *classdev) |
| 294 | { |
| 295 | struct attribute_container *cont = |
| 296 | attribute_container_classdev_to_container(classdev); |
| 297 | struct class_device_attribute **attrs = cont->attrs; |
| 298 | int i, error; |
| 299 | |
| 300 | if (!attrs) |
| 301 | return 0; |
| 302 | |
| 303 | for (i = 0; attrs[i]; i++) { |
| 304 | error = class_device_create_file(classdev, attrs[i]); |
| 305 | if (error) |
| 306 | return error; |
| 307 | } |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | EXPORT_SYMBOL_GPL(attribute_container_add_attrs); |
| 312 | |
| 313 | /** |
| 314 | * attribute_container_add_class_device - same function as class_device_add |
| 315 | * |
| 316 | * @classdev: the class device to add |
| 317 | * |
| 318 | * This performs essentially the same function as class_device_add except for |
| 319 | * attribute containers, namely add the classdev to the system and then |
| 320 | * create the attribute files |
| 321 | */ |
| 322 | int |
| 323 | attribute_container_add_class_device(struct class_device *classdev) |
| 324 | { |
| 325 | int error = class_device_add(classdev); |
| 326 | if (error) |
| 327 | return error; |
| 328 | return attribute_container_add_attrs(classdev); |
| 329 | } |
| 330 | EXPORT_SYMBOL_GPL(attribute_container_add_class_device); |
| 331 | |
| 332 | /** |
| 333 | * attribute_container_add_class_device_adapter - simple adapter for triggers |
| 334 | * |
| 335 | * This function is identical to attribute_container_add_class_device except |
| 336 | * that it is designed to be called from the triggers |
| 337 | */ |
| 338 | int |
| 339 | attribute_container_add_class_device_adapter(struct attribute_container *cont, |
| 340 | struct device *dev, |
| 341 | struct class_device *classdev) |
| 342 | { |
| 343 | return attribute_container_add_class_device(classdev); |
| 344 | } |
| 345 | EXPORT_SYMBOL_GPL(attribute_container_add_class_device_adapter); |
| 346 | |
| 347 | /** |
| 348 | * attribute_container_remove_attrs - remove any attribute files |
| 349 | * |
| 350 | * @classdev: The class device to remove the files from |
| 351 | * |
| 352 | */ |
| 353 | void |
| 354 | attribute_container_remove_attrs(struct class_device *classdev) |
| 355 | { |
| 356 | struct attribute_container *cont = |
| 357 | attribute_container_classdev_to_container(classdev); |
| 358 | struct class_device_attribute **attrs = cont->attrs; |
| 359 | int i; |
| 360 | |
| 361 | if (!attrs) |
| 362 | return; |
| 363 | |
| 364 | for (i = 0; attrs[i]; i++) |
| 365 | class_device_remove_file(classdev, attrs[i]); |
| 366 | } |
| 367 | EXPORT_SYMBOL_GPL(attribute_container_remove_attrs); |
| 368 | |
| 369 | /** |
| 370 | * attribute_container_class_device_del - equivalent of class_device_del |
| 371 | * |
| 372 | * @classdev: the class device |
| 373 | * |
| 374 | * This function simply removes all the attribute files and then calls |
| 375 | * class_device_del. |
| 376 | */ |
| 377 | void |
| 378 | attribute_container_class_device_del(struct class_device *classdev) |
| 379 | { |
| 380 | attribute_container_remove_attrs(classdev); |
| 381 | class_device_del(classdev); |
| 382 | } |
| 383 | EXPORT_SYMBOL_GPL(attribute_container_class_device_del); |
| 384 | |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 385 | /** |
| 386 | * attribute_container_find_class_device - find the corresponding class_device |
| 387 | * |
| 388 | * @cont: the container |
| 389 | * @dev: the generic device |
| 390 | * |
| 391 | * Looks up the device in the container's list of class devices and returns |
| 392 | * the corresponding class_device. |
| 393 | */ |
| 394 | struct class_device * |
| 395 | attribute_container_find_class_device(struct attribute_container *cont, |
| 396 | struct device *dev) |
| 397 | { |
| 398 | struct class_device *cdev = NULL; |
| 399 | struct internal_container *ic; |
| 400 | |
| 401 | spin_lock(&cont->containers_lock); |
| 402 | list_for_each_entry(ic, &cont->containers, node) { |
| 403 | if (ic->classdev.dev == dev) { |
| 404 | cdev = &ic->classdev; |
| 405 | break; |
| 406 | } |
| 407 | } |
| 408 | spin_unlock(&cont->containers_lock); |
| 409 | |
| 410 | return cdev; |
| 411 | } |
| 412 | EXPORT_SYMBOL_GPL(attribute_container_find_class_device); |
| 413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | int __init |
| 415 | attribute_container_init(void) |
| 416 | { |
| 417 | INIT_LIST_HEAD(&attribute_container_list); |
| 418 | return 0; |
| 419 | } |