attribute_container.c 13 KB

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