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