device.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Device manager
  3. *
  4. * Copyright (c) 2013 Google, Inc
  5. *
  6. * (C) Copyright 2012
  7. * Pavel Herrmann <morpheus.ibis@gmail.com>
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <common.h>
  12. #include <malloc.h>
  13. #include <dm/device.h>
  14. #include <dm/device-internal.h>
  15. #include <dm/lists.h>
  16. #include <dm/platdata.h>
  17. #include <dm/uclass.h>
  18. #include <dm/uclass-internal.h>
  19. #include <dm/util.h>
  20. #include <linux/err.h>
  21. #include <linux/list.h>
  22. /**
  23. * device_chld_unbind() - Unbind all device's children from the device
  24. *
  25. * On error, the function continues to unbind all children, and reports the
  26. * first error.
  27. *
  28. * @dev: The device that is to be stripped of its children
  29. * @return 0 on success, -ve on error
  30. */
  31. static int device_chld_unbind(struct device *dev)
  32. {
  33. struct device *pos, *n;
  34. int ret, saved_ret = 0;
  35. assert(dev);
  36. list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
  37. ret = device_unbind(pos);
  38. if (ret && !saved_ret)
  39. saved_ret = ret;
  40. }
  41. return saved_ret;
  42. }
  43. /**
  44. * device_chld_remove() - Stop all device's children
  45. * @dev: The device whose children are to be removed
  46. * @return 0 on success, -ve on error
  47. */
  48. static int device_chld_remove(struct device *dev)
  49. {
  50. struct device *pos, *n;
  51. int ret;
  52. assert(dev);
  53. list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
  54. ret = device_remove(pos);
  55. if (ret)
  56. return ret;
  57. }
  58. return 0;
  59. }
  60. int device_bind(struct device *parent, struct driver *drv, const char *name,
  61. void *platdata, int of_offset, struct device **devp)
  62. {
  63. struct device *dev;
  64. struct uclass *uc;
  65. int ret = 0;
  66. *devp = NULL;
  67. if (!name)
  68. return -EINVAL;
  69. ret = uclass_get(drv->id, &uc);
  70. if (ret)
  71. return ret;
  72. dev = calloc(1, sizeof(struct device));
  73. if (!dev)
  74. return -ENOMEM;
  75. INIT_LIST_HEAD(&dev->sibling_node);
  76. INIT_LIST_HEAD(&dev->child_head);
  77. INIT_LIST_HEAD(&dev->uclass_node);
  78. dev->platdata = platdata;
  79. dev->name = name;
  80. dev->of_offset = of_offset;
  81. dev->parent = parent;
  82. dev->driver = drv;
  83. dev->uclass = uc;
  84. if (!dev->platdata && drv->platdata_auto_alloc_size)
  85. dev->flags |= DM_FLAG_ALLOC_PDATA;
  86. /* put dev into parent's successor list */
  87. if (parent)
  88. list_add_tail(&dev->sibling_node, &parent->child_head);
  89. ret = uclass_bind_device(dev);
  90. if (ret)
  91. goto fail_bind;
  92. /* if we fail to bind we remove device from successors and free it */
  93. if (drv->bind) {
  94. ret = drv->bind(dev);
  95. if (ret) {
  96. if (uclass_unbind_device(dev)) {
  97. dm_warn("Failed to unbind dev '%s' on error path\n",
  98. dev->name);
  99. }
  100. goto fail_bind;
  101. }
  102. }
  103. if (parent)
  104. dm_dbg("Bound device %s to %s\n", dev->name, parent->name);
  105. *devp = dev;
  106. return 0;
  107. fail_bind:
  108. list_del(&dev->sibling_node);
  109. free(dev);
  110. return ret;
  111. }
  112. int device_bind_by_name(struct device *parent, const struct driver_info *info,
  113. struct device **devp)
  114. {
  115. struct driver *drv;
  116. drv = lists_driver_lookup_name(info->name);
  117. if (!drv)
  118. return -ENOENT;
  119. return device_bind(parent, drv, info->name, (void *)info->platdata,
  120. -1, devp);
  121. }
  122. int device_unbind(struct device *dev)
  123. {
  124. struct driver *drv;
  125. int ret;
  126. if (!dev)
  127. return -EINVAL;
  128. if (dev->flags & DM_FLAG_ACTIVATED)
  129. return -EINVAL;
  130. drv = dev->driver;
  131. assert(drv);
  132. if (drv->unbind) {
  133. ret = drv->unbind(dev);
  134. if (ret)
  135. return ret;
  136. }
  137. ret = device_chld_unbind(dev);
  138. if (ret)
  139. return ret;
  140. ret = uclass_unbind_device(dev);
  141. if (ret)
  142. return ret;
  143. if (dev->parent)
  144. list_del(&dev->sibling_node);
  145. free(dev);
  146. return 0;
  147. }
  148. /**
  149. * device_free() - Free memory buffers allocated by a device
  150. * @dev: Device that is to be started
  151. */
  152. static void device_free(struct device *dev)
  153. {
  154. int size;
  155. if (dev->driver->priv_auto_alloc_size) {
  156. free(dev->priv);
  157. dev->priv = NULL;
  158. }
  159. if (dev->flags & DM_FLAG_ALLOC_PDATA) {
  160. free(dev->platdata);
  161. dev->platdata = NULL;
  162. }
  163. size = dev->uclass->uc_drv->per_device_auto_alloc_size;
  164. if (size) {
  165. free(dev->uclass_priv);
  166. dev->uclass_priv = NULL;
  167. }
  168. }
  169. int device_probe(struct device *dev)
  170. {
  171. struct driver *drv;
  172. int size = 0;
  173. int ret;
  174. if (!dev)
  175. return -EINVAL;
  176. if (dev->flags & DM_FLAG_ACTIVATED)
  177. return 0;
  178. drv = dev->driver;
  179. assert(drv);
  180. /* Allocate private data and platdata if requested */
  181. if (drv->priv_auto_alloc_size) {
  182. dev->priv = calloc(1, drv->priv_auto_alloc_size);
  183. if (!dev->priv) {
  184. ret = -ENOMEM;
  185. goto fail;
  186. }
  187. }
  188. /* Allocate private data if requested */
  189. if (dev->flags & DM_FLAG_ALLOC_PDATA) {
  190. dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
  191. if (!dev->platdata) {
  192. ret = -ENOMEM;
  193. goto fail;
  194. }
  195. }
  196. size = dev->uclass->uc_drv->per_device_auto_alloc_size;
  197. if (size) {
  198. dev->uclass_priv = calloc(1, size);
  199. if (!dev->uclass_priv) {
  200. ret = -ENOMEM;
  201. goto fail;
  202. }
  203. }
  204. /* Ensure all parents are probed */
  205. if (dev->parent) {
  206. ret = device_probe(dev->parent);
  207. if (ret)
  208. goto fail;
  209. }
  210. if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
  211. ret = drv->ofdata_to_platdata(dev);
  212. if (ret)
  213. goto fail;
  214. }
  215. if (drv->probe) {
  216. ret = drv->probe(dev);
  217. if (ret)
  218. goto fail;
  219. }
  220. dev->flags |= DM_FLAG_ACTIVATED;
  221. ret = uclass_post_probe_device(dev);
  222. if (ret) {
  223. dev->flags &= ~DM_FLAG_ACTIVATED;
  224. goto fail_uclass;
  225. }
  226. return 0;
  227. fail_uclass:
  228. if (device_remove(dev)) {
  229. dm_warn("%s: Device '%s' failed to remove on error path\n",
  230. __func__, dev->name);
  231. }
  232. fail:
  233. device_free(dev);
  234. return ret;
  235. }
  236. int device_remove(struct device *dev)
  237. {
  238. struct driver *drv;
  239. int ret;
  240. if (!dev)
  241. return -EINVAL;
  242. if (!(dev->flags & DM_FLAG_ACTIVATED))
  243. return 0;
  244. drv = dev->driver;
  245. assert(drv);
  246. ret = uclass_pre_remove_device(dev);
  247. if (ret)
  248. return ret;
  249. ret = device_chld_remove(dev);
  250. if (ret)
  251. goto err;
  252. if (drv->remove) {
  253. ret = drv->remove(dev);
  254. if (ret)
  255. goto err_remove;
  256. }
  257. device_free(dev);
  258. dev->flags &= ~DM_FLAG_ACTIVATED;
  259. return 0;
  260. err_remove:
  261. /* We can't put the children back */
  262. dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
  263. __func__, dev->name);
  264. err:
  265. ret = uclass_post_probe_device(dev);
  266. if (ret) {
  267. dm_warn("%s: Device '%s' failed to post_probe on error path\n",
  268. __func__, dev->name);
  269. }
  270. return ret;
  271. }
  272. void *dev_get_platdata(struct device *dev)
  273. {
  274. if (!dev) {
  275. dm_warn("%s: null device", __func__);
  276. return NULL;
  277. }
  278. return dev->platdata;
  279. }
  280. void *dev_get_priv(struct device *dev)
  281. {
  282. if (!dev) {
  283. dm_warn("%s: null device", __func__);
  284. return NULL;
  285. }
  286. return dev->priv;
  287. }