attribute_container.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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 <[email protected]>
  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. int
  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. return 0;
  72. }
  73. EXPORT_SYMBOL_GPL(attribute_container_register);
  74. /**
  75. * attribute_container_unregister - remove a container registration
  76. *
  77. * @cont: previously registered container to remove
  78. */
  79. int
  80. attribute_container_unregister(struct attribute_container *cont)
  81. {
  82. int retval = -EBUSY;
  83. mutex_lock(&attribute_container_mutex);
  84. spin_lock(&cont->containers.k_lock);
  85. if (!list_empty(&cont->containers.k_list))
  86. goto out;
  87. retval = 0;
  88. list_del(&cont->node);
  89. out:
  90. spin_unlock(&cont->containers.k_lock);
  91. mutex_unlock(&attribute_container_mutex);
  92. return retval;
  93. }
  94. EXPORT_SYMBOL_GPL(attribute_container_unregister);
  95. /* private function used as class release */
  96. static void attribute_container_release(struct device *classdev)
  97. {
  98. struct internal_container *ic
  99. = container_of(classdev, struct internal_container, classdev);
  100. struct device *dev = classdev->parent;
  101. kfree(ic);
  102. put_device(dev);
  103. }
  104. /**
  105. * attribute_container_add_device - see if any container is interested in dev
  106. *
  107. * @dev: device to add attributes to
  108. * @fn: function to trigger addition of class device.
  109. *
  110. * This function allocates storage for the class device(s) to be
  111. * attached to dev (one for each matching attribute_container). If no
  112. * fn is provided, the code will simply register the class device via
  113. * device_add. If a function is provided, it is expected to add
  114. * the class device at the appropriate time. One of the things that
  115. * might be necessary is to allocate and initialise the classdev and
  116. * then add it a later time. To do this, call this routine for
  117. * allocation and initialisation and then use
  118. * attribute_container_device_trigger() to call device_add() on
  119. * it. Note: after this, the class device contains a reference to dev
  120. * which is not relinquished until the release of the classdev.
  121. */
  122. void
  123. attribute_container_add_device(struct device *dev,
  124. int (*fn)(struct attribute_container *,
  125. struct device *,
  126. struct device *))
  127. {
  128. struct attribute_container *cont;
  129. mutex_lock(&attribute_container_mutex);
  130. list_for_each_entry(cont, &attribute_container_list, node) {
  131. struct internal_container *ic;
  132. if (attribute_container_no_classdevs(cont))
  133. continue;
  134. if (!cont->match(cont, dev))
  135. continue;
  136. ic = kzalloc(sizeof(*ic), GFP_KERNEL);
  137. if (!ic) {
  138. dev_err(dev, "failed to allocate class container\n");
  139. continue;
  140. }
  141. ic->cont = cont;
  142. device_initialize(&ic->classdev);
  143. ic->classdev.parent = get_device(dev);
  144. ic->classdev.class = cont->class;
  145. cont->class->dev_release = attribute_container_release;
  146. dev_set_name(&ic->classdev, "%s", dev_name(dev));
  147. if (fn)
  148. fn(cont, dev, &ic->classdev);
  149. else
  150. attribute_container_add_class_device(&ic->classdev);
  151. klist_add_tail(&ic->node, &cont->containers);
  152. }
  153. mutex_unlock(&attribute_container_mutex);
  154. }
  155. /* FIXME: can't break out of this unless klist_iter_exit is also
  156. * called before doing the break
  157. */
  158. #define klist_for_each_entry(pos, head, member, iter) \
  159. for (klist_iter_init(head, iter); (pos = ({ \
  160. struct klist_node *n = klist_next(iter); \
  161. n ? container_of(n, typeof(*pos), member) : \
  162. ({ klist_iter_exit(iter) ; NULL; }); \
  163. })) != NULL;)
  164. /**
  165. * attribute_container_remove_device - make device eligible for removal.
  166. *
  167. * @dev: The generic device
  168. * @fn: A function to call to remove the device
  169. *
  170. * This routine triggers device removal. If fn is NULL, then it is
  171. * simply done via device_unregister (note that if something
  172. * still has a reference to the classdev, then the memory occupied
  173. * will not be freed until the classdev is released). If you want a
  174. * two phase release: remove from visibility and then delete the
  175. * device, then you should use this routine with a fn that calls
  176. * device_del() and then use attribute_container_device_trigger()
  177. * to do the final put on the classdev.
  178. */
  179. void
  180. attribute_container_remove_device(struct device *dev,
  181. void (*fn)(struct attribute_container *,
  182. struct device *,
  183. struct device *))
  184. {
  185. struct attribute_container *cont;
  186. mutex_lock(&attribute_container_mutex);
  187. list_for_each_entry(cont, &attribute_container_list, node) {
  188. struct internal_container *ic;
  189. struct klist_iter iter;
  190. if (attribute_container_no_classdevs(cont))
  191. continue;
  192. if (!cont->match(cont, dev))
  193. continue;
  194. klist_for_each_entry(ic, &cont->containers, node, &iter) {
  195. if (dev != ic->classdev.parent)
  196. continue;
  197. klist_del(&ic->node);
  198. if (fn)
  199. fn(cont, dev, &ic->classdev);
  200. else {
  201. attribute_container_remove_attrs(&ic->classdev);
  202. device_unregister(&ic->classdev);
  203. }
  204. }
  205. }
  206. mutex_unlock(&attribute_container_mutex);
  207. }
  208. static int
  209. do_attribute_container_device_trigger_safe(struct device *dev,
  210. struct attribute_container *cont,
  211. int (*fn)(struct attribute_container *,
  212. struct device *, struct device *),
  213. int (*undo)(struct attribute_container *,
  214. struct device *, struct device *))
  215. {
  216. int ret;
  217. struct internal_container *ic, *failed;
  218. struct klist_iter iter;
  219. if (attribute_container_no_classdevs(cont))
  220. return fn(cont, dev, NULL);
  221. klist_for_each_entry(ic, &cont->containers, node, &iter) {
  222. if (dev == ic->classdev.parent) {
  223. ret = fn(cont, dev, &ic->classdev);
  224. if (ret) {
  225. failed = ic;
  226. klist_iter_exit(&iter);
  227. goto fail;
  228. }
  229. }
  230. }
  231. return 0;
  232. fail:
  233. if (!undo)
  234. return ret;
  235. /* Attempt to undo the work partially done. */
  236. klist_for_each_entry(ic, &cont->containers, node, &iter) {
  237. if (ic == failed) {
  238. klist_iter_exit(&iter);
  239. break;
  240. }
  241. if (dev == ic->classdev.parent)
  242. undo(cont, dev, &ic->classdev);
  243. }
  244. return ret;
  245. }
  246. /**
  247. * attribute_container_device_trigger_safe - execute a trigger for each
  248. * matching classdev or fail all of them.
  249. *
  250. * @dev: The generic device to run the trigger for
  251. * @fn: the function to execute for each classdev.
  252. * @undo: A function to undo the work previously done in case of error
  253. *
  254. * This function is a safe version of
  255. * attribute_container_device_trigger. It stops on the first error and
  256. * undo the partial work that has been done, on previous classdev. It
  257. * is guaranteed that either they all succeeded, or none of them
  258. * succeeded.
  259. */
  260. int
  261. attribute_container_device_trigger_safe(struct device *dev,
  262. int (*fn)(struct attribute_container *,
  263. struct device *,
  264. struct device *),
  265. int (*undo)(struct attribute_container *,
  266. struct device *,
  267. struct device *))
  268. {
  269. struct attribute_container *cont, *failed = NULL;
  270. int ret = 0;
  271. mutex_lock(&attribute_container_mutex);
  272. list_for_each_entry(cont, &attribute_container_list, node) {
  273. if (!cont->match(cont, dev))
  274. continue;
  275. ret = do_attribute_container_device_trigger_safe(dev, cont,
  276. fn, undo);
  277. if (ret) {
  278. failed = cont;
  279. break;
  280. }
  281. }
  282. if (ret && !WARN_ON(!undo)) {
  283. list_for_each_entry(cont, &attribute_container_list, node) {
  284. if (failed == cont)
  285. break;
  286. if (!cont->match(cont, dev))
  287. continue;
  288. do_attribute_container_device_trigger_safe(dev, cont,
  289. undo, NULL);
  290. }
  291. }
  292. mutex_unlock(&attribute_container_mutex);
  293. return ret;
  294. }
  295. /**
  296. * attribute_container_device_trigger - execute a trigger for each matching classdev
  297. *
  298. * @dev: The generic device to run the trigger for
  299. * @fn: the function to execute for each classdev.
  300. *
  301. * This function is for executing a trigger when you need to know both
  302. * the container and the classdev. If you only care about the
  303. * container, then use attribute_container_trigger() instead.
  304. */
  305. void
  306. attribute_container_device_trigger(struct device *dev,
  307. int (*fn)(struct attribute_container *,
  308. struct device *,
  309. struct device *))
  310. {
  311. struct attribute_container *cont;
  312. mutex_lock(&attribute_container_mutex);
  313. list_for_each_entry(cont, &attribute_container_list, node) {
  314. struct internal_container *ic;
  315. struct klist_iter iter;
  316. if (!cont->match(cont, dev))
  317. continue;
  318. if (attribute_container_no_classdevs(cont)) {
  319. fn(cont, dev, NULL);
  320. continue;
  321. }
  322. klist_for_each_entry(ic, &cont->containers, node, &iter) {
  323. if (dev == ic->classdev.parent)
  324. fn(cont, dev, &ic->classdev);
  325. }
  326. }
  327. mutex_unlock(&attribute_container_mutex);
  328. }
  329. /**
  330. * attribute_container_trigger - trigger a function for each matching container
  331. *
  332. * @dev: The generic device to activate the trigger for
  333. * @fn: the function to trigger
  334. *
  335. * This routine triggers a function that only needs to know the
  336. * matching containers (not the classdev) associated with a device.
  337. * It is more lightweight than attribute_container_device_trigger, so
  338. * should be used in preference unless the triggering function
  339. * actually needs to know the classdev.
  340. */
  341. void
  342. attribute_container_trigger(struct device *dev,
  343. int (*fn)(struct attribute_container *,
  344. struct device *))
  345. {
  346. struct attribute_container *cont;
  347. mutex_lock(&attribute_container_mutex);
  348. list_for_each_entry(cont, &attribute_container_list, node) {
  349. if (cont->match(cont, dev))
  350. fn(cont, dev);
  351. }
  352. mutex_unlock(&attribute_container_mutex);
  353. }
  354. /**
  355. * attribute_container_add_attrs - add attributes
  356. *
  357. * @classdev: The class device
  358. *
  359. * This simply creates all the class device sysfs files from the
  360. * attributes listed in the container
  361. */
  362. int
  363. attribute_container_add_attrs(struct device *classdev)
  364. {
  365. struct attribute_container *cont =
  366. attribute_container_classdev_to_container(classdev);
  367. struct device_attribute **attrs = cont->attrs;
  368. int i, error;
  369. BUG_ON(attrs && cont->grp);
  370. if (!attrs && !cont->grp)
  371. return 0;
  372. if (cont->grp)
  373. return sysfs_create_group(&classdev->kobj, cont->grp);
  374. for (i = 0; attrs[i]; i++) {
  375. sysfs_attr_init(&attrs[i]->attr);
  376. error = device_create_file(classdev, attrs[i]);
  377. if (error)
  378. return error;
  379. }
  380. return 0;
  381. }
  382. /**
  383. * attribute_container_add_class_device - same function as device_add
  384. *
  385. * @classdev: the class device to add
  386. *
  387. * This performs essentially the same function as device_add except for
  388. * attribute containers, namely add the classdev to the system and then
  389. * create the attribute files
  390. */
  391. int
  392. attribute_container_add_class_device(struct device *classdev)
  393. {
  394. int error = device_add(classdev);
  395. if (error)
  396. return error;
  397. return attribute_container_add_attrs(classdev);
  398. }
  399. /**
  400. * attribute_container_add_class_device_adapter - simple adapter for triggers
  401. *
  402. * @cont: the container to register.
  403. * @dev: the generic device to activate the trigger for
  404. * @classdev: the class device to add
  405. *
  406. * This function is identical to attribute_container_add_class_device except
  407. * that it is designed to be called from the triggers
  408. */
  409. int
  410. attribute_container_add_class_device_adapter(struct attribute_container *cont,
  411. struct device *dev,
  412. struct device *classdev)
  413. {
  414. return attribute_container_add_class_device(classdev);
  415. }
  416. /**
  417. * attribute_container_remove_attrs - remove any attribute files
  418. *
  419. * @classdev: The class device to remove the files from
  420. *
  421. */
  422. void
  423. attribute_container_remove_attrs(struct device *classdev)
  424. {
  425. struct attribute_container *cont =
  426. attribute_container_classdev_to_container(classdev);
  427. struct device_attribute **attrs = cont->attrs;
  428. int i;
  429. if (!attrs && !cont->grp)
  430. return;
  431. if (cont->grp) {
  432. sysfs_remove_group(&classdev->kobj, cont->grp);
  433. return ;
  434. }
  435. for (i = 0; attrs[i]; i++)
  436. device_remove_file(classdev, attrs[i]);
  437. }
  438. /**
  439. * attribute_container_class_device_del - equivalent of class_device_del
  440. *
  441. * @classdev: the class device
  442. *
  443. * This function simply removes all the attribute files and then calls
  444. * device_del.
  445. */
  446. void
  447. attribute_container_class_device_del(struct device *classdev)
  448. {
  449. attribute_container_remove_attrs(classdev);
  450. device_del(classdev);
  451. }
  452. /**
  453. * attribute_container_find_class_device - find the corresponding class_device
  454. *
  455. * @cont: the container
  456. * @dev: the generic device
  457. *
  458. * Looks up the device in the container's list of class devices and returns
  459. * the corresponding class_device.
  460. */
  461. struct device *
  462. attribute_container_find_class_device(struct attribute_container *cont,
  463. struct device *dev)
  464. {
  465. struct device *cdev = NULL;
  466. struct internal_container *ic;
  467. struct klist_iter iter;
  468. klist_for_each_entry(ic, &cont->containers, node, &iter) {
  469. if (ic->classdev.parent == dev) {
  470. cdev = &ic->classdev;
  471. /* FIXME: must exit iterator then break */
  472. klist_iter_exit(&iter);
  473. break;
  474. }
  475. }
  476. return cdev;
  477. }
  478. EXPORT_SYMBOL_GPL(attribute_container_find_class_device);