usb.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 no USB devices are found after we stop the stack */
  85. static int dm_test_usb_stop(struct unit_test_state *uts)
  86. {
  87. struct udevice *dev;
  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(0, count_usb_devices());
  97. return 0;
  98. }
  99. DM_TEST(dm_test_usb_stop, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  100. static int dm_test_usb_keyb(struct unit_test_state *uts)
  101. {
  102. struct udevice *dev;
  103. state_set_skip_delays(true);
  104. ut_assertok(usb_init());
  105. /* Initially there should be no characters */
  106. ut_asserteq(0, tstc());
  107. ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL, "keyb",
  108. &dev));
  109. /*
  110. * Add a string to the USB keyboard buffer - it should appear in
  111. * stdin
  112. */
  113. ut_assertok(sandbox_usb_keyb_add_string(dev, "ab"));
  114. ut_asserteq(1, tstc());
  115. ut_asserteq('a', getc());
  116. ut_asserteq(1, tstc());
  117. ut_asserteq('b', getc());
  118. ut_asserteq(0, tstc());
  119. ut_assertok(usb_stop());
  120. return 0;
  121. }
  122. DM_TEST(dm_test_usb_keyb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);