1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
11 #include <asm/state.h>
13 #include <dm/device-internal.h>
15 #include <dm/uclass-internal.h>
18 /* Test that sandbox USB works correctly */
19 static int dm_test_usb_base(struct unit_test_state *uts)
23 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_USB, 0, &bus));
24 ut_assertok(uclass_get_device(UCLASS_USB, 0, &bus));
25 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_USB, 2, &bus));
29 DM_TEST(dm_test_usb_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
32 * Test that we can use the flash stick. This is more of a functional test. It
33 * covers scanning the bug, setting up a hub and a flash stick and reading
34 * data from the flash stick.
36 static int dm_test_usb_flash(struct unit_test_state *uts)
39 struct blk_desc *dev_desc;
42 state_set_skip_delays(true);
43 ut_assertok(usb_init());
44 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
45 ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc));
47 /* Read a few blocks and look for the string we expect */
48 ut_asserteq(512, dev_desc->blksz);
49 memset(cmp, '\0', sizeof(cmp));
50 ut_asserteq(2, blk_dread(dev_desc, 0, 2, cmp));
51 ut_assertok(strcmp(cmp, "this is a test"));
52 ut_assertok(usb_stop());
56 DM_TEST(dm_test_usb_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
58 /* test that we can handle multiple storage devices */
59 static int dm_test_usb_multi(struct unit_test_state *uts)
63 state_set_skip_delays(true);
64 ut_assertok(usb_init());
65 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
66 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
67 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
68 ut_assertok(usb_stop());
72 DM_TEST(dm_test_usb_multi, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
74 static int count_usb_devices(void)
81 ret = uclass_get(UCLASS_USB_HUB, &uc);
85 uclass_foreach_dev(hub, uc) {
89 for (device_find_first_child(hub, &dev);
91 device_find_next_child(&dev)) {
99 /* test that no USB devices are found after we stop the stack */
100 static int dm_test_usb_stop(struct unit_test_state *uts)
104 /* Scan and check that all devices are present */
105 state_set_skip_delays(true);
106 ut_assertok(usb_init());
107 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
108 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
109 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
110 ut_asserteq(6, count_usb_devices());
111 ut_assertok(usb_stop());
112 ut_asserteq(0, count_usb_devices());
116 DM_TEST(dm_test_usb_stop, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
118 static int dm_test_usb_keyb(struct unit_test_state *uts)
122 state_set_skip_delays(true);
123 ut_assertok(usb_init());
125 /* Initially there should be no characters */
126 ut_asserteq(0, tstc());
128 ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL, "keyb",
132 * Add a string to the USB keyboard buffer - it should appear in
135 ut_assertok(sandbox_usb_keyb_add_string(dev, "ab"));
136 ut_asserteq(1, tstc());
137 ut_asserteq('a', getc());
138 ut_asserteq(1, tstc());
139 ut_asserteq('b', getc());
140 ut_asserteq(0, tstc());
142 ut_assertok(usb_stop());
146 DM_TEST(dm_test_usb_keyb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);