attribute_container.c 12 KB

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