bind.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2018 JJ Hiblot <jjhiblot@ti.com>
  4. */
  5. #include <common.h>
  6. #include <command.h>
  7. #include <dm.h>
  8. #include <dm/device-internal.h>
  9. #include <dm/lists.h>
  10. #include <dm/root.h>
  11. #include <dm/uclass-internal.h>
  12. static int bind_by_class_index(const char *uclass, int index,
  13. const char *drv_name)
  14. {
  15. static enum uclass_id uclass_id;
  16. struct udevice *dev;
  17. struct udevice *parent;
  18. int ret;
  19. struct driver *drv;
  20. drv = lists_driver_lookup_name(drv_name);
  21. if (!drv) {
  22. printf("Cannot find driver '%s'\n", drv_name);
  23. return -ENOENT;
  24. }
  25. uclass_id = uclass_get_by_name(uclass);
  26. if (uclass_id == UCLASS_INVALID) {
  27. printf("%s is not a valid uclass\n", uclass);
  28. return -EINVAL;
  29. }
  30. ret = uclass_find_device(uclass_id, index, &parent);
  31. if (!parent || ret) {
  32. printf("Cannot find device %d of class %s\n", index, uclass);
  33. return ret;
  34. }
  35. ret = device_bind_with_driver_data(parent, drv, drv->name, 0,
  36. ofnode_null(), &dev);
  37. if (!dev || ret) {
  38. printf("Unable to bind. err:%d\n", ret);
  39. return ret;
  40. }
  41. return 0;
  42. }
  43. static int find_dev(const char *uclass, int index, struct udevice **devp)
  44. {
  45. static enum uclass_id uclass_id;
  46. int rc;
  47. uclass_id = uclass_get_by_name(uclass);
  48. if (uclass_id == UCLASS_INVALID) {
  49. printf("%s is not a valid uclass\n", uclass);
  50. return -EINVAL;
  51. }
  52. rc = uclass_find_device(uclass_id, index, devp);
  53. if (!*devp || rc) {
  54. printf("Cannot find device %d of class %s\n", index, uclass);
  55. return rc;
  56. }
  57. return 0;
  58. }
  59. static int unbind_by_class_index(const char *uclass, int index)
  60. {
  61. int ret;
  62. struct udevice *dev;
  63. ret = find_dev(uclass, index, &dev);
  64. if (ret)
  65. return ret;
  66. ret = device_remove(dev, DM_REMOVE_NORMAL);
  67. if (ret) {
  68. printf("Unable to remove. err:%d\n", ret);
  69. return ret;
  70. }
  71. ret = device_unbind(dev);
  72. if (ret) {
  73. printf("Unable to unbind. err:%d\n", ret);
  74. return ret;
  75. }
  76. return 0;
  77. }
  78. static int unbind_child_by_class_index(const char *uclass, int index,
  79. const char *drv_name)
  80. {
  81. struct udevice *parent;
  82. int ret;
  83. struct driver *drv;
  84. drv = lists_driver_lookup_name(drv_name);
  85. if (!drv) {
  86. printf("Cannot find driver '%s'\n", drv_name);
  87. return -ENOENT;
  88. }
  89. ret = find_dev(uclass, index, &parent);
  90. if (ret)
  91. return ret;
  92. ret = device_chld_remove(parent, drv, DM_REMOVE_NORMAL);
  93. if (ret)
  94. printf("Unable to remove all. err:%d\n", ret);
  95. ret = device_chld_unbind(parent, drv);
  96. if (ret)
  97. printf("Unable to unbind all. err:%d\n", ret);
  98. return ret;
  99. }
  100. static int bind_by_node_path(const char *path, const char *drv_name)
  101. {
  102. struct udevice *dev;
  103. struct udevice *parent = NULL;
  104. int ret;
  105. ofnode ofnode;
  106. struct driver *drv;
  107. drv = lists_driver_lookup_name(drv_name);
  108. if (!drv) {
  109. printf("%s is not a valid driver name\n", drv_name);
  110. return -ENOENT;
  111. }
  112. ofnode = ofnode_path(path);
  113. if (!ofnode_valid(ofnode)) {
  114. printf("%s is not a valid node path\n", path);
  115. return -EINVAL;
  116. }
  117. while (ofnode_valid(ofnode)) {
  118. if (!device_find_global_by_ofnode(ofnode, &parent))
  119. break;
  120. ofnode = ofnode_get_parent(ofnode);
  121. }
  122. if (!parent) {
  123. printf("Cannot find a parent device for node path %s\n", path);
  124. return -ENODEV;
  125. }
  126. ofnode = ofnode_path(path);
  127. ret = lists_bind_fdt(parent, ofnode, &dev, false);
  128. if (!dev || ret) {
  129. printf("Unable to bind. err:%d\n", ret);
  130. return ret;
  131. }
  132. return 0;
  133. }
  134. static int unbind_by_node_path(const char *path)
  135. {
  136. struct udevice *dev;
  137. int ret;
  138. ofnode ofnode;
  139. ofnode = ofnode_path(path);
  140. if (!ofnode_valid(ofnode)) {
  141. printf("%s is not a valid node path\n", path);
  142. return -EINVAL;
  143. }
  144. ret = device_find_global_by_ofnode(ofnode, &dev);
  145. if (!dev || ret) {
  146. printf("Cannot find a device with path %s\n", path);
  147. return -ENODEV;
  148. }
  149. ret = device_remove(dev, DM_REMOVE_NORMAL);
  150. if (ret) {
  151. printf("Unable to remove. err:%d\n", ret);
  152. return ret;
  153. }
  154. ret = device_unbind(dev);
  155. if (ret) {
  156. printf("Unable to unbind. err:%d\n", ret);
  157. return ret;
  158. }
  159. return 0;
  160. }
  161. static int do_bind_unbind(struct cmd_tbl *cmdtp, int flag, int argc,
  162. char *const argv[])
  163. {
  164. int ret = 0;
  165. bool bind;
  166. bool by_node;
  167. if (argc < 2)
  168. return CMD_RET_USAGE;
  169. bind = (argv[0][0] == 'b');
  170. by_node = (argv[1][0] == '/');
  171. if (by_node && bind) {
  172. if (argc != 3)
  173. return CMD_RET_USAGE;
  174. ret = bind_by_node_path(argv[1], argv[2]);
  175. } else if (by_node && !bind) {
  176. if (argc != 2)
  177. return CMD_RET_USAGE;
  178. ret = unbind_by_node_path(argv[1]);
  179. } else if (!by_node && bind) {
  180. int index = (argc > 2) ? dectoul(argv[2], NULL) : 0;
  181. if (argc != 4)
  182. return CMD_RET_USAGE;
  183. ret = bind_by_class_index(argv[1], index, argv[3]);
  184. } else if (!by_node && !bind) {
  185. int index = (argc > 2) ? dectoul(argv[2], NULL) : 0;
  186. if (argc == 3)
  187. ret = unbind_by_class_index(argv[1], index);
  188. else if (argc == 4)
  189. ret = unbind_child_by_class_index(argv[1], index,
  190. argv[3]);
  191. else
  192. return CMD_RET_USAGE;
  193. }
  194. if (ret)
  195. return CMD_RET_FAILURE;
  196. else
  197. return CMD_RET_SUCCESS;
  198. }
  199. U_BOOT_CMD(
  200. bind, 4, 0, do_bind_unbind,
  201. "Bind a device to a driver",
  202. "<node path> <driver>\n"
  203. "bind <class> <index> <driver>\n"
  204. );
  205. U_BOOT_CMD(
  206. unbind, 4, 0, do_bind_unbind,
  207. "Unbind a device from a driver",
  208. "<node path>\n"
  209. "unbind <class> <index>\n"
  210. "unbind <class> <index> <driver>\n"
  211. );