device.c 7.1 KB

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