usb.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Google, Inc
  4. */
  5. #include <common.h>
  6. #include <console.h>
  7. #include <dm.h>
  8. #include <usb.h>
  9. #include <asm/io.h>
  10. #include <asm/state.h>
  11. #include <asm/test.h>
  12. #include <dm/device-internal.h>
  13. #include <dm/test.h>
  14. #include <dm/uclass-internal.h>
  15. #include <test/ut.h>
  16. /* Test that sandbox USB works correctly */
  17. static int dm_test_usb_base(struct unit_test_state *uts)
  18. {
  19. struct udevice *bus;
  20. ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_USB, 0, &bus));
  21. ut_assertok(uclass_get_device(UCLASS_USB, 0, &bus));
  22. ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_USB, 2, &bus));
  23. return 0;
  24. }
  25. DM_TEST(dm_test_usb_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  26. /*
  27. * Test that we can use the flash stick. This is more of a functional test. It
  28. * covers scanning the bug, setting up a hub and a flash stick and reading
  29. * data from the flash stick.
  30. */
  31. static int dm_test_usb_flash(struct unit_test_state *uts)
  32. {
  33. struct udevice *dev;
  34. struct blk_desc *dev_desc;
  35. char cmp[1024];
  36. state_set_skip_delays(true);
  37. ut_assertok(usb_init());
  38. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
  39. ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc));
  40. /* Read a few blocks and look for the string we expect */
  41. ut_asserteq(512, dev_desc->blksz);
  42. memset(cmp, '\0', sizeof(cmp));
  43. ut_asserteq(2, blk_dread(dev_desc, 0, 2, cmp));
  44. ut_assertok(strcmp(cmp, "this is a test"));
  45. ut_assertok(usb_stop());
  46. return 0;
  47. }
  48. DM_TEST(dm_test_usb_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  49. /* test that we can handle multiple storage devices */
  50. static int dm_test_usb_multi(struct unit_test_state *uts)
  51. {
  52. struct udevice *dev;
  53. state_set_skip_delays(true);
  54. ut_assertok(usb_init());
  55. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
  56. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
  57. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
  58. ut_assertok(usb_stop());
  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 no USB devices are found after we stop the stack */
  83. static int dm_test_usb_stop(struct unit_test_state *uts)
  84. {
  85. struct udevice *dev;
  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(0, count_usb_devices());
  95. return 0;
  96. }
  97. DM_TEST(dm_test_usb_stop, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  98. static int dm_test_usb_keyb(struct unit_test_state *uts)
  99. {
  100. struct udevice *dev;
  101. state_set_skip_delays(true);
  102. ut_assertok(usb_init());
  103. /* Initially there should be no characters */
  104. ut_asserteq(0, tstc());
  105. ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL, "keyb",
  106. &dev));
  107. /*
  108. * Add a string to the USB keyboard buffer - it should appear in
  109. * stdin
  110. */
  111. ut_assertok(sandbox_usb_keyb_add_string(dev, "ab"));
  112. ut_asserteq(1, tstc());
  113. ut_asserteq('a', getc());
  114. ut_asserteq(1, tstc());
  115. ut_asserteq('b', getc());
  116. ut_asserteq(0, tstc());
  117. ut_assertok(usb_stop());
  118. return 0;
  119. }
  120. DM_TEST(dm_test_usb_keyb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);