usb.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright (C) 2015 Google, Inc
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <console.h>
  8. #include <dm.h>
  9. #include <usb.h>
  10. #include <asm/io.h>
  11. #include <asm/state.h>
  12. #include <asm/test.h>
  13. #include <dm/device-internal.h>
  14. #include <dm/test.h>
  15. #include <dm/uclass-internal.h>
  16. #include <test/ut.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. /* Test that sandbox USB works correctly */
  19. static int dm_test_usb_base(struct unit_test_state *uts)
  20. {
  21. struct udevice *bus;
  22. ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_USB, 0, &bus));
  23. ut_assertok(uclass_get_device(UCLASS_USB, 0, &bus));
  24. ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_USB, 2, &bus));
  25. return 0;
  26. }
  27. DM_TEST(dm_test_usb_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  28. /*
  29. * Test that we can use the flash stick. This is more of a functional test. It
  30. * covers scanning the bug, setting up a hub and a flash stick and reading
  31. * data from the flash stick.
  32. */
  33. static int dm_test_usb_flash(struct unit_test_state *uts)
  34. {
  35. struct udevice *dev;
  36. struct blk_desc *dev_desc;
  37. char cmp[1024];
  38. state_set_skip_delays(true);
  39. ut_assertok(usb_init());
  40. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
  41. ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc));
  42. /* Read a few blocks and look for the string we expect */
  43. ut_asserteq(512, dev_desc->blksz);
  44. memset(cmp, '\0', sizeof(cmp));
  45. ut_asserteq(2, blk_dread(dev_desc, 0, 2, cmp));
  46. ut_assertok(strcmp(cmp, "this is a test"));
  47. ut_assertok(usb_stop());
  48. return 0;
  49. }
  50. DM_TEST(dm_test_usb_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  51. /* test that we can handle multiple storage devices */
  52. static int dm_test_usb_multi(struct unit_test_state *uts)
  53. {
  54. struct udevice *dev;
  55. state_set_skip_delays(true);
  56. ut_assertok(usb_init());
  57. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
  58. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
  59. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
  60. ut_assertok(usb_stop());
  61. return 0;
  62. }
  63. DM_TEST(dm_test_usb_multi, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  64. static int count_usb_devices(void)
  65. {
  66. struct udevice *hub;
  67. struct uclass *uc;
  68. int count = 0;
  69. int ret;
  70. ret = uclass_get(UCLASS_USB_HUB, &uc);
  71. if (ret)
  72. return ret;
  73. uclass_foreach_dev(hub, uc) {
  74. struct udevice *dev;
  75. count++;
  76. for (device_find_first_child(hub, &dev);
  77. dev;
  78. device_find_next_child(&dev)) {
  79. count++;
  80. }
  81. }
  82. return count;
  83. }
  84. /* test that we can remove an emulated device and it is then not found */
  85. static int dm_test_usb_remove(struct unit_test_state *uts)
  86. {
  87. struct udevice *dev, *emul;
  88. /* Scan and check that all devices are present */
  89. state_set_skip_delays(true);
  90. ut_assertok(usb_init());
  91. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
  92. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
  93. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
  94. ut_asserteq(6, count_usb_devices());
  95. ut_assertok(usb_stop());
  96. ut_asserteq(6, count_usb_devices());
  97. /* Remove the second emulation device */
  98. ut_assertok(uclass_find_device_by_name(UCLASS_USB_EMUL, "flash-stick@1",
  99. &dev));
  100. ut_assertok(device_unbind(dev));
  101. /* Rescan - only the first and third should be present */
  102. ut_assertok(usb_init());
  103. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
  104. ut_assertok(usb_emul_find_for_dev(dev, &emul));
  105. ut_asserteq_str("flash-stick@0", emul->name);
  106. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
  107. ut_assertok(usb_emul_find_for_dev(dev, &emul));
  108. ut_asserteq_str("flash-stick@2", emul->name);
  109. ut_asserteq(-ENODEV, uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
  110. ut_asserteq(5, count_usb_devices());
  111. ut_assertok(usb_stop());
  112. ut_asserteq(5, count_usb_devices());
  113. return 0;
  114. }
  115. DM_TEST(dm_test_usb_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  116. const char usb_tree_base[] =
  117. " 1 Hub (12 Mb/s, 100mA)\n"
  118. " | sandbox hub 2345\n"
  119. " |\n"
  120. " |\b+-2 Mass Storage (12 Mb/s, 100mA)\n"
  121. " | sandbox flash flash-stick@0\n"
  122. " | \n"
  123. " |\b+-3 Mass Storage (12 Mb/s, 100mA)\n"
  124. " | sandbox flash flash-stick@1\n"
  125. " | \n"
  126. " |\b+-4 Mass Storage (12 Mb/s, 100mA)\n"
  127. " | sandbox flash flash-stick@2\n"
  128. " | \n"
  129. " |\b+-5 Human Interface (12 Mb/s, 100mA)\n"
  130. " sandbox keyboard keyb@3\n"
  131. " \n";
  132. /* test that the 'usb tree' command output looks correct */
  133. static int dm_test_usb_tree(struct unit_test_state *uts)
  134. {
  135. char *data;
  136. int len;
  137. state_set_skip_delays(true);
  138. ut_assertok(usb_init());
  139. console_record_reset_enable();
  140. usb_show_tree();
  141. len = membuff_getraw(&gd->console_out, -1, true, &data);
  142. if (len)
  143. data[len] = '\0';
  144. ut_asserteq_str(usb_tree_base, data);
  145. ut_assertok(usb_stop());
  146. return 0;
  147. }
  148. DM_TEST(dm_test_usb_tree, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  149. const char usb_tree_remove[] =
  150. " 1 Hub (12 Mb/s, 100mA)\n"
  151. " | sandbox hub 2345\n"
  152. " |\n"
  153. " |\b+-2 Mass Storage (12 Mb/s, 100mA)\n"
  154. " | sandbox flash flash-stick@0\n"
  155. " | \n"
  156. " |\b+-3 Mass Storage (12 Mb/s, 100mA)\n"
  157. " | sandbox flash flash-stick@2\n"
  158. " | \n"
  159. " |\b+-4 Human Interface (12 Mb/s, 100mA)\n"
  160. " sandbox keyboard keyb@3\n"
  161. " \n";
  162. /*
  163. * test that the 'usb tree' command output looks correct when we remove a
  164. * device
  165. */
  166. static int dm_test_usb_tree_remove(struct unit_test_state *uts)
  167. {
  168. struct udevice *dev;
  169. char *data;
  170. int len;
  171. /* Remove the second emulation device */
  172. ut_assertok(uclass_find_device_by_name(UCLASS_USB_EMUL, "flash-stick@1",
  173. &dev));
  174. ut_assertok(device_unbind(dev));
  175. state_set_skip_delays(true);
  176. ut_assertok(usb_init());
  177. console_record_reset_enable();
  178. usb_show_tree();
  179. len = membuff_getraw(&gd->console_out, -1, true, &data);
  180. if (len)
  181. data[len] = '\0';
  182. ut_asserteq_str(usb_tree_remove, data);
  183. ut_assertok(usb_stop());
  184. return 0;
  185. }
  186. DM_TEST(dm_test_usb_tree_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  187. const char usb_tree_reorder[] =
  188. " 1 Hub (12 Mb/s, 100mA)\n"
  189. " | sandbox hub 2345\n"
  190. " |\n"
  191. " |\b+-2 Mass Storage (12 Mb/s, 100mA)\n"
  192. " | sandbox flash flash-stick@0\n"
  193. " | \n"
  194. " |\b+-3 Mass Storage (12 Mb/s, 100mA)\n"
  195. " | sandbox flash flash-stick@2\n"
  196. " | \n"
  197. " |\b+-4 Human Interface (12 Mb/s, 100mA)\n"
  198. " | sandbox keyboard keyb@3\n"
  199. " | \n"
  200. " |\b+-5 Mass Storage (12 Mb/s, 100mA)\n"
  201. " sandbox flash flash-stick@1\n"
  202. " \n";
  203. /*
  204. * test that the 'usb tree' command output looks correct when we reorder two
  205. * devices.
  206. */
  207. static int dm_test_usb_tree_reorder(struct unit_test_state *uts)
  208. {
  209. struct udevice *dev, *parent;
  210. char *data;
  211. int len;
  212. /* Remove the second emulation device */
  213. ut_assertok(uclass_find_device_by_name(UCLASS_USB_EMUL, "flash-stick@1",
  214. &dev));
  215. parent = dev->parent;
  216. /* Reorder the devices in the parent list and uclass list */
  217. list_del(&dev->sibling_node);
  218. list_add_tail(&dev->sibling_node, &parent->child_head);
  219. list_del(&dev->uclass_node);
  220. list_add_tail(&dev->uclass_node, &dev->uclass->dev_head);
  221. state_set_skip_delays(true);
  222. ut_assertok(usb_init());
  223. console_record_reset_enable();
  224. usb_show_tree();
  225. len = membuff_getraw(&gd->console_out, -1, true, &data);
  226. if (len)
  227. data[len] = '\0';
  228. ut_asserteq_str(usb_tree_reorder, data);
  229. ut_assertok(usb_stop());
  230. return 0;
  231. }
  232. DM_TEST(dm_test_usb_tree_reorder, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  233. static int dm_test_usb_keyb(struct unit_test_state *uts)
  234. {
  235. struct udevice *dev;
  236. state_set_skip_delays(true);
  237. ut_assertok(usb_init());
  238. /* Initially there should be no characters */
  239. ut_asserteq(0, tstc());
  240. ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL, "keyb",
  241. &dev));
  242. /*
  243. * Add a string to the USB keyboard buffer - it should appear in
  244. * stdin
  245. */
  246. ut_assertok(sandbox_usb_keyb_add_string(dev, "ab"));
  247. ut_asserteq(1, tstc());
  248. ut_asserteq('a', getc());
  249. ut_asserteq(1, tstc());
  250. ut_asserteq('b', getc());
  251. ut_asserteq(0, tstc());
  252. ut_assertok(usb_stop());
  253. return 0;
  254. }
  255. DM_TEST(dm_test_usb_keyb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);