attribute_container.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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/device.h>
  15. #include <linux/kernel.h>
  16. #include <linux/slab.h>
  17. #include <linux/list.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include "base.h"
  21. /* This is a private structure used to tie the classdev and the
  22. * container .. it should never be visible outside this file */
  23. struct internal_container {
  24. struct klist_node node;
  25. struct attribute_container *cont;
  26. struct device classdev;
  27. };
  28. static void internal_container_klist_get(struct klist_node *n)
  29. {
  30. struct internal_container *ic =
  31. container_of(n, struct internal_container, node);
  32. get_device(&ic->classdev);
  33. }
  34. static void internal_container_klist_put(struct klist_node *n)
  35. {
  36. struct internal_container *ic =
  37. container_of(n, struct internal_container, node);
  38. put_device(&ic->classdev);
  39. }
  40. /**
  41. * attribute_container_classdev_to_container - given a classdev, return the container
  42. *
  43. * @classdev: the class device created by attribute_container_add_device.
  44. *
  45. * Returns the container associated with this classdev.
  46. */
  47. struct attribute_container *
  48. attribute_container_classdev_to_container(struct device *classdev)
  49. {
  50. struct internal_container *ic =
  51. container_of(classdev, struct internal_container, classdev);
  52. return ic->cont;
  53. }
  54. EXPORT_SYMBOL_GPL(attribute_container_classdev_to_container);
  55. static LIST_HEAD(attribute_container_list);
  56. static DEFINE_MUTEX(attribute_container_mutex);
  57. /**
  58. * attribute_container_register - register an attribute container
  59. *
  60. * @cont: The container to register. This must be allocated by the
  61. * callee and should also be zeroed by it.
  62. */
  63. int
  64. attribute_container_register(struct attribute_container *cont)
  65. {
  66. INIT_LIST_HEAD(&cont->node);
  67. klist_init(&cont->containers, internal_container_klist_get,
  68. internal_container_klist_put);
  69. mutex_lock(&attribute_container_mutex);
  70. list_add_tail(&cont->node, &attribute_container_list);
  71. mutex_unlock(&attribute_container_mutex);
  72. return 0;
  73. }
  74. EXPORT_SYMBOL_GPL(attribute_container_register);
  75. /**
  76. * attribute_container_unregister - remove a container registration
  77. *
  78. * @cont: previously registered container to remove
  79. */
  80. int
  81. attribute_container_unregister(struct attribute_container *cont)
  82. {
  83. int retval = -EBUSY;
  84. mutex_lock(&attribute_container_mutex);
  85. spin_lock(&cont->containers.k_lock);
  86. if (!list_empty(&cont->containers.k_list))
  87. goto out;
  88. retval = 0;
  89. list_del(&cont->node);
  90. out:
  91. spin_unlock(&cont->containers.k_lock);
  92. mutex_unlock(&attribute_container_mutex);
  93. return retval;
  94. }
  95. EXPORT_SYMBOL_GPL(attribute_container_unregister);
  96. /* private function used as class release */
  97. static void attribute_container_release(struct device *classdev)
  98. {
  99. struct internal_container *ic
  100. = container_of(classdev, struct internal_container, classdev);
  101. struct device *dev = classdev->parent;
  102. kfree(ic);
  103. put_device(dev);
  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. * 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 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 device *))
  128. {
  129. struct attribute_container *cont;
  130. mutex_lock(&attribute_container_mutex);
  131. list_for_each_entry(cont, &attribute_container_list, node) {
  132. struct internal_container *ic;
  133. if (attribute_container_no_classdevs(cont))
  134. continue;
  135. if (!cont->match(cont, dev))
  136. continue;
  137. ic = kzalloc(sizeof(*ic), GFP_KERNEL);
  138. if (!ic) {
  139. dev_err(dev, "failed to allocate class container\n");
  140. continue;
  141. }
  142. ic->cont = cont;
  143. device_initialize(&ic->classdev);
  144. ic->classdev.parent = get_device(dev);
  145. ic->classdev.class = cont->class;
  146. cont->class->dev_release = attribute_container_release;
  147. dev_set_name(&ic->classdev, "%s", dev_name(dev));
  148. if (fn)
  149. fn(cont, dev, &ic->classdev);
  150. else
  151. attribute_container_add_class_device(&ic->classdev);
  152. klist_add_tail(&ic->node, &cont->containers);
  153. }
  154. mutex_unlock(&attribute_container_mutex);
  155. }
  156. /* FIXME: can't break out of this unless klist_iter_exit is also
  157. * called before doing the break
  158. */
  159. #define klist_for_each_entry(pos, head, member, iter) \
  160. for (klist_iter_init(head, iter); (pos = ({ \
  161. struct klist_node *n = klist_next(iter); \
  162. n ? container_of(n, typeof(*pos), member) : \
  163. ({ klist_iter_exit(iter) ; NULL; }); \
  164. })) != NULL;)
  165. /**
  166. * attribute_container_remove_device - make device eligible for removal.
  167. *
  168. * @dev: The generic device
  169. * @fn: A function to call to remove the device
  170. *
  171. * This routine triggers device removal. If fn is NULL, then it is
  172. * simply done via device_unregister (note that if something
  173. * still has a reference to the classdev, then the memory occupied
  174. * will not be freed until the classdev is released). If you want a
  175. * two phase release: remove from visibility and then delete the
  176. * device, then you should use this routine with a fn that calls
  177. * device_del() and then use attribute_container_device_trigger()
  178. * to do the final put on the classdev.
  179. */
  180. void
  181. attribute_container_remove_device(struct device *dev,
  182. void (*fn)(struct attribute_container *,
  183. struct device *,
  184. struct device *))
  185. {
  186. struct attribute_container *cont;
  187. mutex_lock(&attribute_container_mutex);
  188. list_for_each_entry(cont, &attribute_container_list, node) {
  189. struct internal_container *ic;
  190. struct klist_iter iter;
  191. if (attribute_container_no_classdevs(cont))
  192. continue;
  193. if (!cont->match(cont, dev))
  194. continue;
  195. klist_for_each_entry(ic, &cont->containers, node, &iter) {
  196. if (dev != ic->classdev.parent)
  197. continue;
  198. klist_del(&ic->node);
  199. if (fn)
  200. fn(cont, dev, &ic->classdev);
  201. else {
  202. attribute_container_remove_attrs(&ic->classdev);
  203. device_unregister(&ic->classdev);
  204. }
  205. }
  206. }
  207. mutex_unlock(&attribute_container_mutex);
  208. }
  209. /**
  210. * attribute_container_device_trigger - execute a trigger for each matching classdev
  211. *
  212. * @dev: The generic device to run the trigger for
  213. * @fn the function to execute for each classdev.
  214. *
  215. * This funcion is for executing a trigger when you need to know both
  216. * the container and the classdev. If you only care about the
  217. * container, then use attribute_container_trigger() instead.
  218. */
  219. void
  220. attribute_container_device_trigger(struct device *dev,
  221. int (*fn)(struct attribute_container *,
  222. struct device *,
  223. struct device *))
  224. {
  225. struct attribute_container *cont;
  226. mutex_lock(&attribute_container_mutex);
  227. list_for_each_entry(cont, &attribute_container_list, node) {
  228. struct internal_container *ic;
  229. struct klist_iter iter;
  230. if (!cont->match(cont, dev))
  231. continue;
  232. if (attribute_container_no_classdevs(cont)) {
  233. fn(cont, dev, NULL);
  234. continue;
  235. }
  236. klist_for_each_entry(ic, &cont->containers, node, &iter) {
  237. if (dev == ic->classdev.parent)
  238. fn(cont, dev, &ic->classdev);
  239. }
  240. }
  241. mutex_unlock(&attribute_container_mutex);
  242. }
  243. /**
  244. * attribute_container_trigger - trigger a function for each matching container
  245. *
  246. * @dev: The generic device to activate the trigger for
  247. * @fn: the function to trigger
  248. *
  249. * This routine triggers a function that only needs to know the
  250. * matching containers (not the classdev) associated with a device.
  251. * It is more lightweight than attribute_container_device_trigger, so
  252. * should be used in preference unless the triggering function
  253. * actually needs to know the classdev.
  254. */
  255. void
  256. attribute_container_trigger(struct device *dev,
  257. int (*fn)(struct attribute_container *,
  258. struct device *))
  259. {
  260. struct attribute_container *cont;
  261. mutex_lock(&attribute_container_mutex);
  262. list_for_each_entry(cont, &attribute_container_list, node) {
  263. if (cont->match(cont, dev))
  264. fn(cont, dev);
  265. }
  266. mutex_unlock(&attribute_container_mutex);
  267. }
  268. /**
  269. * attribute_container_add_attrs - add attributes
  270. *
  271. * @classdev: The class device
  272. *
  273. * This simply creates all the class device sysfs files from the
  274. * attributes listed in the container
  275. */
  276. int
  277. attribute_container_add_attrs(struct device *classdev)
  278. {
  279. struct attribute_container *cont =
  280. attribute_container_classdev_to_container(classdev);
  281. struct device_attribute **attrs = cont->attrs;
  282. int i, error;
  283. BUG_ON(attrs && cont->grp);
  284. if (!attrs && !cont->grp)
  285. return 0;
  286. if (cont->grp)
  287. return sysfs_create_group(&classdev->kobj, cont->grp);
  288. for (i = 0; attrs[i]; i++) {
  289. sysfs_attr_init(&attrs[i]->attr);
  290. error = device_create_file(classdev, attrs[i]);
  291. if (error)
  292. return error;
  293. }
  294. return 0;
  295. }
  296. /**
  297. * attribute_container_add_class_device - same function as device_add
  298. *
  299. * @classdev: the class device to add
  300. *
  301. * This performs essentially the same function as device_add except for
  302. * attribute containers, namely add the classdev to the system and then
  303. * create the attribute files
  304. */
  305. int
  306. attribute_container_add_class_device(struct device *classdev)
  307. {
  308. int error = device_add(classdev);
  309. if (error)
  310. return error;
  311. return attribute_container_add_attrs(classdev);
  312. }
  313. /**
  314. * attribute_container_add_class_device_adapter - simple adapter for triggers
  315. *
  316. * This function is identical to attribute_container_add_class_device except
  317. * that it is designed to be called from the triggers
  318. */
  319. int
  320. attribute_container_add_class_device_adapter(struct attribute_container *cont,
  321. struct device *dev,
  322. struct device *classdev)
  323. {
  324. return attribute_container_add_class_device(classdev);
  325. }
  326. /**
  327. * attribute_container_remove_attrs - remove any attribute files
  328. *
  329. * @classdev: The class device to remove the files from
  330. *
  331. */
  332. void
  333. attribute_container_remove_attrs(struct device *classdev)
  334. {
  335. struct attribute_container *cont =
  336. attribute_container_classdev_to_container(classdev);
  337. struct device_attribute **attrs = cont->attrs;
  338. int i;
  339. if (!attrs && !cont->grp)
  340. return;
  341. if (cont->grp) {
  342. sysfs_remove_group(&classdev->kobj, cont->grp);
  343. return ;
  344. }
  345. for (i = 0; attrs[i]; i++)
  346. device_remove_file(classdev, attrs[i]);
  347. }
  348. /**
  349. * attribute_container_class_device_del - equivalent of class_device_del
  350. *
  351. * @classdev: the class device
  352. *
  353. * This function simply removes all the attribute files and then calls
  354. * device_del.
  355. */
  356. void
  357. attribute_container_class_device_del(struct device *classdev)
  358. {
  359. attribute_container_remove_attrs(classdev);
  360. device_del(classdev);
  361. }
  362. /**
  363. * attribute_container_find_class_device - find the corresponding class_device
  364. *
  365. * @cont: the container
  366. * @dev: the generic device
  367. *
  368. * Looks up the device in the container's list of class devices and returns
  369. * the corresponding class_device.
  370. */
  371. struct device *
  372. attribute_container_find_class_device(struct attribute_container *cont,
  373. struct device *dev)
  374. {
  375. struct device *cdev = NULL;
  376. struct internal_container *ic;
  377. struct klist_iter iter;
  378. klist_for_each_entry(ic, &cont->containers, node, &iter) {
  379. if (ic->classdev.parent == dev) {
  380. cdev = &ic->classdev;
  381. /* FIXME: must exit iterator then break */
  382. klist_iter_exit(&iter);
  383. break;
  384. }
  385. }
  386. return cdev;
  387. }
  388. EXPORT_SYMBOL_GPL(attribute_container_find_class_device);