devres.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * drivers/extcon/devres.c - EXTCON device's resource management
  3. *
  4. * Copyright (C) 2016 Samsung Electronics
  5. * Author: Chanwoo Choi <cw00.choi@samsung.com>
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include "extcon.h"
  17. static int devm_extcon_dev_match(struct device *dev, void *res, void *data)
  18. {
  19. struct extcon_dev **r = res;
  20. if (WARN_ON(!r || !*r))
  21. return 0;
  22. return *r == data;
  23. }
  24. static void devm_extcon_dev_release(struct device *dev, void *res)
  25. {
  26. extcon_dev_free(*(struct extcon_dev **)res);
  27. }
  28. static void devm_extcon_dev_unreg(struct device *dev, void *res)
  29. {
  30. extcon_dev_unregister(*(struct extcon_dev **)res);
  31. }
  32. struct extcon_dev_notifier_devres {
  33. struct extcon_dev *edev;
  34. unsigned int id;
  35. struct notifier_block *nb;
  36. };
  37. static void devm_extcon_dev_notifier_unreg(struct device *dev, void *res)
  38. {
  39. struct extcon_dev_notifier_devres *this = res;
  40. extcon_unregister_notifier(this->edev, this->id, this->nb);
  41. }
  42. static void devm_extcon_dev_notifier_all_unreg(struct device *dev, void *res)
  43. {
  44. struct extcon_dev_notifier_devres *this = res;
  45. extcon_unregister_notifier_all(this->edev, this->nb);
  46. }
  47. /**
  48. * devm_extcon_dev_allocate - Allocate managed extcon device
  49. * @dev: the device owning the extcon device being created
  50. * @supported_cable: the array of the supported external connectors
  51. * ending with EXTCON_NONE.
  52. *
  53. * This function manages automatically the memory of extcon device using device
  54. * resource management and simplify the control of freeing the memory of extcon
  55. * device.
  56. *
  57. * Returns the pointer memory of allocated extcon_dev if success
  58. * or ERR_PTR(err) if fail
  59. */
  60. struct extcon_dev *devm_extcon_dev_allocate(struct device *dev,
  61. const unsigned int *supported_cable)
  62. {
  63. struct extcon_dev **ptr, *edev;
  64. ptr = devres_alloc(devm_extcon_dev_release, sizeof(*ptr), GFP_KERNEL);
  65. if (!ptr)
  66. return ERR_PTR(-ENOMEM);
  67. edev = extcon_dev_allocate(supported_cable);
  68. if (IS_ERR(edev)) {
  69. devres_free(ptr);
  70. return edev;
  71. }
  72. edev->dev.parent = dev;
  73. *ptr = edev;
  74. devres_add(dev, ptr);
  75. return edev;
  76. }
  77. EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate);
  78. /**
  79. * devm_extcon_dev_free() - Resource-managed extcon_dev_unregister()
  80. * @dev: the device owning the extcon device being created
  81. * @edev: the extcon device to be freed
  82. *
  83. * Free the memory that is allocated with devm_extcon_dev_allocate()
  84. * function.
  85. */
  86. void devm_extcon_dev_free(struct device *dev, struct extcon_dev *edev)
  87. {
  88. WARN_ON(devres_release(dev, devm_extcon_dev_release,
  89. devm_extcon_dev_match, edev));
  90. }
  91. EXPORT_SYMBOL_GPL(devm_extcon_dev_free);
  92. /**
  93. * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
  94. * @dev: the device owning the extcon device being created
  95. * @edev: the extcon device to be registered
  96. *
  97. * this function, that extcon device is automatically unregistered on driver
  98. * detach. Internally this function calls extcon_dev_register() function.
  99. * To get more information, refer that function.
  100. *
  101. * If extcon device is registered with this function and the device needs to be
  102. * unregistered separately, devm_extcon_dev_unregister() should be used.
  103. *
  104. * Returns 0 if success or negaive error number if failure.
  105. */
  106. int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev)
  107. {
  108. struct extcon_dev **ptr;
  109. int ret;
  110. ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL);
  111. if (!ptr)
  112. return -ENOMEM;
  113. ret = extcon_dev_register(edev);
  114. if (ret) {
  115. devres_free(ptr);
  116. return ret;
  117. }
  118. *ptr = edev;
  119. devres_add(dev, ptr);
  120. return 0;
  121. }
  122. EXPORT_SYMBOL_GPL(devm_extcon_dev_register);
  123. /**
  124. * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
  125. * @dev: the device owning the extcon device being created
  126. * @edev: the extcon device to unregistered
  127. *
  128. * Unregister extcon device that is registered with devm_extcon_dev_register()
  129. * function.
  130. */
  131. void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev)
  132. {
  133. WARN_ON(devres_release(dev, devm_extcon_dev_unreg,
  134. devm_extcon_dev_match, edev));
  135. }
  136. EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister);
  137. /**
  138. * devm_extcon_register_notifier() - Resource-managed extcon_register_notifier()
  139. * @dev: the device owning the extcon device being created
  140. * @edev: the extcon device
  141. * @id: the unique id among the extcon enumeration
  142. * @nb: a notifier block to be registered
  143. *
  144. * This function manages automatically the notifier of extcon device using
  145. * device resource management and simplify the control of unregistering
  146. * the notifier of extcon device.
  147. *
  148. * Note that the second parameter given to the callback of nb (val) is
  149. * "old_state", not the current state. The current state can be retrieved
  150. * by looking at the third pameter (edev pointer)'s state value.
  151. *
  152. * Returns 0 if success or negaive error number if failure.
  153. */
  154. int devm_extcon_register_notifier(struct device *dev, struct extcon_dev *edev,
  155. unsigned int id, struct notifier_block *nb)
  156. {
  157. struct extcon_dev_notifier_devres *ptr;
  158. int ret;
  159. ptr = devres_alloc(devm_extcon_dev_notifier_unreg, sizeof(*ptr),
  160. GFP_KERNEL);
  161. if (!ptr)
  162. return -ENOMEM;
  163. ret = extcon_register_notifier(edev, id, nb);
  164. if (ret) {
  165. devres_free(ptr);
  166. return ret;
  167. }
  168. ptr->edev = edev;
  169. ptr->id = id;
  170. ptr->nb = nb;
  171. devres_add(dev, ptr);
  172. return 0;
  173. }
  174. EXPORT_SYMBOL(devm_extcon_register_notifier);
  175. /**
  176. * devm_extcon_unregister_notifier()
  177. - Resource-managed extcon_unregister_notifier()
  178. * @dev: the device owning the extcon device being created
  179. * @edev: the extcon device
  180. * @id: the unique id among the extcon enumeration
  181. * @nb: a notifier block to be registered
  182. */
  183. void devm_extcon_unregister_notifier(struct device *dev,
  184. struct extcon_dev *edev, unsigned int id,
  185. struct notifier_block *nb)
  186. {
  187. WARN_ON(devres_release(dev, devm_extcon_dev_notifier_unreg,
  188. devm_extcon_dev_match, edev));
  189. }
  190. EXPORT_SYMBOL(devm_extcon_unregister_notifier);
  191. /**
  192. * devm_extcon_register_notifier_all()
  193. * - Resource-managed extcon_register_notifier_all()
  194. * @dev: the device owning the extcon device being created
  195. * @edev: the extcon device
  196. * @nb: a notifier block to be registered
  197. *
  198. * This function manages automatically the notifier of extcon device using
  199. * device resource management and simplify the control of unregistering
  200. * the notifier of extcon device. To get more information, refer that function.
  201. *
  202. * Returns 0 if success or negaive error number if failure.
  203. */
  204. int devm_extcon_register_notifier_all(struct device *dev, struct extcon_dev *edev,
  205. struct notifier_block *nb)
  206. {
  207. struct extcon_dev_notifier_devres *ptr;
  208. int ret;
  209. ptr = devres_alloc(devm_extcon_dev_notifier_all_unreg, sizeof(*ptr),
  210. GFP_KERNEL);
  211. if (!ptr)
  212. return -ENOMEM;
  213. ret = extcon_register_notifier_all(edev, nb);
  214. if (ret) {
  215. devres_free(ptr);
  216. return ret;
  217. }
  218. ptr->edev = edev;
  219. ptr->nb = nb;
  220. devres_add(dev, ptr);
  221. return 0;
  222. }
  223. EXPORT_SYMBOL(devm_extcon_register_notifier_all);
  224. /**
  225. * devm_extcon_unregister_notifier_all()
  226. * - Resource-managed extcon_unregister_notifier_all()
  227. * @dev: the device owning the extcon device being created
  228. * @edev: the extcon device
  229. * @nb: a notifier block to be registered
  230. */
  231. void devm_extcon_unregister_notifier_all(struct device *dev,
  232. struct extcon_dev *edev,
  233. struct notifier_block *nb)
  234. {
  235. WARN_ON(devres_release(dev, devm_extcon_dev_notifier_all_unreg,
  236. devm_extcon_dev_match, edev));
  237. }
  238. EXPORT_SYMBOL(devm_extcon_unregister_notifier_all);