attribute_container.c 11 KB

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