]>
git.ipfire.org Git - people/ms/u-boot.git/blob - test/dm/usb.c
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 we can remove an emulated device and it is then not found */
103 static int dm_test_usb_remove(struct unit_test_state
*uts
)
105 struct udevice
*dev
, *emul
;
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(6, count_usb_devices());
117 /* Remove the second emulation device */
118 ut_assertok(uclass_find_device_by_name(UCLASS_USB_EMUL
, "flash-stick@1",
120 ut_assertok(device_unbind(dev
));
122 /* Rescan - only the first and third should be present */
123 ut_assertok(usb_init());
124 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE
, 0, &dev
));
125 ut_assertok(usb_emul_find_for_dev(dev
, &emul
));
126 ut_asserteq_str("flash-stick@0", emul
->name
);
127 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE
, 1, &dev
));
128 ut_assertok(usb_emul_find_for_dev(dev
, &emul
));
129 ut_asserteq_str("flash-stick@2", emul
->name
);
131 ut_asserteq(-ENODEV
, uclass_get_device(UCLASS_MASS_STORAGE
, 2, &dev
));
133 ut_asserteq(5, count_usb_devices());
134 ut_assertok(usb_stop());
135 ut_asserteq(5, count_usb_devices());
139 DM_TEST(dm_test_usb_remove
, DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);
141 const char usb_tree_base
[] =
142 " 1 Hub (12 Mb/s, 100mA)\n"
143 " | sandbox hub 2345\n"
145 " |\b+-2 Mass Storage (12 Mb/s, 100mA)\n"
146 " | sandbox flash flash-stick@0\n"
148 " |\b+-3 Mass Storage (12 Mb/s, 100mA)\n"
149 " | sandbox flash flash-stick@1\n"
151 " |\b+-4 Mass Storage (12 Mb/s, 100mA)\n"
152 " | sandbox flash flash-stick@2\n"
154 " |\b+-5 Human Interface (12 Mb/s, 100mA)\n"
155 " sandbox keyboard keyb@3\n"
158 /* test that the 'usb tree' command output looks correct */
159 static int dm_test_usb_tree(struct unit_test_state
*uts
)
164 state_set_skip_delays(true);
165 ut_assertok(usb_init());
166 console_record_reset_enable();
168 len
= membuff_getraw(&gd
->console_out
, -1, true, &data
);
171 ut_asserteq_str(usb_tree_base
, data
);
172 ut_assertok(usb_stop());
176 DM_TEST(dm_test_usb_tree
, DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);
178 const char usb_tree_remove
[] =
179 " 1 Hub (12 Mb/s, 100mA)\n"
180 " | sandbox hub 2345\n"
182 " |\b+-2 Mass Storage (12 Mb/s, 100mA)\n"
183 " | sandbox flash flash-stick@0\n"
185 " |\b+-3 Mass Storage (12 Mb/s, 100mA)\n"
186 " | sandbox flash flash-stick@2\n"
188 " |\b+-4 Human Interface (12 Mb/s, 100mA)\n"
189 " sandbox keyboard keyb@3\n"
193 * test that the 'usb tree' command output looks correct when we remove a
196 static int dm_test_usb_tree_remove(struct unit_test_state
*uts
)
202 /* Remove the second emulation device */
203 ut_assertok(uclass_find_device_by_name(UCLASS_USB_EMUL
, "flash-stick@1",
205 ut_assertok(device_unbind(dev
));
207 state_set_skip_delays(true);
208 ut_assertok(usb_init());
209 console_record_reset_enable();
211 len
= membuff_getraw(&gd
->console_out
, -1, true, &data
);
214 ut_asserteq_str(usb_tree_remove
, data
);
215 ut_assertok(usb_stop());
219 DM_TEST(dm_test_usb_tree_remove
, DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);
221 const char usb_tree_reorder
[] =
222 " 1 Hub (12 Mb/s, 100mA)\n"
223 " | sandbox hub 2345\n"
225 " |\b+-2 Mass Storage (12 Mb/s, 100mA)\n"
226 " | sandbox flash flash-stick@0\n"
228 " |\b+-3 Mass Storage (12 Mb/s, 100mA)\n"
229 " | sandbox flash flash-stick@2\n"
231 " |\b+-4 Human Interface (12 Mb/s, 100mA)\n"
232 " | sandbox keyboard keyb@3\n"
234 " |\b+-5 Mass Storage (12 Mb/s, 100mA)\n"
235 " sandbox flash flash-stick@1\n"
239 * test that the 'usb tree' command output looks correct when we reorder two
242 static int dm_test_usb_tree_reorder(struct unit_test_state
*uts
)
244 struct udevice
*dev
, *parent
;
248 /* Remove the second emulation device */
249 ut_assertok(uclass_find_device_by_name(UCLASS_USB_EMUL
, "flash-stick@1",
251 parent
= dev
->parent
;
253 /* Reorder the devices in the parent list and uclass list */
254 list_del(&dev
->sibling_node
);
255 list_add_tail(&dev
->sibling_node
, &parent
->child_head
);
257 list_del(&dev
->uclass_node
);
258 list_add_tail(&dev
->uclass_node
, &dev
->uclass
->dev_head
);
260 state_set_skip_delays(true);
261 ut_assertok(usb_init());
262 console_record_reset_enable();
264 len
= membuff_getraw(&gd
->console_out
, -1, true, &data
);
267 ut_asserteq_str(usb_tree_reorder
, data
);
268 ut_assertok(usb_stop());
272 DM_TEST(dm_test_usb_tree_reorder
, DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);
274 static int dm_test_usb_keyb(struct unit_test_state
*uts
)
278 state_set_skip_delays(true);
279 ut_assertok(usb_init());
281 /* Initially there should be no characters */
282 ut_asserteq(0, tstc());
284 ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL
, "keyb",
288 * Add a string to the USB keyboard buffer - it should appear in
291 ut_assertok(sandbox_usb_keyb_add_string(dev
, "ab"));
292 ut_asserteq(1, tstc());
293 ut_asserteq('a', getc());
294 ut_asserteq(1, tstc());
295 ut_asserteq('b', getc());
296 ut_asserteq(0, tstc());
298 ut_assertok(usb_stop());
302 DM_TEST(dm_test_usb_keyb
, DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);