]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
e00cb223 SG |
2 | /* |
3 | * Copyright (C) 2015 Google, Inc | |
e00cb223 SG |
4 | */ |
5 | ||
6 | #include <common.h> | |
24b852a7 | 7 | #include <console.h> |
e00cb223 SG |
8 | #include <dm.h> |
9 | #include <usb.h> | |
10 | #include <asm/io.h> | |
3884c98c | 11 | #include <asm/state.h> |
bff1a71e | 12 | #include <asm/test.h> |
3884c98c | 13 | #include <dm/device-internal.h> |
e00cb223 | 14 | #include <dm/test.h> |
431cbd6d | 15 | #include <dm/uclass-internal.h> |
e721b882 | 16 | #include <test/ut.h> |
e00cb223 SG |
17 | |
18 | /* Test that sandbox USB works correctly */ | |
e721b882 | 19 | static int dm_test_usb_base(struct unit_test_state *uts) |
e00cb223 SG |
20 | { |
21 | struct udevice *bus; | |
22 | ||
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)); | |
26 | ||
27 | return 0; | |
28 | } | |
29 | DM_TEST(dm_test_usb_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); | |
30 | ||
31 | /* | |
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. | |
35 | */ | |
e721b882 | 36 | static int dm_test_usb_flash(struct unit_test_state *uts) |
e00cb223 SG |
37 | { |
38 | struct udevice *dev; | |
4101f687 | 39 | struct blk_desc *dev_desc; |
e00cb223 SG |
40 | char cmp[1024]; |
41 | ||
3884c98c | 42 | state_set_skip_delays(true); |
e00cb223 SG |
43 | ut_assertok(usb_init()); |
44 | ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev)); | |
ebac37cf | 45 | ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc)); |
e00cb223 SG |
46 | |
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)); | |
2a981dc2 | 50 | ut_asserteq(2, blk_dread(dev_desc, 0, 2, cmp)); |
e00cb223 | 51 | ut_assertok(strcmp(cmp, "this is a test")); |
61ccd886 | 52 | ut_assertok(usb_stop()); |
e00cb223 SG |
53 | |
54 | return 0; | |
55 | } | |
56 | DM_TEST(dm_test_usb_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); | |
431cbd6d SG |
57 | |
58 | /* test that we can handle multiple storage devices */ | |
59 | static int dm_test_usb_multi(struct unit_test_state *uts) | |
60 | { | |
61 | struct udevice *dev; | |
62 | ||
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)); | |
61ccd886 | 68 | ut_assertok(usb_stop()); |
431cbd6d SG |
69 | |
70 | return 0; | |
71 | } | |
72 | DM_TEST(dm_test_usb_multi, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); | |
73 | ||
74 | static int count_usb_devices(void) | |
75 | { | |
76 | struct udevice *hub; | |
77 | struct uclass *uc; | |
78 | int count = 0; | |
79 | int ret; | |
80 | ||
81 | ret = uclass_get(UCLASS_USB_HUB, &uc); | |
82 | if (ret) | |
83 | return ret; | |
84 | ||
85 | uclass_foreach_dev(hub, uc) { | |
86 | struct udevice *dev; | |
87 | ||
88 | count++; | |
89 | for (device_find_first_child(hub, &dev); | |
90 | dev; | |
91 | device_find_next_child(&dev)) { | |
92 | count++; | |
93 | } | |
94 | } | |
95 | ||
96 | return count; | |
97 | } | |
98 | ||
f4d4f7d4 BM |
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) | |
431cbd6d | 101 | { |
f4d4f7d4 | 102 | struct udevice *dev; |
431cbd6d SG |
103 | |
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)); | |
a57a8174 | 110 | ut_asserteq(6, count_usb_devices()); |
431cbd6d | 111 | ut_assertok(usb_stop()); |
f4d4f7d4 | 112 | ut_asserteq(0, count_usb_devices()); |
cd716372 SG |
113 | |
114 | return 0; | |
115 | } | |
f4d4f7d4 | 116 | DM_TEST(dm_test_usb_stop, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
bff1a71e SG |
117 | |
118 | static int dm_test_usb_keyb(struct unit_test_state *uts) | |
119 | { | |
120 | struct udevice *dev; | |
121 | ||
122 | state_set_skip_delays(true); | |
123 | ut_assertok(usb_init()); | |
124 | ||
125 | /* Initially there should be no characters */ | |
126 | ut_asserteq(0, tstc()); | |
127 | ||
128 | ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL, "keyb", | |
129 | &dev)); | |
130 | ||
131 | /* | |
132 | * Add a string to the USB keyboard buffer - it should appear in | |
133 | * stdin | |
134 | */ | |
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()); | |
141 | ||
142 | ut_assertok(usb_stop()); | |
143 | ||
144 | return 0; | |
145 | } | |
146 | DM_TEST(dm_test_usb_keyb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |