bind.c 5.2 KB

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