usb.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. block_dev_desc_t *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(get_device("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, dev_desc->block_read(dev_desc, 0, 2, cmp));
  46. ut_assertok(strcmp(cmp, "this is a test"));
  47. return 0;
  48. }
  49. DM_TEST(dm_test_usb_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  50. /* test that we can handle multiple storage devices */
  51. static int dm_test_usb_multi(struct unit_test_state *uts)
  52. {
  53. struct udevice *dev;
  54. state_set_skip_delays(true);
  55. ut_assertok(usb_init());
  56. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
  57. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
  58. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
  59. return 0;
  60. }
  61. DM_TEST(dm_test_usb_multi, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  62. static int count_usb_devices(void)
  63. {
  64. struct udevice *hub;
  65. struct uclass *uc;
  66. int count = 0;
  67. int ret;
  68. ret = uclass_get(UCLASS_USB_HUB, &uc);
  69. if (ret)
  70. return ret;
  71. uclass_foreach_dev(hub, uc) {
  72. struct udevice *dev;
  73. count++;
  74. for (device_find_first_child(hub, &dev);
  75. dev;
  76. device_find_next_child(&dev)) {
  77. count++;
  78. }
  79. }
  80. return count;
  81. }
  82. /* test that we can remove an emulated device and it is then not found */
  83. static int dm_test_usb_remove(struct unit_test_state *uts)
  84. {
  85. struct udevice *dev, *emul;
  86. /* Scan and check that all devices are present */
  87. state_set_skip_delays(true);
  88. ut_assertok(usb_init());
  89. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
  90. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
  91. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
  92. ut_asserteq(6, count_usb_devices());
  93. ut_assertok(usb_stop());
  94. ut_asserteq(6, count_usb_devices());
  95. /* Remove the second emulation device */
  96. ut_assertok(uclass_find_device_by_name(UCLASS_USB_EMUL, "flash-stick@1",
  97. &dev));
  98. ut_assertok(device_unbind(dev));
  99. /* Rescan - only the first and third should be present */
  100. ut_assertok(usb_init());
  101. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
  102. ut_assertok(usb_emul_find_for_dev(dev, &emul));
  103. ut_asserteq_str("flash-stick@0", emul->name);
  104. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
  105. ut_assertok(usb_emul_find_for_dev(dev, &emul));
  106. ut_asserteq_str("flash-stick@2", emul->name);
  107. ut_asserteq(-ENODEV, uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
  108. ut_asserteq(5, count_usb_devices());
  109. ut_assertok(usb_stop());
  110. ut_asserteq(5, count_usb_devices());
  111. return 0;
  112. }
  113. DM_TEST(dm_test_usb_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  114. const char usb_tree_base[] =
  115. " 1 Hub (12 Mb/s, 100mA)\n"
  116. " | sandbox hub 2345\n"
  117. " |\n"
  118. " |\b+-2 Mass Storage (12 Mb/s, 100mA)\n"
  119. " | sandbox flash flash-stick@0\n"
  120. " | \n"
  121. " |\b+-3 Mass Storage (12 Mb/s, 100mA)\n"
  122. " | sandbox flash flash-stick@1\n"
  123. " | \n"
  124. " |\b+-4 Mass Storage (12 Mb/s, 100mA)\n"
  125. " | sandbox flash flash-stick@2\n"
  126. " | \n"
  127. " |\b+-5 Human Interface (12 Mb/s, 100mA)\n"
  128. " sandbox keyboard keyb@3\n"
  129. " \n";
  130. /* test that the 'usb tree' command output looks correct */
  131. static int dm_test_usb_tree(struct unit_test_state *uts)
  132. {
  133. char *data;
  134. int len;
  135. state_set_skip_delays(true);
  136. ut_assertok(usb_init());
  137. console_record_reset_enable();
  138. usb_show_tree();
  139. len = membuff_getraw(&gd->console_out, -1, true, &data);
  140. if (len)
  141. data[len] = '\0';
  142. ut_asserteq_str(usb_tree_base, data);
  143. ut_assertok(usb_stop());
  144. return 0;
  145. }
  146. DM_TEST(dm_test_usb_tree, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  147. const char usb_tree_remove[] =
  148. " 1 Hub (12 Mb/s, 100mA)\n"
  149. " | sandbox hub 2345\n"
  150. " |\n"
  151. " |\b+-2 Mass Storage (12 Mb/s, 100mA)\n"
  152. " | sandbox flash flash-stick@0\n"
  153. " | \n"
  154. " |\b+-3 Mass Storage (12 Mb/s, 100mA)\n"
  155. " | sandbox flash flash-stick@2\n"
  156. " | \n"
  157. " |\b+-4 Human Interface (12 Mb/s, 100mA)\n"
  158. " sandbox keyboard keyb@3\n"
  159. " \n";
  160. /*
  161. * test that the 'usb tree' command output looks correct when we remove a
  162. * device
  163. */
  164. static int dm_test_usb_tree_remove(struct unit_test_state *uts)
  165. {
  166. struct udevice *dev;
  167. char *data;
  168. int len;
  169. /* Remove the second emulation device */
  170. ut_assertok(uclass_find_device_by_name(UCLASS_USB_EMUL, "flash-stick@1",
  171. &dev));
  172. ut_assertok(device_unbind(dev));
  173. state_set_skip_delays(true);
  174. ut_assertok(usb_init());
  175. console_record_reset_enable();
  176. usb_show_tree();
  177. len = membuff_getraw(&gd->console_out, -1, true, &data);
  178. if (len)
  179. data[len] = '\0';
  180. ut_asserteq_str(usb_tree_remove, data);
  181. ut_assertok(usb_stop());
  182. return 0;
  183. }
  184. DM_TEST(dm_test_usb_tree_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  185. const char usb_tree_reorder[] =
  186. " 1 Hub (12 Mb/s, 100mA)\n"
  187. " | sandbox hub 2345\n"
  188. " |\n"
  189. " |\b+-2 Mass Storage (12 Mb/s, 100mA)\n"
  190. " | sandbox flash flash-stick@0\n"
  191. " | \n"
  192. " |\b+-3 Mass Storage (12 Mb/s, 100mA)\n"
  193. " | sandbox flash flash-stick@2\n"
  194. " | \n"
  195. " |\b+-4 Human Interface (12 Mb/s, 100mA)\n"
  196. " | sandbox keyboard keyb@3\n"
  197. " | \n"
  198. " |\b+-5 Mass Storage (12 Mb/s, 100mA)\n"
  199. " sandbox flash flash-stick@1\n"
  200. " \n";
  201. /*
  202. * test that the 'usb tree' command output looks correct when we reorder two
  203. * devices.
  204. */
  205. static int dm_test_usb_tree_reorder(struct unit_test_state *uts)
  206. {
  207. struct udevice *dev, *parent;
  208. char *data;
  209. int len;
  210. /* Remove the second emulation device */
  211. ut_assertok(uclass_find_device_by_name(UCLASS_USB_EMUL, "flash-stick@1",
  212. &dev));
  213. parent = dev->parent;
  214. /* Reorder the devices in the parent list and uclass list */
  215. list_del(&dev->sibling_node);
  216. list_add_tail(&dev->sibling_node, &parent->child_head);
  217. list_del(&dev->uclass_node);
  218. list_add_tail(&dev->uclass_node, &dev->uclass->dev_head);
  219. state_set_skip_delays(true);
  220. ut_assertok(usb_init());
  221. console_record_reset_enable();
  222. usb_show_tree();
  223. len = membuff_getraw(&gd->console_out, -1, true, &data);
  224. if (len)
  225. data[len] = '\0';
  226. ut_asserteq_str(usb_tree_reorder, data);
  227. ut_assertok(usb_stop());
  228. return 0;
  229. }
  230. DM_TEST(dm_test_usb_tree_reorder, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  231. static int dm_test_usb_keyb(struct unit_test_state *uts)
  232. {
  233. struct udevice *dev;
  234. state_set_skip_delays(true);
  235. ut_assertok(usb_init());
  236. /* Initially there should be no characters */
  237. ut_asserteq(0, tstc());
  238. ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL, "keyb",
  239. &dev));
  240. /*
  241. * Add a string to the USB keyboard buffer - it should appear in
  242. * stdin
  243. */
  244. ut_assertok(sandbox_usb_keyb_add_string(dev, "ab"));
  245. ut_asserteq(1, tstc());
  246. ut_asserteq('a', getc());
  247. ut_asserteq(1, tstc());
  248. ut_asserteq('b', getc());
  249. ut_asserteq(0, tstc());
  250. ut_assertok(usb_stop());
  251. return 0;
  252. }
  253. DM_TEST(dm_test_usb_keyb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);