2 * Copyright (C) 2015 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/state.h>
14 #include <dm/device-internal.h>
16 #include <dm/uclass-internal.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 /* Test that sandbox USB works correctly */
22 static int dm_test_usb_base(struct unit_test_state *uts)
26 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_USB, 0, &bus));
27 ut_assertok(uclass_get_device(UCLASS_USB, 0, &bus));
28 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_USB, 2, &bus));
32 DM_TEST(dm_test_usb_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
35 * Test that we can use the flash stick. This is more of a functional test. It
36 * covers scanning the bug, setting up a hub and a flash stick and reading
37 * data from the flash stick.
39 static int dm_test_usb_flash(struct unit_test_state *uts)
42 struct blk_desc *dev_desc;
45 state_set_skip_delays(true);
46 ut_assertok(usb_init());
47 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
48 ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc));
50 /* Read a few blocks and look for the string we expect */
51 ut_asserteq(512, dev_desc->blksz);
52 memset(cmp, '\0', sizeof(cmp));
53 ut_asserteq(2, blk_dread(dev_desc, 0, 2, cmp));
54 ut_assertok(strcmp(cmp, "this is a test"));
55 ut_assertok(usb_stop());
59 DM_TEST(dm_test_usb_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
61 /* test that we can handle multiple storage devices */
62 static int dm_test_usb_multi(struct unit_test_state *uts)
66 state_set_skip_delays(true);
67 ut_assertok(usb_init());
68 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
69 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
70 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
71 ut_assertok(usb_stop());
75 DM_TEST(dm_test_usb_multi, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
77 static int count_usb_devices(void)
84 ret = uclass_get(UCLASS_USB_HUB, &uc);
88 uclass_foreach_dev(hub, uc) {
92 for (device_find_first_child(hub, &dev);
94 device_find_next_child(&dev)) {
102 /* test that no USB devices are found after we stop the stack */
103 static int dm_test_usb_stop(struct unit_test_state *uts)
107 /* Scan and check that all devices are present */
108 state_set_skip_delays(true);
109 ut_assertok(usb_init());
110 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
111 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
112 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
113 ut_asserteq(6, count_usb_devices());
114 ut_assertok(usb_stop());
115 ut_asserteq(0, count_usb_devices());
119 DM_TEST(dm_test_usb_stop, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
121 static int dm_test_usb_keyb(struct unit_test_state *uts)
125 state_set_skip_delays(true);
126 ut_assertok(usb_init());
128 /* Initially there should be no characters */
129 ut_asserteq(0, tstc());
131 ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL, "keyb",
135 * Add a string to the USB keyboard buffer - it should appear in
138 ut_assertok(sandbox_usb_keyb_add_string(dev, "ab"));
139 ut_asserteq(1, tstc());
140 ut_asserteq('a', getc());
141 ut_asserteq(1, tstc());
142 ut_asserteq('b', getc());
143 ut_asserteq(0, tstc());
145 ut_assertok(usb_stop());
149 DM_TEST(dm_test_usb_keyb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);