bind.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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/uclass-internal.h>
  11. static int bind_by_class_index(const char *uclass, int index,
  12. const char *drv_name)
  13. {
  14. static enum uclass_id uclass_id;
  15. struct udevice *dev;
  16. struct udevice *parent;
  17. int ret;
  18. struct driver *drv;
  19. drv = lists_driver_lookup_name(drv_name);
  20. if (!drv) {
  21. printf("Cannot find driver '%s'\n", drv_name);
  22. return -ENOENT;
  23. }
  24. uclass_id = uclass_get_by_name(uclass);
  25. if (uclass_id == UCLASS_INVALID) {
  26. printf("%s is not a valid uclass\n", uclass);
  27. return -EINVAL;
  28. }
  29. ret = uclass_find_device(uclass_id, index, &parent);
  30. if (!parent || ret) {
  31. printf("Cannot find device %d of class %s\n", index, uclass);
  32. return ret;
  33. }
  34. ret = device_bind_with_driver_data(parent, drv, drv->name, 0,
  35. ofnode_null(), &dev);
  36. if (!dev || ret) {
  37. printf("Unable to bind. err:%d\n", ret);
  38. return ret;
  39. }
  40. return 0;
  41. }
  42. static int find_dev(const char *uclass, int index, struct udevice **devp)
  43. {
  44. static enum uclass_id uclass_id;
  45. int rc;
  46. uclass_id = uclass_get_by_name(uclass);
  47. if (uclass_id == UCLASS_INVALID) {
  48. printf("%s is not a valid uclass\n", uclass);
  49. return -EINVAL;
  50. }
  51. rc = uclass_find_device(uclass_id, index, devp);
  52. if (!*devp || rc) {
  53. printf("Cannot find device %d of class %s\n", index, uclass);
  54. return rc;
  55. }
  56. return 0;
  57. }
  58. static int unbind_by_class_index(const char *uclass, int index)
  59. {
  60. int ret;
  61. struct udevice *dev;
  62. ret = find_dev(uclass, index, &dev);
  63. if (ret)
  64. return ret;
  65. ret = device_remove(dev, DM_REMOVE_NORMAL);
  66. if (ret) {
  67. printf("Unable to remove. err:%d\n", ret);
  68. return ret;
  69. }
  70. ret = device_unbind(dev);
  71. if (ret) {
  72. printf("Unable to unbind. err:%d\n", ret);
  73. return ret;
  74. }
  75. return 0;
  76. }
  77. static int unbind_child_by_class_index(const char *uclass, int index,
  78. const char *drv_name)
  79. {
  80. struct udevice *parent;
  81. int ret;
  82. struct driver *drv;
  83. drv = lists_driver_lookup_name(drv_name);
  84. if (!drv) {
  85. printf("Cannot find driver '%s'\n", drv_name);
  86. return -ENOENT;
  87. }
  88. ret = find_dev(uclass, index, &parent);
  89. if (ret)
  90. return ret;
  91. ret = device_chld_remove(parent, drv, DM_REMOVE_NORMAL);
  92. if (ret)
  93. printf("Unable to remove all. err:%d\n", ret);
  94. ret = device_chld_unbind(parent, drv);
  95. if (ret)
  96. printf("Unable to unbind all. err:%d\n", ret);
  97. return ret;
  98. }
  99. static int bind_by_node_path(const char *path, const char *drv_name)
  100. {
  101. struct udevice *dev;
  102. struct udevice *parent = NULL;
  103. int ret;
  104. ofnode ofnode;
  105. struct driver *drv;
  106. drv = lists_driver_lookup_name(drv_name);
  107. if (!drv) {
  108. printf("%s is not a valid driver name\n", drv_name);
  109. return -ENOENT;
  110. }
  111. ofnode = ofnode_path(path);
  112. if (!ofnode_valid(ofnode)) {
  113. printf("%s is not a valid node path\n", path);
  114. return -EINVAL;
  115. }
  116. while (ofnode_valid(ofnode)) {
  117. if (!device_find_global_by_ofnode(ofnode, &parent))
  118. break;
  119. ofnode = ofnode_get_parent(ofnode);
  120. }
  121. if (!parent) {
  122. printf("Cannot find a parent device for node path %s\n", path);
  123. return -ENODEV;
  124. }
  125. ofnode = ofnode_path(path);
  126. ret = device_bind_with_driver_data(parent, drv, ofnode_get_name(ofnode),
  127. 0, ofnode, &dev);
  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) ? simple_strtoul(argv[2], NULL, 10) : 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) ? simple_strtoul(argv[2], NULL, 10) : 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. );