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#include "base.h" 23 24/* This is a private structure used to tie the classdev and the 25 * container .. it should never be visible outside this file */ 26struct internal_container { 27 struct klist_node node; 28 struct attribute_container *cont; 29 struct class_device classdev; 30}; 31 32static 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 39static 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 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 */ 54struct attribute_container * 55attribute_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} 61EXPORT_SYMBOL_GPL(attribute_container_classdev_to_container); 62 63static struct list_head attribute_container_list; 64 65static DEFINE_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 */ 73int 74attribute_container_register(struct attribute_container *cont) 75{ 76 INIT_LIST_HEAD(&cont->node); 77 klist_init(&cont->containers,internal_container_klist_get, 78 internal_container_klist_put); 79 80 mutex_lock(&attribute_container_mutex); 81 list_add_tail(&cont->node, &attribute_container_list); 82 mutex_unlock(&attribute_container_mutex); 83 84 return 0; 85} 86EXPORT_SYMBOL_GPL(attribute_container_register); 87 88/** 89 * attribute_container_unregister - remove a container registration 90 * 91 * @cont: previously registered container to remove 92 */ 93int 94attribute_container_unregister(struct attribute_container *cont) 95{ 96 int retval = -EBUSY; 97 mutex_lock(&attribute_container_mutex); 98 spin_lock(&cont->containers.k_lock); 99 if (!list_empty(&cont->containers.k_list)) 100 goto out; 101 retval = 0; 102 list_del(&cont->node); 103 out: 104 spin_unlock(&cont->containers.k_lock); 105 mutex_unlock(&attribute_container_mutex); 106 return retval; 107 108} 109EXPORT_SYMBOL_GPL(attribute_container_unregister); 110 111/* private function used as class release */ 112static 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 */ 140void 141attribute_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 mutex_lock(&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; 157 158 ic = kzalloc(sizeof(*ic), GFP_KERNEL); 159 if (!ic) { 160 dev_printk(KERN_ERR, dev, "failed to allocate class container\n"); 161 continue; 162 } 163 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); 174 klist_add_tail(&ic->node, &cont->containers); 175 } 176 mutex_unlock(&attribute_container_mutex); 177} 178 179#define klist_for_each_entry(pos, head, member, iter) \ 180 for (klist_iter_init(head, iter); (pos = ({ \ 181 struct klist_node *n = klist_next(iter); \ 182 n ? container_of(n, typeof(*pos), member) : \ 183 ({ klist_iter_exit(iter) ; NULL; }); \ 184 }) ) != NULL; ) 185 186 187/** 188 * attribute_container_remove_device - make device eligible for removal. 189 * 190 * @dev: The generic device 191 * @fn: A function to call to remove the device 192 * 193 * This routine triggers device removal. If fn is NULL, then it is 194 * simply done via class_device_unregister (note that if something 195 * still has a reference to the classdev, then the memory occupied 196 * will not be freed until the classdev is released). If you want a 197 * two phase release: remove from visibility and then delete the 198 * device, then you should use this routine with a fn that calls 199 * class_device_del() and then use 200 * attribute_container_device_trigger() to do the final put on the 201 * classdev. 202 */ 203void 204attribute_container_remove_device(struct device *dev, 205 void (*fn)(struct attribute_container *, 206 struct device *, 207 struct class_device *)) 208{ 209 struct attribute_container *cont; 210 211 mutex_lock(&attribute_container_mutex); 212 list_for_each_entry(cont, &attribute_container_list, node) { 213 struct internal_container *ic; 214 struct klist_iter iter; 215 216 if (attribute_container_no_classdevs(cont)) 217 continue; 218 219 if (!cont->match(cont, dev)) 220 continue; 221 222 klist_for_each_entry(ic, &cont->containers, node, &iter) { 223 if (dev != ic->classdev.dev) 224 continue; 225 klist_del(&ic->node); 226 if (fn) 227 fn(cont, dev, &ic->classdev); 228 else { 229 attribute_container_remove_attrs(&ic->classdev); 230 class_device_unregister(&ic->classdev); 231 } 232 } 233 } 234 mutex_unlock(&attribute_container_mutex); 235} 236 237/** 238 * attribute_container_device_trigger - execute a trigger for each matching classdev 239 * 240 * @dev: The generic device to run the trigger for 241 * @fn the function to execute for each classdev. 242 * 243 * This funcion is for executing a trigger when you need to know both 244 * the container and the classdev. If you only care about the 245 * container, then use attribute_container_trigger() instead. 246 */ 247void 248attribute_container_device_trigger(struct device *dev, 249 int (*fn)(struct attribute_container *, 250 struct device *, 251 struct class_device *)) 252{ 253 struct attribute_container *cont; 254 255 mutex_lock(&attribute_container_mutex); 256 list_for_each_entry(cont, &attribute_container_list, node) { 257 struct internal_container *ic; 258 struct klist_iter iter; 259 260 if (!cont->match(cont, dev)) 261 continue; 262 263 if (attribute_container_no_classdevs(cont)) { 264 fn(cont, dev, NULL); 265 continue; 266 } 267 268 klist_for_each_entry(ic, &cont->containers, node, &iter) { 269 if (dev == ic->classdev.dev) 270 fn(cont, dev, &ic->classdev); 271 } 272 } 273 mutex_unlock(&attribute_container_mutex); 274} 275 276/** 277 * attribute_container_trigger - trigger a function for each matching container 278 * 279 * @dev: The generic device to activate the trigger for 280 * @fn: the function to trigger 281 * 282 * This routine triggers a function that only needs to know the 283 * matching containers (not the classdev) associated with a device. 284 * It is more lightweight than attribute_container_device_trigger, so 285 * should be used in preference unless the triggering function 286 * actually needs to know the classdev. 287 */ 288void 289attribute_container_trigger(struct device *dev, 290 int (*fn)(struct attribute_container *, 291 struct device *)) 292{ 293 struct attribute_container *cont; 294 295 mutex_lock(&attribute_container_mutex); 296 list_for_each_entry(cont, &attribute_container_list, node) { 297 if (cont->match(cont, dev)) 298 fn(cont, dev); 299 } 300 mutex_unlock(&attribute_container_mutex); 301} 302 303/** 304 * attribute_container_add_attrs - add attributes 305 * 306 * @classdev: The class device 307 * 308 * This simply creates all the class device sysfs files from the 309 * attributes listed in the container 310 */ 311int 312attribute_container_add_attrs(struct class_device *classdev) 313{ 314 struct attribute_container *cont = 315 attribute_container_classdev_to_container(classdev); 316 struct class_device_attribute **attrs = cont->attrs; 317 int i, error; 318 319 if (!attrs) 320 return 0; 321 322 for (i = 0; attrs[i]; i++) { 323 error = class_device_create_file(classdev, attrs[i]); 324 if (error) 325 return error; 326 } 327 328 return 0; 329} 330 331/** 332 * attribute_container_add_class_device - same function as class_device_add 333 * 334 * @classdev: the class device to add 335 * 336 * This performs essentially the same function as class_device_add except for 337 * attribute containers, namely add the classdev to the system and then 338 * create the attribute files 339 */ 340int 341attribute_container_add_class_device(struct class_device *classdev) 342{ 343 int error = class_device_add(classdev); 344 if (error) 345 return error; 346 return attribute_container_add_attrs(classdev); 347} 348 349/** 350 * attribute_container_add_class_device_adapter - simple adapter for triggers 351 * 352 * This function is identical to attribute_container_add_class_device except 353 * that it is designed to be called from the triggers 354 */ 355int 356attribute_container_add_class_device_adapter(struct attribute_container *cont, 357 struct device *dev, 358 struct class_device *classdev) 359{ 360 return attribute_container_add_class_device(classdev); 361} 362 363/** 364 * attribute_container_remove_attrs - remove any attribute files 365 * 366 * @classdev: The class device to remove the files from 367 * 368 */ 369void 370attribute_container_remove_attrs(struct class_device *classdev) 371{ 372 struct attribute_container *cont = 373 attribute_container_classdev_to_container(classdev); 374 struct class_device_attribute **attrs = cont->attrs; 375 int i; 376 377 if (!attrs) 378 return; 379 380 for (i = 0; attrs[i]; i++) 381 class_device_remove_file(classdev, attrs[i]); 382} 383 384/** 385 * attribute_container_class_device_del - equivalent of class_device_del 386 * 387 * @classdev: the class device 388 * 389 * This function simply removes all the attribute files and then calls 390 * class_device_del. 391 */ 392void 393attribute_container_class_device_del(struct class_device *classdev) 394{ 395 attribute_container_remove_attrs(classdev); 396 class_device_del(classdev); 397} 398 399/** 400 * attribute_container_find_class_device - find the corresponding class_device 401 * 402 * @cont: the container 403 * @dev: the generic device 404 * 405 * Looks up the device in the container's list of class devices and returns 406 * the corresponding class_device. 407 */ 408struct class_device * 409attribute_container_find_class_device(struct attribute_container *cont, 410 struct device *dev) 411{ 412 struct class_device *cdev = NULL; 413 struct internal_container *ic; 414 struct klist_iter iter; 415 416 klist_for_each_entry(ic, &cont->containers, node, &iter) { 417 if (ic->classdev.dev == dev) { 418 cdev = &ic->classdev; 419 klist_iter_exit(&iter); 420 break; 421 } 422 } 423 424 return cdev; 425} 426EXPORT_SYMBOL_GPL(attribute_container_find_class_device); 427 428int __init 429attribute_container_init(void) 430{ 431 INIT_LIST_HEAD(&attribute_container_list); 432 return 0; 433} 434